2018-07-11 23:43:49 +00:00
|
|
|
package net
|
2015-12-11 20:07:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
|
2018-07-11 23:43:49 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/system"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2015-12-11 20:07:32 +00:00
|
|
|
"github.com/shirou/gopsutil/net"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNetStats(t *testing.T) {
|
2018-07-11 23:43:49 +00:00
|
|
|
var mps system.MockPS
|
2015-12-11 20:07:32 +00:00
|
|
|
var err error
|
|
|
|
defer mps.AssertExpectations(t)
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
2016-05-19 14:05:08 +00:00
|
|
|
netio := net.IOCountersStat{
|
2015-12-11 20:07:32 +00:00
|
|
|
Name: "eth0",
|
|
|
|
BytesSent: 1123,
|
|
|
|
BytesRecv: 8734422,
|
|
|
|
PacketsSent: 781,
|
|
|
|
PacketsRecv: 23456,
|
|
|
|
Errin: 832,
|
|
|
|
Errout: 8,
|
|
|
|
Dropin: 7,
|
|
|
|
Dropout: 1,
|
|
|
|
}
|
|
|
|
|
2016-05-19 14:05:08 +00:00
|
|
|
mps.On("NetIO").Return([]net.IOCountersStat{netio}, nil)
|
2015-12-11 20:07:32 +00:00
|
|
|
|
2016-05-19 14:05:08 +00:00
|
|
|
netprotos := []net.ProtoCountersStat{
|
2018-10-19 20:32:54 +00:00
|
|
|
{
|
2015-12-11 20:07:32 +00:00
|
|
|
Protocol: "Udp",
|
|
|
|
Stats: map[string]int64{
|
|
|
|
"InDatagrams": 4655,
|
|
|
|
"NoPorts": 892592,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
mps.On("NetProto").Return(netprotos, nil)
|
|
|
|
|
2016-05-19 14:05:08 +00:00
|
|
|
netstats := []net.ConnectionStat{
|
2018-10-19 20:32:54 +00:00
|
|
|
{
|
2015-12-11 20:07:32 +00:00
|
|
|
Type: syscall.SOCK_DGRAM,
|
|
|
|
},
|
2018-10-19 20:32:54 +00:00
|
|
|
{
|
2015-12-11 20:07:32 +00:00
|
|
|
Status: "ESTABLISHED",
|
|
|
|
},
|
2018-10-19 20:32:54 +00:00
|
|
|
{
|
2015-12-11 20:07:32 +00:00
|
|
|
Status: "ESTABLISHED",
|
|
|
|
},
|
2018-10-19 20:32:54 +00:00
|
|
|
{
|
2015-12-11 20:07:32 +00:00
|
|
|
Status: "CLOSE",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mps.On("NetConnections").Return(netstats, nil)
|
|
|
|
|
|
|
|
err = (&NetIOStats{ps: &mps, skipChecks: true}).Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ntags := map[string]string{
|
|
|
|
"interface": "eth0",
|
|
|
|
}
|
|
|
|
|
2016-01-05 23:58:35 +00:00
|
|
|
fields1 := map[string]interface{}{
|
|
|
|
"bytes_sent": uint64(1123),
|
|
|
|
"bytes_recv": uint64(8734422),
|
|
|
|
"packets_sent": uint64(781),
|
|
|
|
"packets_recv": uint64(23456),
|
|
|
|
"err_in": uint64(832),
|
|
|
|
"err_out": uint64(8),
|
|
|
|
"drop_in": uint64(7),
|
|
|
|
"drop_out": uint64(1),
|
|
|
|
}
|
2016-01-06 00:28:15 +00:00
|
|
|
acc.AssertContainsTaggedFields(t, "net", fields1, ntags)
|
2016-01-05 23:58:35 +00:00
|
|
|
|
|
|
|
fields2 := map[string]interface{}{
|
|
|
|
"udp_noports": int64(892592),
|
|
|
|
"udp_indatagrams": int64(4655),
|
|
|
|
}
|
2016-01-14 18:31:47 +00:00
|
|
|
ntags = map[string]string{
|
|
|
|
"interface": "all",
|
|
|
|
}
|
|
|
|
acc.AssertContainsTaggedFields(t, "net", fields2, ntags)
|
2015-12-11 20:07:32 +00:00
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
acc.Metrics = nil
|
2015-12-11 20:07:32 +00:00
|
|
|
|
|
|
|
err = (&NetStats{&mps}).Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2016-01-05 23:58:35 +00:00
|
|
|
fields3 := map[string]interface{}{
|
|
|
|
"tcp_established": 2,
|
|
|
|
"tcp_syn_sent": 0,
|
|
|
|
"tcp_syn_recv": 0,
|
|
|
|
"tcp_fin_wait1": 0,
|
|
|
|
"tcp_fin_wait2": 0,
|
|
|
|
"tcp_time_wait": 0,
|
|
|
|
"tcp_close": 1,
|
|
|
|
"tcp_close_wait": 0,
|
|
|
|
"tcp_last_ack": 0,
|
|
|
|
"tcp_listen": 0,
|
|
|
|
"tcp_closing": 0,
|
|
|
|
"tcp_none": 0,
|
|
|
|
"udp_socket": 1,
|
|
|
|
}
|
2016-01-06 00:28:15 +00:00
|
|
|
acc.AssertContainsTaggedFields(t, "netstat", fields3, make(map[string]string))
|
2018-03-14 04:08:21 +00:00
|
|
|
|
|
|
|
acc.Metrics = nil
|
|
|
|
err = (&NetIOStats{ps: &mps, IgnoreProtocolStats: true}).Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
acc.AssertDoesNotContainsTaggedFields(t, "netstat", fields3, make(map[string]string))
|
2015-12-11 20:07:32 +00:00
|
|
|
}
|