-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
net_response.go
277 lines (253 loc) · 6.72 KB
/
net_response.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//go:generate ../../../tools/readme_config_includer/generator
package net_response
import (
"bufio"
_ "embed"
"errors"
"fmt"
"net"
"net/textproto"
"regexp"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal/choice"
"github.com/influxdata/telegraf/plugins/inputs"
)
//go:embed sample.conf
var sampleConfig string
type ResultType uint64
const (
Success ResultType = 0
Timeout ResultType = 1
ConnectionFailed ResultType = 2
ReadFailed ResultType = 3
StringMismatch ResultType = 4
)
// NetResponse struct
type NetResponse struct {
Address string
Timeout config.Duration
ReadTimeout config.Duration
Send string
Expect string
Protocol string
}
func (*NetResponse) SampleConfig() string {
return sampleConfig
}
// TCPGather will execute if there are TCP tests defined in the configuration.
// It will return a map[string]interface{} for fields and a map[string]string for tags
func (n *NetResponse) TCPGather() (map[string]string, map[string]interface{}, error) {
// Prepare returns
tags := make(map[string]string)
fields := make(map[string]interface{})
// Start Timer
start := time.Now()
// Connecting
conn, err := net.DialTimeout("tcp", n.Address, time.Duration(n.Timeout))
// Stop timer
responseTime := time.Since(start).Seconds()
// Handle error
if err != nil {
var e net.Error
if errors.As(err, &e) && e.Timeout() {
setResult(Timeout, fields, tags, n.Expect)
} else {
setResult(ConnectionFailed, fields, tags, n.Expect)
}
return tags, fields, nil
}
defer conn.Close()
// Send string if needed
if n.Send != "" {
msg := []byte(n.Send)
if _, gerr := conn.Write(msg); gerr != nil {
return nil, nil, gerr
}
// Stop timer
responseTime = time.Since(start).Seconds()
}
// Read string if needed
if n.Expect != "" {
// Set read timeout
if gerr := conn.SetReadDeadline(time.Now().Add(time.Duration(n.ReadTimeout))); gerr != nil {
return nil, nil, gerr
}
// Prepare reader
reader := bufio.NewReader(conn)
tp := textproto.NewReader(reader)
// Read
data, err := tp.ReadLine()
// Stop timer
responseTime = time.Since(start).Seconds()
// Handle error
if err != nil {
setResult(ReadFailed, fields, tags, n.Expect)
} else {
// Looking for string in answer
regEx := regexp.MustCompile(`.*` + n.Expect + `.*`)
find := regEx.FindString(data)
if find != "" {
setResult(Success, fields, tags, n.Expect)
} else {
setResult(StringMismatch, fields, tags, n.Expect)
}
}
} else {
setResult(Success, fields, tags, n.Expect)
}
fields["response_time"] = responseTime
return tags, fields, nil
}
// UDPGather will execute if there are UDP tests defined in the configuration.
// It will return a map[string]interface{} for fields and a map[string]string for tags
func (n *NetResponse) UDPGather() (map[string]string, map[string]interface{}, error) {
// Prepare returns
tags := make(map[string]string)
fields := make(map[string]interface{})
// Start Timer
start := time.Now()
// Resolving
udpAddr, err := net.ResolveUDPAddr("udp", n.Address)
// Handle error
if err != nil {
setResult(ConnectionFailed, fields, tags, n.Expect)
return tags, fields, nil
}
// Connecting
conn, err := net.DialUDP("udp", nil, udpAddr)
// Handle error
if err != nil {
setResult(ConnectionFailed, fields, tags, n.Expect)
return tags, fields, nil
}
defer conn.Close()
// Send string
msg := []byte(n.Send)
if _, gerr := conn.Write(msg); gerr != nil {
return nil, nil, gerr
}
// Read string
// Set read timeout
if gerr := conn.SetReadDeadline(time.Now().Add(time.Duration(n.ReadTimeout))); gerr != nil {
return nil, nil, gerr
}
// Read
buf := make([]byte, 1024)
_, _, err = conn.ReadFromUDP(buf)
// Stop timer
responseTime := time.Since(start).Seconds()
// Handle error
if err != nil {
setResult(ReadFailed, fields, tags, n.Expect)
return tags, fields, nil
}
// Looking for string in answer
regEx := regexp.MustCompile(`.*` + n.Expect + `.*`)
find := regEx.FindString(string(buf))
if find != "" {
setResult(Success, fields, tags, n.Expect)
} else {
setResult(StringMismatch, fields, tags, n.Expect)
}
fields["response_time"] = responseTime
return tags, fields, nil
}
// Init performs one time setup of the plugin and returns an error if the
// configuration is invalid.
func (n *NetResponse) Init() error {
// Set default values
if n.Timeout == 0 {
n.Timeout = config.Duration(time.Second)
}
if n.ReadTimeout == 0 {
n.ReadTimeout = config.Duration(time.Second)
}
// Check send and expected string
if n.Protocol == "udp" && n.Send == "" {
return errors.New("send string cannot be empty")
}
if n.Protocol == "udp" && n.Expect == "" {
return errors.New("expected string cannot be empty")
}
// Prepare host and port
host, port, err := net.SplitHostPort(n.Address)
if err != nil {
return err
}
if host == "" {
n.Address = "localhost:" + port
}
if port == "" {
return errors.New("bad port in config option address")
}
if err := choice.Check(n.Protocol, []string{"tcp", "udp"}); err != nil {
return fmt.Errorf("config option protocol: %w", err)
}
return nil
}
// Gather is called by telegraf when the plugin is executed on its interval.
// It will call either UDPGather or TCPGather based on the configuration and
// also fill an Accumulator that is supplied.
func (n *NetResponse) Gather(acc telegraf.Accumulator) error {
// Prepare host and port
host, port, err := net.SplitHostPort(n.Address)
if err != nil {
return err
}
// Prepare data
tags := map[string]string{"server": host, "port": port}
var fields map[string]interface{}
var returnTags map[string]string
// Gather data
switch n.Protocol {
case "tcp":
returnTags, fields, err = n.TCPGather()
if err != nil {
return err
}
tags["protocol"] = "tcp"
case "udp":
returnTags, fields, err = n.UDPGather()
if err != nil {
return err
}
tags["protocol"] = "udp"
}
// Merge the tags
for k, v := range returnTags {
tags[k] = v
}
// Add metrics
acc.AddFields("net_response", fields, tags)
return nil
}
func setResult(result ResultType, fields map[string]interface{}, tags map[string]string, expect string) {
var tag string
switch result {
case Success:
tag = "success"
case Timeout:
tag = "timeout"
case ConnectionFailed:
tag = "connection_failed"
case ReadFailed:
tag = "read_failed"
case StringMismatch:
tag = "string_mismatch"
}
tags["result"] = tag
fields["result_code"] = uint64(result)
// deprecated in 1.7; use result tag
fields["result_type"] = tag
// deprecated in 1.4; use result tag
if expect != "" {
fields["string_found"] = result == Success
}
}
func init() {
inputs.Add("net_response", func() telegraf.Input {
return &NetResponse{}
})
}