Add --collector.ethtool.ignored-devices

Other network related collectors allow to filter out unwanted devices.
Add this support to the new ethtool collector as well.

Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
This commit is contained in:
Benjamin Drung 2021-07-19 11:44:28 +02:00 committed by Johannes 'fish' Ziemke
parent 4d0c1650b5
commit 3afd382e75
2 changed files with 18 additions and 9 deletions

View File

@ -33,11 +33,13 @@ import (
"github.com/prometheus/procfs/sysfs" "github.com/prometheus/procfs/sysfs"
"github.com/safchain/ethtool" "github.com/safchain/ethtool"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"gopkg.in/alecthomas/kingpin.v2"
) )
var ( var (
receivedRegex = regexp.MustCompile(`_rx_`) ethtoolIgnoredDevices = kingpin.Flag("collector.ethtool.ignored-devices", "Regexp of net devices to ignore for ethtool collector.").Default("^$").String()
transmittedRegex = regexp.MustCompile(`_tx_`) receivedRegex = regexp.MustCompile(`_rx_`)
transmittedRegex = regexp.MustCompile(`_tx_`)
) )
type EthtoolStats interface { type EthtoolStats interface {
@ -52,10 +54,11 @@ func (e *ethtoolStats) Stats(intf string) (map[string]uint64, error) {
} }
type ethtoolCollector struct { type ethtoolCollector struct {
fs sysfs.FS fs sysfs.FS
entries map[string]*prometheus.Desc entries map[string]*prometheus.Desc
logger log.Logger ignoredDevicesPattern *regexp.Regexp
stats EthtoolStats logger log.Logger
stats EthtoolStats
} }
// makeEthtoolCollector is the internal constructor for EthtoolCollector. // makeEthtoolCollector is the internal constructor for EthtoolCollector.
@ -69,8 +72,10 @@ func makeEthtoolCollector(logger log.Logger) (*ethtoolCollector, error) {
// Pre-populate some common ethtool metrics. // Pre-populate some common ethtool metrics.
return &ethtoolCollector{ return &ethtoolCollector{
fs: fs, fs: fs,
stats: &ethtoolStats{}, ignoredDevicesPattern: regexp.MustCompile(*ethtoolIgnoredDevices),
logger: logger,
stats: &ethtoolStats{},
entries: map[string]*prometheus.Desc{ entries: map[string]*prometheus.Desc{
"rx_bytes": prometheus.NewDesc( "rx_bytes": prometheus.NewDesc(
"node_ethtool_received_bytes_total", "node_ethtool_received_bytes_total",
@ -108,7 +113,6 @@ func makeEthtoolCollector(logger log.Logger) (*ethtoolCollector, error) {
[]string{"device"}, nil, []string{"device"}, nil,
), ),
}, },
logger: logger,
}, nil }, nil
} }
@ -139,6 +143,10 @@ func (c *ethtoolCollector) Update(ch chan<- prometheus.Metric) error {
var stats map[string]uint64 var stats map[string]uint64
var err error var err error
if c.ignoredDevicesPattern.MatchString(device) {
continue
}
stats, err = c.stats.Stats(device) stats, err = c.stats.Stats(device)
// If Stats() returns EOPNOTSUPP it doesn't support ethtool stats. Log that only at Debug level. // If Stats() returns EOPNOTSUPP it doesn't support ethtool stats. Log that only at Debug level.

View File

@ -96,6 +96,7 @@ func TestEthtoolCollector(t *testing.T) {
prometheus.NewDesc("node_ethtool_transmitted_packets_total", "Network interface packets sent", []string{"device"}, nil).String(), prometheus.NewDesc("node_ethtool_transmitted_packets_total", "Network interface packets sent", []string{"device"}, nil).String(),
} }
*ethtoolIgnoredDevices = "^$"
*sysPath = "fixtures/sys" *sysPath = "fixtures/sys"
collector, err := NewEthtoolTestCollector(log.NewNopLogger()) collector, err := NewEthtoolTestCollector(log.NewNopLogger())