Add parsing /proc/net/snmp6 file for netstat-linux (#615)

* Add parsing /proc/net/snmp6 file

* add /proc/net/snmp6 fixture

* fix e2e test

* gofmt

* remove unuser variable

* safe checks

* add tests

* change help format
This commit is contained in:
Aleksey Zhukov 2017-07-08 21:16:35 +03:00 committed by Ben Kochie
commit 7a914e58f2
5 changed files with 581 additions and 175 deletions

View file

@ -51,11 +51,18 @@ func (c *netStatCollector) Update(ch chan<- prometheus.Metric) error {
if err != nil {
return fmt.Errorf("couldn't get SNMP stats: %s", err)
}
snmp6Stats, err := getSNMP6Stats(procFilePath("net/snmp6"))
if err != nil {
return fmt.Errorf("couldn't get SNMP6 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 k, v := range snmp6Stats {
netStats[k] = v
}
for protocol, protocolStats := range netStats {
for name, value := range protocolStats {
key := protocol + "_" + name
@ -66,7 +73,7 @@ func (c *netStatCollector) Update(ch chan<- prometheus.Metric) error {
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(Namespace, netStatsSubsystem, key),
fmt.Sprintf("Protocol %s statistic %s.", protocol, name),
fmt.Sprintf("Statistic %s.", protocol+name),
nil, nil,
),
prometheus.UntypedValue, v,
@ -110,3 +117,38 @@ func parseNetStats(r io.Reader, fileName string) (map[string]map[string]string,
return netStats, scanner.Err()
}
func getSNMP6Stats(fileName string) (map[string]map[string]string, error) {
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer file.Close()
return parseSNMP6Stats(file)
}
func parseSNMP6Stats(r io.Reader) (map[string]map[string]string, error) {
var (
netStats = map[string]map[string]string{}
scanner = bufio.NewScanner(r)
)
for scanner.Scan() {
stat := strings.Fields(scanner.Text())
if len(stat) < 2 {
continue
}
// Expect to have "6" in metric name, skip line otherwise
if sixIndex := strings.Index(stat[0], "6"); sixIndex != -1 {
protocol := stat[0][:sixIndex+1]
name := stat[0][sixIndex+1:]
if _, present := netStats[protocol]; !present {
netStats[protocol] = map[string]string{}
}
netStats[protocol][name] = stat[1]
}
}
return netStats, scanner.Err()
}