From a528966dcd5b48d9f2be2f3ebe199e6d764d7089 Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Mon, 16 Apr 2018 14:34:39 +0200 Subject: [PATCH] Fix parsing of interface aliases in netdev linux Very old kernels expose interface aliases as `foo0:0`, adjust the line parsing to handle these names. Signed-off-by: Ben Kochie --- collector/fixtures/e2e-output.txt | 16 ++++++++++++++++ collector/fixtures/proc/net/dev | 1 + collector/netdev_linux.go | 23 +++++++++++++++-------- collector/netdev_linux_test.go | 6 +++++- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/collector/fixtures/e2e-output.txt b/collector/fixtures/e2e-output.txt index 1795826d..d550f879 100644 --- a/collector/fixtures/e2e-output.txt +++ b/collector/fixtures/e2e-output.txt @@ -1668,6 +1668,7 @@ node_netstat_Udp_OutDatagrams 53028 # TYPE node_network_receive_bytes_total counter node_network_receive_bytes_total{device="docker0"} 6.4910168e+07 node_network_receive_bytes_total{device="eth0"} 6.8210035552e+10 +node_network_receive_bytes_total{device="ibr10:30"} 0 node_network_receive_bytes_total{device="lo"} 4.35303245e+08 node_network_receive_bytes_total{device="lxcbr0"} 0 node_network_receive_bytes_total{device="tun0"} 1888 @@ -1677,6 +1678,7 @@ node_network_receive_bytes_total{device="wlan0"} 1.0437182923e+10 # TYPE node_network_receive_compressed_total counter node_network_receive_compressed_total{device="docker0"} 0 node_network_receive_compressed_total{device="eth0"} 0 +node_network_receive_compressed_total{device="ibr10:30"} 0 node_network_receive_compressed_total{device="lo"} 0 node_network_receive_compressed_total{device="lxcbr0"} 0 node_network_receive_compressed_total{device="tun0"} 0 @@ -1686,6 +1688,7 @@ node_network_receive_compressed_total{device="wlan0"} 0 # TYPE node_network_receive_drop_total counter node_network_receive_drop_total{device="docker0"} 0 node_network_receive_drop_total{device="eth0"} 0 +node_network_receive_drop_total{device="ibr10:30"} 0 node_network_receive_drop_total{device="lo"} 0 node_network_receive_drop_total{device="lxcbr0"} 0 node_network_receive_drop_total{device="tun0"} 0 @@ -1695,6 +1698,7 @@ node_network_receive_drop_total{device="wlan0"} 0 # TYPE node_network_receive_errs_total counter node_network_receive_errs_total{device="docker0"} 0 node_network_receive_errs_total{device="eth0"} 0 +node_network_receive_errs_total{device="ibr10:30"} 0 node_network_receive_errs_total{device="lo"} 0 node_network_receive_errs_total{device="lxcbr0"} 0 node_network_receive_errs_total{device="tun0"} 0 @@ -1704,6 +1708,7 @@ node_network_receive_errs_total{device="wlan0"} 0 # TYPE node_network_receive_fifo_total counter node_network_receive_fifo_total{device="docker0"} 0 node_network_receive_fifo_total{device="eth0"} 0 +node_network_receive_fifo_total{device="ibr10:30"} 0 node_network_receive_fifo_total{device="lo"} 0 node_network_receive_fifo_total{device="lxcbr0"} 0 node_network_receive_fifo_total{device="tun0"} 0 @@ -1713,6 +1718,7 @@ node_network_receive_fifo_total{device="wlan0"} 0 # TYPE node_network_receive_frame_total counter node_network_receive_frame_total{device="docker0"} 0 node_network_receive_frame_total{device="eth0"} 0 +node_network_receive_frame_total{device="ibr10:30"} 0 node_network_receive_frame_total{device="lo"} 0 node_network_receive_frame_total{device="lxcbr0"} 0 node_network_receive_frame_total{device="tun0"} 0 @@ -1722,6 +1728,7 @@ node_network_receive_frame_total{device="wlan0"} 0 # TYPE node_network_receive_multicast_total counter node_network_receive_multicast_total{device="docker0"} 0 node_network_receive_multicast_total{device="eth0"} 0 +node_network_receive_multicast_total{device="ibr10:30"} 0 node_network_receive_multicast_total{device="lo"} 0 node_network_receive_multicast_total{device="lxcbr0"} 0 node_network_receive_multicast_total{device="tun0"} 0 @@ -1731,6 +1738,7 @@ node_network_receive_multicast_total{device="wlan0"} 0 # TYPE node_network_receive_packets_total counter node_network_receive_packets_total{device="docker0"} 1.065585e+06 node_network_receive_packets_total{device="eth0"} 5.20993275e+08 +node_network_receive_packets_total{device="ibr10:30"} 0 node_network_receive_packets_total{device="lo"} 1.832522e+06 node_network_receive_packets_total{device="lxcbr0"} 0 node_network_receive_packets_total{device="tun0"} 24 @@ -1740,6 +1748,7 @@ node_network_receive_packets_total{device="wlan0"} 1.3899359e+07 # TYPE node_network_transmit_bytes_total counter node_network_transmit_bytes_total{device="docker0"} 2.681662018e+09 node_network_transmit_bytes_total{device="eth0"} 9.315587528e+09 +node_network_transmit_bytes_total{device="ibr10:30"} 0 node_network_transmit_bytes_total{device="lo"} 4.35303245e+08 node_network_transmit_bytes_total{device="lxcbr0"} 2.630299e+06 node_network_transmit_bytes_total{device="tun0"} 67120 @@ -1749,6 +1758,7 @@ node_network_transmit_bytes_total{device="wlan0"} 2.85164936e+09 # TYPE node_network_transmit_carrier_total counter node_network_transmit_carrier_total{device="docker0"} 0 node_network_transmit_carrier_total{device="eth0"} 0 +node_network_transmit_carrier_total{device="ibr10:30"} 0 node_network_transmit_carrier_total{device="lo"} 0 node_network_transmit_carrier_total{device="lxcbr0"} 0 node_network_transmit_carrier_total{device="tun0"} 0 @@ -1758,6 +1768,7 @@ node_network_transmit_carrier_total{device="wlan0"} 0 # TYPE node_network_transmit_colls_total counter node_network_transmit_colls_total{device="docker0"} 0 node_network_transmit_colls_total{device="eth0"} 0 +node_network_transmit_colls_total{device="ibr10:30"} 0 node_network_transmit_colls_total{device="lo"} 0 node_network_transmit_colls_total{device="lxcbr0"} 0 node_network_transmit_colls_total{device="tun0"} 0 @@ -1767,6 +1778,7 @@ node_network_transmit_colls_total{device="wlan0"} 0 # TYPE node_network_transmit_compressed_total counter node_network_transmit_compressed_total{device="docker0"} 0 node_network_transmit_compressed_total{device="eth0"} 0 +node_network_transmit_compressed_total{device="ibr10:30"} 0 node_network_transmit_compressed_total{device="lo"} 0 node_network_transmit_compressed_total{device="lxcbr0"} 0 node_network_transmit_compressed_total{device="tun0"} 0 @@ -1776,6 +1788,7 @@ node_network_transmit_compressed_total{device="wlan0"} 0 # TYPE node_network_transmit_drop_total counter node_network_transmit_drop_total{device="docker0"} 0 node_network_transmit_drop_total{device="eth0"} 0 +node_network_transmit_drop_total{device="ibr10:30"} 0 node_network_transmit_drop_total{device="lo"} 0 node_network_transmit_drop_total{device="lxcbr0"} 0 node_network_transmit_drop_total{device="tun0"} 0 @@ -1785,6 +1798,7 @@ node_network_transmit_drop_total{device="wlan0"} 0 # TYPE node_network_transmit_errs_total counter node_network_transmit_errs_total{device="docker0"} 0 node_network_transmit_errs_total{device="eth0"} 0 +node_network_transmit_errs_total{device="ibr10:30"} 0 node_network_transmit_errs_total{device="lo"} 0 node_network_transmit_errs_total{device="lxcbr0"} 0 node_network_transmit_errs_total{device="tun0"} 0 @@ -1794,6 +1808,7 @@ node_network_transmit_errs_total{device="wlan0"} 0 # TYPE node_network_transmit_fifo_total counter node_network_transmit_fifo_total{device="docker0"} 0 node_network_transmit_fifo_total{device="eth0"} 0 +node_network_transmit_fifo_total{device="ibr10:30"} 0 node_network_transmit_fifo_total{device="lo"} 0 node_network_transmit_fifo_total{device="lxcbr0"} 0 node_network_transmit_fifo_total{device="tun0"} 0 @@ -1803,6 +1818,7 @@ node_network_transmit_fifo_total{device="wlan0"} 0 # TYPE node_network_transmit_packets_total counter node_network_transmit_packets_total{device="docker0"} 1.929779e+06 node_network_transmit_packets_total{device="eth0"} 4.3451486e+07 +node_network_transmit_packets_total{device="ibr10:30"} 0 node_network_transmit_packets_total{device="lo"} 1.832522e+06 node_network_transmit_packets_total{device="lxcbr0"} 28339 node_network_transmit_packets_total{device="tun0"} 934 diff --git a/collector/fixtures/proc/net/dev b/collector/fixtures/proc/net/dev index 96dffa05..8b2dd6cf 100644 --- a/collector/fixtures/proc/net/dev +++ b/collector/fixtures/proc/net/dev @@ -7,3 +7,4 @@ veth4B09XN: 648 8 0 0 0 0 0 0 1943284 lxcbr0: 0 0 0 0 0 0 0 0 2630299 28339 0 0 0 0 0 0 wlan0: 10437182923 13899359 0 0 0 0 0 0 2851649360 11726200 0 0 0 0 0 0 docker0: 64910168 1065585 0 0 0 0 0 0 2681662018 1929779 0 0 0 0 0 0 +ibr10:30: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 diff --git a/collector/netdev_linux.go b/collector/netdev_linux.go index 19f60d74..468e444e 100644 --- a/collector/netdev_linux.go +++ b/collector/netdev_linux.go @@ -27,7 +27,8 @@ import ( ) var ( - procNetDevFieldSep = regexp.MustCompile("[ :] *") + procNetDevInterfaceRE = regexp.MustCompile(`^([\w:]+): *(.+)$`) + procNetDevFieldSep = regexp.MustCompile(` +`) ) func getNetDevStats(ignore *regexp.Regexp) (map[string]map[string]string, error) { @@ -52,28 +53,34 @@ func parseNetDevStats(r io.Reader, ignore *regexp.Regexp) (map[string]map[string receiveHeader := strings.Fields(parts[1]) transmitHeader := strings.Fields(parts[2]) - headerLength := len(receiveHeader) + len(transmitHeader) + 1 + headerLength := len(receiveHeader) + len(transmitHeader) netDev := map[string]map[string]string{} for scanner.Scan() { line := strings.TrimLeft(scanner.Text(), " ") - parts := procNetDevFieldSep.Split(line, -1) - if len(parts) != headerLength { - return nil, fmt.Errorf("invalid line in net/dev: %s", scanner.Text()) + parts := procNetDevInterfaceRE.FindStringSubmatch(line) + if len(parts) != 3 { + return nil, fmt.Errorf("couldn't get interface name, invalid line in net/dev: %q", line) } - dev := parts[0][:len(parts[0])] + dev := parts[1] if ignore.MatchString(dev) { log.Debugf("Ignoring device: %s", dev) continue } + + values := procNetDevFieldSep.Split(strings.TrimLeft(parts[2], " "), -1) + if len(values) != headerLength { + return nil, fmt.Errorf("couldn't get values, invalid line in net/dev: %q", parts[2]) + } + netDev[dev] = map[string]string{} for i := 0; i < len(receiveHeader); i++ { - netDev[dev]["receive_"+receiveHeader[i]] = parts[i+1] + netDev[dev]["receive_"+receiveHeader[i]] = values[i] } for i := 0; i < len(transmitHeader); i++ { - netDev[dev]["transmit_"+transmitHeader[i]] = parts[i+1+len(receiveHeader)] + netDev[dev]["transmit_"+transmitHeader[i]] = values[i+len(receiveHeader)] } } return netDev, scanner.Err() diff --git a/collector/netdev_linux_test.go b/collector/netdev_linux_test.go index 2adc12de..1c655b99 100644 --- a/collector/netdev_linux_test.go +++ b/collector/netdev_linux_test.go @@ -43,11 +43,15 @@ func TestNetDevStats(t *testing.T) { t.Errorf("want netstat tun0 packets %s, got %s", want, got) } - if want, got := 6, len(netStats); want != got { + if want, got := 7, len(netStats); want != got { t.Errorf("want count of devices to be %d, got %d", want, got) } if _, ok := netStats["veth4B09XN"]["transmit_bytes"]; ok { t.Error("want fixture interface veth4B09XN to not exist, but it does") } + + if want, got := "0", netStats["ibr10:30"]["receive_fifo"]; want != got { + t.Error("want fixture interface ibr10:30 to exist, but it does not") + } }