@ -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 ) {
return netlinkStats ( links , filter , logger ) , nil
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 ] )
func netlinkStats ( links [ ] rtnetlink . LinkMessage , filter * deviceFilter , logger log . Logger ) netDevStats {
transmitHeader := strings . Fields ( parts [ 2 ] )
metrics := netDevStats { }
headerLength := len ( receiveHeader ) + len ( transmitHeader )
netDev := netDevStats { }
for _ , msg := range links {
for scanner . Scan ( ) {
name := msg . Attributes . Name
line := strings . TrimLeft ( scanner . Text ( ) , " " )
stats := msg . Attributes . Stats64
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 ]
if filter . ignored ( name ) {
if filter . ignored ( dev ) {
level . Debug ( logger ) . Log ( "msg" , "Ignoring device" , "device" , name )
level . Debug ( logger ) . Log ( "msg" , "Ignoring device" , "device" , dev )
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 ,
devStats := map [ string ] uint64 { }
"receive_bytes" : stats . RXBytes ,
addStats := func ( key , value string ) {
"transmit_bytes" : stats . TXBytes ,
v , err := strconv . ParseUint ( value , 0 , 64 )
"receive_errs" : stats . RXErrors ,
if err != nil {
"transmit_errs" : stats . TXErrors ,
level . Debug ( logger ) . Log ( "msg" , "invalid value in netstats" , "key" , key , "value" , value , "err" , err )
"receive_drop" : stats . RXDropped + stats . RXMissedErrors ,
return
"transmit_drop" : stats . TXDropped ,
}
"receive_multicast" : stats . Multicast ,
"transmit_colls" : stats . Collisions ,
devStats [ key ] = v
"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 ,
}
}
for i := 0 ; i < len ( receiveHeader ) ; i ++ {
addStats ( "receive_" + receiveHeader [ i ] , values [ i ] )
}
}
for i := 0 ; i < len ( transmitHeader ) ; i ++ {
return metrics
addStats ( "transmit_" + transmitHeader [ i ] , values [ i + len ( receiveHeader ) ] )
}
netDev [ dev ] = devStats
}
return netDev , scanner . Err ( )
}
}