forked from SumoLogic/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nginx_sts.go
304 lines (266 loc) · 8.98 KB
/
nginx_sts.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
package nginx_sts
import (
"bufio"
"encoding/json"
"fmt"
"net"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/inputs"
)
type NginxSTS struct {
Urls []string `toml:"urls"`
ResponseTimeout internal.Duration `toml:"response_timeout"`
tls.ClientConfig
client *http.Client
}
var sampleConfig = `
## An array of ngx_http_status_module or status URI to gather stats.
urls = ["http://localhost/status"]
## HTTP response timeout (default: 5s)
response_timeout = "5s"
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false
`
func (n *NginxSTS) SampleConfig() string {
return sampleConfig
}
func (n *NginxSTS) Description() string {
return "Read Nginx virtual host traffic status module information (nginx-module-sts)"
}
func (n *NginxSTS) 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 *NginxSTS) createHTTPClient() (*http.Client, error) {
if n.ResponseTimeout.Duration < time.Second {
n.ResponseTimeout.Duration = time.Second * 5
}
tlsConfig, err := n.ClientConfig.TLSConfig()
if err != nil {
return nil, err
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
Timeout: n.ResponseTimeout.Duration,
}
return client, nil
}
func (n *NginxSTS) gatherURL(addr *url.URL, acc telegraf.Accumulator) error {
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)
}
contentType := strings.Split(resp.Header.Get("Content-Type"), ";")[0]
switch contentType {
case "application/json":
return gatherStatusURL(bufio.NewReader(resp.Body), getTags(addr), acc)
default:
return fmt.Errorf("%s returned unexpected content type %s", addr.String(), contentType)
}
}
type NginxSTSResponse struct {
Connections struct {
Active uint64 `json:"active"`
Reading uint64 `json:"reading"`
Writing uint64 `json:"writing"`
Waiting uint64 `json:"waiting"`
Accepted uint64 `json:"accepted"`
Handled uint64 `json:"handled"`
Requests uint64 `json:"requests"`
} `json:"connections"`
Hostname string `json:"hostName"`
StreamFilterZones map[string]map[string]Server `json:"streamFilterZones"`
StreamServerZones map[string]Server `json:"streamServerZones"`
StreamUpstreamZones map[string][]Upstream `json:"streamUpstreamZones"`
}
type Server struct {
ConnectCounter uint64 `json:"connectCounter"`
InBytes uint64 `json:"inBytes"`
OutBytes uint64 `json:"outBytes"`
SessionMsecCounter uint64 `json:"sessionMsecCounter"`
SessionMsec uint64 `json:"sessionMsec"`
Responses struct {
OneXx uint64 `json:"1xx"`
TwoXx uint64 `json:"2xx"`
ThreeXx uint64 `json:"3xx"`
FourXx uint64 `json:"4xx"`
FiveXx uint64 `json:"5xx"`
} `json:"responses"`
}
type Upstream struct {
Server string `json:"server"`
ConnectCounter uint64 `json:"connectCounter"`
InBytes uint64 `json:"inBytes"`
OutBytes uint64 `json:"outBytes"`
Responses struct {
OneXx uint64 `json:"1xx"`
TwoXx uint64 `json:"2xx"`
ThreeXx uint64 `json:"3xx"`
FourXx uint64 `json:"4xx"`
FiveXx uint64 `json:"5xx"`
} `json:"responses"`
SessionMsecCounter uint64 `json:"sessionMsecCounter"`
SessionMsec uint64 `json:"sessionMsec"`
USessionMsecCounter uint64 `json:"uSessionMsecCounter"`
USessionMsec uint64 `json:"uSessionMsec"`
UConnectMsecCounter uint64 `json:"uConnectMsecCounter"`
UConnectMsec uint64 `json:"uConnectMsec"`
UFirstByteMsecCounter uint64 `json:"uFirstByteMsecCounter"`
UFirstByteMsec uint64 `json:"uFirstByteMsec"`
Weight uint64 `json:"weight"`
MaxFails uint64 `json:"maxFails"`
FailTimeout uint64 `json:"failTimeout"`
Backup bool `json:"backup"`
Down bool `json:"down"`
}
func gatherStatusURL(r *bufio.Reader, tags map[string]string, acc telegraf.Accumulator) error {
dec := json.NewDecoder(r)
status := &NginxSTSResponse{}
if err := dec.Decode(status); err != nil {
return fmt.Errorf("Error while decoding JSON response")
}
acc.AddFields("nginx_sts_connections", map[string]interface{}{
"active": status.Connections.Active,
"reading": status.Connections.Reading,
"writing": status.Connections.Writing,
"waiting": status.Connections.Waiting,
"accepted": status.Connections.Accepted,
"handled": status.Connections.Handled,
"requests": status.Connections.Requests,
}, tags)
for zoneName, zone := range status.StreamServerZones {
zoneTags := map[string]string{}
for k, v := range tags {
zoneTags[k] = v
}
zoneTags["zone"] = zoneName
acc.AddFields("nginx_sts_server", map[string]interface{}{
"connects": zone.ConnectCounter,
"in_bytes": zone.InBytes,
"out_bytes": zone.OutBytes,
"session_msec_counter": zone.SessionMsecCounter,
"session_msec": zone.SessionMsec,
"response_1xx_count": zone.Responses.OneXx,
"response_2xx_count": zone.Responses.TwoXx,
"response_3xx_count": zone.Responses.ThreeXx,
"response_4xx_count": zone.Responses.FourXx,
"response_5xx_count": zone.Responses.FiveXx,
}, zoneTags)
}
for filterName, filters := range status.StreamFilterZones {
for filterKey, upstream := range filters {
filterTags := map[string]string{}
for k, v := range tags {
filterTags[k] = v
}
filterTags["filter_key"] = filterKey
filterTags["filter_name"] = filterName
acc.AddFields("nginx_sts_filter", map[string]interface{}{
"connects": upstream.ConnectCounter,
"in_bytes": upstream.InBytes,
"out_bytes": upstream.OutBytes,
"session_msec_counter": upstream.SessionMsecCounter,
"session_msec": upstream.SessionMsec,
"response_1xx_count": upstream.Responses.OneXx,
"response_2xx_count": upstream.Responses.TwoXx,
"response_3xx_count": upstream.Responses.ThreeXx,
"response_4xx_count": upstream.Responses.FourXx,
"response_5xx_count": upstream.Responses.FiveXx,
}, filterTags)
}
}
for upstreamName, upstreams := range status.StreamUpstreamZones {
for _, upstream := range upstreams {
upstreamServerTags := map[string]string{}
for k, v := range tags {
upstreamServerTags[k] = v
}
upstreamServerTags["upstream"] = upstreamName
upstreamServerTags["upstream_address"] = upstream.Server
acc.AddFields("nginx_sts_upstream", map[string]interface{}{
"connects": upstream.ConnectCounter,
"session_msec": upstream.SessionMsec,
"session_msec_counter": upstream.SessionMsecCounter,
"upstream_session_msec": upstream.USessionMsec,
"upstream_session_msec_counter": upstream.USessionMsecCounter,
"upstream_connect_msec": upstream.UConnectMsec,
"upstream_connect_msec_counter": upstream.UConnectMsecCounter,
"upstream_firstbyte_msec": upstream.UFirstByteMsec,
"upstream_firstbyte_msec_counter": upstream.UFirstByteMsecCounter,
"in_bytes": upstream.InBytes,
"out_bytes": upstream.OutBytes,
"response_1xx_count": upstream.Responses.OneXx,
"response_2xx_count": upstream.Responses.TwoXx,
"response_3xx_count": upstream.Responses.ThreeXx,
"response_4xx_count": upstream.Responses.FourXx,
"response_5xx_count": upstream.Responses.FiveXx,
"weight": upstream.Weight,
"max_fails": upstream.MaxFails,
"fail_timeout": upstream.FailTimeout,
"backup": upstream.Backup,
"down": upstream.Down,
}, upstreamServerTags)
}
}
return nil
}
// Get tag(s) for the nginx plugin
func getTags(addr *url.URL) 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{"source": host, "port": port}
}
func init() {
inputs.Add("nginx_sts", func() telegraf.Input {
return &NginxSTS{}
})
}