Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add protocol stats to the network plugin #382

Merged
merged 2 commits into from
Dec 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [#415](https://github.com/influxdb/telegraf/issues/415): memcached plugin: support unix sockets
- [#418](https://github.com/influxdb/telegraf/pull/418): memcached plugin additional unit tests.
- [#408](https://github.com/influxdb/telegraf/pull/408): MailChimp plugin.
- [#382](https://github.com/influxdb/telegraf/pull/382): Add system wide network protocol stats to `net` plugin.

### Bugfixes
- [#405](https://github.com/influxdb/telegraf/issues/405): Prometheus output cardinality issue
Expand Down
31 changes: 25 additions & 6 deletions plugins/system/mock_PS.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package system

import "github.com/stretchr/testify/mock"
import (
"github.com/stretchr/testify/mock"

import "github.com/shirou/gopsutil/cpu"
import "github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"

import "github.com/shirou/gopsutil/load"
import "github.com/shirou/gopsutil/mem"
import "github.com/shirou/gopsutil/net"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
)

type MockPS struct {
mock.Mock
Expand All @@ -21,6 +23,7 @@ func (m *MockPS) LoadAvg() (*load.LoadAvgStat, error) {

return r0, r1
}

func (m *MockPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error) {
ret := m.Called()

Expand All @@ -29,6 +32,7 @@ func (m *MockPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error) {

return r0, r1
}

func (m *MockPS) DiskUsage() ([]*disk.DiskUsageStat, error) {
ret := m.Called()

Expand All @@ -37,6 +41,7 @@ func (m *MockPS) DiskUsage() ([]*disk.DiskUsageStat, error) {

return r0, r1
}

func (m *MockPS) NetIO() ([]net.NetIOCountersStat, error) {
ret := m.Called()

Expand All @@ -45,6 +50,16 @@ func (m *MockPS) NetIO() ([]net.NetIOCountersStat, error) {

return r0, r1
}

func (m *MockPS) NetProto() ([]net.NetProtoCountersStat, error) {
ret := m.Called()

r0 := ret.Get(0).([]net.NetProtoCountersStat)
r1 := ret.Error(1)

return r0, r1
}

func (m *MockPS) DiskIO() (map[string]disk.DiskIOCountersStat, error) {
ret := m.Called()

Expand All @@ -53,6 +68,7 @@ func (m *MockPS) DiskIO() (map[string]disk.DiskIOCountersStat, error) {

return r0, r1
}

func (m *MockPS) VMStat() (*mem.VirtualMemoryStat, error) {
ret := m.Called()

Expand All @@ -61,6 +77,7 @@ func (m *MockPS) VMStat() (*mem.VirtualMemoryStat, error) {

return r0, r1
}

func (m *MockPS) SwapStat() (*mem.SwapMemoryStat, error) {
ret := m.Called()

Expand All @@ -69,6 +86,7 @@ func (m *MockPS) SwapStat() (*mem.SwapMemoryStat, error) {

return r0, r1
}

func (m *MockPS) DockerStat() ([]*DockerContainerStat, error) {
ret := m.Called()

Expand All @@ -77,6 +95,7 @@ func (m *MockPS) DockerStat() ([]*DockerContainerStat, error) {

return r0, r1
}

func (m *MockPS) NetConnections() ([]net.NetConnectionStat, error) {
ret := m.Called()

Expand Down
12 changes: 12 additions & 0 deletions plugins/system/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package system
import (
"fmt"
"net"
"strings"

"github.com/influxdb/telegraf/plugins"
)
Expand Down Expand Up @@ -79,6 +80,17 @@ func (s *NetIOStats) Gather(acc plugins.Accumulator) error {
acc.Add("drop_out", io.Dropout, tags)
}

// Get system wide stats for different network protocols
// (ignore these stats if the call fails)
netprotos, _ := s.ps.NetProto()
for _, proto := range netprotos {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will just do nothing on unsupported systems, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct

for stat, value := range proto.Stats {
name := fmt.Sprintf("%s_%s", strings.ToLower(proto.Protocol),
strings.ToLower(stat))
acc.Add(name, value, nil)
}
}

return nil
}

Expand Down
5 changes: 5 additions & 0 deletions plugins/system/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type PS interface {
CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error)
DiskUsage() ([]*disk.DiskUsageStat, error)
NetIO() ([]net.NetIOCountersStat, error)
NetProto() ([]net.NetProtoCountersStat, error)
DiskIO() (map[string]disk.DiskIOCountersStat, error)
VMStat() (*mem.VirtualMemoryStat, error)
SwapStat() (*mem.SwapMemoryStat, error)
Expand Down Expand Up @@ -88,6 +89,10 @@ func (s *systemPS) DiskUsage() ([]*disk.DiskUsageStat, error) {
return usage, nil
}

func (s *systemPS) NetProto() ([]net.NetProtoCountersStat, error) {
return net.NetProtoCounters(nil)
}

func (s *systemPS) NetIO() ([]net.NetIOCountersStat, error) {
return net.NetIOCounters(true)
}
Expand Down
13 changes: 13 additions & 0 deletions plugins/system/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ func TestSystemStats_GenerateStats(t *testing.T) {

mps.On("NetIO").Return([]net.NetIOCountersStat{netio}, nil)

netprotos := []net.NetProtoCountersStat{
net.NetProtoCountersStat{
Protocol: "Udp",
Stats: map[string]int64{
"InDatagrams": 4655,
"NoPorts": 892592,
},
},
}
mps.On("NetProto").Return(netprotos, nil)

vms := &mem.VirtualMemoryStat{
Total: 12400,
Available: 7600,
Expand Down Expand Up @@ -273,6 +284,8 @@ func TestSystemStats_GenerateStats(t *testing.T) {
assert.NoError(t, acc.ValidateTaggedValue("err_out", uint64(8), ntags))
assert.NoError(t, acc.ValidateTaggedValue("drop_in", uint64(7), ntags))
assert.NoError(t, acc.ValidateTaggedValue("drop_out", uint64(1), ntags))
assert.NoError(t, acc.ValidateValue("udp_noports", int64(892592)))
assert.NoError(t, acc.ValidateValue("udp_indatagrams", int64(4655)))

preDiskIOPoints := len(acc.Points)

Expand Down