Add NetIO
This commit is contained in:
parent
3d753d721a
commit
065e094121
|
@ -5,6 +5,7 @@ import "github.com/stretchr/testify/mock"
|
|||
import "github.com/influxdb/tivan/plugins/system/ps/cpu"
|
||||
import "github.com/influxdb/tivan/plugins/system/ps/disk"
|
||||
import "github.com/influxdb/tivan/plugins/system/ps/load"
|
||||
import "github.com/influxdb/tivan/plugins/system/ps/net"
|
||||
|
||||
type MockPS struct {
|
||||
mock.Mock
|
||||
|
@ -34,3 +35,11 @@ func (m *MockPS) DiskUsage() ([]*disk.DiskUsageStat, error) {
|
|||
|
||||
return r0, r1
|
||||
}
|
||||
func (m *MockPS) NetIO() ([]net.NetIOCountersStat, error) {
|
||||
ret := m.Called()
|
||||
|
||||
r0 := ret.Get(0).([]net.NetIOCountersStat)
|
||||
r1 := ret.Error(1)
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
|
|
@ -7,12 +7,14 @@ import (
|
|||
"github.com/influxdb/tivan/plugins/system/ps/cpu"
|
||||
"github.com/influxdb/tivan/plugins/system/ps/disk"
|
||||
"github.com/influxdb/tivan/plugins/system/ps/load"
|
||||
"github.com/influxdb/tivan/plugins/system/ps/net"
|
||||
)
|
||||
|
||||
type PS interface {
|
||||
LoadAvg() (*load.LoadAvgStat, error)
|
||||
CPUTimes() ([]cpu.CPUTimesStat, error)
|
||||
DiskUsage() ([]*disk.DiskUsageStat, error)
|
||||
NetIO() ([]net.NetIOCountersStat, error)
|
||||
}
|
||||
|
||||
type SystemStats struct {
|
||||
|
@ -72,6 +74,26 @@ func (s *SystemStats) Gather(acc plugins.Accumulator) error {
|
|||
acc.Add("inodes_used", du.InodesTotal-du.InodesFree, tags)
|
||||
}
|
||||
|
||||
netio, err := s.ps.NetIO()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, io := range netio {
|
||||
tags := map[string]string{
|
||||
"interface": io.Name,
|
||||
}
|
||||
|
||||
acc.Add("bytes_sent", io.BytesSent, tags)
|
||||
acc.Add("bytes_recv", io.BytesRecv, tags)
|
||||
acc.Add("packets_sent", io.PacketsSent, tags)
|
||||
acc.Add("packets_recv", io.PacketsRecv, tags)
|
||||
acc.Add("err_in", io.Errin, tags)
|
||||
acc.Add("err_out", io.Errout, tags)
|
||||
acc.Add("drop_in", io.Dropin, tags)
|
||||
acc.Add("drop_out", io.Dropout, tags)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -105,6 +127,10 @@ func (s *systemPS) DiskUsage() ([]*disk.DiskUsageStat, error) {
|
|||
return usage, nil
|
||||
}
|
||||
|
||||
func (s *systemPS) NetIO() ([]net.NetIOCountersStat, error) {
|
||||
return net.NetIOCounters(true)
|
||||
}
|
||||
|
||||
func init() {
|
||||
plugins.Add("system", func() plugins.Plugin {
|
||||
return &SystemStats{ps: &systemPS{}}
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"github.com/influxdb/tivan/plugins/system/ps/cpu"
|
||||
"github.com/influxdb/tivan/plugins/system/ps/disk"
|
||||
"github.com/influxdb/tivan/plugins/system/ps/load"
|
||||
"github.com/influxdb/tivan/plugins/system/ps/net"
|
||||
"github.com/influxdb/tivan/testutil"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
@ -55,6 +56,20 @@ func TestSystemStats_GenerateStats(t *testing.T) {
|
|||
|
||||
mps.On("DiskUsage").Return([]*disk.DiskUsageStat{du}, nil)
|
||||
|
||||
netio := net.NetIOCountersStat{
|
||||
Name: "eth0",
|
||||
BytesSent: 1123,
|
||||
BytesRecv: 8734422,
|
||||
PacketsSent: 781,
|
||||
PacketsRecv: 23456,
|
||||
Errin: 832,
|
||||
Errout: 8,
|
||||
Dropin: 7,
|
||||
Dropout: 1,
|
||||
}
|
||||
|
||||
mps.On("NetIO").Return([]net.NetIOCountersStat{netio}, nil)
|
||||
|
||||
err := ss.Gather(&acc)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
@ -84,4 +99,17 @@ func TestSystemStats_GenerateStats(t *testing.T) {
|
|||
assert.True(t, acc.CheckTaggedValue("inodes_total", uint64(1234), tags))
|
||||
assert.True(t, acc.CheckTaggedValue("inodes_free", uint64(234), tags))
|
||||
assert.True(t, acc.CheckTaggedValue("inodes_used", uint64(1000), tags))
|
||||
|
||||
ntags := map[string]string{
|
||||
"interface": "eth0",
|
||||
}
|
||||
|
||||
assert.True(t, acc.CheckTaggedValue("bytes_sent", uint64(1123), ntags))
|
||||
assert.True(t, acc.CheckTaggedValue("bytes_recv", uint64(8734422), ntags))
|
||||
assert.True(t, acc.CheckTaggedValue("packets_sent", uint64(781), ntags))
|
||||
assert.True(t, acc.CheckTaggedValue("packets_recv", uint64(23456), ntags))
|
||||
assert.True(t, acc.CheckTaggedValue("err_in", uint64(832), ntags))
|
||||
assert.True(t, acc.CheckTaggedValue("err_out", uint64(8), ntags))
|
||||
assert.True(t, acc.CheckTaggedValue("drop_in", uint64(7), ntags))
|
||||
assert.True(t, acc.CheckTaggedValue("drop_out", uint64(1), ntags))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue