1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-26 13:48:59 +08:00
shirou_gopsutil/net_linux.go

39 lines
827 B
Go
Raw Normal View History

2014-04-28 10:16:38 +09:00
// +build linux
package gopsutil
import (
"strings"
)
2014-04-30 15:32:05 +09:00
func NetIOCounters() ([]NetIOCountersStat, error) {
2014-04-28 10:16:38 +09:00
filename := "/proc/net/dev"
lines, err := ReadLines(filename)
2014-04-29 14:59:22 +09:00
if err != nil {
2014-04-30 16:16:07 +09:00
return make([]NetIOCountersStat, 0), err
2014-04-28 10:16:38 +09:00
}
statlen := len(lines) - 1
2014-04-30 16:16:07 +09:00
ret := make([]NetIOCountersStat, 0, statlen)
2014-04-28 10:16:38 +09:00
for _, line := range lines[2:] {
fields := strings.Fields(line)
2014-04-29 14:59:22 +09:00
if fields[0] == "" {
2014-04-28 10:16:38 +09:00
continue
}
2014-04-30 16:16:07 +09:00
nic := NetIOCountersStat{
Name: strings.Trim(fields[0], ":"),
BytesRecv: parseUint64(fields[1]),
Errin: parseUint64(fields[2]),
Dropin: parseUint64(fields[3]),
BytesSent: parseUint64(fields[9]),
PacketsSent: parseUint64(fields[10]),
Errout: parseUint64(fields[11]),
Dropout: parseUint64(fields[12]),
2014-04-28 10:16:38 +09:00
}
ret = append(ret, nic)
}
return ret, nil
}