2014-04-28 10:16:38 +09:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package gopsutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-04-30 16:28:06 +09:00
|
|
|
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
|
2014-04-28 10:16:38 +09:00
|
|
|
filename := "/proc/net/dev"
|
2014-04-30 16:22:54 +09:00
|
|
|
lines, err := readLines(filename)
|
2014-04-29 14:59:22 +09:00
|
|
|
if err != nil {
|
2014-05-01 12:01:30 +09:00
|
|
|
return nil, 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], ":"),
|
2014-05-18 22:43:12 +09:00
|
|
|
BytesRecv: mustParseUint64(fields[1]),
|
|
|
|
Errin: mustParseUint64(fields[2]),
|
|
|
|
Dropin: mustParseUint64(fields[3]),
|
|
|
|
BytesSent: mustParseUint64(fields[9]),
|
|
|
|
PacketsSent: mustParseUint64(fields[10]),
|
|
|
|
Errout: mustParseUint64(fields[11]),
|
|
|
|
Dropout: mustParseUint64(fields[12]),
|
2014-04-28 10:16:38 +09:00
|
|
|
}
|
|
|
|
ret = append(ret, nic)
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|