Skip to content

Commit 61f290b

Browse files
committed
Add include and exclude kingpin flags, following example in systemd collector
Signed-off-by: Conall O'Brien <conall@conall.net>
1 parent ff7f9d6 commit 61f290b

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

collector/hwmon_linux.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,32 @@ package collector
1818

1919
import (
2020
"errors"
21+
"fmt"
2122
"os"
2223
"path/filepath"
2324
"regexp"
2425
"strconv"
2526
"strings"
2627

28+
"github.com/alecthomas/kingpin/v2"
2729
"github.com/go-kit/log"
2830
"github.com/go-kit/log/level"
2931
"github.com/prometheus/client_golang/prometheus"
3032
"golang.org/x/sys/unix"
3133
)
3234

3335
var (
36+
hwmonIncludeSet bool
37+
hwmonInclude = kingpin.Flag("collector.hwmon.unit-include", "Regexp of hwmon units to include. Units must both match include and not match exclude to be included.").Default(".+").PreAction(func(c *kingpin.ParseContext) error {
38+
hwmonIncludeSet = true
39+
return nil
40+
}).String()
41+
hwmonExcludeSet bool
42+
hwmonExclude = kingpin.Flag("collector.hwmon.unit-exclude", "Regexp of hwmon units to exclude. Units must both match include and not match exclude to be included.").Default("").PreAction(func(c *kingpin.ParseContext) error {
43+
hwmonExcludeSet = true
44+
return nil
45+
}).String()
46+
3447
hwmonInvalidMetricChars = regexp.MustCompile("[^a-z0-9:_]")
3548
hwmonFilenameFormat = regexp.MustCompile(`^(?P<type>[^0-9]+)(?P<id>[0-9]*)?(_(?P<property>.+))?$`)
3649
hwmonLabelDesc = []string{"chip", "sensor"}
@@ -47,13 +60,25 @@ func init() {
4760
}
4861

4962
type hwMonCollector struct {
50-
logger log.Logger
63+
hwmonIncludePattern *regexp.Regexp
64+
hwmonExcludePattern *regexp.Regexp
65+
logger log.Logger
5166
}
5267

5368
// NewHwMonCollector returns a new Collector exposing /sys/class/hwmon stats
5469
// (similar to lm-sensors).
5570
func NewHwMonCollector(logger log.Logger) (Collector, error) {
56-
return &hwMonCollector{logger}, nil
71+
72+
level.Info(logger).Log("msg", "Parsed flag --collector.hwmon.unit-include", "flag", *hwmonInclude)
73+
hwmonIncludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *hwmonInclude))
74+
level.Info(logger).Log("msg", "Parsed flag --collector.hwmon.unit-exclude", "flag", *hwmonExclude)
75+
hwmonExcludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *hwmonExclude))
76+
77+
return &hwMonCollector{
78+
hwmonIncludePattern: hwmonIncludePattern,
79+
hwmonExcludePattern: hwmonExcludePattern,
80+
logger: logger,
81+
}, nil
5782
}
5883

5984
func cleanMetricName(name string) string {

0 commit comments

Comments
 (0)