forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nstat.go
234 lines (211 loc) · 5.39 KB
/
nstat.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package nstat
import (
"bytes"
"io/ioutil"
"os"
"strconv"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)
var (
zeroByte = []byte("0")
newLineByte = []byte("\n")
colonByte = []byte(":")
)
// default file paths
const (
NET_NETSTAT = "/net/netstat"
NET_SNMP = "/net/snmp"
NET_SNMP6 = "/net/snmp6"
NET_PROC = "/proc"
)
// env variable names
const (
ENV_NETSTAT = "PROC_NET_NETSTAT"
ENV_SNMP = "PROC_NET_SNMP"
ENV_SNMP6 = "PROC_NET_SNMP6"
ENV_ROOT = "PROC_ROOT"
)
type Nstat struct {
ProcNetNetstat string `toml:"proc_net_netstat"`
ProcNetSNMP string `toml:"proc_net_snmp"`
ProcNetSNMP6 string `toml:"proc_net_snmp6"`
DumpZeros bool `toml:"dump_zeros"`
}
var sampleConfig = `
## file paths for proc files. If empty default paths will be used:
## /proc/net/netstat, /proc/net/snmp, /proc/net/snmp6
## These can also be overridden with env variables, see README.
proc_net_netstat = "/proc/net/netstat"
proc_net_snmp = "/proc/net/snmp"
proc_net_snmp6 = "/proc/net/snmp6"
## dump metrics with 0 values too
dump_zeros = true
`
func (ns *Nstat) Description() string {
return "Collect kernel snmp counters and network interface statistics"
}
func (ns *Nstat) SampleConfig() string {
return sampleConfig
}
func (ns *Nstat) Gather(acc telegraf.Accumulator) error {
// load paths, get from env if config values are empty
ns.loadPaths()
netstat, err := ioutil.ReadFile(ns.ProcNetNetstat)
if err != nil {
return err
}
// collect netstat data
err = ns.gatherNetstat(netstat, acc)
if err != nil {
return err
}
// collect SNMP data
snmp, err := ioutil.ReadFile(ns.ProcNetSNMP)
if err != nil {
return err
}
err = ns.gatherSNMP(snmp, acc)
if err != nil {
return err
}
// collect SNMP6 data, if SNMP6 directory exists (IPv6 enabled)
snmp6, err := ioutil.ReadFile(ns.ProcNetSNMP6)
if err == nil {
err = ns.gatherSNMP6(snmp6, acc)
if err != nil {
return err
}
} else if !os.IsNotExist(err) {
return err
}
return nil
}
func (ns *Nstat) gatherNetstat(data []byte, acc telegraf.Accumulator) error {
metrics, err := loadUglyTable(data, ns.DumpZeros)
if err != nil {
return err
}
tags := map[string]string{
"name": "netstat",
}
acc.AddFields("nstat", metrics, tags)
return nil
}
func (ns *Nstat) gatherSNMP(data []byte, acc telegraf.Accumulator) error {
metrics, err := loadUglyTable(data, ns.DumpZeros)
if err != nil {
return err
}
tags := map[string]string{
"name": "snmp",
}
acc.AddFields("nstat", metrics, tags)
return nil
}
func (ns *Nstat) gatherSNMP6(data []byte, acc telegraf.Accumulator) error {
metrics, err := loadGoodTable(data, ns.DumpZeros)
if err != nil {
return err
}
tags := map[string]string{
"name": "snmp6",
}
acc.AddFields("nstat", metrics, tags)
return nil
}
// loadPaths can be used to read paths firstly from config
// if it is empty then try read from env variables
func (ns *Nstat) loadPaths() {
if ns.ProcNetNetstat == "" {
ns.ProcNetNetstat = proc(ENV_NETSTAT, NET_NETSTAT)
}
if ns.ProcNetSNMP == "" {
ns.ProcNetSNMP = proc(ENV_SNMP, NET_SNMP)
}
if ns.ProcNetSNMP6 == "" {
ns.ProcNetSNMP6 = proc(ENV_SNMP6, NET_SNMP6)
}
}
// loadGoodTable can be used to parse string heap that
// headers and values are arranged in right order
func loadGoodTable(table []byte, dumpZeros bool) (map[string]interface{}, error) {
entries := map[string]interface{}{}
fields := bytes.Fields(table)
var value int64
var err error
// iterate over two values each time
// first value is header, second is value
for i := 0; i < len(fields); i = i + 2 {
// counter is zero
if bytes.Equal(fields[i+1], zeroByte) {
if !dumpZeros {
continue
} else {
entries[string(fields[i])] = int64(0)
continue
}
}
// the counter is not zero, so parse it.
value, err = strconv.ParseInt(string(fields[i+1]), 10, 64)
if err == nil {
entries[string(fields[i])] = value
}
}
return entries, nil
}
// loadUglyTable can be used to parse string heap that
// the headers and values are splitted with a newline
func loadUglyTable(table []byte, dumpZeros bool) (map[string]interface{}, error) {
entries := map[string]interface{}{}
// split the lines by newline
lines := bytes.Split(table, newLineByte)
var value int64
var err error
// iterate over lines, take 2 lines each time
// first line contains header names
// second line contains values
for i := 0; i < len(lines); i = i + 2 {
if len(lines[i]) == 0 {
continue
}
headers := bytes.Fields(lines[i])
prefix := bytes.TrimSuffix(headers[0], colonByte)
metrics := bytes.Fields(lines[i+1])
for j := 1; j < len(headers); j++ {
// counter is zero
if bytes.Equal(metrics[j], zeroByte) {
if !dumpZeros {
continue
} else {
entries[string(append(prefix, headers[j]...))] = int64(0)
continue
}
}
// the counter is not zero, so parse it.
value, err = strconv.ParseInt(string(metrics[j]), 10, 64)
if err == nil {
entries[string(append(prefix, headers[j]...))] = value
}
}
}
return entries, nil
}
// proc can be used to read file paths from env
func proc(env, path string) string {
// try to read full file path
if p := os.Getenv(env); p != "" {
return p
}
// try to read root path, or use default root path
root := os.Getenv(ENV_ROOT)
if root == "" {
root = NET_PROC
}
return root + path
}
func init() {
inputs.Add("nstat", func() telegraf.Input {
return &Nstat{}
})
}