@@ -15,6 +15,7 @@ import (
1515
1616const (
1717 procNetStat = "/proc/net/netstat"
18+ procSNMPStat = "/proc/net/snmp"
1819 netStatsSubsystem = "netstat"
1920)
2021
@@ -35,10 +36,19 @@ func NewNetStatCollector() (Collector, error) {
3536}
3637
3738func (c * netStatCollector ) Update (ch chan <- prometheus.Metric ) (err error ) {
38- netStats , err := getNetStats ()
39+ netStats , err := getNetStats (procNetStat )
3940 if err != nil {
4041 return fmt .Errorf ("couldn't get netstats: %s" , err )
4142 }
43+ snmpStats , err := getNetStats (procSNMPStat )
44+ if err != nil {
45+ return fmt .Errorf ("couldn't get SNMP stats: %s" , err )
46+ }
47+ // Merge the results of snmpStats into netStats (collisions are possible, but
48+ // we know that the keys are always unique for the given use case)
49+ for k , v := range snmpStats {
50+ netStats [k ] = v
51+ }
4252 for protocol , protocolStats := range netStats {
4353 for name , value := range protocolStats {
4454 key := protocol + "_" + name
@@ -48,7 +58,7 @@ func (c *netStatCollector) Update(ch chan<- prometheus.Metric) (err error) {
4858 Namespace : Namespace ,
4959 Subsystem : netStatsSubsystem ,
5060 Name : key ,
51- Help : fmt .Sprintf ("%s %s from /proc/net/netstat." , protocol , name ),
61+ Help : fmt .Sprintf ("%s %s from /proc/net/{ netstat,snmp} ." , protocol , name ),
5262 },
5363 )
5464 }
@@ -65,17 +75,17 @@ func (c *netStatCollector) Update(ch chan<- prometheus.Metric) (err error) {
6575 return err
6676}
6777
68- func getNetStats () (map [string ]map [string ]string , error ) {
69- file , err := os .Open (procNetStat )
78+ func getNetStats (fileName string ) (map [string ]map [string ]string , error ) {
79+ file , err := os .Open (fileName )
7080 if err != nil {
7181 return nil , err
7282 }
7383 defer file .Close ()
7484
75- return parseNetStats (file )
85+ return parseNetStats (file , fileName )
7686}
7787
78- func parseNetStats (r io.Reader ) (map [string ]map [string ]string , error ) {
88+ func parseNetStats (r io.Reader , fileName string ) (map [string ]map [string ]string , error ) {
7989 var (
8090 netStats = map [string ]map [string ]string {}
8191 scanner = bufio .NewScanner (r )
@@ -90,7 +100,7 @@ func parseNetStats(r io.Reader) (map[string]map[string]string, error) {
90100 netStats [protocol ] = map [string ]string {}
91101 if len (nameParts ) != len (valueParts ) {
92102 return nil , fmt .Errorf ("mismatch field count mismatch in %s: %s" ,
93- procNetStat , protocol )
103+ fileName , protocol )
94104 }
95105 for i := 1 ; i < len (nameParts ); i ++ {
96106 netStats [protocol ][nameParts [i ]] = valueParts [i ]
0 commit comments