From dc38e448bd6978b00c3c7da34bf4e9ec9c3ee577 Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Mon, 5 Oct 2015 22:49:01 +0900 Subject: [PATCH] add tcp connections stat plugin. --- plugins/system/ps.go | 5 ++++ plugins/system/tcpconn.go | 60 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 plugins/system/tcpconn.go diff --git a/plugins/system/ps.go b/plugins/system/ps.go index e715fbd58..94a8a68da 100644 --- a/plugins/system/ps.go +++ b/plugins/system/ps.go @@ -32,6 +32,7 @@ type PS interface { VMStat() (*mem.VirtualMemoryStat, error) SwapStat() (*mem.SwapMemoryStat, error) DockerStat() ([]*DockerContainerStat, error) + NetConnections() ([]net.NetConnectionStat, error) } func add(acc plugins.Accumulator, @@ -90,6 +91,10 @@ func (s *systemPS) NetIO() ([]net.NetIOCountersStat, error) { return net.NetIOCounters(true) } +func (s *systemPS) NetConnections() ([]net.NetConnectionStat, error) { + return net.NetConnections("all") +} + func (s *systemPS) DiskIO() (map[string]disk.DiskIOCountersStat, error) { m, err := disk.DiskIOCounters() if err == common.NotImplementedError { diff --git a/plugins/system/tcpconn.go b/plugins/system/tcpconn.go new file mode 100644 index 000000000..27d5f1da9 --- /dev/null +++ b/plugins/system/tcpconn.go @@ -0,0 +1,60 @@ +package system + +import ( + "fmt" + + "github.com/influxdb/telegraf/plugins" +) + +type TCPConnectionStats struct { + ps PS +} + +func (_ *TCPConnectionStats) Description() string { + return "Read metrics about TCP status such as established, time wait etc" +} + +var tcpstatSampleConfig = "" + +func (_ *TCPConnectionStats) SampleConfig() string { + return tcpstatSampleConfig +} + +func (s *TCPConnectionStats) Gather(acc plugins.Accumulator) error { + netconns, err := s.ps.NetConnections() + if err != nil { + return fmt.Errorf("error getting net connections info: %s", err) + } + counts := make(map[string]int) + + // TODO: add family to tags or else + tags := map[string]string{} + for _, netcon := range netconns { + c, ok := counts[netcon.Status] + if !ok { + counts[netcon.Status] = 0 + } + counts[netcon.Status] = c + 1 + } + + acc.Add("established", counts["ESTABLISHED"], tags) + acc.Add("syn_sent", counts["SYN_SENT"], tags) + acc.Add("syn_recv", counts["SYN_RECV"], tags) + acc.Add("fin_wait1", counts["FIN_WAIT1"], tags) + acc.Add("fin_wait2", counts["FIN_WAIT2"], tags) + acc.Add("time_wait", counts["TIME_WAIT"], tags) + acc.Add("close", counts["CLOSE"], tags) + acc.Add("close_wait", counts["CLOSE_WAIT"], tags) + acc.Add("last_ack", counts["LAST_ACK"], tags) + acc.Add("listen", counts["LISTEN"], tags) + acc.Add("closing", counts["CLOSING"], tags) + acc.Add("none", counts["NONE"], tags) + + return nil +} + +func init() { + plugins.Add("tcpconn", func() plugins.Plugin { + return &TCPConnectionStats{ps: &systemPS{}} + }) +}