From 3afd382e7589cd6ba6cfff19c996011421d9c03b Mon Sep 17 00:00:00 2001 From: Benjamin Drung Date: Mon, 19 Jul 2021 11:44:28 +0200 Subject: [PATCH] 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 --- collector/ethtool_linux.go | 26 +++++++++++++++++--------- collector/ethtool_linux_test.go | 1 + 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/collector/ethtool_linux.go b/collector/ethtool_linux.go index ac9f65bb..26b091f8 100644 --- a/collector/ethtool_linux.go +++ b/collector/ethtool_linux.go @@ -33,11 +33,13 @@ import ( "github.com/prometheus/procfs/sysfs" "github.com/safchain/ethtool" "golang.org/x/sys/unix" + "gopkg.in/alecthomas/kingpin.v2" ) var ( - receivedRegex = regexp.MustCompile(`_rx_`) - transmittedRegex = regexp.MustCompile(`_tx_`) + ethtoolIgnoredDevices = kingpin.Flag("collector.ethtool.ignored-devices", "Regexp of net devices to ignore for ethtool collector.").Default("^$").String() + receivedRegex = regexp.MustCompile(`_rx_`) + transmittedRegex = regexp.MustCompile(`_tx_`) ) type EthtoolStats interface { @@ -52,10 +54,11 @@ func (e *ethtoolStats) Stats(intf string) (map[string]uint64, error) { } type ethtoolCollector struct { - fs sysfs.FS - entries map[string]*prometheus.Desc - logger log.Logger - stats EthtoolStats + fs sysfs.FS + entries map[string]*prometheus.Desc + ignoredDevicesPattern *regexp.Regexp + logger log.Logger + stats EthtoolStats } // makeEthtoolCollector is the internal constructor for EthtoolCollector. @@ -69,8 +72,10 @@ func makeEthtoolCollector(logger log.Logger) (*ethtoolCollector, error) { // Pre-populate some common ethtool metrics. return ðtoolCollector{ - fs: fs, - stats: ðtoolStats{}, + fs: fs, + ignoredDevicesPattern: regexp.MustCompile(*ethtoolIgnoredDevices), + logger: logger, + stats: ðtoolStats{}, entries: map[string]*prometheus.Desc{ "rx_bytes": prometheus.NewDesc( "node_ethtool_received_bytes_total", @@ -108,7 +113,6 @@ func makeEthtoolCollector(logger log.Logger) (*ethtoolCollector, error) { []string{"device"}, nil, ), }, - logger: logger, }, nil } @@ -139,6 +143,10 @@ func (c *ethtoolCollector) Update(ch chan<- prometheus.Metric) error { var stats map[string]uint64 var err error + if c.ignoredDevicesPattern.MatchString(device) { + continue + } + stats, err = c.stats.Stats(device) // If Stats() returns EOPNOTSUPP it doesn't support ethtool stats. Log that only at Debug level. diff --git a/collector/ethtool_linux_test.go b/collector/ethtool_linux_test.go index 91fb55e0..f2a4e087 100644 --- a/collector/ethtool_linux_test.go +++ b/collector/ethtool_linux_test.go @@ -96,6 +96,7 @@ func TestEthtoolCollector(t *testing.T) { prometheus.NewDesc("node_ethtool_transmitted_packets_total", "Network interface packets sent", []string{"device"}, nil).String(), } + *ethtoolIgnoredDevices = "^$" *sysPath = "fixtures/sys" collector, err := NewEthtoolTestCollector(log.NewNopLogger())