implements net_io_counters on linux.
This commit is contained in:
parent
d1b1221e7f
commit
6c59081710
1
net.go
1
net.go
|
@ -1,6 +1,7 @@
|
||||||
package gopsutil
|
package gopsutil
|
||||||
|
|
||||||
type Net_io_countersStat struct {
|
type Net_io_countersStat struct {
|
||||||
|
Name string `json:"name""` // interface name
|
||||||
Bytes_sent uint64 `json:"bytes_sent""` // number of bytes sent
|
Bytes_sent uint64 `json:"bytes_sent""` // number of bytes sent
|
||||||
Bytes_recv uint64 `json:"bytes_recv"` // number of bytes received
|
Bytes_recv uint64 `json:"bytes_recv"` // number of bytes received
|
||||||
Packets_sent uint64 `json:"packets_sent"` // number of packets sent
|
Packets_sent uint64 `json:"packets_sent"` // number of packets sent
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
// +build linux
|
||||||
|
|
||||||
|
package gopsutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Net_io_counters() ([]Net_io_countersStat, error) {
|
||||||
|
filename := "/proc/net/dev"
|
||||||
|
lines, err := ReadLines(filename)
|
||||||
|
if err != nil{
|
||||||
|
return make([]Net_io_countersStat, 0), err
|
||||||
|
}
|
||||||
|
|
||||||
|
statlen := len(lines) - 1
|
||||||
|
|
||||||
|
ret := make([]Net_io_countersStat, 0, statlen)
|
||||||
|
|
||||||
|
for _, line := range lines[2:] {
|
||||||
|
fields := strings.Fields(line)
|
||||||
|
if fields[0] == ""{
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
nic := Net_io_countersStat{
|
||||||
|
Name: strings.Trim(fields[0], ":"),
|
||||||
|
Bytes_recv: parseUint64(fields[1]),
|
||||||
|
Errin: parseUint64(fields[2]),
|
||||||
|
Dropin: parseUint64(fields[3]),
|
||||||
|
Bytes_sent: parseUint64(fields[9]),
|
||||||
|
Packets_sent: parseUint64(fields[10]),
|
||||||
|
Errout: parseUint64(fields[11]),
|
||||||
|
Dropout: parseUint64(fields[12]),
|
||||||
|
}
|
||||||
|
ret = append(ret, nic)
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
|
}
|
Loading…
Reference in New Issue