MCPcopy Create free account
hub / github.com/prometheus/procfs / parseNetstat

Function parseNetstat

netstat.go:53–82  ·  view source on GitHub ↗

parseNetstat parses the metrics from `/proc/net/stat/` file and returns a NetStat structure.

(filePath string)

Source from the content-addressed store, hash-verified

51// parseNetstat parses the metrics from `/proc/net/stat/` file
52// and returns a NetStat structure.
53func parseNetstat(filePath string) (NetStat, error) {
54 netStat := NetStat{
55 Stats: make(map[string][]uint64),
56 }
57 file, err := os.Open(filePath)
58 if err != nil {
59 return netStat, err
60 }
61 defer file.Close()
62
63 scanner := bufio.NewScanner(file)
64 scanner.Scan()
65
66 // First string is always a header for stats
67 var headers []string
68 headers = append(headers, strings.Fields(scanner.Text())...)
69
70 // Other strings represent per-CPU counters
71 for scanner.Scan() {
72 for num, counter := range strings.Fields(scanner.Text()) {
73 value, err := strconv.ParseUint(counter, 16, 64)
74 if err != nil {
75 return NetStat{}, err
76 }
77 netStat.Stats[headers[num]] = append(netStat.Stats[headers[num]], value)
78 }
79 }
80
81 return netStat, nil
82}

Callers 1

NetStatMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected