diff options
-rw-r--r-- | CHANGELOG.md | 5 | ||||
-rw-r--r-- | collector/netdev_bsd.go | 8 | ||||
-rw-r--r-- | collector/netdev_common.go | 25 | ||||
-rw-r--r-- | collector/netdev_darwin.go | 6 | ||||
-rw-r--r-- | collector/netdev_linux.go | 12 | ||||
-rw-r--r-- | collector/netdev_linux_test.go | 24 | ||||
-rw-r--r-- | collector/netdev_openbsd.go | 8 |
7 files changed, 72 insertions, 16 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index f5ab4fe..eca02bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md | |||
@@ -2,9 +2,12 @@ | |||
2 | 2 | ||
3 | ### **Breaking changes** | 3 | ### **Breaking changes** |
4 | 4 | ||
5 | * The netdev collector CLI argument `--collector.netdev.ignored-devices` was renamed to `--collector.netdev.device-blacklist` in order to conform with the systemd collector. #1279 | ||
6 | |||
7 | |||
5 | ### Changes | 8 | ### Changes |
6 | 9 | ||
7 | * [CHANGE] | 10 | * [CHANGE] Add `--collector.netdev.device-whitelist`. #1279 |
8 | * [FEATURE] | 11 | * [FEATURE] |
9 | * [ENHANCEMENT] | 12 | * [ENHANCEMENT] |
10 | * [BUGFIX] Fix incorrect sysctl call in BSD meminfo collector, resulting in broken swap metrics on FreeBSD #1345 | 13 | * [BUGFIX] Fix incorrect sysctl call in BSD meminfo collector, resulting in broken swap metrics on FreeBSD #1345 |
diff --git a/collector/netdev_bsd.go b/collector/netdev_bsd.go index d846daf..18eca22 100644 --- a/collector/netdev_bsd.go +++ b/collector/netdev_bsd.go | |||
@@ -34,7 +34,7 @@ import ( | |||
34 | */ | 34 | */ |
35 | import "C" | 35 | import "C" |
36 | 36 | ||
37 | func getNetDevStats(ignore *regexp.Regexp) (map[string]map[string]string, error) { | 37 | func getNetDevStats(ignore *regexp.Regexp, accept *regexp.Regexp) (map[string]map[string]string, error) { |
38 | netDev := map[string]map[string]string{} | 38 | netDev := map[string]map[string]string{} |
39 | 39 | ||
40 | var ifap, ifa *C.struct_ifaddrs | 40 | var ifap, ifa *C.struct_ifaddrs |
@@ -46,7 +46,11 @@ func getNetDevStats(ignore *regexp.Regexp) (map[string]map[string]string, error) | |||
46 | for ifa = ifap; ifa != nil; ifa = ifa.ifa_next { | 46 | for ifa = ifap; ifa != nil; ifa = ifa.ifa_next { |
47 | if ifa.ifa_addr.sa_family == C.AF_LINK { | 47 | if ifa.ifa_addr.sa_family == C.AF_LINK { |
48 | dev := C.GoString(ifa.ifa_name) | 48 | dev := C.GoString(ifa.ifa_name) |
49 | if ignore.MatchString(dev) { | 49 | if ignore != nil && ignore.MatchString(dev) { |
50 | log.Debugf("Ignoring device: %s", dev) | ||
51 | continue | ||
52 | } | ||
53 | if accept != nil && !accept.MatchString(dev) { | ||
50 | log.Debugf("Ignoring device: %s", dev) | 54 | log.Debugf("Ignoring device: %s", dev) |
51 | continue | 55 | continue |
52 | } | 56 | } |
diff --git a/collector/netdev_common.go b/collector/netdev_common.go index 9fa3cd2..0c11023 100644 --- a/collector/netdev_common.go +++ b/collector/netdev_common.go | |||
@@ -17,6 +17,7 @@ | |||
17 | package collector | 17 | package collector |
18 | 18 | ||
19 | import ( | 19 | import ( |
20 | "errors" | ||
20 | "fmt" | 21 | "fmt" |
21 | "regexp" | 22 | "regexp" |
22 | "strconv" | 23 | "strconv" |
@@ -26,12 +27,14 @@ import ( | |||
26 | ) | 27 | ) |
27 | 28 | ||
28 | var ( | 29 | var ( |
29 | netdevIgnoredDevices = kingpin.Flag("collector.netdev.ignored-devices", "Regexp of net devices to ignore for netdev collector.").Default("^$").String() | 30 | netdevIgnoredDevices = kingpin.Flag("collector.netdev.device-blacklist", "Regexp of net devices to blacklist (mutually exclusive to device-whitelist).").String() |
31 | netdevAcceptDevices = kingpin.Flag("collector.netdev.device-whitelist", "Regexp of net devices to whitelist (mutually exclusive to device-blacklist).").String() | ||
30 | ) | 32 | ) |
31 | 33 | ||
32 | type netDevCollector struct { | 34 | type netDevCollector struct { |
33 | subsystem string | 35 | subsystem string |
34 | ignoredDevicesPattern *regexp.Regexp | 36 | ignoredDevicesPattern *regexp.Regexp |
37 | acceptDevicesPattern *regexp.Regexp | ||
35 | metricDescs map[string]*prometheus.Desc | 38 | metricDescs map[string]*prometheus.Desc |
36 | } | 39 | } |
37 | 40 | ||
@@ -41,16 +44,30 @@ func init() { | |||
41 | 44 | ||
42 | // NewNetDevCollector returns a new Collector exposing network device stats. | 45 | // NewNetDevCollector returns a new Collector exposing network device stats. |
43 | func NewNetDevCollector() (Collector, error) { | 46 | func NewNetDevCollector() (Collector, error) { |
44 | pattern := regexp.MustCompile(*netdevIgnoredDevices) | 47 | if *netdevIgnoredDevices != "" && *netdevAcceptDevices != "" { |
48 | return nil, errors.New("device-blacklist & accept-devices are mutually exclusive") | ||
49 | } | ||
50 | |||
51 | var ignorePattern *regexp.Regexp = nil | ||
52 | if *netdevIgnoredDevices != "" { | ||
53 | ignorePattern = regexp.MustCompile(*netdevIgnoredDevices) | ||
54 | } | ||
55 | |||
56 | var acceptPattern *regexp.Regexp = nil | ||
57 | if *netdevAcceptDevices != "" { | ||
58 | acceptPattern = regexp.MustCompile(*netdevAcceptDevices) | ||
59 | } | ||
60 | |||
45 | return &netDevCollector{ | 61 | return &netDevCollector{ |
46 | subsystem: "network", | 62 | subsystem: "network", |
47 | ignoredDevicesPattern: pattern, | 63 | ignoredDevicesPattern: ignorePattern, |
64 | acceptDevicesPattern: acceptPattern, | ||
48 | metricDescs: map[string]*prometheus.Desc{}, | 65 | metricDescs: map[string]*prometheus.Desc{}, |
49 | }, nil | 66 | }, nil |
50 | } | 67 | } |
51 | 68 | ||
52 | func (c *netDevCollector) Update(ch chan<- prometheus.Metric) error { | 69 | func (c *netDevCollector) Update(ch chan<- prometheus.Metric) error { |
53 | netDev, err := getNetDevStats(c.ignoredDevicesPattern) | 70 | netDev, err := getNetDevStats(c.ignoredDevicesPattern, c.acceptDevicesPattern) |
54 | if err != nil { | 71 | if err != nil { |
55 | return fmt.Errorf("couldn't get netstats: %s", err) | 72 | return fmt.Errorf("couldn't get netstats: %s", err) |
56 | } | 73 | } |
diff --git a/collector/netdev_darwin.go b/collector/netdev_darwin.go index b02ed40..c07e7fa 100644 --- a/collector/netdev_darwin.go +++ b/collector/netdev_darwin.go | |||
@@ -27,7 +27,7 @@ import ( | |||
27 | "golang.org/x/sys/unix" | 27 | "golang.org/x/sys/unix" |
28 | ) | 28 | ) |
29 | 29 | ||
30 | func getNetDevStats(ignore *regexp.Regexp) (map[string]map[string]string, error) { | 30 | func getNetDevStats(ignore *regexp.Regexp, accept *regexp.Regexp) (map[string]map[string]string, error) { |
31 | netDev := map[string]map[string]string{} | 31 | netDev := map[string]map[string]string{} |
32 | 32 | ||
33 | ifs, err := net.Interfaces() | 33 | ifs, err := net.Interfaces() |
@@ -46,6 +46,10 @@ func getNetDevStats(ignore *regexp.Regexp) (map[string]map[string]string, error) | |||
46 | log.Debugf("Ignoring device: %s", iface.Name) | 46 | log.Debugf("Ignoring device: %s", iface.Name) |
47 | continue | 47 | continue |
48 | } | 48 | } |
49 | if accept != nil && !accept.MatchString(iface.Name) { | ||
50 | log.Debugf("Ignoring device: %s", iface.Name) | ||
51 | continue | ||
52 | } | ||
49 | 53 | ||
50 | devStats := map[string]string{} | 54 | devStats := map[string]string{} |
51 | devStats["receive_packets"] = strconv.FormatUint(ifaceData.Data.Ipackets, 10) | 55 | devStats["receive_packets"] = strconv.FormatUint(ifaceData.Data.Ipackets, 10) |
diff --git a/collector/netdev_linux.go b/collector/netdev_linux.go index 4ea09a3..a5c6163 100644 --- a/collector/netdev_linux.go +++ b/collector/netdev_linux.go | |||
@@ -31,17 +31,17 @@ var ( | |||
31 | procNetDevFieldSep = regexp.MustCompile(` +`) | 31 | procNetDevFieldSep = regexp.MustCompile(` +`) |
32 | ) | 32 | ) |
33 | 33 | ||
34 | func getNetDevStats(ignore *regexp.Regexp) (map[string]map[string]string, error) { | 34 | func getNetDevStats(ignore *regexp.Regexp, accept *regexp.Regexp) (map[string]map[string]string, error) { |
35 | file, err := os.Open(procFilePath("net/dev")) | 35 | file, err := os.Open(procFilePath("net/dev")) |
36 | if err != nil { | 36 | if err != nil { |
37 | return nil, err | 37 | return nil, err |
38 | } | 38 | } |
39 | defer file.Close() | 39 | defer file.Close() |
40 | 40 | ||
41 | return parseNetDevStats(file, ignore) | 41 | return parseNetDevStats(file, ignore, accept) |
42 | } | 42 | } |
43 | 43 | ||
44 | func parseNetDevStats(r io.Reader, ignore *regexp.Regexp) (map[string]map[string]string, error) { | 44 | func parseNetDevStats(r io.Reader, ignore *regexp.Regexp, accept *regexp.Regexp) (map[string]map[string]string, error) { |
45 | scanner := bufio.NewScanner(r) | 45 | scanner := bufio.NewScanner(r) |
46 | scanner.Scan() // skip first header | 46 | scanner.Scan() // skip first header |
47 | scanner.Scan() | 47 | scanner.Scan() |
@@ -64,7 +64,11 @@ func parseNetDevStats(r io.Reader, ignore *regexp.Regexp) (map[string]map[string | |||
64 | } | 64 | } |
65 | 65 | ||
66 | dev := parts[1] | 66 | dev := parts[1] |
67 | if ignore.MatchString(dev) { | 67 | if ignore != nil && ignore.MatchString(dev) { |
68 | log.Debugf("Ignoring device: %s", dev) | ||
69 | continue | ||
70 | } | ||
71 | if accept != nil && !accept.MatchString(dev) { | ||
68 | log.Debugf("Ignoring device: %s", dev) | 72 | log.Debugf("Ignoring device: %s", dev) |
69 | continue | 73 | continue |
70 | } | 74 | } |
diff --git a/collector/netdev_linux_test.go b/collector/netdev_linux_test.go index 7228ae2..163c87f 100644 --- a/collector/netdev_linux_test.go +++ b/collector/netdev_linux_test.go | |||
@@ -19,14 +19,14 @@ import ( | |||
19 | "testing" | 19 | "testing" |
20 | ) | 20 | ) |
21 | 21 | ||
22 | func TestNetDevStats(t *testing.T) { | 22 | func TestNetDevStatsIgnore(t *testing.T) { |
23 | file, err := os.Open("fixtures/proc/net/dev") | 23 | file, err := os.Open("fixtures/proc/net/dev") |
24 | if err != nil { | 24 | if err != nil { |
25 | t.Fatal(err) | 25 | t.Fatal(err) |
26 | } | 26 | } |
27 | defer file.Close() | 27 | defer file.Close() |
28 | 28 | ||
29 | netStats, err := parseNetDevStats(file, regexp.MustCompile("^veth")) | 29 | netStats, err := parseNetDevStats(file, regexp.MustCompile("^veth"), nil) |
30 | if err != nil { | 30 | if err != nil { |
31 | t.Fatal(err) | 31 | t.Fatal(err) |
32 | } | 32 | } |
@@ -59,3 +59,23 @@ func TestNetDevStats(t *testing.T) { | |||
59 | t.Error("want fixture interface 💩0 to exist, but it does not") | 59 | t.Error("want fixture interface 💩0 to exist, but it does not") |
60 | } | 60 | } |
61 | } | 61 | } |
62 | |||
63 | func TestNetDevStatsAccept(t *testing.T) { | ||
64 | file, err := os.Open("fixtures/proc/net/dev") | ||
65 | if err != nil { | ||
66 | t.Fatal(err) | ||
67 | } | ||
68 | defer file.Close() | ||
69 | |||
70 | netStats, err := parseNetDevStats(file, nil, regexp.MustCompile("^💩0$")) | ||
71 | if err != nil { | ||
72 | t.Fatal(err) | ||
73 | } | ||
74 | |||
75 | if want, got := 1, len(netStats); want != got { | ||
76 | t.Errorf("want count of devices to be %d, got %d", want, got) | ||
77 | } | ||
78 | if want, got := "72", netStats["💩0"]["receive_multicast"]; want != got { | ||
79 | t.Error("want fixture interface 💩0 to exist, but it does not") | ||
80 | } | ||
81 | } | ||
diff --git a/collector/netdev_openbsd.go b/collector/netdev_openbsd.go index 0e9c900..b080cbc 100644 --- a/collector/netdev_openbsd.go +++ b/collector/netdev_openbsd.go | |||
@@ -31,7 +31,7 @@ import ( | |||
31 | */ | 31 | */ |
32 | import "C" | 32 | import "C" |
33 | 33 | ||
34 | func getNetDevStats(ignore *regexp.Regexp) (map[string]map[string]string, error) { | 34 | func getNetDevStats(ignore *regexp.Regexp, accept *regexp.Regexp) (map[string]map[string]string, error) { |
35 | netDev := map[string]map[string]string{} | 35 | netDev := map[string]map[string]string{} |
36 | 36 | ||
37 | var ifap, ifa *C.struct_ifaddrs | 37 | var ifap, ifa *C.struct_ifaddrs |
@@ -43,7 +43,11 @@ func getNetDevStats(ignore *regexp.Regexp) (map[string]map[string]string, error) | |||
43 | for ifa = ifap; ifa != nil; ifa = ifa.ifa_next { | 43 | for ifa = ifap; ifa != nil; ifa = ifa.ifa_next { |
44 | if ifa.ifa_addr.sa_family == C.AF_LINK { | 44 | if ifa.ifa_addr.sa_family == C.AF_LINK { |
45 | dev := C.GoString(ifa.ifa_name) | 45 | dev := C.GoString(ifa.ifa_name) |
46 | if ignore.MatchString(dev) { | 46 | if ignore != nil && ignore.MatchString(dev) { |
47 | log.Debugf("Ignoring device: %s", dev) | ||
48 | continue | ||
49 | } | ||
50 | if accept != nil && !accept.MatchString(dev) { | ||
47 | log.Debugf("Ignoring device: %s", dev) | 51 | log.Debugf("Ignoring device: %s", dev) |
48 | continue | 52 | continue |
49 | } | 53 | } |