-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
844 lines (809 loc) · 26 KB
/
main.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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
package main
import (
"database/sql"
"fmt"
"math"
"net/http"
"strconv"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
)
const (
namespace = "manticore"
)
var (
labels = []string{"index"}
)
// Exporter collects metrics from a manticore server.
type Exporter struct {
manticore string
up *prometheus.Desc
uptime *prometheus.Desc
connections *prometheus.Desc
maxed_out *prometheus.Desc
version *prometheus.Desc
mysql_version *prometheus.Desc
command_search *prometheus.Desc
command_update *prometheus.Desc
command_delete *prometheus.Desc
command_keywords *prometheus.Desc
command_persist *prometheus.Desc
command_status *prometheus.Desc
command_flushattrs *prometheus.Desc
command_set *prometheus.Desc
command_insert *prometheus.Desc
command_replace *prometheus.Desc
command_commit *prometheus.Desc
command_suggest *prometheus.Desc
command_json *prometheus.Desc
command_callpq *prometheus.Desc
agent_connect *prometheus.Desc
agent_retry *prometheus.Desc
queries *prometheus.Desc
dist_queries *prometheus.Desc
workers_total *prometheus.Desc
workers_active *prometheus.Desc
work_queue_length *prometheus.Desc
query_wall *prometheus.Desc
query_cpu *prometheus.Desc
dist_wall *prometheus.Desc
dist_local *prometheus.Desc
dist_wait *prometheus.Desc
query_reads *prometheus.Desc
query_readkb *prometheus.Desc
query_readtime *prometheus.Desc
avg_query_wall *prometheus.Desc
avg_query_cpu *prometheus.Desc
avg_dist_wall *prometheus.Desc
avg_dist_local *prometheus.Desc
avg_dist_wait *prometheus.Desc
avg_query_reads *prometheus.Desc
avg_query_readkb *prometheus.Desc
avg_query_readtime *prometheus.Desc
qcache_max_bytes *prometheus.Desc
qcache_thresh_msec *prometheus.Desc
qcache_ttl_sec *prometheus.Desc
qcache_cached_queries *prometheus.Desc
qcache_used_bytes *prometheus.Desc
qcache_hits *prometheus.Desc
index_count *prometheus.Desc
indexed_documents *prometheus.Desc
indexed_bytes *prometheus.Desc
field_tokens_title *prometheus.Desc
field_tokens_body *prometheus.Desc
total_tokens *prometheus.Desc
ram_bytes *prometheus.Desc
disk_bytes *prometheus.Desc
mem_limit *prometheus.Desc
threads_count *prometheus.Desc
cluster_status *prometheus.Desc
cluster_size *prometheus.Desc
cluster_node_state *prometheus.Desc
}
func NewExporter(server string, port string, timeout time.Duration) *Exporter {
c := "@tcp(" + server + ":" + port + ")/"
return &Exporter{
manticore: c,
up: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
"Could the manticore server be reached.",
nil,
nil,
),
uptime: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "uptime"),
"Number of seconds since the server started.",
nil,
nil,
),
connections: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "connections"),
"Number of connections since the server started.",
nil,
nil,
),
maxed_out: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "maxed_out"),
"Number of max children barier since the server started.",
nil,
nil,
),
version: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "version"),
"Server version.",
nil,
nil,
),
mysql_version: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "mysql_version"),
"Mysql version.",
nil,
nil,
),
command_search: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "command_search"),
"Number of search command since server start.",
nil,
nil,
),
command_update: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "command_update"),
"Number of update command since server start.",
nil,
nil,
),
command_delete: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "command_delete"),
"Number of delete command since server start.",
nil,
nil,
),
command_keywords: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "command_keywords"),
"Number of keywords command since server start.",
nil,
nil,
),
command_persist: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "command_persist"),
"Number of persist command since server start.",
nil,
nil,
),
command_status: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "command_status"),
"Number of status command since server start.",
nil,
nil,
),
command_flushattrs: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "command_flushattrs"),
"Number of flushattrs command since server start.",
nil,
nil,
),
command_set: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "command_set"),
"Number of set command since server start.",
nil,
nil,
),
command_insert: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "command_insert"),
"Number of insert command since server start.",
nil,
nil,
),
command_replace: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "command_replace"),
"Number of replace command since server start.",
nil,
nil,
),
command_commit: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "command_commit"),
"Number of commit command since server start.",
nil,
nil,
),
command_suggest: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "command_suggest"),
"Number of suggest command since server start.",
nil,
nil,
),
command_json: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "command_json"),
"Number of json command since server start.",
nil,
nil,
),
command_callpq: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "command_callpq"),
"Number of callpq command since server start.",
nil,
nil,
),
agent_connect: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "agent_connect"),
"Number of agent connect since server start.",
nil,
nil,
),
agent_retry: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "agent_retry"),
"Number of agent retry since server start.",
nil,
nil,
),
queries: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "queries"),
"Number of queries since server start.",
nil,
nil,
),
dist_queries: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "dist_queries"),
"Number of distributed queries since server start.",
nil,
nil,
),
workers_total: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "workers_total"),
"Number of workers total since server start.",
nil,
nil,
),
workers_active: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "workers_active"),
"Number of workers active since server start.",
nil,
nil,
),
work_queue_length: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "work_queue_length"),
"Number of work queue length since server start.",
nil,
nil,
),
query_wall: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "query_wall"),
"Number of query_wall since server start.",
nil,
nil,
),
query_cpu: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "query_cpu"),
"Number of query_cpu since server start.",
nil,
nil,
),
dist_wall: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "dist_wall"),
"Number of dist_wall since server start.",
nil,
nil,
),
dist_local: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "dist_local"),
"Number of dist_local since server start.",
nil,
nil,
),
dist_wait: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "dist_wait"),
"Number of dist_wait since server start.",
nil,
nil,
),
query_reads: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "query_reads"),
"Total number of read operations since server start.",
nil,
nil,
),
query_readkb: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "query_readkb"),
"Total number of KB read since server start.",
nil,
nil,
),
query_readtime: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "query_readtime"),
"Total time spent doing read operations (in seconds) since server start.",
nil,
nil,
),
avg_query_wall: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "avg_query_wall"),
"Number of avg_query_wall since server start.",
nil,
nil,
),
avg_query_cpu: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "avg_query_cpu"),
"Number of avg_query_cpu since server start.",
nil,
nil,
),
avg_dist_wall: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "avg_dist_wall"),
"Number of avg_dist_wall since server start.",
nil,
nil,
),
avg_dist_local: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "avg_dist_local"),
"Number of avg_dist_local since server start.",
nil,
nil,
),
avg_dist_wait: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "avg_dist_wait"),
"Number of avg_dist_wait since server start.",
nil,
nil,
),
avg_query_reads: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "avg_query_reads"),
"Number of avg_query_reads since server start.",
nil,
nil,
),
avg_query_readkb: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "avg_query_readkb"),
"Number of avg_query_readkb since server start.",
nil,
nil,
),
avg_query_readtime: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "avg_query_readtime"),
"Number of avg_query_readtime since server start.",
nil,
nil,
),
qcache_max_bytes: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "qcache_max_bytes"),
"Number of qcache_max_bytes since server start.",
nil,
nil,
),
qcache_thresh_msec: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "qcache_thresh_msec"),
"Number of qcache_thresh_msec since server start.",
nil,
nil,
),
qcache_ttl_sec: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "qcache_ttl_sec"),
"Number of qcache_ttl_sec since server start.",
nil,
nil,
),
qcache_cached_queries: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "qcache_cached_queries"),
"Number of qcache_cached_queries since server start.",
nil,
nil,
),
qcache_used_bytes: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "qcache_used_bytes"),
"Number of qcache_used_bytes since server start.",
nil,
nil,
),
qcache_hits: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "qcache_hits"),
"Number of qcache_hits since server start.",
nil,
nil,
),
index_count: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "index_count"),
"Number of indexes.",
nil,
nil,
),
indexed_documents: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "indexed_documents"),
"Number of documents indexed",
labels,
nil,
),
indexed_bytes: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "indexed_bytes"),
"Size (in bytes) of indexed documents",
labels,
nil,
),
field_tokens_title: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "field_tokens_title"),
"Sums of per-field length titles over the entire index",
labels,
nil,
),
field_tokens_body: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "field_tokens_body"),
"Sums of per-field length bodies over the entire index",
labels,
nil,
),
total_tokens: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "total_tokens"),
"Total tokens",
labels,
nil,
),
ram_bytes: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "ram_bytes"),
"Total size (in bytes) of the RAM-resident index portion",
labels,
nil,
),
disk_bytes: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "disk_bytes"),
"Total size (in bytes) of indexes on disk",
labels,
nil,
),
mem_limit: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "mem_limit"),
"Memory limit",
labels,
nil,
),
threads_count: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "threads_count"),
"Number of threads",
[]string{"state"},
nil,
),
cluster_status: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "cluster_status"),
"Cluster Status.",
nil,
nil,
),
cluster_size: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "cluster_size"),
"Cluster Size.",
nil,
nil,
),
cluster_node_state: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "cluster_node_state"),
"Cluster Node State.",
nil,
nil,
),
}
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- e.up
ch <- e.uptime
ch <- e.connections
ch <- e.maxed_out
ch <- e.version
ch <- e.mysql_version
ch <- e.command_search
ch <- e.command_update
ch <- e.command_delete
ch <- e.command_keywords
ch <- e.command_persist
ch <- e.command_status
ch <- e.command_flushattrs
ch <- e.command_set
ch <- e.command_insert
ch <- e.command_replace
ch <- e.command_commit
ch <- e.command_suggest
ch <- e.command_json
ch <- e.command_callpq
ch <- e.agent_connect
ch <- e.agent_retry
ch <- e.queries
ch <- e.dist_queries
ch <- e.workers_total
ch <- e.workers_active
ch <- e.work_queue_length
ch <- e.query_wall
ch <- e.query_cpu
ch <- e.dist_wall
ch <- e.dist_local
ch <- e.dist_wait
ch <- e.query_reads
ch <- e.query_readkb
ch <- e.query_readtime
ch <- e.avg_query_wall
ch <- e.avg_query_cpu
ch <- e.avg_dist_wall
ch <- e.avg_dist_local
ch <- e.avg_dist_wait
ch <- e.avg_query_reads
ch <- e.avg_query_readkb
ch <- e.avg_query_readtime
ch <- e.qcache_max_bytes
ch <- e.qcache_thresh_msec
ch <- e.qcache_ttl_sec
ch <- e.qcache_cached_queries
ch <- e.qcache_used_bytes
ch <- e.qcache_hits
ch <- e.index_count
ch <- e.indexed_documents
ch <- e.indexed_bytes
ch <- e.field_tokens_title
ch <- e.field_tokens_body
ch <- e.total_tokens
ch <- e.ram_bytes
ch <- e.disk_bytes
ch <- e.mem_limit
ch <- e.threads_count
ch <- e.cluster_size
ch <- e.cluster_status
ch <- e.cluster_node_state
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
status := 1
db, err := sql.Open("mysql", e.manticore)
if err != nil {
log.Error(err)
return
}
// will close DB connection while return
defer db.Close()
rows, err := db.Query("SHOW STATUS")
if err != nil {
log.Error(err)
return
}
variables := make(map[string]string)
for rows.Next() {
var metric string
var counter string
err = rows.Scan(&metric, &counter)
if err != nil {
log.Error(err)
return
}
variables[metric] = counter
}
// Cluster fields
var cluster = variables["cluster_name"]
var cluster_node_states = map[string]string{
"closed": "0",
"destroyed": "1",
"joining": "2",
"donor": "3",
"synced": "4",
}
var cluster_node_statuses = map[string]string{
"primary": "0",
"non-primary": "1",
}
for k, v := range variables {
var override_value string
switch {
case k == "uptime":
ch <- prometheus.MustNewConstMetric(e.uptime, prometheus.GaugeValue, parse(v))
case k == "connections":
ch <- prometheus.MustNewConstMetric(e.connections, prometheus.CounterValue, parse(v))
case k == "maxed_out":
ch <- prometheus.MustNewConstMetric(e.maxed_out, prometheus.CounterValue, parse(v))
case k == "version":
ch <- prometheus.MustNewConstMetric(e.version, prometheus.CounterValue, parse(v))
case k == "mysql_version":
ch <- prometheus.MustNewConstMetric(e.mysql_version, prometheus.CounterValue, parse(v))
case k == "command_search":
ch <- prometheus.MustNewConstMetric(e.command_search, prometheus.CounterValue, parse(v))
case k == "command_update":
ch <- prometheus.MustNewConstMetric(e.command_update, prometheus.CounterValue, parse(v))
case k == "command_delete":
ch <- prometheus.MustNewConstMetric(e.command_delete, prometheus.CounterValue, parse(v))
case k == "command_keywords":
ch <- prometheus.MustNewConstMetric(e.command_keywords, prometheus.CounterValue, parse(v))
case k == "command_persist":
ch <- prometheus.MustNewConstMetric(e.command_persist, prometheus.CounterValue, parse(v))
case k == "command_status":
ch <- prometheus.MustNewConstMetric(e.command_status, prometheus.CounterValue, parse(v))
case k == "command_flushattrs":
ch <- prometheus.MustNewConstMetric(e.command_flushattrs, prometheus.CounterValue, parse(v))
case k == "command_set":
ch <- prometheus.MustNewConstMetric(e.command_set, prometheus.CounterValue, parse(v))
case k == "command_insert":
ch <- prometheus.MustNewConstMetric(e.command_insert, prometheus.CounterValue, parse(v))
case k == "command_replace":
ch <- prometheus.MustNewConstMetric(e.command_replace, prometheus.CounterValue, parse(v))
case k == "command_commit":
ch <- prometheus.MustNewConstMetric(e.command_commit, prometheus.CounterValue, parse(v))
case k == "command_flushattrs":
ch <- prometheus.MustNewConstMetric(e.command_json, prometheus.CounterValue, parse(v))
case k == "command_flushattrs":
ch <- prometheus.MustNewConstMetric(e.command_callpq, prometheus.CounterValue, parse(v))
case k == "agent_connect":
ch <- prometheus.MustNewConstMetric(e.agent_connect, prometheus.CounterValue, parse(v))
case k == "agent_retry":
ch <- prometheus.MustNewConstMetric(e.agent_retry, prometheus.CounterValue, parse(v))
case k == "queries":
ch <- prometheus.MustNewConstMetric(e.queries, prometheus.CounterValue, parse(v))
case k == "dist_queries":
ch <- prometheus.MustNewConstMetric(e.dist_queries, prometheus.CounterValue, parse(v))
case k == "workers_total":
ch <- prometheus.MustNewConstMetric(e.workers_total, prometheus.CounterValue, parse(v))
case k == "workers_active":
ch <- prometheus.MustNewConstMetric(e.workers_active, prometheus.CounterValue, parse(v))
case k == "work_queue_length":
ch <- prometheus.MustNewConstMetric(e.work_queue_length, prometheus.CounterValue, parse(v))
case k == "query_wall":
ch <- prometheus.MustNewConstMetric(e.query_wall, prometheus.CounterValue, parse(v))
case k == "query_cpu":
ch <- prometheus.MustNewConstMetric(e.query_cpu, prometheus.GaugeValue, parse(v))
case k == "dist_wall":
ch <- prometheus.MustNewConstMetric(e.dist_wall, prometheus.CounterValue, parse(v))
case k == "dist_local":
ch <- prometheus.MustNewConstMetric(e.dist_local, prometheus.CounterValue, parse(v))
case k == "dist_wait":
ch <- prometheus.MustNewConstMetric(e.dist_wait, prometheus.CounterValue, parse(v))
case k == "query_reads":
ch <- prometheus.MustNewConstMetric(e.query_reads, prometheus.CounterValue, parse(v))
case k == "query_readkb":
ch <- prometheus.MustNewConstMetric(e.query_readkb, prometheus.CounterValue, parse(v))
case k == "query_readtime":
ch <- prometheus.MustNewConstMetric(e.query_readtime, prometheus.CounterValue, parse(v))
case k == "avg_query_wall":
ch <- prometheus.MustNewConstMetric(e.avg_query_wall, prometheus.CounterValue, parse(v))
case k == "avg_query_cpu":
ch <- prometheus.MustNewConstMetric(e.avg_query_cpu, prometheus.GaugeValue, parse(v))
case k == "avg_dist_wall":
ch <- prometheus.MustNewConstMetric(e.avg_dist_wall, prometheus.CounterValue, parse(v))
case k == "avg_dist_local":
ch <- prometheus.MustNewConstMetric(e.avg_dist_local, prometheus.CounterValue, parse(v))
case k == "avg_dist_wait":
ch <- prometheus.MustNewConstMetric(e.avg_dist_wait, prometheus.CounterValue, parse(v))
case k == "avg_query_reads":
ch <- prometheus.MustNewConstMetric(e.avg_query_reads, prometheus.GaugeValue, parse(v))
case k == "avg_query_readkb":
ch <- prometheus.MustNewConstMetric(e.avg_query_readkb, prometheus.GaugeValue, parse(v))
case k == "avg_query_readtime":
ch <- prometheus.MustNewConstMetric(e.avg_query_readtime, prometheus.GaugeValue, parse(v))
case k == "qcache_max_bytes":
ch <- prometheus.MustNewConstMetric(e.qcache_max_bytes, prometheus.CounterValue, parse(v))
case k == "qcache_thresh_msec":
ch <- prometheus.MustNewConstMetric(e.qcache_thresh_msec, prometheus.CounterValue, parse(v))
case k == "qcache_ttl_sec":
ch <- prometheus.MustNewConstMetric(e.qcache_ttl_sec, prometheus.CounterValue, parse(v))
case k == "qcache_cached_queries":
ch <- prometheus.MustNewConstMetric(e.qcache_cached_queries, prometheus.CounterValue, parse(v))
case k == "qcache_used_bytes":
ch <- prometheus.MustNewConstMetric(e.qcache_used_bytes, prometheus.CounterValue, parse(v))
case k == "qcache_used_bytes":
ch <- prometheus.MustNewConstMetric(e.qcache_used_bytes, prometheus.CounterValue, parse(v))
case k == fmt.Sprintf("cluster_%s_size", cluster):
ch <- prometheus.MustNewConstMetric(e.cluster_size, prometheus.CounterValue, parse(v))
case k == fmt.Sprintf("cluster_%s_status", cluster):
override_value = cluster_node_statuses[v]
ch <- prometheus.MustNewConstMetric(e.cluster_status, prometheus.CounterValue, parse(override_value))
case k == fmt.Sprintf("cluster_%s_node_state", cluster):
override_value = cluster_node_states[v]
ch <- prometheus.MustNewConstMetric(e.cluster_node_state, prometheus.CounterValue, parse(override_value))
}
}
//Collect Indexes
databases := make(map[string]string)
indexes, err := db.Query("SHOW TABLES")
if err != nil {
log.Error(err)
return
}
for indexes.Next() {
var index string
var index_type string
err = indexes.Scan(&index, &index_type)
if err != nil {
log.Error(err)
return
}
databases[index] = index_type
}
ch <- prometheus.MustNewConstMetric(e.index_count, prometheus.GaugeValue, float64(len(databases)))
//Collect metrics per index
for index, _ := range databases {
// Distributed indexes has no index status
if databases[index] == "distributed" {
continue
}
metrics, err := db.Query("SHOW INDEX " + index + " STATUS")
if err != nil {
log.Error(err)
return
}
for metrics.Next() {
var metric string
var value string
err := metrics.Scan(&metric, &value)
if err != nil {
log.Error(err)
return
}
switch {
case metric == "indexed_documents":
ch <- prometheus.MustNewConstMetric(e.indexed_documents, prometheus.CounterValue, parse(value), index)
case metric == "indexed_bytes":
ch <- prometheus.MustNewConstMetric(e.indexed_bytes, prometheus.CounterValue, parse(value), index)
case metric == "field_tokens_title":
ch <- prometheus.MustNewConstMetric(e.field_tokens_title, prometheus.CounterValue, parse(value), index)
case metric == "field_tokens_body":
ch <- prometheus.MustNewConstMetric(e.field_tokens_body, prometheus.CounterValue, parse(value), index)
case metric == "total_tokens":
ch <- prometheus.MustNewConstMetric(e.total_tokens, prometheus.CounterValue, parse(value), index)
case metric == "ram_bytes":
ch <- prometheus.MustNewConstMetric(e.ram_bytes, prometheus.CounterValue, parse(value), index)
case metric == "disk_bytes":
ch <- prometheus.MustNewConstMetric(e.disk_bytes, prometheus.CounterValue, parse(value), index)
case metric == "mem_limit":
ch <- prometheus.MustNewConstMetric(e.mem_limit, prometheus.CounterValue, parse(value), index)
}
}
}
threads_rows, err := db.Query("SHOW THREADS")
if err != nil {
log.Error(err)
return
}
threads := make(map[string][]string)
for threads_rows.Next() {
var tid string
var name string
var proto string
var state string
var host string
var connid string
var time string
var work_time string
var work_time_cpu string
var thd_eff string
var jobs_done string
var last_job_took string
var in_idle string
var info string
err := threads_rows.Scan(&tid, &name, &proto, &state, &host, &connid, &time, &work_time, &work_time_cpu, &thd_eff, &jobs_done, &last_job_took, &in_idle, &info)
if err != nil {
log.Error(err)
return
}
threads[state] = append(threads[state], tid)
}
for threads_state, threads_times := range threads {
ch <- prometheus.MustNewConstMetric(e.threads_count, prometheus.CounterValue, float64(len(threads_times)), threads_state)
}
ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, float64(status))
}
func parse(stat string) float64 {
switch {
case stat == "ON":
return 1.0
case stat == "OFF":
return 0
}
v, err := strconv.ParseFloat(stat, 64)
if v == 0 {
return 0.0
}
if err != nil {
log.Errorf("Failed to parse %s: %s", stat, err)
v = math.NaN()
}
return v
}
func main() {
var (
address = kingpin.Flag("manticore.address", "manticore server address.").Default("127.0.0.1").String()
port = kingpin.Flag("manticore.port", "manticore server port.").Default("9306").String()
timeout = kingpin.Flag("manticore.timeout", "manticore connect timeout.").Default("1s").Duration()
listenAddress = kingpin.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9247").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
)
log.AddFlags(kingpin.CommandLine)
kingpin.Version(version.Print("manticore_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
log.Infoln("Starting manticore_exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
prometheus.MustRegister(NewExporter(*address, *port, *timeout))
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>ManticoreSearch Exporter</title></head>
<body>
<h1>ManticoreSearch Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Infoln("Starting HTTP server on", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}