/
net2.go
206 lines (179 loc) · 5.49 KB
/
net2.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package net2
import (
"fmt"
"net"
"strings"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/filter"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/inputs/system"
netutil "github.com/shirou/gopsutil/net"
)
type NetIOStats struct {
filter filter.Filter
ps system.PS
skipChecks bool
//IgnoreProtocolStats bool
Interfaces []string
prevMetrics map[string]netutil.IOCountersStat
preEpoch int64
}
func (_ *NetIOStats) Description() string {
return "Read metrics about network interface usage"
}
var netSampleConfig = `
## By default, telegraf gathers stats from any up interface (excluding loopback)
## Setting interfaces will tell it to gather these explicit interfaces,
## regardless of status.
##
# interfaces = ["eth0"]
##
## On linux systems telegraf also collects protocol stats.
## Setting ignore_protocol_stats to true will skip reporting of protocol metrics.
##
# ignore_protocol_stats = false
##
`
func (_ *NetIOStats) SampleConfig() string {
return netSampleConfig
}
func (s *NetIOStats) Gather(acc telegraf.Accumulator) error {
netio, err := s.ps.NetIO()
if err != nil {
return fmt.Errorf("error getting net io info: %s", err)
}
if s.filter == nil {
if s.filter, err = filter.Compile(s.Interfaces); err != nil {
return fmt.Errorf("error compiling filter: %s", err)
}
}
interfaces, err := net.Interfaces()
if err != nil {
return fmt.Errorf("error getting list of interfaces: %s", err)
}
interfacesByName := map[string]net.Interface{}
for _, iface := range interfaces {
interfacesByName[iface.Name] = iface
}
nowEpoch := time.Now().UnixNano()
gapNano := int64(0)
if s.preEpoch > 0 {
gapNano = nowEpoch - s.preEpoch
}
oneCounter := netutil.IOCountersStat{}
tatalCounter := netutil.IOCountersStat{}
for _, io := range netio {
if len(s.Interfaces) != 0 {
var found bool
if s.filter.Match(io.Name) {
found = true
}
if !found {
continue
}
} else if !s.skipChecks {
if strings.HasPrefix(io.Name, "ib") { //not collect ib cards
continue
}
iface, ok := interfacesByName[io.Name]
if !ok {
continue
}
if iface.Flags&net.FlagLoopback == net.FlagLoopback {
continue
}
if iface.Flags&net.FlagUp == 0 {
continue
}
}
tags := map[string]string{
"interface": io.Name,
"name": io.Name, //redanduncy for adapt UI of Mali version
}
if gapNano > 0 {
oneCounter.BytesSent = uint64(int64(io.BytesSent-
s.prevMetrics[io.Name].BytesSent) * int64(1e9) / gapNano)
oneCounter.BytesRecv = uint64(int64(io.BytesRecv-
s.prevMetrics[io.Name].BytesRecv) * int64(1e9) / gapNano)
oneCounter.PacketsSent = uint64(int64(io.PacketsSent-
s.prevMetrics[io.Name].PacketsSent) * int64(1e9) / gapNano)
oneCounter.PacketsRecv = uint64(int64(io.PacketsRecv-
s.prevMetrics[io.Name].PacketsRecv) * int64(1e9) / gapNano)
oneCounter.Errin = uint64(int64(io.Errin-
s.prevMetrics[io.Name].Errin) * int64(1e9) / gapNano)
oneCounter.Errout = uint64(int64(io.Errout-
s.prevMetrics[io.Name].Errout) * int64(1e9) / gapNano)
oneCounter.Dropin = uint64(int64(io.Dropin-
s.prevMetrics[io.Name].Dropin) * int64(1e9) / gapNano)
oneCounter.Dropout = uint64(int64(io.Dropout-
s.prevMetrics[io.Name].Dropout) * int64(1e9) / gapNano)
tatalCounter.BytesSent += oneCounter.BytesSent
tatalCounter.BytesRecv += oneCounter.BytesRecv
tatalCounter.PacketsSent += oneCounter.PacketsSent
tatalCounter.PacketsRecv += oneCounter.PacketsRecv
tatalCounter.Errin += oneCounter.Errin
tatalCounter.Errout += oneCounter.Errout
tatalCounter.Dropin += oneCounter.Dropin
tatalCounter.Dropout += oneCounter.Dropout
fields := map[string]interface{}{
"sent_bytes": io.BytesSent, //redanduncy for adapt UI of Mali version
"recv_bytes": io.BytesRecv, //redanduncy for adapt UI of Mali version
"bytes_sent": io.BytesSent,
"bytes_recv": io.BytesRecv,
"packets_sent": io.PacketsSent,
"packets_recv": io.PacketsRecv,
"err_in": io.Errin,
"err_out": io.Errout,
"drop_in": io.Dropin,
"drop_out": io.Dropout,
}
acc.AddCounter("net", fields, tags)
}
s.prevMetrics[io.Name] = io
}
if gapNano > 0 {
totalTags := map[string]string{}
totalFields := map[string]interface{}{
"bytes_sent": tatalCounter.BytesSent,
"bytes_recv": tatalCounter.BytesRecv,
"write_total": tatalCounter.BytesSent,
"read_total": tatalCounter.BytesRecv,
"packets_sent": tatalCounter.PacketsSent,
"packets_recv": tatalCounter.PacketsRecv,
"err_in": tatalCounter.Errin,
"err_out": tatalCounter.Errout,
"drop_in": tatalCounter.Dropin,
"drop_out": tatalCounter.Dropout,
}
acc.AddCounter("net_total", totalFields, totalTags)
}
s.preEpoch = nowEpoch
// Get system wide stats for different network protocols
// (ignore these stats if the call fails)
/*
if !s.IgnoreProtocolStats {
netprotos, _ := s.ps.NetProto()
fields := make(map[string]interface{})
for _, proto := range netprotos {
for stat, value := range proto.Stats {
name := fmt.Sprintf("%s_%s", strings.ToLower(proto.Protocol),
strings.ToLower(stat))
fields[name] = value
}
}
tags := map[string]string{
"interface": "all",
}
acc.AddFields("net", fields, tags)
}
*/
return nil
}
func init() {
pm := map[string]netutil.IOCountersStat{}
inputs.Add("net2", func() telegraf.Input {
return &NetIOStats{ps: system.NewSystemPS(),
prevMetrics: pm}
})
}