From 64affe205bdca5505bf079a413f6ea9f21cfc0fe Mon Sep 17 00:00:00 2001 From: Ian Hansen Date: Mon, 13 Jul 2015 17:18:34 -0600 Subject: [PATCH] Allows netdev devices to be ignored --- collector/netdev.go | 23 +++++++++++++++++------ collector/netdev_test.go | 11 ++++++++++- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/collector/netdev.go b/collector/netdev.go index a630ff16..ee1de005 100644 --- a/collector/netdev.go +++ b/collector/netdev.go @@ -4,6 +4,7 @@ package collector import ( "bufio" + "flag" "fmt" "io" "os" @@ -12,6 +13,7 @@ import ( "strings" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/log" ) const ( @@ -20,10 +22,13 @@ const ( ) var ( - fieldSep = regexp.MustCompile("[ :] *") + fieldSep = regexp.MustCompile("[ :] *") + netdevIgnoredDevices = flag.String("collector.netdev.ignored-devices", "^$", "Regexp of net devices to ignore for netdev collector.") ) type netDevCollector struct { + ignoredDevicesPattern *regexp.Regexp + metrics map[string]*prometheus.GaugeVec } @@ -35,12 +40,13 @@ func init() { // network device stats. func NewNetDevCollector() (Collector, error) { return &netDevCollector{ - metrics: map[string]*prometheus.GaugeVec{}, + ignoredDevicesPattern: regexp.MustCompile(*netdevIgnoredDevices), + metrics: map[string]*prometheus.GaugeVec{}, }, nil } func (c *netDevCollector) Update(ch chan<- prometheus.Metric) (err error) { - netDev, err := getNetDevStats() + netDev, err := getNetDevStats(c.ignoredDevicesPattern) if err != nil { return fmt.Errorf("Couldn't get netstats: %s", err) } @@ -73,17 +79,17 @@ func (c *netDevCollector) Update(ch chan<- prometheus.Metric) (err error) { return err } -func getNetDevStats() (map[string]map[string]map[string]string, error) { +func getNetDevStats(ignore *regexp.Regexp) (map[string]map[string]map[string]string, error) { file, err := os.Open(procNetDev) if err != nil { return nil, err } defer file.Close() - return parseNetDevStats(file) + return parseNetDevStats(file, ignore) } -func parseNetDevStats(r io.Reader) (map[string]map[string]map[string]string, error) { +func parseNetDevStats(r io.Reader, ignore *regexp.Regexp) (map[string]map[string]map[string]string, error) { netDev := map[string]map[string]map[string]string{} netDev["transmit"] = map[string]map[string]string{} netDev["receive"] = map[string]map[string]string{} @@ -115,6 +121,11 @@ func parseNetDevStats(r io.Reader) (map[string]map[string]map[string]string, err if err != nil { return nil, err } + + if ignore.MatchString(dev) { + log.Debugf("Ignoring device: %s", dev) + continue + } netDev["transmit"][dev] = transmit netDev["receive"][dev] = receive } diff --git a/collector/netdev_test.go b/collector/netdev_test.go index 7eb22413..ce60ca3b 100644 --- a/collector/netdev_test.go +++ b/collector/netdev_test.go @@ -2,6 +2,7 @@ package collector import ( "os" + "regexp" "testing" ) @@ -12,7 +13,7 @@ func TestNetDevStats(t *testing.T) { } defer file.Close() - netStats, err := parseNetDevStats(file) + netStats, err := parseNetDevStats(file, regexp.MustCompile("^veth")) if err != nil { t.Fatal(err) } @@ -28,4 +29,12 @@ func TestNetDevStats(t *testing.T) { if want, got := "934", netStats["transmit"]["tun0"]["packets"]; want != got { t.Errorf("want netstat tun0 packets %s, got %s", want, got) } + + if want, got := 6, len(netStats["transmit"]); want != got { + t.Errorf("want count of devices to be %d, got %d", want, got) + } + + if _, ok := netStats["transmit"]["veth4B09XN"]; ok != false { + t.Error("want fixture interface veth4B09XN to not exist, but it does") + } }