collector/netdev_linux.go: Use netlink to get stats

Instead of parsing `/proc/net/dev` to get network interface statistics, get
them from a netlink call.

Internally, both come from the [rtnl_link_stats64] struct, but with
`/proc/net/dev`, some of the values are aggregated together in
[dev_seq_printf_stats], so we get less information out of them.

This commit maintains compatibility by aggregating those stats back into the
same metrics.

[rtnl_link_stats64]:    https://github.com/torvalds/linux/blob/master/include/uapi/linux/if_link.h#L42-L246
[dev_seq_printf_stats]: https://github.com/torvalds/linux/blob/master/net/core/net-procfs.c#L75-L97

Signed-off-by: Benoît Knecht <bknecht@protonmail.ch>
This commit is contained in:
Benoît Knecht 2021-07-07 11:05:06 +02:00
parent 5d6738e6c5
commit f23a956c4f
2 changed files with 149 additions and 84 deletions

View File

@ -17,86 +17,60 @@
package collector package collector
import ( import (
"bufio"
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
"github.com/go-kit/log" "github.com/go-kit/log"
"github.com/go-kit/log/level" "github.com/go-kit/log/level"
)
var ( "github.com/jsimonetti/rtnetlink"
procNetDevInterfaceRE = regexp.MustCompile(`^(.+): *(.+)$`)
procNetDevFieldSep = regexp.MustCompile(` +`)
) )
func getNetDevStats(filter *deviceFilter, logger log.Logger) (netDevStats, error) { func getNetDevStats(filter *deviceFilter, logger log.Logger) (netDevStats, error) {
file, err := os.Open(procFilePath("net/dev")) conn, err := rtnetlink.Dial(nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer file.Close() defer conn.Close()
return parseNetDevStats(file, filter, logger) links, err := conn.Link.List()
} if err != nil {
return nil, err
func parseNetDevStats(r io.Reader, filter *deviceFilter, logger log.Logger) (netDevStats, error) {
scanner := bufio.NewScanner(r)
scanner.Scan() // skip first header
scanner.Scan()
parts := strings.Split(scanner.Text(), "|")
if len(parts) != 3 { // interface + receive + transmit
return nil, fmt.Errorf("invalid header line in net/dev: %s",
scanner.Text())
} }
receiveHeader := strings.Fields(parts[1]) return netlinkStats(links, filter, logger), nil
transmitHeader := strings.Fields(parts[2]) }
headerLength := len(receiveHeader) + len(transmitHeader)
netDev := netDevStats{} func netlinkStats(links []rtnetlink.LinkMessage, filter *deviceFilter, logger log.Logger) netDevStats {
for scanner.Scan() { metrics := netDevStats{}
line := strings.TrimLeft(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[1] for _, msg := range links {
if filter.ignored(dev) { name := msg.Attributes.Name
level.Debug(logger).Log("msg", "Ignoring device", "device", dev) stats := msg.Attributes.Stats64
if filter.ignored(name) {
level.Debug(logger).Log("msg", "Ignoring device", "device", name)
continue continue
} }
values := procNetDevFieldSep.Split(strings.TrimLeft(parts[2], " "), -1) // https://github.com/torvalds/linux/blob/master/include/uapi/linux/if_link.h#L42-L246
if len(values) != headerLength { // https://github.com/torvalds/linux/blob/master/net/core/net-procfs.c#L75-L97
return nil, fmt.Errorf("couldn't get values, invalid line in net/dev: %q", parts[2]) metrics[name] = map[string]uint64{
"receive_packets": stats.RXPackets,
"transmit_packets": stats.TXPackets,
"receive_bytes": stats.RXBytes,
"transmit_bytes": stats.TXBytes,
"receive_errs": stats.RXErrors,
"transmit_errs": stats.TXErrors,
"receive_drop": stats.RXDropped + stats.RXMissedErrors,
"transmit_drop": stats.TXDropped,
"receive_multicast": stats.Multicast,
"transmit_colls": stats.Collisions,
"receive_frame": stats.RXLengthErrors + stats.RXOverErrors + stats.RXCRCErrors + stats.RXFrameErrors,
"receive_fifo": stats.RXFIFOErrors,
"transmit_carrier": stats.TXAbortedErrors + stats.TXCarrierErrors + stats.TXHeartbeatErrors + stats.TXWindowErrors,
"transmit_fifo": stats.TXFIFOErrors,
"receive_compressed": stats.RXCompressed,
"transmit_compressed": stats.TXCompressed,
} }
devStats := map[string]uint64{}
addStats := func(key, value string) {
v, err := strconv.ParseUint(value, 0, 64)
if err != nil {
level.Debug(logger).Log("msg", "invalid value in netstats", "key", key, "value", value, "err", err)
return
}
devStats[key] = v
}
for i := 0; i < len(receiveHeader); i++ {
addStats("receive_"+receiveHeader[i], values[i])
}
for i := 0; i < len(transmitHeader); i++ {
addStats("transmit_"+transmitHeader[i], values[i+len(receiveHeader)])
}
netDev[dev] = devStats
} }
return netDev, scanner.Err()
return metrics
} }

View File

@ -14,25 +14,125 @@
package collector package collector
import ( import (
"os"
"testing" "testing"
"github.com/go-kit/log" "github.com/go-kit/log"
"github.com/jsimonetti/rtnetlink"
) )
func TestNetDevStatsIgnore(t *testing.T) { var links = []rtnetlink.LinkMessage{
file, err := os.Open("fixtures/proc/net/dev") {
if err != nil { Attributes: &rtnetlink.LinkAttributes{
t.Fatal(err) Name: "tun0",
} Stats64: &rtnetlink.LinkStats64{
defer file.Close() RXPackets: 24,
TXPackets: 934,
RXBytes: 1888,
TXBytes: 67120,
},
},
},
{
Attributes: &rtnetlink.LinkAttributes{
Name: "veth4B09XN",
Stats64: &rtnetlink.LinkStats64{
RXPackets: 8,
TXPackets: 10640,
RXBytes: 648,
TXBytes: 1943284,
},
},
},
{
Attributes: &rtnetlink.LinkAttributes{
Name: "lo",
Stats64: &rtnetlink.LinkStats64{
RXPackets: 1832522,
TXPackets: 1832522,
RXBytes: 435303245,
TXBytes: 435303245,
},
},
},
{
Attributes: &rtnetlink.LinkAttributes{
Name: "eth0",
Stats64: &rtnetlink.LinkStats64{
RXPackets: 520993275,
TXPackets: 43451486,
RXBytes: 68210035552,
TXBytes: 9315587528,
},
},
},
{
Attributes: &rtnetlink.LinkAttributes{
Name: "lxcbr0",
Stats64: &rtnetlink.LinkStats64{
TXPackets: 28339,
TXBytes: 2630299,
},
},
},
{
Attributes: &rtnetlink.LinkAttributes{
Name: "wlan0",
Stats64: &rtnetlink.LinkStats64{
RXPackets: 13899359,
TXPackets: 11726200,
RXBytes: 10437182923,
TXBytes: 2851649360,
},
},
},
{
Attributes: &rtnetlink.LinkAttributes{
Name: "docker0",
Stats64: &rtnetlink.LinkStats64{
RXPackets: 1065585,
TXPackets: 1929779,
RXBytes: 64910168,
TXBytes: 2681662018,
},
},
},
{
Attributes: &rtnetlink.LinkAttributes{
Name: "ibr10:30",
Stats64: &rtnetlink.LinkStats64{},
},
},
{
Attributes: &rtnetlink.LinkAttributes{
Name: "flannel.1",
Stats64: &rtnetlink.LinkStats64{
RXPackets: 228499337,
TXPackets: 258369223,
RXBytes: 18144009813,
TXBytes: 20758990068,
TXDropped: 64,
},
},
},
{
Attributes: &rtnetlink.LinkAttributes{
Name: "💩0",
Stats64: &rtnetlink.LinkStats64{
RXPackets: 105557,
TXPackets: 304261,
RXBytes: 57750104,
TXBytes: 404570255,
Multicast: 72,
},
},
},
}
func TestNetDevStatsIgnore(t *testing.T) {
filter := newDeviceFilter("^veth", "") filter := newDeviceFilter("^veth", "")
netStats, err := parseNetDevStats(file, &filter, log.NewNopLogger()) netStats := netlinkStats(links, &filter, log.NewNopLogger())
if err != nil {
t.Fatal(err)
}
if want, got := uint64(10437182923), netStats["wlan0"]["receive_bytes"]; want != got { if want, got := uint64(10437182923), netStats["wlan0"]["receive_bytes"]; want != got {
t.Errorf("want netstat wlan0 bytes %v, got %v", want, got) t.Errorf("want netstat wlan0 bytes %v, got %v", want, got)
@ -64,17 +164,8 @@ func TestNetDevStatsIgnore(t *testing.T) {
} }
func TestNetDevStatsAccept(t *testing.T) { func TestNetDevStatsAccept(t *testing.T) {
file, err := os.Open("fixtures/proc/net/dev")
if err != nil {
t.Fatal(err)
}
defer file.Close()
filter := newDeviceFilter("", "^💩0$") filter := newDeviceFilter("", "^💩0$")
netStats, err := parseNetDevStats(file, &filter, log.NewNopLogger()) netStats := netlinkStats(links, &filter, log.NewNopLogger())
if err != nil {
t.Fatal(err)
}
if want, got := 1, len(netStats); want != got { if want, got := 1, len(netStats); want != got {
t.Errorf("want count of devices to be %d, got %d", want, got) t.Errorf("want count of devices to be %d, got %d", want, got)