forked from shirou/gopsutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_linux.go
39 lines (33 loc) · 885 Bytes
/
net_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// +build linux
package gopsutil
import (
"strings"
)
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
filename := "/proc/net/dev"
lines, err := readLines(filename)
if err != nil {
return nil, err
}
statlen := len(lines) - 1
ret := make([]NetIOCountersStat, 0, statlen)
for _, line := range lines[2:] {
fields := strings.Fields(line)
if fields[0] == "" {
continue
}
nic := NetIOCountersStat{
Name: strings.Trim(fields[0], ":"),
BytesRecv: mustParseUint64(fields[1]),
PacketsRecv: mustParseUint64(fields[2]),
Errin: mustParseUint64(fields[3]),
Dropin: mustParseUint64(fields[4]),
BytesSent: mustParseUint64(fields[9]),
PacketsSent: mustParseUint64(fields[10]),
Errout: mustParseUint64(fields[11]),
Dropout: mustParseUint64(fields[12]),
}
ret = append(ret, nic)
}
return ret, nil
}