forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tengine.go
339 lines (313 loc) · 8.85 KB
/
tengine.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package tengine
import (
"bufio"
"fmt"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
"io"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/inputs"
)
type Tengine struct {
Urls []string
ResponseTimeout config.Duration
tls.ClientConfig
client *http.Client
}
var sampleConfig = `
# An array of Tengine reqstat module URI to gather stats.
urls = ["http://127.0.0.1/us"]
# HTTP response timeout (default: 5s)
# response_timeout = "5s"
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.cer"
# tls_key = "/etc/telegraf/key.key"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false
`
func (n *Tengine) SampleConfig() string {
return sampleConfig
}
func (n *Tengine) Description() string {
return "Read Tengine's basic status information (ngx_http_reqstat_module)"
}
func (n *Tengine) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup
// Create an HTTP client that is re-used for each
// collection interval
if n.client == nil {
client, err := n.createHTTPClient()
if err != nil {
return err
}
n.client = client
}
for _, u := range n.Urls {
addr, err := url.Parse(u)
if err != nil {
acc.AddError(fmt.Errorf("Unable to parse address '%s': %s", u, err))
continue
}
wg.Add(1)
go func(addr *url.URL) {
defer wg.Done()
acc.AddError(n.gatherURL(addr, acc))
}(addr)
}
wg.Wait()
return nil
}
func (n *Tengine) createHTTPClient() (*http.Client, error) {
tlsCfg, err := n.ClientConfig.TLSConfig()
if err != nil {
return nil, err
}
if n.ResponseTimeout < config.Duration(time.Second) {
n.ResponseTimeout = config.Duration(time.Second * 5)
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsCfg,
},
Timeout: time.Duration(n.ResponseTimeout),
}
return client, nil
}
type TengineStatus struct {
host string
bytesIn uint64
bytesOut uint64
connTotal uint64
reqTotal uint64
http2xx uint64
http3xx uint64
http4xx uint64
http5xx uint64
httpOtherStatus uint64
rt uint64
upsReq uint64
upsRt uint64
upsTries uint64
http200 uint64
http206 uint64
http302 uint64
http304 uint64
http403 uint64
http404 uint64
http416 uint64
http499 uint64
http500 uint64
http502 uint64
http503 uint64
http504 uint64
http508 uint64
httpOtherDetailStatus uint64
httpUps4xx uint64
httpUps5xx uint64
}
func (n *Tengine) gatherURL(addr *url.URL, acc telegraf.Accumulator) error {
var tengineStatus TengineStatus
resp, err := n.client.Get(addr.String())
if err != nil {
return fmt.Errorf("error making HTTP request to %s: %s", addr.String(), err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%s returned HTTP status %s", addr.String(), resp.Status)
}
r := bufio.NewReader(resp.Body)
for {
line, err := r.ReadString('\n')
if err != nil || io.EOF == err {
break
}
lineSplit := strings.Split(strings.TrimSpace(line), ",")
if len(lineSplit) != 30 {
continue
}
tengineStatus.host = lineSplit[0]
if err != nil {
return err
}
tengineStatus.bytesIn, err = strconv.ParseUint(lineSplit[1], 10, 64)
if err != nil {
return err
}
tengineStatus.bytesOut, err = strconv.ParseUint(lineSplit[2], 10, 64)
if err != nil {
return err
}
tengineStatus.connTotal, err = strconv.ParseUint(lineSplit[3], 10, 64)
if err != nil {
return err
}
tengineStatus.reqTotal, err = strconv.ParseUint(lineSplit[4], 10, 64)
if err != nil {
return err
}
tengineStatus.http2xx, err = strconv.ParseUint(lineSplit[5], 10, 64)
if err != nil {
return err
}
tengineStatus.http3xx, err = strconv.ParseUint(lineSplit[6], 10, 64)
if err != nil {
return err
}
tengineStatus.http4xx, err = strconv.ParseUint(lineSplit[7], 10, 64)
if err != nil {
return err
}
tengineStatus.http5xx, err = strconv.ParseUint(lineSplit[8], 10, 64)
if err != nil {
return err
}
tengineStatus.httpOtherStatus, err = strconv.ParseUint(lineSplit[9], 10, 64)
if err != nil {
return err
}
tengineStatus.rt, err = strconv.ParseUint(lineSplit[10], 10, 64)
if err != nil {
return err
}
tengineStatus.upsReq, err = strconv.ParseUint(lineSplit[11], 10, 64)
if err != nil {
return err
}
tengineStatus.upsRt, err = strconv.ParseUint(lineSplit[12], 10, 64)
if err != nil {
return err
}
tengineStatus.upsTries, err = strconv.ParseUint(lineSplit[13], 10, 64)
if err != nil {
return err
}
tengineStatus.http200, err = strconv.ParseUint(lineSplit[14], 10, 64)
if err != nil {
return err
}
tengineStatus.http206, err = strconv.ParseUint(lineSplit[15], 10, 64)
if err != nil {
return err
}
tengineStatus.http302, err = strconv.ParseUint(lineSplit[16], 10, 64)
if err != nil {
return err
}
tengineStatus.http304, err = strconv.ParseUint(lineSplit[17], 10, 64)
if err != nil {
return err
}
tengineStatus.http403, err = strconv.ParseUint(lineSplit[18], 10, 64)
if err != nil {
return err
}
tengineStatus.http404, err = strconv.ParseUint(lineSplit[19], 10, 64)
if err != nil {
return err
}
tengineStatus.http416, err = strconv.ParseUint(lineSplit[20], 10, 64)
if err != nil {
return err
}
tengineStatus.http499, err = strconv.ParseUint(lineSplit[21], 10, 64)
if err != nil {
return err
}
tengineStatus.http500, err = strconv.ParseUint(lineSplit[22], 10, 64)
if err != nil {
return err
}
tengineStatus.http502, err = strconv.ParseUint(lineSplit[23], 10, 64)
if err != nil {
return err
}
tengineStatus.http503, err = strconv.ParseUint(lineSplit[24], 10, 64)
if err != nil {
return err
}
tengineStatus.http504, err = strconv.ParseUint(lineSplit[25], 10, 64)
if err != nil {
return err
}
tengineStatus.http508, err = strconv.ParseUint(lineSplit[26], 10, 64)
if err != nil {
return err
}
tengineStatus.httpOtherDetailStatus, err = strconv.ParseUint(lineSplit[27], 10, 64)
if err != nil {
return err
}
tengineStatus.httpUps4xx, err = strconv.ParseUint(lineSplit[28], 10, 64)
if err != nil {
return err
}
tengineStatus.httpUps5xx, err = strconv.ParseUint(lineSplit[29], 10, 64)
if err != nil {
return err
}
tags := getTags(addr, tengineStatus.host)
fields := map[string]interface{}{
"bytes_in": tengineStatus.bytesIn,
"bytes_out": tengineStatus.bytesOut,
"conn_total": tengineStatus.connTotal,
"req_total": tengineStatus.reqTotal,
"http_2xx": tengineStatus.http2xx,
"http_3xx": tengineStatus.http3xx,
"http_4xx": tengineStatus.http4xx,
"http_5xx": tengineStatus.http5xx,
"http_other_status": tengineStatus.httpOtherStatus,
"rt": tengineStatus.rt,
"ups_req": tengineStatus.upsReq,
"ups_rt": tengineStatus.upsRt,
"ups_tries": tengineStatus.upsTries,
"http_200": tengineStatus.http200,
"http_206": tengineStatus.http206,
"http_302": tengineStatus.http302,
"http_304": tengineStatus.http304,
"http_403": tengineStatus.http403,
"http_404": tengineStatus.http404,
"http_416": tengineStatus.http416,
"http_499": tengineStatus.http499,
"http_500": tengineStatus.http500,
"http_502": tengineStatus.http502,
"http_503": tengineStatus.http503,
"http_504": tengineStatus.http504,
"http_508": tengineStatus.http508,
"http_other_detail_status": tengineStatus.httpOtherDetailStatus,
"http_ups_4xx": tengineStatus.httpUps4xx,
"http_ups_5xx": tengineStatus.httpUps5xx,
}
acc.AddFields("tengine", fields, tags)
}
// Return the potential error of the loop-read
return err
}
// Get tag(s) for the tengine plugin
func getTags(addr *url.URL, serverName string) map[string]string {
h := addr.Host
host, port, err := net.SplitHostPort(h)
if err != nil {
host = addr.Host
if addr.Scheme == "http" {
port = "80"
} else if addr.Scheme == "https" {
port = "443"
} else {
port = ""
}
}
return map[string]string{"server": host, "port": port, "server_name": serverName}
}
func init() {
inputs.Add("tengine", func() telegraf.Input {
return &Tengine{}
})
}