generated from sensu/check-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetrics.go
427 lines (409 loc) · 11.5 KB
/
metrics.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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package main
import (
"database/sql"
"fmt"
"io"
"os"
"github.com/prometheus/client_golang/prometheus"
)
func init() {
for _, v := range prometheusMetrics {
prometheus.MustRegister(v)
}
// unregister the default collectors. We could also create a new registry,
// but this was actually easier to figure out how to do.
prometheus.Unregister(prometheus.NewGoCollector())
prometheus.Unregister(prometheus.NewBuildInfoCollector())
prometheus.Unregister(prometheus.NewExpvarCollector(nil))
prometheus.Unregister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
}
var metrics = []string{
"active_servers",
"backup_servers",
"bin",
"bout",
"chkfail",
"ctime",
"dreq",
"dresp",
"econ",
"ereq",
"eresp",
"http_response_1xx",
"http_response_2xx",
"http_response_3xx",
"http_response_4xx",
"http_response_5xx",
"http_response_other",
"qcur",
"qmax",
"qtime",
"rate",
"rtime",
"scur",
"slim",
"smax",
"ttime",
"weight",
"wredis",
"wretr",
}
var nameLookup = map[string]string{
"proxy": "pxname",
"sv": "svname",
"active_servers": "act",
"backup_servers": "bck",
"cli_abort": "cli_abrt",
"srv_abort": "srv_abrt",
"http_response_1xx": "hrsp_1xx",
"http_response_2xx": "hrsp_2xx",
"http_response_3xx": "hrsp_3xx",
"http_response_4xx": "hrsp_4xx",
"http_response_5xx": "hrsp_5xx",
"http_response_other": "hrsp_other",
}
func lookupHelp(key string) string {
if k, ok := nameLookup[key]; ok {
key = k
}
return helpLookup[key]
}
var reverseNameLookup = map[string]string{
"pxname": "proxy",
"svname": "sv",
"act": "active_servers",
"bck": "backup_servers",
"cli_abrt": "cli_abort",
"srv_abrt": "srv_abort",
"hrsp_1xx": "http_response_1xx",
"hrsp_2xx": "http_response_2xx",
"hrsp_3xx": "http_response_3xx",
"hrsp_4xx": "http_response_4xx",
"hrsp_5xx": "http_response_5xx",
"hrsp_other": "http_response_other",
}
var helpLookup = map[string]string{
"pxname": "proxy name",
"svname": "service name",
"qcur": "current queued requests",
"qmax": "max queued requests",
"scur": "session current",
"smax": "session max",
"slim": "session limit",
"stot": "session total",
"bin": "bytes in",
"bout": "bytes out",
"dreq": "request denied security",
"dresp": "response denied security",
"ereq": "request errors",
"econ": "connection errors",
"eresp": "response errors",
"wretr": "warning retries",
"wredis": "warning redispatched",
"status": "status",
"weight": "weight",
"act": "servers active",
"bck": "servers backup",
"chkfail": "healthcheck failed",
"chkdown": "healthcheck transitions",
"lastchg": "healthcheck seconds since change",
"downtime": "healthcheck downtime",
"qlimit": "server queue limit",
"pid": "process id",
"iid": "proxy id",
"sid": "server id",
"throttle": "server throttle percent",
"lbtot": "server selected",
"tracked": "tracked server id",
"type": "type",
"rate": "session rate",
"rate_lim": "session rate limit",
"rate_max": "session rate max",
"check_status": "check status",
"check_code": "check code",
"check_duration": "healthcheck duration",
"hrsp_1xx": "response status 1xx",
"hrsp_2xx": "response status 2xx",
"hrsp_3xx": "response status 3xx",
"hrsp_4xx": "response status 4xx",
"hrsp_5xx": "response status 5xx",
"hrsp_other": "response status other",
"hanafail": "failed healthcheck details",
"req_rate": "requests per second",
"req_rate_max": "requests per second max",
"req_tot": "total requests",
"cli_abrt": "client transfer aborts",
"srv_abrt": "server transfer aborts",
"comp_in": "compressor in",
"comp_out": "compressor out",
"comp_byp": "compressor bytes",
"comp_rsp": "compressor responses",
"lastsess": "session last assigned seconds",
"last_chk": "healthcheck contents",
"last_agt": "agent check contents",
"qtime": "queue time",
"ctime": "connect time",
"rtime": "response time",
"ttime": "average time",
"agent_status": "agent status",
"agent_code": "agent code",
"agent_duration": "agent duration",
"check_desc": "check description",
"agent_desc": "agent description",
"check_rise": "check rise",
"check_fall": "check fall",
"check_health": "check health",
"agent_rise": "agent rise",
"agent_fall": "agent fall",
"agent_health": "agent health",
"addr": "address",
"cookie": "cookie",
"mode": "mode",
"algo": "algorithm",
"conn_rate": "connection rate",
"conn_rate_max": "connection rate max",
"conn_tot": "connection tot",
"intercepted": "requests intercepted",
"dcon": "connection requests denied",
"dses": "session requests denied",
}
var prometheusMetrics = map[string]*prometheus.GaugeVec{
"active_servers": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_active_servers",
Help: lookupHelp("active_servers"),
}, tags),
"backup_servers": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_backup_servers",
Help: lookupHelp("backup_servers"),
}, tags),
"bin": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_bin",
Help: lookupHelp("bin"),
}, tags),
"bout": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_bout",
Help: lookupHelp("bout"),
}, tags),
"chkfail": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_chkfail",
Help: lookupHelp("chkfail"),
}, tags),
"ctime": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_ctime",
Help: lookupHelp("ctime"),
}, tags),
"dreq": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_dreq",
Help: lookupHelp("dreq"),
}, tags),
"dresp": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_dresp",
Help: lookupHelp("dresp"),
}, tags),
"econ": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_econ",
Help: lookupHelp("econ"),
}, tags),
"ereq": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_ereq",
Help: lookupHelp("ereq"),
}, tags),
"eresp": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_eresp",
Help: lookupHelp("eresp"),
}, tags),
"http_response_1xx": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_http_response_1xx",
Help: lookupHelp("http_response_1xx"),
}, tags),
"http_response_2xx": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_http_response_2xx",
Help: lookupHelp("http_response_2xx"),
}, tags),
"http_response_3xx": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_http_response_3xx",
Help: lookupHelp("http_response_3xx"),
}, tags),
"http_response_4xx": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_http_response_4xx",
Help: lookupHelp("http_response_4xx"),
}, tags),
"http_response_5xx": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_http_response_5xx",
Help: lookupHelp("http_response_5xx"),
}, tags),
"http_response_other": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_http_response_other",
Help: lookupHelp("http_response_other"),
}, tags),
"qcur": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_qcur",
Help: lookupHelp("qcur"),
}, tags),
"qmax": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_qmax",
Help: lookupHelp("qmax"),
}, tags),
"qtime": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_qtime",
Help: lookupHelp("qtime"),
}, tags),
"rate": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_rate",
Help: lookupHelp("rate"),
}, tags),
"rtime": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_rtime",
Help: lookupHelp("rtime"),
}, tags),
"scur": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_scur",
Help: lookupHelp("scur"),
}, tags),
"slim": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_slim",
Help: lookupHelp("slim"),
}, tags),
"smax": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_smax",
Help: lookupHelp("smax"),
}, tags),
"ttime": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_ttime",
Help: lookupHelp("ttime"),
}, tags),
"weight": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_weight",
Help: lookupHelp("weight"),
}, tags),
"wredis": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_wredis",
Help: lookupHelp("wredis"),
}, tags),
"wretr": prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "haproxy_wretr",
Help: lookupHelp("wretr"),
}, tags),
}
var instanceTypes = []string{
"frontend",
"backend",
"server",
"listener",
}
func lookupName(metric string) string {
val, ok := nameLookup[metric]
if ok {
return val
}
return metric
}
// do not modify this without also modifying tags
var itags = []interface{}{
"pxname",
"addr",
"type",
//"proxy_system",
//"component",
"svname",
}
// do not modify this without also modifying itags
var tags = []string{
"proxy",
"host",
"type",
//"proxy_system",
//"component",
"sv",
}
type Row struct {
ProxyCluster sql.NullString
Host sql.NullString
Type sql.NullInt64
ProxySystem sql.NullString
Component sql.NullString
Proxy string
Service string
MetricName string
Metric sql.NullFloat64
}
// ScanArgs returns the arguments which are passed positionally to rows.Scan.
// If you want to add support for a new tag, this list must be added to.
func (r *Row) ScanArgs() []interface{} {
return []interface{}{
&r.Proxy,
&r.Host,
&r.Type,
//&r.ProxySystem,
//&r.Component,
&r.Service,
&r.Metric,
&r.MetricName,
}
}
// SetPrometheus writes the contents of the row to the prometheus gatherer.
func (r Row) SetPrometheus() {
if !r.Metric.Valid {
return
}
name, ok := reverseNameLookup[r.MetricName]
if !ok {
name = r.MetricName
}
gauge, ok := prometheusMetrics[name]
if !ok {
panic(fmt.Sprintf("can't find metric name: %s", r.MetricName))
}
var hapType string
if !r.Type.Valid {
hapType = ""
} else if r.Type.Int64 < int64(len(instanceTypes)) {
hapType = instanceTypes[r.Type.Int64]
}
gauge.WithLabelValues(r.Proxy, r.Host.String, hapType, r.Service).Set(r.Metric.Float64)
}
// outputMetrics writes all the scraped CSV metrics to prometheus, and then
// scrapes prometheus to produce the final output.
func outputMetrics(data *statsData) error {
db, err := createDB(data)
if err != nil {
return err
}
defer db.Close()
for _, metric := range metrics {
metric = lookupName(metric)
fmtstr := "%s,'%s'"
for range itags {
fmtstr = "%s," + fmtstr
}
cols := fmt.Sprintf(fmtstr, append(itags, []interface{}{metric, metric}...)...)
query := fmt.Sprintf("SELECT %s FROM metrics;", cols)
err := doQuery(db, query)
if err != nil {
return err
}
}
client := netListener.Client()
resp, err := client.Get("http://fake/")
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(os.Stdout, resp.Body)
return err
}
func doQuery(db *sql.DB, query string) error {
rows, err := db.Query(query)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var row Row
if err := rows.Scan(row.ScanArgs()...); err != nil {
return err
}
row.SetPrometheus()
}
return nil
}