Skip to content

Commit 406cbe9

Browse files
committed
implements pernic of NetIOCounters.
1 parent a73ca04 commit 406cbe9

File tree

8 files changed

+98
-4
lines changed

8 files changed

+98
-4
lines changed

README.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ swap_memory x x x x
138138
disk_partitions x x x x x
139139
disk_io_counters x x
140140
disk_usage x x x x x
141-
net_io_counters x x x x x
141+
net_io_counters x x x b x
142142
boot_time x x x x b
143143
users x x x x x
144144
pids x x x x x
@@ -252,7 +252,8 @@ I have been influenced by the following great works:
252252
- goprocinfo: https://github.com/c9s/goprocinfo
253253
- go-ps: https://github.com/mitchellh/go-ps
254254
- ohai: https://github.com/opscode/ohai/
255-
255+
- bosun: https://github.com/bosun-monitor/bosun/tree/master/cmd/scollector/collectors
256+
- mackerel: https://github.com/mackerelio/mackerel-agent/tree/master/metrics
256257

257258
How to Contribute
258259
---------------------------

common/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// common is a port of psutil(http://pythonhosted.org/psutil/).
2+
// gopsutil is a port of psutil(http://pythonhosted.org/psutil/).
33
// This covers these architectures.
44
// - linux (amd64, arm)
55
// - freebsd (amd64)

net/net.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,21 @@ func NetInterfaces() ([]NetInterfaceStat, error) {
117117

118118
return ret, nil
119119
}
120+
121+
func getNetIOCountersAll(n []NetIOCountersStat) ([]NetIOCountersStat, error) {
122+
r := NetIOCountersStat{
123+
Name: "all",
124+
}
125+
for _, nic := range n {
126+
r.BytesRecv += nic.BytesRecv
127+
r.PacketsRecv += nic.PacketsRecv
128+
r.Errin += nic.Errin
129+
r.Dropin += nic.Dropin
130+
r.BytesSent += nic.BytesSent
131+
r.PacketsSent += nic.PacketsSent
132+
r.Errout += nic.Errout
133+
r.Dropout += nic.Dropout
134+
}
135+
136+
return []NetIOCountersStat{r}, nil
137+
}

net/net_darwin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,9 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
5757
ret = append(ret, n)
5858
}
5959

60+
if pernic == false {
61+
return getNetIOCountersAll(ret)
62+
}
63+
6064
return ret, nil
6165
}

net/net_freebsd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,9 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
6666
ret = append(ret, n)
6767
}
6868

69+
if pernic == false {
70+
return getNetIOCountersAll(ret)
71+
}
72+
6973
return ret, nil
7074
}

net/net_linux.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import (
99
common "github.com/shirou/gopsutil/common"
1010
)
1111

12+
// NetIOCounters returnes network I/O statistics for every network
13+
// interface installed on the system. If pernic argument is false,
14+
// return only sum of all information (which name is 'all'). If true,
15+
// every network interface installed on the system is returned
16+
// separately.
1217
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
1318
filename := "/proc/net/dev"
1419
lines, err := common.ReadLines(filename)
@@ -77,5 +82,10 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
7782
}
7883
ret = append(ret, nic)
7984
}
85+
86+
if pernic == false {
87+
return getNetIOCountersAll(ret)
88+
}
89+
8090
return ret, nil
8191
}

net/net_test.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,28 @@ func TestNetConnectionStatString(t *testing.T) {
3838

3939
}
4040

41-
func TestNetIOCounters(t *testing.T) {
41+
func TestNetIOCountersAll(t *testing.T) {
42+
v, err := NetIOCounters(false)
43+
per, err := NetIOCounters(true)
44+
if err != nil {
45+
t.Errorf("Could not get NetIOCounters: %v", err)
46+
}
47+
if len(v) != 1 {
48+
t.Errorf("Could not get NetIOCounters: %v", v)
49+
}
50+
if v[0].Name != "all" {
51+
t.Errorf("Invalid NetIOCounters: %v", v)
52+
}
53+
var pr uint64
54+
for _, p := range per {
55+
pr += p.PacketsRecv
56+
}
57+
if v[0].PacketsRecv != pr {
58+
t.Errorf("invalid sum value: %v, %v", v[0].PacketsRecv, pr)
59+
}
60+
}
61+
62+
func TestNetIOCountersPerNic(t *testing.T) {
4263
v, err := NetIOCounters(true)
4364
if err != nil {
4465
t.Errorf("Could not get NetIOCounters: %v", err)
@@ -53,6 +74,38 @@ func TestNetIOCounters(t *testing.T) {
5374
}
5475
}
5576

77+
func Test_getNetIOCountersAll(t *testing.T) {
78+
n := []NetIOCountersStat{
79+
NetIOCountersStat{
80+
Name: "a",
81+
BytesRecv: 10,
82+
PacketsRecv: 10,
83+
},
84+
NetIOCountersStat{
85+
Name: "b",
86+
BytesRecv: 10,
87+
PacketsRecv: 10,
88+
Errin: 10,
89+
},
90+
}
91+
ret, err := getNetIOCountersAll(n)
92+
if err != nil {
93+
t.Error(err)
94+
}
95+
if len(ret) != 1 {
96+
t.Errorf("invalid return count")
97+
}
98+
if ret[0].Name != "all" {
99+
t.Errorf("invalid return name")
100+
}
101+
if ret[0].BytesRecv != 20 {
102+
t.Errorf("invalid count bytesrecv")
103+
}
104+
if ret[0].Errin != 10 {
105+
t.Errorf("invalid count errin")
106+
}
107+
}
108+
56109
func TestNetInterfaces(t *testing.T) {
57110
v, err := NetInterfaces()
58111
if err != nil {

net/net_windows.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
6666
ret = append(ret, c)
6767
}
6868
}
69+
70+
if pernic == false {
71+
return getNetIOCountersAll(ret)
72+
}
6973
return ret, nil
7074
}
7175

0 commit comments

Comments
 (0)