-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind_exporter_stats.go
610 lines (591 loc) · 19.1 KB
/
bind_exporter_stats.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
package main
import (
"bytes"
"io/ioutil"
"math"
"os/exec"
"regexp"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
// ftp://ftp.isc.org/isc/bind9/9.9.0/doc/arm/Bv9ARM.ch06.html
var (
subReg, _ = regexp.Compile(" ?\\+\\+ ?")
viewReg, _ = regexp.Compile("[\\(|\\)|\\<|/]")
numReg, _ = regexp.Compile(`[0-9]+`)
letReg, _ = regexp.Compile(`[0-9a-zA-Z()><-]+`)
up = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
"Was the Bind instance query successful?",
nil, nil,
)
bootTime = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "boot_time_seconds"),
"Start time of the BIND process since unix epoch in seconds.",
nil, nil,
)
nameServerStatistics = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "name_server_stats_total"),
"Name Server Statistics Counters.",
[]string{"type"}, nil,
)
outgoingQueries = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "outgoing_queries_total"),
"Outgoing Queries.",
[]string{"view", "type"}, nil,
)
incomingQueries = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "incoming_queries_total"),
"Number of incoming DNS queries.",
[]string{"type"}, nil,
)
incomingRequests = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "incoming_requests_total"),
"Number of incoming DNS requests.",
[]string{"opcode"}, nil,
)
resolverMetricStatsFile = map[string]*prometheus.Desc{
"GlueFetchv4": prometheus.NewDesc(
prometheus.BuildFQName(namespace, RESOLVER_STATS, "ipv4_ns_total"),
"IPv4 NS address fetches.",
[]string{"view"}, nil,
),
"GlueFetchv6": prometheus.NewDesc(
prometheus.BuildFQName(namespace, RESOLVER_STATS, "ipv6_ns_total"),
"IPv6 NS address fetches.",
[]string{"view"}, nil,
),
"EDNS0Fail": prometheus.NewDesc(
prometheus.BuildFQName(namespace, RESOLVER_STATS, "query_edns_failures_total"),
"EDNS(0) query failures.",
[]string{"view"}, nil,
),
"Mismatch": prometheus.NewDesc(
prometheus.BuildFQName(namespace, RESOLVER_STATS, "response_mismatch_total"),
"Number of mismatch responses received.",
[]string{"view"}, nil,
),
"Retry": prometheus.NewDesc(
prometheus.BuildFQName(namespace, RESOLVER_STATS, "query_retries_total"),
"Number of resolver query retries.",
[]string{"view"}, nil,
),
"Truncated": prometheus.NewDesc(
prometheus.BuildFQName(namespace, RESOLVER_STATS, "response_truncated_total"),
"Number of truncated responses received.",
[]string{"view"}, nil,
),
"Queryv4": prometheus.NewDesc(
prometheus.BuildFQName(namespace, RESOLVER_STATS, "ipv4_queries_sent_total"),
"IPv4 queries sent.",
[]string{"view"}, nil,
),
"Queryv6": prometheus.NewDesc(
prometheus.BuildFQName(namespace, RESOLVER_STATS, "ipv6_queries_sent_total"),
"IPv6 queries sent.",
[]string{"view"}, nil,
),
"Responsev4": prometheus.NewDesc(
prometheus.BuildFQName(namespace, RESOLVER_STATS, "ipv4_responses_received_total"),
"IPv4 responses received.",
[]string{"view"}, nil,
),
"Responsev6": prometheus.NewDesc(
prometheus.BuildFQName(namespace, RESOLVER_STATS, "ipv6_responses_received_total"),
"IPv6 responses received.",
[]string{"view"}, nil,
),
"NXDOMAIN": prometheus.NewDesc(
prometheus.BuildFQName(namespace, RESOLVER_STATS, "nxdomain_received_total"),
"NXDOMAIN received.",
[]string{"view"}, nil,
),
"SERVFAIL": prometheus.NewDesc(
prometheus.BuildFQName(namespace, RESOLVER_STATS, "servfail_received_total"),
"SERVFAIL received.",
[]string{"view"}, nil,
),
"QryRTTnn": prometheus.NewDesc(
prometheus.BuildFQName(namespace, RESOLVER_STATS, "queries_with_rtt_milliseconds_histogram"),
"Frequency table on round trip times (RTTs) of queries. Each nn specifies the corresponding frequency.",
[]string{"view"}, nil,
),
"QueryTimeout": prometheus.NewDesc(
prometheus.BuildFQName(namespace, RESOLVER_STATS, "query_timeouts_total"),
"Query timeouts.",
[]string{"view"}, nil,
),
}
socketIO = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "socket_io_total"),
"Socket I/O statistics counters are defined per socket types.",
[]string{"type"}, nil,
)
zoneMetricStats = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "zone_maintenance_total"),
"Zone Maintenance Statistics Counters.",
[]string{"type"}, nil,
)
cacheRRsetsStats = prometheus.NewDesc(
prometheus.BuildFQName(namespace, CACHE_STATS, "cache_rrsets"),
"Number of RRSets in Cache database.",
[]string{"view", "type"}, nil,
)
cacheStatistics = prometheus.NewDesc(
prometheus.BuildFQName(namespace, CACHE_STATS, "statistics"),
"Cache Statistics.",
[]string{"view", "type"}, nil,
)
cacheMetricStatsFile = map[string]*prometheus.Desc{
"Buckets": prometheus.NewDesc(
prometheus.BuildFQName(namespace, CACHE_STATS, "database_buckets"),
"cache database hash buckets",
[]string{"view"}, nil,
),
"Nodes": prometheus.NewDesc(
prometheus.BuildFQName(namespace, CACHE_STATS, "database_nodes"),
"cache database nodes",
[]string{"view"}, nil,
),
"UseHeapHighest": prometheus.NewDesc(
prometheus.BuildFQName(namespace, CACHE_STATS, "use_heap_highest"),
"cache heap highest memory in use",
[]string{"view"}, nil,
),
"UseHeapMemory": prometheus.NewDesc(
prometheus.BuildFQName(namespace, CACHE_STATS, "use_heap_memory"),
"cache heap memory in use",
[]string{"view"}, nil,
),
"TotalHeapMemory": prometheus.NewDesc(
prometheus.BuildFQName(namespace, CACHE_STATS, "heap_memory_total"),
"cache heap memory total",
[]string{"view"}, nil,
),
"Hits": prometheus.NewDesc(
prometheus.BuildFQName(namespace, CACHE_STATS, "hits"),
"cache hits",
[]string{"view"}, nil,
),
"QueryHits": prometheus.NewDesc(
prometheus.BuildFQName(namespace, CACHE_STATS, "query_hits"),
"cache hits from query",
[]string{"view"}, nil,
),
"Misses": prometheus.NewDesc(
prometheus.BuildFQName(namespace, CACHE_STATS, "misses"),
"cache misses",
[]string{"view"}, nil,
),
"QueryMisses": prometheus.NewDesc(
prometheus.BuildFQName(namespace, CACHE_STATS, "query_misses"),
"cache misses from query",
[]string{"view"}, nil,
),
"DelTTL": prometheus.NewDesc(
prometheus.BuildFQName(namespace, CACHE_STATS, "delete_ttl"),
"cache records deleted due to TTL expiration",
[]string{"view"}, nil,
),
"DelMem": prometheus.NewDesc(
prometheus.BuildFQName(namespace, CACHE_STATS, "delete_memory"),
"cache records deleted due to memory exhaustion",
[]string{"view"}, nil,
),
"UseTreeHighest": prometheus.NewDesc(
prometheus.BuildFQName(namespace, CACHE_STATS, "use_tree_highest"),
"cache tree highest memory in use",
[]string{"view"}, nil,
),
"UseTreeMemory": prometheus.NewDesc(
prometheus.BuildFQName(namespace, CACHE_STATS, "use_tree_memory"),
"cache tree memory in use",
[]string{"view"}, nil,
),
"TotalTreeMemory": prometheus.NewDesc(
prometheus.BuildFQName(namespace, CACHE_STATS, "tree_memory_total"),
"cache tree memory total",
[]string{"view"}, nil,
),
}
nameServerMap = map[string]string{
"IPv requests received": "IPv",
"TCP requests received": "ReqTCP",
"duplicate queries received": "QryDuplicate",
"queries caused recursion": "QryRecursion",
"queries dropped": "QryDropped",
"queries resulted in NXDOMAIN": "QryNXDOMAIN",
"queries resulted in SERVFAIL": "QrySERVFAIL",
"queries resulted in authoritative answer": "QryAuthAns",
"queries resulted in non authoritative answer": "QryNoauthAns",
"queries resulted in nxrrset": "QryNxrrset",
"queries resulted in referral answer": "QryReferral",
"queries resulted in successful answer": "QrySuccess",
"requests with EDNS received": "ReqEdns",
"requests with TSIG received": "ReqTSIG",
"responses sent": "Response",
"responses with EDNS sent": "RespEDNS",
"truncated responses sent": "RespTruncated",
}
resolverStatisticsMap = map[string]string{
"IPv4 queries sent": "Queryv4",
"IPv6 queries sent": "Queryv6",
"IPv4 responses received": "Responsev4",
"IPv6 responses received": "Responsev6",
"NXDOMAIN received": "NXDOMAIN",
"SERVFAIL received": "SERVFAIL",
"FORMERR received": "FORMERR",
"Mismatch responses received": "Mismatch",
"Other errors received": "OtherError",
"EDNS(0) query failures": "EDNS0Fail",
"IPv4 NS address fetches": "GlueFetchv4",
"IPv6 NS address fetches": "GlueFetchv6",
"queries with RTT 10-100ms": "QryRTTnn",
"queries with RTT 100-500ms": "QryRTTnn",
"queries with RTT 500-800ms": "QryRTTnn",
"queries with RTT 800-1600ms": "QryRTTnn",
"queries with RTT < 10ms": "QryRTTnn",
"queries with RTT > 1600ms": "QryRTTnn",
"query retries": "Retry",
"query timeouts": "QueryTimeout",
"truncated responses received": "Truncated",
}
socketMap = map[string]string{
"Raw sockets opened": "Raw_Open",
"Raw sockets active": "Raw_Active",
"TCP IPv4 connections accepted": "TCPv4_Accept",
"TCP IPv4 sockets active": "TCPv4_Active",
"TCP IPv4 sockets closed": "TCPv4_Close",
"TCP IPv4 sockets opened": "TCPv4_Open",
"TCP IPv6 socket bind failures": "TCPv6_BindFail",
"TCP IPv6 sockets closed": "TCPv6_Close",
"TCP IPv6 sockets opened": "TCPv6_Open",
"UDP IPv4 connections established": "UDPv4_Conn",
"UDP IPv4 send errors": "UDPv4_SendErr",
"UDP IPv4 sockets active": "UDPv4_Active",
"UDP IPv4 sockets closed": "UDPv4_Close",
"UDP IPv4 sockets opened": "UDPv4_Open",
}
zoneMap = map[string]string{
"IPv6 notifies sent": "NotifyOutv6",
"IPv6 notifies received": "NotifyInv6",
"IPv6 SOA queries sent": "SOAOutv6",
"IPv6 AXFR requested": "AXFRReqv6",
"IPv6 IXFR requested": "IXFRReqv6",
"IPv4 IXFR requested": "IXFRReqv4",
"IPv4 SOA queries sent": "SOAOutv4",
"IPv4 notifies received": "NotifyInv4",
"IPv4 notifies sent": "NotifyOutv4",
"IPv4 AXFR requested": "AXFRReqv4",
"notifies rejected": "NotifyRej",
"Incoming notifies rejected": "NotifyRej",
"transfer requests succeeded": "XfrSuccess",
"Zone transfer requests succeeded": "XfrSuccess",
"Zone transfer requests failed": "XfrFail",
"transfer requests failed": "XfrFail",
}
cacheStatsMap = map[string]string{
"cache database hash buckets": "Buckets",
"cache database nodes": "Nodes",
"cache heap highest memory in use": "UseHeapHighest",
"cache heap memory in use": "UseHeapMemory",
"cache heap memory total": "TotalHeapMemory",
"cache hits": "Hits",
"cache hits (from query)": "QueryHits",
"cache misses": "Misses",
"cache misses (from query)": "QueryMisses",
"cache records deleted due to TTL expiration": "DelTTL",
"cache records deleted due to memory exhaustion": "DelMem",
"cache tree highest memory in use": "UseTreeHighest",
"cache tree memory in use": "UseTreeMemory",
"cache tree memory total": "TotalTreeMemory",
}
)
type statsCollector struct {
filePath string
rndc string
}
// newServerCollector implements collectorConstructor.
func NewStatsCollector(fd, rndc string) prometheus.Collector {
return &statsCollector{
filePath: fd,
rndc: rndc,
}
}
// Describe implements prometheus.Collector.
func (c *statsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- up
ch <- bootTime
ch <- nameServerStatistics
ch <- incomingQueries
ch <- incomingRequests
ch <- incomingRequests
ch <- socketIO
ch <- zoneMetricStats
ch <- cacheRRsetsStats
ch <- cacheStatistics
for _, desc := range resolverMetricStatsFile {
ch <- desc
}
for _, desc := range cacheMetricStatsFile {
ch <- desc
}
}
// Collect implements prometheus.Collector.
func (c *statsCollector) Collect(ch chan<- prometheus.Metric) {
var outInfo bytes.Buffer
rcmd := exec.Command("/bin/sh", c.rndc)
rcmd.Stdout = &outInfo
err := rcmd.Run()
log.Info("sh info:", outInfo.String())
if err != nil {
log.Error(err)
ch <- prometheus.MustNewConstMetric(
up, prometheus.GaugeValue, 0,
)
return
}
contentBs, err := ioutil.ReadFile(c.filePath)
if err != nil || len(contentBs) < 10 {
log.Error(err)
ch <- prometheus.MustNewConstMetric(
up, prometheus.GaugeValue, 0,
)
return
}
statsInfo := ParserStats(string(contentBs))
// log.Println(statsInfo)
ch <- prometheus.MustNewConstMetric(
bootTime, prometheus.GaugeValue, float64(statsInfo.BootTime),
)
if mds, ok := statsInfo.ModuleMap["Incoming Requests"]; ok {
for _, md := range mds {
for key, value := range md.Info {
ch <- prometheus.MustNewConstMetric(
incomingRequests, prometheus.GaugeValue, value, key,
)
}
}
}
if mds, ok := statsInfo.ModuleMap["Incoming Queries"]; ok {
for _, md := range mds {
for key, value := range md.Info {
ch <- prometheus.MustNewConstMetric(
incomingQueries, prometheus.CounterValue, value, key,
)
}
}
}
// Name Server Statistics
if mds, ok := statsInfo.ModuleMap["Name Server Statistics"]; ok {
for _, md := range mds {
for key, value := range md.Info {
if tk, kok := nameServerMap[key]; kok {
ch <- prometheus.MustNewConstMetric(
nameServerStatistics, prometheus.CounterValue, value, tk,
)
}
}
}
}
// Outgoing Queries
if mds, ok := statsInfo.ModuleMap["Outgoing Queries"]; ok {
for _, md := range mds {
for key, value := range md.Info {
ch <- prometheus.MustNewConstMetric(
outgoingQueries, prometheus.CounterValue, value, md.View, key,
)
}
}
}
// Resolver Statistics resolverQueries
if mds, ok := statsInfo.ModuleMap["Resolver Statistics"]; ok {
for _, md := range mds {
for key, value := range md.Info {
if rk, kok := resolverStatisticsMap[key]; kok {
if rk != "QryRTTnn" {
if pd, pok := resolverMetricStatsFile[rk]; pok {
ch <- prometheus.MustNewConstMetric(
pd, prometheus.CounterValue, value, md.View,
)
}
}
}
}
if pd, pok := resolverMetricStatsFile["QryRTTnn"]; pok {
if buckets, count, err := getHistogram(md); err == nil {
ch <- prometheus.MustNewConstHistogram(
pd, count, math.NaN(), buckets, md.View,
)
}
}
}
}
// Socket IO Statistics
if mds, ok := statsInfo.ModuleMap["Socket IO Statistics"]; ok {
for _, md := range mds {
for key, value := range md.Info {
if tk, kok := socketMap[key]; kok {
ch <- prometheus.MustNewConstMetric(
socketIO, prometheus.CounterValue, value, tk,
)
}
}
}
}
// Zone Maintenance Statistics
if mds, ok := statsInfo.ModuleMap["Zone Maintenance Statistics"]; ok {
for _, md := range mds {
for key, value := range md.Info {
if tk, kok := zoneMap[key]; kok {
ch <- prometheus.MustNewConstMetric(
zoneMetricStats, prometheus.CounterValue, value, tk,
)
}
}
}
}
// Cache DB RRsets
if mds, ok := statsInfo.ModuleMap["Cache DB RRsets"]; ok {
for _, md := range mds {
for key, value := range md.Info {
/*if len(md.View) < 1 {
continue
}*/
idx := strings.Index(md.View, "(")
if idx < 0 {
idx = len(md.View)
}
view := strings.Trim(md.View[0:idx], " ")
ch <- prometheus.MustNewConstMetric(
cacheRRsetsStats, prometheus.CounterValue, value, view, key,
)
}
}
}
// Cache Statistics
if mds, ok := statsInfo.ModuleMap["Cache Statistics"]; ok {
for _, md := range mds {
for key, value := range md.Info {
if rk, kok := cacheStatsMap[key]; kok {
if pd, pok := cacheMetricStatsFile[rk]; pok {
ch <- prometheus.MustNewConstMetric(
pd, prometheus.GaugeValue, value, md.View,
)
}
}
}
}
}
ch <- prometheus.MustNewConstMetric(
up, prometheus.GaugeValue, 1,
)
}
type RttHistog struct {
key string
le float64
}
var (
rttList = []RttHistog{
RttHistog{"queries with RTT < 10ms", 10},
RttHistog{"queries with RTT 10-100ms", 100},
RttHistog{"queries with RTT 100-500ms", 500},
RttHistog{"queries with RTT 500-800ms", 800},
RttHistog{"queries with RTT 800-1600ms", 1600},
RttHistog{"queries with RTT > 1600ms", 2000},
}
)
func getHistogram(md Module) (map[float64]uint64, uint64, error) {
buckets := map[float64]uint64{}
var count uint64
for _, rtt := range rttList {
if value, ok := md.Info[rtt.key]; ok {
buckets[rtt.le] = count + uint64(value)
count += uint64(value)
}
}
return buckets, count, nil
}
// easygen: json
type Module struct {
View string `json:"view"`
Info map[string]float64 `json:"info"`
}
// easygen: json
type StatusInfo struct {
BootTime int64 `json:"boot_time"`
ModuleMap map[string][]Module `json:"module_map"`
}
func ParserStats(content string) *StatusInfo {
lines := strings.Split(content, "\n")
sub := ""
stats := StatusInfo{
ModuleMap: map[string][]Module{},
} // map[string][]Module{}
ts := []string{}
im := &Module{
Info: map[string]float64{},
}
view := ""
for _, line := range lines {
num := []string{}
zimu := []string{}
if strings.HasPrefix(line, "+++") {
// 提取时间戳
ts = numReg.FindAllString(line, -1)
} else if strings.HasPrefix(line, "---") {
break
} else if strings.HasPrefix(line, "++") {
// sub = ""
if len(im.Info) > 0 || len(im.View) > 0 {
stats.ModuleMap[sub] = append(stats.ModuleMap[sub], *im)
im = &Module{
Info: map[string]float64{},
View: "",
}
}
im.View = ""
sub = subReg.ReplaceAllString(line, "")
sub = viewReg.ReplaceAllString(sub, "")
// sub = sss3.ReplaceAllString(sub, "")
} else if strings.HasPrefix(line, "[") {
view = strings.ReplaceAll(line, "[", "")
view = strings.ReplaceAll(view, "View:", "")
view = strings.ReplaceAll(view, "]", "")
view = strings.Trim(view, " ")
if len(im.Info) > 0 {
stats.ModuleMap[sub] = append(stats.ModuleMap[sub], *im)
}
im = &Module{
Info: map[string]float64{},
View: view,
}
// fmt.Println(sub, view)
} else {
num = numReg.FindAllString(line, 1)
/*zimu = letReg.FindAllString(line, -1)
if len(num) > 0 && len(zimu) > 0 {
v, _ := strconv.ParseFloat(num[0], 10)
im.Info[strings.Join(zimu, " ")] = v
}*/
if len(num) > 0 {
line = strings.Replace(line, num[0], "", 1)
zimu = letReg.FindAllString(line, -1)
if len(zimu) > 0 {
v, _ := strconv.ParseFloat(num[0], 10)
im.Info[strings.Join(zimu, " ")] = v
}
}
}
}
if len(ts) > 0 {
ti, _ := strconv.ParseInt(ts[0], 10, 64)
stats.BootTime = ti
}
/*fmt.Println(stats, ts[0])
bs, _ := json.Marshal(stats)
fmt.Println(string(bs))*/
return &stats
}