81 lines
1.7 KiB
Go
81 lines
1.7 KiB
Go
package netstat
|
|
|
|
import (
|
|
"log"
|
|
"syscall"
|
|
|
|
"flashcat.cloud/categraf/config"
|
|
"flashcat.cloud/categraf/inputs"
|
|
"flashcat.cloud/categraf/inputs/system"
|
|
"github.com/toolkits/pkg/container/list"
|
|
)
|
|
|
|
const inputName = "netstat"
|
|
|
|
type NetStats struct {
|
|
ps system.PS
|
|
config.Interval
|
|
}
|
|
|
|
func init() {
|
|
ps := system.NewSystemPS()
|
|
inputs.Add(inputName, func() inputs.Input {
|
|
return &NetStats{
|
|
ps: ps,
|
|
}
|
|
})
|
|
}
|
|
|
|
func (s *NetStats) Prefix() string {
|
|
return inputName
|
|
}
|
|
|
|
func (s *NetStats) Drop() {}
|
|
|
|
func (s *NetStats) Init() error {
|
|
return nil
|
|
}
|
|
|
|
func (s *NetStats) Gather(slist *list.SafeList) {
|
|
netconns, err := s.ps.NetConnections()
|
|
if err != nil {
|
|
log.Println("E! failed to get net connections:", err)
|
|
return
|
|
}
|
|
|
|
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"]++
|
|
continue // UDP has no status
|
|
}
|
|
c, ok := counts[netcon.Status]
|
|
if !ok {
|
|
counts[netcon.Status] = 0
|
|
}
|
|
counts[netcon.Status] = c + 1
|
|
}
|
|
|
|
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"],
|
|
}
|
|
|
|
inputs.PushSamples(slist, fields, tags)
|
|
}
|