|
|
@ -15,6 +15,7 @@ import ( |
|
|
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
const ( |
|
|
|
procNetStat = "/proc/net/netstat" |
|
|
|
procNetStat = "/proc/net/netstat" |
|
|
|
|
|
|
|
procSNMPStat = "/proc/net/snmp" |
|
|
|
netStatsSubsystem = "netstat" |
|
|
|
netStatsSubsystem = "netstat" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
@ -35,10 +36,19 @@ func NewNetStatCollector() (Collector, error) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (c *netStatCollector) Update(ch chan<- prometheus.Metric) (err error) { |
|
|
|
func (c *netStatCollector) Update(ch chan<- prometheus.Metric) (err error) { |
|
|
|
netStats, err := getNetStats() |
|
|
|
netStats, err := getNetStats(procNetStat) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("couldn't get netstats: %s", err) |
|
|
|
return fmt.Errorf("couldn't get netstats: %s", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
snmpStats, err := getNetStats(procSNMPStat) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("couldn't get SNMP stats: %s", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Merge the results of snmpStats into netStats (collisions are possible, but
|
|
|
|
|
|
|
|
// we know that the keys are always unique for the given use case)
|
|
|
|
|
|
|
|
for k, v := range snmpStats { |
|
|
|
|
|
|
|
netStats[k] = v |
|
|
|
|
|
|
|
} |
|
|
|
for protocol, protocolStats := range netStats { |
|
|
|
for protocol, protocolStats := range netStats { |
|
|
|
for name, value := range protocolStats { |
|
|
|
for name, value := range protocolStats { |
|
|
|
key := protocol + "_" + name |
|
|
|
key := protocol + "_" + name |
|
|
@ -48,7 +58,7 @@ func (c *netStatCollector) Update(ch chan<- prometheus.Metric) (err error) { |
|
|
|
Namespace: Namespace, |
|
|
|
Namespace: Namespace, |
|
|
|
Subsystem: netStatsSubsystem, |
|
|
|
Subsystem: netStatsSubsystem, |
|
|
|
Name: key, |
|
|
|
Name: key, |
|
|
|
Help: fmt.Sprintf("%s %s from /proc/net/netstat.", protocol, name), |
|
|
|
Help: fmt.Sprintf("%s %s from /proc/net/{netstat,snmp}.", protocol, name), |
|
|
|
}, |
|
|
|
}, |
|
|
|
) |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
@ -65,17 +75,17 @@ func (c *netStatCollector) Update(ch chan<- prometheus.Metric) (err error) { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func getNetStats() (map[string]map[string]string, error) { |
|
|
|
func getNetStats(fileName string) (map[string]map[string]string, error) { |
|
|
|
file, err := os.Open(procNetStat) |
|
|
|
file, err := os.Open(fileName) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
defer file.Close() |
|
|
|
defer file.Close() |
|
|
|
|
|
|
|
|
|
|
|
return parseNetStats(file) |
|
|
|
return parseNetStats(file, fileName) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func parseNetStats(r io.Reader) (map[string]map[string]string, error) { |
|
|
|
func parseNetStats(r io.Reader, fileName string) (map[string]map[string]string, error) { |
|
|
|
var ( |
|
|
|
var ( |
|
|
|
netStats = map[string]map[string]string{} |
|
|
|
netStats = map[string]map[string]string{} |
|
|
|
scanner = bufio.NewScanner(r) |
|
|
|
scanner = bufio.NewScanner(r) |
|
|
@ -90,7 +100,7 @@ func parseNetStats(r io.Reader) (map[string]map[string]string, error) { |
|
|
|
netStats[protocol] = map[string]string{} |
|
|
|
netStats[protocol] = map[string]string{} |
|
|
|
if len(nameParts) != len(valueParts) { |
|
|
|
if len(nameParts) != len(valueParts) { |
|
|
|
return nil, fmt.Errorf("mismatch field count mismatch in %s: %s", |
|
|
|
return nil, fmt.Errorf("mismatch field count mismatch in %s: %s", |
|
|
|
procNetStat, protocol) |
|
|
|
fileName, protocol) |
|
|
|
} |
|
|
|
} |
|
|
|
for i := 1; i < len(nameParts); i++ { |
|
|
|
for i := 1; i < len(nameParts); i++ { |
|
|
|
netStats[protocol][nameParts[i]] = valueParts[i] |
|
|
|
netStats[protocol][nameParts[i]] = valueParts[i] |
|
|
|