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

75 lines
1.5 KiB
Go
Raw Normal View History

2014-04-28 10:16:38 +09:00
// +build linux
package gopsutil
import (
"strconv"
2014-04-28 10:16:38 +09:00
"strings"
)
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 {
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
}
bytesRecv, err := strconv.ParseUint(fields[1], 10, 64)
if err != nil {
return ret, err
}
packetsRecv, err := strconv.ParseUint(fields[2], 10, 64)
if err != nil {
return ret, err
}
errIn, err := strconv.ParseUint(fields[3], 10, 64)
if err != nil {
return ret, err
}
dropIn, err := strconv.ParseUint(fields[4], 10, 64)
if err != nil {
return ret, err
}
bytesSent, err := strconv.ParseUint(fields[9], 10, 64)
if err != nil {
return ret, err
}
packetsSent, err := strconv.ParseUint(fields[10], 10, 64)
if err != nil {
return ret, err
}
errOut, err := strconv.ParseUint(fields[11], 10, 64)
if err != nil {
return ret, err
}
dropOut, err := strconv.ParseUint(fields[14], 10, 64)
if err != nil {
return ret, err
}
2014-04-30 16:16:07 +09:00
nic := NetIOCountersStat{
Name: strings.Trim(fields[0], ":"),
BytesRecv: bytesRecv,
PacketsRecv: packetsRecv,
Errin: errIn,
Dropin: dropIn,
BytesSent: bytesSent,
PacketsSent: packetsSent,
Errout: errOut,
Dropout: dropOut,
2014-04-28 10:16:38 +09:00
}
ret = append(ret, nic)
}
return ret, nil
}