Skip to content

Commit

Permalink
Add include and exclude filter for hwmon collector (#2699)
Browse files Browse the repository at this point in the history
* Add include and exclude flags chip name flags to hwmon collector, following example in systemd collector

---------

Signed-off-by: Conall O'Brien <[email protected]>
Co-authored-by: Ben Kochie <[email protected]>
  • Loading branch information
conallob and SuperQ committed Jul 7, 2023
1 parent ed57c15 commit 8b4dc82
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions collector/hwmon_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ import (
"strconv"
"strings"

"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sys/unix"
)

var (
collectorHWmonChipInclude = kingpin.Flag("collector.hwmon.chip-include", "Regexp of hwmon chip to include (mutually exclusive to device-exclude).").String()
collectorHWmonChipExclude = kingpin.Flag("collector.hwmon.chip-exclude", "Regexp of hwmon chip to exclude (mutually exclusive to device-include).").String()

hwmonInvalidMetricChars = regexp.MustCompile("[^a-z0-9:_]")
hwmonFilenameFormat = regexp.MustCompile(`^(?P<type>[^0-9]+)(?P<id>[0-9]*)?(_(?P<property>.+))?$`)
hwmonLabelDesc = []string{"chip", "sensor"}
Expand All @@ -47,13 +51,18 @@ func init() {
}

type hwMonCollector struct {
logger log.Logger
deviceFilter deviceFilter
logger log.Logger
}

// NewHwMonCollector returns a new Collector exposing /sys/class/hwmon stats
// (similar to lm-sensors).
func NewHwMonCollector(logger log.Logger) (Collector, error) {
return &hwMonCollector{logger}, nil

return &hwMonCollector{
logger: logger,
deviceFilter: newDeviceFilter(*collectorHWmonChipExclude, *collectorHWmonChipExclude),
}, nil
}

func cleanMetricName(name string) string {
Expand Down Expand Up @@ -154,6 +163,10 @@ func (c *hwMonCollector) updateHwmon(ch chan<- prometheus.Metric, dir string) er
return err
}

if c.deviceFilter.ignored(hwmonName) {
return nil
}

data := make(map[string]map[string]string)
err = collectSensorData(dir, data)
if err != nil {
Expand Down

0 comments on commit 8b4dc82

Please sign in to comment.