implements pernic of NetIOCounters.

pull/32/head
Shirou WAKAYAMA 11 years ago
parent a73ca04d3a
commit 406cbe9b87

@ -138,7 +138,7 @@ swap_memory x x x x
disk_partitions x x x x x
disk_io_counters x x
disk_usage x x x x x
net_io_counters x x x x x
net_io_counters x x x b x
boot_time x x x x b
users x x x x x
pids x x x x x
@ -252,7 +252,8 @@ I have been influenced by the following great works:
- goprocinfo: https://github.com/c9s/goprocinfo
- go-ps: https://github.com/mitchellh/go-ps
- ohai: https://github.com/opscode/ohai/
- bosun: https://github.com/bosun-monitor/bosun/tree/master/cmd/scollector/collectors
- mackerel: https://github.com/mackerelio/mackerel-agent/tree/master/metrics
How to Contribute
---------------------------

@ -1,5 +1,5 @@
//
// common is a port of psutil(http://pythonhosted.org/psutil/).
// gopsutil is a port of psutil(http://pythonhosted.org/psutil/).
// This covers these architectures.
// - linux (amd64, arm)
// - freebsd (amd64)

@ -117,3 +117,21 @@ func NetInterfaces() ([]NetInterfaceStat, error) {
return ret, nil
}
func getNetIOCountersAll(n []NetIOCountersStat) ([]NetIOCountersStat, error) {
r := NetIOCountersStat{
Name: "all",
}
for _, nic := range n {
r.BytesRecv += nic.BytesRecv
r.PacketsRecv += nic.PacketsRecv
r.Errin += nic.Errin
r.Dropin += nic.Dropin
r.BytesSent += nic.BytesSent
r.PacketsSent += nic.PacketsSent
r.Errout += nic.Errout
r.Dropout += nic.Dropout
}
return []NetIOCountersStat{r}, nil
}

@ -57,5 +57,9 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
ret = append(ret, n)
}
if pernic == false {
return getNetIOCountersAll(ret)
}
return ret, nil
}

@ -66,5 +66,9 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
ret = append(ret, n)
}
if pernic == false {
return getNetIOCountersAll(ret)
}
return ret, nil
}

@ -9,6 +9,11 @@ import (
common "github.com/shirou/gopsutil/common"
)
// NetIOCounters returnes network I/O statistics for every network
// interface installed on the system. If pernic argument is false,
// return only sum of all information (which name is 'all'). If true,
// every network interface installed on the system is returned
// separately.
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
filename := "/proc/net/dev"
lines, err := common.ReadLines(filename)
@ -77,5 +82,10 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
}
ret = append(ret, nic)
}
if pernic == false {
return getNetIOCountersAll(ret)
}
return ret, nil
}

@ -38,7 +38,28 @@ func TestNetConnectionStatString(t *testing.T) {
}
func TestNetIOCounters(t *testing.T) {
func TestNetIOCountersAll(t *testing.T) {
v, err := NetIOCounters(false)
per, err := NetIOCounters(true)
if err != nil {
t.Errorf("Could not get NetIOCounters: %v", err)
}
if len(v) != 1 {
t.Errorf("Could not get NetIOCounters: %v", v)
}
if v[0].Name != "all" {
t.Errorf("Invalid NetIOCounters: %v", v)
}
var pr uint64
for _, p := range per {
pr += p.PacketsRecv
}
if v[0].PacketsRecv != pr {
t.Errorf("invalid sum value: %v, %v", v[0].PacketsRecv, pr)
}
}
func TestNetIOCountersPerNic(t *testing.T) {
v, err := NetIOCounters(true)
if err != nil {
t.Errorf("Could not get NetIOCounters: %v", err)
@ -53,6 +74,38 @@ func TestNetIOCounters(t *testing.T) {
}
}
func Test_getNetIOCountersAll(t *testing.T) {
n := []NetIOCountersStat{
NetIOCountersStat{
Name: "a",
BytesRecv: 10,
PacketsRecv: 10,
},
NetIOCountersStat{
Name: "b",
BytesRecv: 10,
PacketsRecv: 10,
Errin: 10,
},
}
ret, err := getNetIOCountersAll(n)
if err != nil {
t.Error(err)
}
if len(ret) != 1 {
t.Errorf("invalid return count")
}
if ret[0].Name != "all" {
t.Errorf("invalid return name")
}
if ret[0].BytesRecv != 20 {
t.Errorf("invalid count bytesrecv")
}
if ret[0].Errin != 10 {
t.Errorf("invalid count errin")
}
}
func TestNetInterfaces(t *testing.T) {
v, err := NetInterfaces()
if err != nil {

@ -66,6 +66,10 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
ret = append(ret, c)
}
}
if pernic == false {
return getNetIOCountersAll(ret)
}
return ret, nil
}

Loading…
Cancel
Save