forked from qiniu/logkit
/
netstat.go
117 lines (102 loc) · 3.53 KB
/
netstat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package system
import (
"fmt"
"syscall"
"github.com/qiniu/logkit/metric"
. "github.com/qiniu/logkit/utils/models"
)
const (
TypeMetricNetstat = "netstat"
MetricNetstatUsages = "网络连接情况(netstat)"
// TypeMetricNetstat 信息中的字段
KeyNetstatTcpEstablished = "netstat_tcp_established"
KeyNetstatTcpSynSent = "netstat_tcp_syn_sent"
KeyNetstatTcpSynRecv = "netstat_tcp_syn_recv"
KeyNetstatTcpFinWait1 = "netstat_tcp_fin_wait1"
KeyNetstatTcpFinWait2 = "netstat_tcp_fin_wait2"
KeyNetstatTcpTimeWait = "netstat_tcp_time_wait"
KeyNetstatTcpClose = "netstat_tcp_close"
KeyNetstatTcpCloseWait = "netstat_tcp_close_wait"
KeyNetstatTcpLastAck = "netstat_tcp_last_ack"
KeyNetstatTcpListen = "netstat_tcp_listen"
KeyNetstatTcpClosing = "netstat_tcp_closing"
KeyNetstatTcpNone = "netstat_tcp_none"
KeyNetstatUdpSocket = "netstat_udp_socket"
)
// KeyNetStatUsages TypeMetricNetstat 的字段名称
var KeyNetStatUsages = KeyValueSlice{
{KeyNetstatTcpEstablished, "ESTABLISHED状态的网络链接数", ""},
{KeyNetstatTcpSynSent, "SYN_SENT状态的网络链接数", ""},
{KeyNetstatTcpSynRecv, "SYN_RECV状态的网络链接数", ""},
{KeyNetstatTcpFinWait1, "FIN_WAIT1状态的网络链接数", ""},
{KeyNetstatTcpFinWait2, "FIN_WAIT2状态的网络链接数", ""},
{KeyNetstatTcpTimeWait, "TIME_WAIT状态的网络链接数", ""},
{KeyNetstatTcpClose, "CLOSE状态的网络链接数", ""},
{KeyNetstatTcpCloseWait, "CLOSE_WAIT状态的网络链接数", ""},
{KeyNetstatTcpLastAck, "LAST_ACK状态的网络链接数", ""},
{KeyNetstatTcpListen, "LISTEN状态的网络链接数", ""},
{KeyNetstatTcpClosing, "CLOSING状态的网络链接数", ""},
{KeyNetstatTcpNone, "NONE状态的网络链接数", ""},
{KeyNetstatUdpSocket, "UDP状态的网络链接数", ""},
}
type NetStats struct {
ps PS
}
func (*NetStats) Name() string {
return TypeMetricNetstat
}
func (*NetStats) Usages() string {
return MetricNetstatUsages
}
func (*NetStats) Tags() []string {
return []string{}
}
func (*NetStats) Config() map[string]interface{} {
config := map[string]interface{}{
metric.OptionString: []Option{},
metric.AttributesString: KeyNetStatUsages,
}
return config
}
func (s *NetStats) Collect() (datas []map[string]interface{}, err error) {
netconns, err := s.ps.NetConnections()
if err != nil {
return nil, fmt.Errorf("error getting net connections info: %s", err)
}
counts := make(map[string]int)
counts["UDP"] = 0
// TODO: add family to results
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
}
fields := map[string]interface{}{
KeyNetstatTcpEstablished: counts["ESTABLISHED"],
KeyNetstatTcpSynSent: counts["SYN_SENT"],
KeyNetstatTcpSynRecv: counts["SYN_RECV"],
KeyNetstatTcpFinWait1: counts["FIN_WAIT1"],
KeyNetstatTcpFinWait2: counts["FIN_WAIT2"],
KeyNetstatTcpTimeWait: counts["TIME_WAIT"],
KeyNetstatTcpClose: counts["CLOSE"],
KeyNetstatTcpCloseWait: counts["CLOSE_WAIT"],
KeyNetstatTcpLastAck: counts["LAST_ACK"],
KeyNetstatTcpListen: counts["LISTEN"],
KeyNetstatTcpClosing: counts["CLOSING"],
KeyNetstatTcpNone: counts["NONE"],
KeyNetstatUdpSocket: counts["UDP"],
}
datas = append(datas, fields)
return
}
func init() {
metric.Add(TypeMetricNetstat, func() metric.Collector {
return &NetStats{ps: newSystemPS()}
})
}