From 072ddabf6276132b661284bf1ca3d0b114a6288a Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Thu, 21 May 2015 00:34:24 +0200 Subject: [PATCH] Fix /proc/net/dev column parsing. This fixes https://github.com/prometheus/node_exporter/issues/73 --- collector/fixtures/net-dev | 1 + collector/netdev.go | 10 ++++++++-- collector/netdev_test.go | 4 ++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/collector/fixtures/net-dev b/collector/fixtures/net-dev index 6209c16b..96dffa05 100644 --- a/collector/fixtures/net-dev +++ b/collector/fixtures/net-dev @@ -3,6 +3,7 @@ Inter-| Receive | Transmit tun0: 1888 24 0 0 0 0 0 0 67120 934 0 0 0 0 0 0 veth4B09XN: 648 8 0 0 0 0 0 0 1943284 10640 0 0 0 0 0 0 lo: 435303245 1832522 0 0 0 0 0 0 435303245 1832522 0 0 0 0 0 0 + eth0:68210035552 520993275 0 0 0 0 0 0 9315587528 43451486 0 0 0 0 0 0 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 diff --git a/collector/netdev.go b/collector/netdev.go index e5d0afdf..bbf87334 100644 --- a/collector/netdev.go +++ b/collector/netdev.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "os" + "regexp" "strconv" "strings" @@ -18,6 +19,10 @@ const ( netDevSubsystem = "network" ) +var ( + fieldSep = regexp.MustCompile("[ :] *") +) + type netDevCollector struct { config Config metrics map[string]*prometheus.GaugeVec @@ -95,13 +100,14 @@ func parseNetDevStats(r io.Reader) (map[string]map[string]map[string]string, err } header := strings.Fields(parts[1]) for scanner.Scan() { - parts := strings.Fields(string(scanner.Text())) + line := strings.TrimLeft(string(scanner.Text()), " ") + parts := fieldSep.Split(line, -1) if len(parts) != 2*len(header)+1 { return nil, fmt.Errorf("Invalid line in %s: %s", procNetDev, scanner.Text()) } - dev := parts[0][:len(parts[0])-1] + dev := parts[0][:len(parts[0])] receive, err := parseNetDevLine(parts[1:len(header)+1], header) if err != nil { return nil, err diff --git a/collector/netdev_test.go b/collector/netdev_test.go index 98011eac..7eb22413 100644 --- a/collector/netdev_test.go +++ b/collector/netdev_test.go @@ -21,6 +21,10 @@ func TestNetDevStats(t *testing.T) { t.Errorf("want netstat wlan0 bytes %s, got %s", want, got) } + if want, got := "68210035552", netStats["receive"]["eth0"]["bytes"]; want != got { + t.Errorf("want netstat eth0 bytes %s, got %s", want, got) + } + if want, got := "934", netStats["transmit"]["tun0"]["packets"]; want != got { t.Errorf("want netstat tun0 packets %s, got %s", want, got) }