2015-10-11 14:13:35 +00:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"syscall"
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-10-11 14:13:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type NetStats struct {
|
|
|
|
ps PS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *NetStats) Description() string {
|
2016-02-09 05:57:26 +00:00
|
|
|
return "Read TCP metrics such as established, time wait and sockets counts."
|
2015-10-11 14:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var tcpstatSampleConfig = ""
|
|
|
|
|
|
|
|
func (_ *NetStats) SampleConfig() string {
|
|
|
|
return tcpstatSampleConfig
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (s *NetStats) Gather(acc telegraf.Accumulator) error {
|
2015-10-11 14:13:35 +00:00
|
|
|
netconns, err := s.ps.NetConnections()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting net connections info: %s", err)
|
|
|
|
}
|
|
|
|
counts := make(map[string]int)
|
|
|
|
counts["UDP"] = 0
|
|
|
|
|
|
|
|
// TODO: add family to tags or else
|
|
|
|
tags := map[string]string{}
|
|
|
|
for _, netcon := range netconns {
|
|
|
|
if netcon.Type == syscall.SOCK_DGRAM {
|
|
|
|
counts["UDP"] += 1
|
|
|
|
continue // UDP has no status
|
|
|
|
}
|
|
|
|
c, ok := counts[netcon.Status]
|
|
|
|
if !ok {
|
|
|
|
counts[netcon.Status] = 0
|
|
|
|
}
|
|
|
|
counts[netcon.Status] = c + 1
|
|
|
|
}
|
2015-12-11 20:07:32 +00:00
|
|
|
|
|
|
|
fields := map[string]interface{}{
|
|
|
|
"tcp_established": counts["ESTABLISHED"],
|
|
|
|
"tcp_syn_sent": counts["SYN_SENT"],
|
|
|
|
"tcp_syn_recv": counts["SYN_RECV"],
|
|
|
|
"tcp_fin_wait1": counts["FIN_WAIT1"],
|
|
|
|
"tcp_fin_wait2": counts["FIN_WAIT2"],
|
|
|
|
"tcp_time_wait": counts["TIME_WAIT"],
|
|
|
|
"tcp_close": counts["CLOSE"],
|
|
|
|
"tcp_close_wait": counts["CLOSE_WAIT"],
|
|
|
|
"tcp_last_ack": counts["LAST_ACK"],
|
|
|
|
"tcp_listen": counts["LISTEN"],
|
|
|
|
"tcp_closing": counts["CLOSING"],
|
|
|
|
"tcp_none": counts["NONE"],
|
|
|
|
"udp_socket": counts["UDP"],
|
|
|
|
}
|
|
|
|
acc.AddFields("netstat", fields, tags)
|
2015-10-11 14:13:35 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("netstat", func() telegraf.Input {
|
2017-04-18 18:42:58 +00:00
|
|
|
return &NetStats{ps: newSystemPS()}
|
2015-10-11 14:13:35 +00:00
|
|
|
})
|
|
|
|
}
|