Merge pull request #76 from prometheus/fix-netdev-parsing

Fix /proc/net/dev column parsing.
This commit is contained in:
Julius Volz 2015-05-21 01:01:38 +02:00
commit 00ddf58a1b
3 changed files with 13 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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)
}