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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
// SPDX-License-Identifier: GPL-3.0-or-later
package nginxplus
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"sync"
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web"
)
const (
urlPathAPIVersions = "/api/"
urlPathAPIEndpointsRoot = "/api/%d"
urlPathAPINginx = "/api/%d/nginx"
urlPathAPIEndpointsHTTP = "/api/%d/http"
urlPathAPIEndpointsStream = "/api/%d/stream"
urlPathAPIConnections = "/api/%d/connections"
urlPathAPISSL = "/api/%d/ssl"
urlPathAPIResolvers = "/api/%d/resolvers"
urlPathAPIHTTPRequests = "/api/%d/http/requests"
urlPathAPIHTTPServerZones = "/api/%d/http/server_zones"
urlPathAPIHTTPLocationZones = "/api/%d/http/location_zones"
urlPathAPIHTTPUpstreams = "/api/%d/http/upstreams"
urlPathAPIHTTPCaches = "/api/%d/http/caches"
urlPathAPIStreamServerZones = "/api/%d/stream/server_zones"
urlPathAPIStreamUpstreams = "/api/%d/stream/upstreams"
)
type nginxMetrics struct {
info *nginxInfo
connections *nginxConnections
ssl *nginxSSL
httpRequests *nginxHTTPRequests
httpServerZones *nginxHTTPServerZones
httpLocationZones *nginxHTTPLocationZones
httpUpstreams *nginxHTTPUpstreams
httpCaches *nginxHTTPCaches
streamServerZones *nginxStreamServerZones
streamUpstreams *nginxStreamUpstreams
resolvers *nginxResolvers
}
func (n *NginxPlus) queryAPIVersion() (int64, error) {
req, _ := web.NewHTTPRequestWithPath(n.Request, urlPathAPIVersions)
var versions nginxAPIVersions
if err := n.doWithDecode(&versions, req); err != nil {
return 0, err
}
if len(versions) == 0 {
return 0, fmt.Errorf("'%s' returned no data", req.URL)
}
return versions[len(versions)-1], nil
}
func (n *NginxPlus) queryAvailableEndpoints() error {
req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIEndpointsRoot, n.apiVersion))
var endpoints []string
if err := n.doWithDecode(&endpoints, req); err != nil {
return err
}
n.Debugf("discovered root endpoints: %v", endpoints)
var hasHTTP, hasStream bool
for _, v := range endpoints {
switch v {
case "nginx":
n.endpoints.nginx = true
case "connections":
n.endpoints.connections = true
case "ssl":
n.endpoints.ssl = true
case "resolvers":
n.endpoints.resolvers = true
case "http":
hasHTTP = true
case "stream":
hasStream = true
}
}
if hasHTTP {
endpoints = endpoints[:0]
req, _ = web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIEndpointsHTTP, n.apiVersion))
if err := n.doWithDecode(&endpoints, req); err != nil {
return err
}
n.Debugf("discovered http endpoints: %v", endpoints)
for _, v := range endpoints {
switch v {
case "requests":
n.endpoints.httpRequest = true
case "server_zones":
n.endpoints.httpServerZones = true
case "location_zones":
n.endpoints.httpLocationZones = true
case "caches":
n.endpoints.httpCaches = true
case "upstreams":
n.endpoints.httpUpstreams = true
}
}
}
if hasStream {
endpoints = endpoints[:0]
req, _ = web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIEndpointsStream, n.apiVersion))
if err := n.doWithDecode(&endpoints, req); err != nil {
return err
}
n.Debugf("discovered stream endpoints: %v", endpoints)
for _, v := range endpoints {
switch v {
case "server_zones":
n.endpoints.streamServerZones = true
case "upstreams":
n.endpoints.streamUpstreams = true
}
}
}
return nil
}
func (n *NginxPlus) queryMetrics() *nginxMetrics {
ms := &nginxMetrics{}
wg := &sync.WaitGroup{}
for _, task := range []struct {
do bool
fn func(*nginxMetrics)
}{
{do: n.endpoints.nginx, fn: n.queryNginxInfo},
{do: n.endpoints.connections, fn: n.queryConnections},
{do: n.endpoints.ssl, fn: n.querySSL},
{do: n.endpoints.httpRequest, fn: n.queryHTTPRequests},
{do: n.endpoints.httpServerZones, fn: n.queryHTTPServerZones},
{do: n.endpoints.httpLocationZones, fn: n.queryHTTPLocationZones},
{do: n.endpoints.httpUpstreams, fn: n.queryHTTPUpstreams},
{do: n.endpoints.httpCaches, fn: n.queryHTTPCaches},
{do: n.endpoints.streamServerZones, fn: n.queryStreamServerZones},
{do: n.endpoints.streamUpstreams, fn: n.queryStreamUpstreams},
{do: n.endpoints.resolvers, fn: n.queryResolvers},
} {
task := task
if task.do {
wg.Add(1)
go func() { task.fn(ms); wg.Done() }()
}
}
wg.Wait()
return ms
}
func (n *NginxPlus) queryNginxInfo(ms *nginxMetrics) {
req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPINginx, n.apiVersion))
var v nginxInfo
if err := n.doWithDecode(&v, req); err != nil {
n.endpoints.nginx = !errors.Is(err, errPathNotFound)
n.Warning(err)
return
}
ms.info = &v
}
func (n *NginxPlus) queryConnections(ms *nginxMetrics) {
req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIConnections, n.apiVersion))
var v nginxConnections
if err := n.doWithDecode(&v, req); err != nil {
n.endpoints.connections = !errors.Is(err, errPathNotFound)
n.Warning(err)
return
}
ms.connections = &v
}
func (n *NginxPlus) querySSL(ms *nginxMetrics) {
req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPISSL, n.apiVersion))
var v nginxSSL
if err := n.doWithDecode(&v, req); err != nil {
n.endpoints.ssl = !errors.Is(err, errPathNotFound)
n.Warning(err)
return
}
ms.ssl = &v
}
func (n *NginxPlus) queryHTTPRequests(ms *nginxMetrics) {
req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIHTTPRequests, n.apiVersion))
var v nginxHTTPRequests
if err := n.doWithDecode(&v, req); err != nil {
n.endpoints.httpRequest = !errors.Is(err, errPathNotFound)
n.Warning(err)
return
}
ms.httpRequests = &v
}
func (n *NginxPlus) queryHTTPServerZones(ms *nginxMetrics) {
req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIHTTPServerZones, n.apiVersion))
var v nginxHTTPServerZones
if err := n.doWithDecode(&v, req); err != nil {
n.endpoints.httpServerZones = !errors.Is(err, errPathNotFound)
n.Warning(err)
return
}
ms.httpServerZones = &v
}
func (n *NginxPlus) queryHTTPLocationZones(ms *nginxMetrics) {
req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIHTTPLocationZones, n.apiVersion))
var v nginxHTTPLocationZones
if err := n.doWithDecode(&v, req); err != nil {
n.endpoints.httpLocationZones = !errors.Is(err, errPathNotFound)
n.Warning(err)
return
}
ms.httpLocationZones = &v
}
func (n *NginxPlus) queryHTTPUpstreams(ms *nginxMetrics) {
req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIHTTPUpstreams, n.apiVersion))
var v nginxHTTPUpstreams
if err := n.doWithDecode(&v, req); err != nil {
n.endpoints.httpUpstreams = !errors.Is(err, errPathNotFound)
n.Warning(err)
return
}
ms.httpUpstreams = &v
}
func (n *NginxPlus) queryHTTPCaches(ms *nginxMetrics) {
req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIHTTPCaches, n.apiVersion))
var v nginxHTTPCaches
if err := n.doWithDecode(&v, req); err != nil {
n.endpoints.httpCaches = !errors.Is(err, errPathNotFound)
n.Warning(err)
return
}
ms.httpCaches = &v
}
func (n *NginxPlus) queryStreamServerZones(ms *nginxMetrics) {
req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIStreamServerZones, n.apiVersion))
var v nginxStreamServerZones
if err := n.doWithDecode(&v, req); err != nil {
n.endpoints.streamServerZones = !errors.Is(err, errPathNotFound)
n.Warning(err)
return
}
ms.streamServerZones = &v
}
func (n *NginxPlus) queryStreamUpstreams(ms *nginxMetrics) {
req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIStreamUpstreams, n.apiVersion))
var v nginxStreamUpstreams
if err := n.doWithDecode(&v, req); err != nil {
n.endpoints.streamUpstreams = !errors.Is(err, errPathNotFound)
n.Warning(err)
return
}
ms.streamUpstreams = &v
}
func (n *NginxPlus) queryResolvers(ms *nginxMetrics) {
req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIResolvers, n.apiVersion))
var v nginxResolvers
if err := n.doWithDecode(&v, req); err != nil {
n.endpoints.resolvers = !errors.Is(err, errPathNotFound)
n.Warning(err)
return
}
ms.resolvers = &v
}
var (
errPathNotFound = errors.New("path not found")
)
func (n *NginxPlus) doWithDecode(dst interface{}, req *http.Request) error {
n.Debugf("executing %s '%s'", req.Method, req.URL)
resp, err := n.httpClient.Do(req)
if err != nil {
return err
}
defer closeBody(resp)
if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("%s returned %d status code (%w)", req.URL, resp.StatusCode, errPathNotFound)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%s returned %d status code (%s)", req.URL, resp.StatusCode, resp.Status)
}
content, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error on reading response from %s : %v", req.URL, err)
}
if err := json.Unmarshal(content, dst); err != nil {
return fmt.Errorf("error on parsing response from %s : %v", req.URL, err)
}
return nil
}
func closeBody(resp *http.Response) {
if resp != nil && resp.Body != nil {
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
}
}
func (n *nginxMetrics) empty() bool {
return n.info != nil &&
n.connections == nil &&
n.ssl == nil &&
n.httpRequests == nil &&
n.httpServerZones == nil &&
n.httpLocationZones == nil &&
n.httpUpstreams == nil &&
n.httpCaches == nil &&
n.streamServerZones == nil &&
n.streamUpstreams == nil &&
n.resolvers != nil
}
|