forked from prometheus/mysqld_exporter
/
mysqld_exporter.go
1250 lines (1179 loc) · 45.4 KB
/
mysqld_exporter.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
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"bytes"
"database/sql"
"flag"
"fmt"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/log"
)
var (
listenAddress = flag.String(
"web.listen-address", ":9104",
"Address to listen on for web interface and telemetry.",
)
metricPath = flag.String(
"web.telemetry-path", "/metrics",
"Path under which to expose metrics.",
)
autoIncrementColumns = flag.Bool(
"collect.auto_increment.columns", false,
"Collect auto_increment columns and max values from information_schema",
)
binlogSize = flag.Bool(
"collect.binlog_size", false,
"Collect the current size of all registered binlog files",
)
perfTableIOWaits = flag.Bool(
"collect.perf_schema.tableiowaits", false,
"Collect metrics from performance_schema.table_io_waits_summary_by_table",
)
perfTableIOWaitsTime = flag.Bool(
"collect.perf_schema.tableiowaitstime", false,
"Collect time metrics from performance_schema.table_io_waits_summary_by_table",
)
perfIndexIOWaits = flag.Bool(
"collect.perf_schema.indexiowaits", false,
"Collect metrics from performance_schema.table_io_waits_summary_by_index_usage",
)
perfIndexIOWaitsTime = flag.Bool(
"collect.perf_schema.indexiowaitstime", false,
"Collect time metrics from performance_schema.table_io_waits_summary_by_index_usage",
)
perfTableLockWaits = flag.Bool(
"collect.perf_schema.tablelocks", false,
"Collect metrics from performance_schema.table_lock_waits_summary_by_table",
)
perfTableLockWaitsTime = flag.Bool(
"collect.perf_schema.tablelockstime", false,
"Collect time metrics from performance_schema.table_lock_waits_summary_by_table",
)
perfEventsStatements = flag.Bool(
"collect.perf_schema.eventsstatements", false,
"Collect time metrics from performance_schema.events_statements_summary_by_digest",
)
perfEventsStatementsLimit = flag.Int(
"collect.perf_schema.eventsstatements.limit", 250,
"Limit the number of events statements digests by response time",
)
perfEventsStatementsTimeLimit = flag.Int(
"collect.perf_schema.eventsstatements.timelimit", 86400,
"Limit how old the 'last_seen' events statements can be, in seconds",
)
perfEventsStatementsDigestTextLimit = flag.Int(
"collect.perf_schema.eventsstatements.digest_text_limit", 120,
"Maximum length of the normalized statement text",
)
userStat = flag.Bool("collect.info_schema.userstats", false,
"If running with userstat=1, set to true to collect user statistics",
)
)
// Metric name parts.
const (
// Namespace for all metrics.
namespace = "mysql"
// Subsystems.
exporter = "exporter"
globalStatus = "global_status"
globalVariables = "global_variables"
informationSchema = "info_schema"
performanceSchema = "perf_schema"
slaveStatus = "slave_status"
binlog = "binlog"
)
// Metric SQL Queries.
const (
globalStatusQuery = `SHOW GLOBAL STATUS`
globalVariablesQuery = `SHOW GLOBAL VARIABLES`
slaveStatusQuery = `SHOW SLAVE STATUS`
binlogQuery = `SHOW BINARY LOGS`
infoSchemaAutoIncrementQuery = `
SELECT table_schema, table_name, column_name, auto_increment,
pow(2, case data_type
when 'tinyint' then 7
when 'smallint' then 15
when 'mediumint' then 23
when 'int' then 31
when 'bigint' then 63
end+(column_type like '% unsigned'))-1 as max_int
FROM information_schema.tables t
JOIN information_schema.columns c USING (table_schema,table_name)
WHERE c.extra = 'auto_increment' AND t.auto_increment IS NOT NULL
`
perfTableIOWaitsQuery = `
SELECT OBJECT_SCHEMA, OBJECT_NAME, COUNT_FETCH, COUNT_INSERT, COUNT_UPDATE, COUNT_DELETE
FROM performance_schema.table_io_waits_summary_by_table
WHERE OBJECT_SCHEMA NOT IN ('mysql', 'performance_schema')
`
perfTableIOWaitsTimeQuery = `
SELECT OBJECT_SCHEMA, OBJECT_NAME, SUM_TIMER_FETCH, SUM_TIMER_INSERT, SUM_TIMER_UPDATE, SUM_TIMER_DELETE
FROM performance_schema.table_io_waits_summary_by_table
WHERE OBJECT_SCHEMA NOT IN ('mysql', 'performance_schema')
`
perfIndexIOWaitsQuery = `
SELECT OBJECT_SCHEMA, OBJECT_NAME, ifnull(INDEX_NAME, 'NONE') as INDEX_NAME, COUNT_FETCH, COUNT_INSERT, COUNT_UPDATE, COUNT_DELETE
FROM performance_schema.table_io_waits_summary_by_index_usage
WHERE OBJECT_SCHEMA NOT IN ('mysql', 'performance_schema')
`
perfIndexIOWaitsTimeQuery = `
SELECT OBJECT_SCHEMA, OBJECT_NAME, ifnull(INDEX_NAME, 'NONE') as INDEX_NAME, SUM_TIMER_FETCH, SUM_TIMER_INSERT, SUM_TIMER_UPDATE, SUM_TIMER_DELETE
FROM performance_schema.table_io_waits_summary_by_index_usage
WHERE OBJECT_SCHEMA NOT IN ('mysql', 'performance_schema')
`
perfTableLockWaitsQuery = `
SELECT
OBJECT_SCHEMA,
OBJECT_NAME,
COUNT_READ_NORMAL,
COUNT_READ_WITH_SHARED_LOCKS,
COUNT_READ_HIGH_PRIORITY,
COUNT_READ_NO_INSERT,
COUNT_WRITE_NORMAL,
COUNT_WRITE_ALLOW_WRITE,
COUNT_WRITE_CONCURRENT_INSERT,
COUNT_WRITE_DELAYED,
COUNT_WRITE_LOW_PRIORITY,
COUNT_READ_EXTERNAL,
COUNT_WRITE_EXTERNAL
FROM performance_schema.table_lock_waits_summary_by_table
WHERE OBJECT_SCHEMA NOT IN ('mysql', 'performance_schema', 'information_schema')
`
perfTableLockWaitsTimeQuery = `
SELECT
OBJECT_SCHEMA,
OBJECT_NAME,
SUM_TIMER_READ_NORMAL,
SUM_TIMER_READ_WITH_SHARED_LOCKS,
SUM_TIMER_READ_HIGH_PRIORITY,
SUM_TIMER_READ_NO_INSERT,
SUM_TIMER_WRITE_NORMAL,
SUM_TIMER_WRITE_ALLOW_WRITE,
SUM_TIMER_WRITE_CONCURRENT_INSERT,
SUM_TIMER_WRITE_DELAYED,
SUM_TIMER_WRITE_LOW_PRIORITY,
SUM_TIMER_READ_EXTERNAL,
SUM_TIMER_WRITE_EXTERNAL
FROM performance_schema.table_lock_waits_summary_by_table
WHERE OBJECT_SCHEMA NOT IN ('mysql', 'performance_schema', 'information_schema')
`
perfEventsStatementsQuery = `
SELECT
ifnull(SCHEMA_NAME, 'NONE') as SCHEMA_NAME,
DIGEST,
LEFT(DIGEST_TEXT, %d) as DIGEST_TEXT,
COUNT_STAR,
SUM_TIMER_WAIT,
SUM_ERRORS,
SUM_WARNINGS,
SUM_ROWS_AFFECTED,
SUM_ROWS_SENT,
SUM_ROWS_EXAMINED,
SUM_CREATED_TMP_DISK_TABLES,
SUM_CREATED_TMP_TABLES
FROM performance_schema.events_statements_summary_by_digest
WHERE SCHEMA_NAME NOT IN ('mysql', 'performance_schema', 'information_schema')
AND last_seen > DATE_SUB(NOW(), INTERVAL %d SECOND)
ORDER BY SUM_TIMER_WAIT DESC
LIMIT %d
`
userStatQuery = `SELECT * FROM information_schema.USER_STATISTICS`
)
// landingPage contains the HTML served at '/'.
// TODO: Make this nicer and more informative.
var landingPage = []byte(`<html>
<head><title>MySQLd exporter</title></head>
<body>
<h1>MySQLd exporter</h1>
<p><a href='` + *metricPath + `'>Metrics</a></p>
</body>
</html>
`)
// Metric descriptors for dynamically created metrics.
var (
binlogSizeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, binlog, "size_bytes"),
"Combined size of all registered binlog files.",
[]string{}, nil,
)
binlogFilesDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, binlog, "files"),
"Number of registered binlog files.",
[]string{}, nil,
)
globalCommandsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, globalStatus, "commands_total"),
"Total number of executed MySQL commands.",
[]string{"command"}, nil,
)
globalConnectionErrorsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, globalStatus, "connection_errors_total"),
"Total number of MySQL connection errors.",
[]string{"error"}, nil,
)
globalInnoDBRowOpsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, globalStatus, "innodb_row_ops_total"),
"Total number of MySQL InnoDB row operations.",
[]string{"operation"}, nil,
)
globalInfoSchemaAutoIncrementDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, informationSchema, "auto_increment_column"),
"The current value of an auto_increment column from information_schema.",
[]string{"schema", "table", "column"}, nil,
)
globalInfoSchemaAutoIncrementMaxDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, informationSchema, "auto_increment_column_max"),
"The max value of an auto_increment column from information_schema.",
[]string{"schema", "table", "column"}, nil,
)
globalPerformanceSchemaLostDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, globalStatus, "performance_schema_lost_total"),
"Total number of MySQL instrumentations that could not be loaded or created due to memory constraints.",
[]string{"instrumentation"}, nil,
)
performanceSchemaTableWaitsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "table_io_waits_total"),
"The total number of table I/O wait events for each table and operation.",
[]string{"schema", "name", "operation"}, nil,
)
performanceSchemaTableWaitsTimeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "table_io_waits_seconds_total"),
"The total time of table I/O wait events for each table and operation.",
[]string{"schema", "name", "operation"}, nil,
)
performanceSchemaIndexWaitsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "index_io_waits_total"),
"The total number of index I/O wait events for each index and operation.",
[]string{"schema", "name", "index", "operation"}, nil,
)
performanceSchemaIndexWaitsTimeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "index_io_waits_seconds_total"),
"The total time of index I/O wait events for each index and operation.",
[]string{"schema", "name", "index", "operation"}, nil,
)
performanceSchemaSQLTableLockWaitsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "sql_lock_waits_total"),
"The total number of SQL lock wait events for each table and operation.",
[]string{"schema", "name", "operation"}, nil,
)
performanceSchemaExternalTableLockWaitsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "external_lock_waits_total"),
"The total number of external lock wait events for each table and operation.",
[]string{"schema", "name", "operation"}, nil,
)
performanceSchemaSQLTableLockWaitsTimeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "sql_lock_waits_seconds_total"),
"The total time of SQL lock wait events for each table and operation.",
[]string{"schema", "name", "operation"}, nil,
)
performanceSchemaExternalTableLockWaitsTimeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "external_lock_waits_seconds_total"),
"The total time of external lock wait events for each table and operation.",
[]string{"schema", "name", "operation"}, nil,
)
performanceSchemaEventsStatementsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "events_statements_total"),
"The total count of events statements by digest.",
[]string{"schema", "digest"}, nil,
)
performanceSchemaEventsStatementsDigestTextDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "events_statements_digest_text"),
"The mapping of query schema text to their digest.",
[]string{"schema", "digest", "digest_text"}, nil,
)
performanceSchemaEventsStatementsTimeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "events_statements_seconds_total"),
"The total time of events statements by digest.",
[]string{"schema", "digest"}, nil,
)
performanceSchemaEventsStatementsErrorsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "events_statements_errors_total"),
"The errors of events statements by digest.",
[]string{"schema", "digest"}, nil,
)
performanceSchemaEventsStatementsWarningsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "events_statements_warnings_total"),
"The warnings of events statements by digest.",
[]string{"schema", "digest"}, nil,
)
performanceSchemaEventsStatementsRowsAffectedDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "events_statements_rows_affected_total"),
"The total rows affected of events statements by digest.",
[]string{"schema", "digest"}, nil,
)
performanceSchemaEventsStatementsRowsSentDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "events_statements_rows_sent_total"),
"The total rows sent of events statements by digest.",
[]string{"schema", "digest"}, nil,
)
performanceSchemaEventsStatementsRowsExaminedDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "events_statements_rows_examined_total"),
"The total rows examined of events statements by digest.",
[]string{"schema", "digest"}, nil,
)
performanceSchemaEventsStatementsTmpTablesDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "events_statements_tmp_tables_total"),
"The total tmp tables of events statements by digest.",
[]string{"schema", "digest"}, nil,
)
performanceSchemaEventsStatementsTmpDiskTablesDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, performanceSchema, "events_statements_tmp_disk_tables_total"),
"The total tmp disk tables of events statements by digest.",
[]string{"schema", "digest"}, nil,
)
// Map known user-statistics values to types. Unknown types will be mapped as
// untyped.
informationSchemaUserStatisticsTypes = map[string]struct {
vtype prometheus.ValueType
desc *prometheus.Desc
}{
"TOTAL_CONNECTIONS": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_total_connections"),
"The number of connections created for this user.",
[]string{"user"}, nil)},
"CONCURRENT_CONNECTIONS": {prometheus.GaugeValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_concurrent_connections"),
"The number of concurrent connections for this user.",
[]string{"user"}, nil)},
"CONNECTED_TIME": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_connected_time_seconds_total"),
"The cumulative number of seconds elapsed while there were connections from this user.",
[]string{"user"}, nil)},
"BUSY_TIME": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_busy_seconds_total"),
"The cumulative number of seconds there was activity on connections from this user.",
[]string{"user"}, nil)},
"CPU_TIME": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_cpu_time_seconds_total"),
"The cumulative CPU time elapsed, in seconds, while servicing this user's connections.",
[]string{"user"}, nil)},
"BYTES_RECEIVED": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_bytes_received_total"),
"The number of bytes received from this user’s connections.",
[]string{"user"}, nil)},
"BYTES_SENT": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_bytes_sent_total"),
"The number of bytes sent to this user’s connections.",
[]string{"user"}, nil)},
"BINLOG_BYTES_WRITTEN": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_binlog_bytes_written_total"),
"The number of bytes written to the binary log from this user’s connections.",
[]string{"user"}, nil)},
"ROWS_FETCHED": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_rows_fetched_total"),
"The number of rows fetched by this user’s connections.",
[]string{"user"}, nil)},
"ROWS_UPDATED": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_rows_updated_total"),
"The number of rows updated by this user’s connections.",
[]string{"user"}, nil)},
"TABLE_ROWS_READ": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_table_rows_read_total"),
"The number of rows read from tables by this user’s connections. (It may be different from ROWS_FETCHED.)",
[]string{"user"}, nil)},
"SELECT_COMMANDS": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_select_commands_total"),
"The number of SELECT commands executed from this user’s connections.",
[]string{"user"}, nil)},
"UPDATE_COMMANDS": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_update_commands_total"),
"The number of UPDATE commands executed from this user’s connections.",
[]string{"user"}, nil)},
"OTHER_COMMANDS": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_other_commands_total"),
"The number of other commands executed from this user’s connections.",
[]string{"user"}, nil)},
"COMMIT_TRANSACTIONS": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_commit_transactions_total"),
"The number of COMMIT commands issued by this user’s connections.",
[]string{"user"}, nil)},
"ROLLBACK_TRANSACTIONS": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_rollback_transactions_total"),
"The number of ROLLBACK commands issued by this user’s connections.",
[]string{"user"}, nil)},
"DENIED_CONNECTIONS": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_denied_connections_total"),
"The number of connections denied to this user.",
[]string{"user"}, nil)},
"LOST_CONNECTIONS": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_lost_connections_total"),
"The number of this user’s connections that were terminated uncleanly.",
[]string{"user"}, nil)},
"ACCESS_DENIED": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_access_denied_total"),
"The number of times this user’s connections issued commands that were denied.",
[]string{"user"}, nil)},
"EMPTY_QUERIES": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_empty_queries_total"),
"The number of times this user’s connections sent empty queries to the server.",
[]string{"user"}, nil)},
"TOTAL_SSL_CONNECTIONS": {prometheus.CounterValue,
prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "user_statistics_total_ssl_connections_total"),
"The number of times this user’s connections connected using SSL to the server.",
[]string{"user"}, nil)},
}
)
// Math constants
const (
picoSeconds = 1e12
)
// Various regexps.
var (
globalStatusRE = regexp.MustCompile(`^(com|connection_errors|innodb_rows|performance_schema)_(.*)$`)
logRE = regexp.MustCompile(`.+\.(\d+)$`)
)
// Exporter collects MySQL metrics. It implements prometheus.Collector.
type Exporter struct {
dsn string
duration, error prometheus.Gauge
totalScrapes prometheus.Counter
}
// NewExporter returns a new MySQL exporter for the provided DSN.
func NewExporter(dsn string) *Exporter {
return &Exporter{
dsn: dsn,
duration: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "last_scrape_duration_seconds",
Help: "Duration of the last scrape of metrics from MySQL.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "scrapes_total",
Help: "Total number of times MySQL was scraped for metrics.",
}),
error: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "last_scrape_error",
Help: "Whether the last scrape of metrics from MySQL resulted in an error (1 for error, 0 for success).",
}),
}
}
// Describe implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
// We cannot know in advance what metrics the exporter will generate
// from MySQL. So we use the poor man's describe method: Run a collect
// and send the descriptors of all the collected metrics. The problem
// here is that we need to connect to the MySQL DB. If it is currently
// unavailable, the descriptors will be incomplete. Since this is a
// stand-alone exporter and not used as a library within other code
// implementing additional metrics, the worst that can happen is that we
// don't detect inconsistent metrics created by this exporter
// itself. Also, a change in the monitored MySQL instance may change the
// exported metrics during the runtime of the exporter.
metricCh := make(chan prometheus.Metric)
doneCh := make(chan struct{})
go func() {
for m := range metricCh {
ch <- m.Desc()
}
close(doneCh)
}()
e.Collect(metricCh)
close(metricCh)
<-doneCh
}
// Collect implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.scrape(ch)
ch <- e.duration
ch <- e.totalScrapes
ch <- e.error
}
func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
e.totalScrapes.Inc()
var err error
defer func(begun time.Time) {
e.duration.Set(time.Since(begun).Seconds())
if err == nil {
e.error.Set(0)
} else {
e.error.Set(1)
}
}(time.Now())
db, err := sql.Open("mysql", e.dsn)
if err != nil {
log.Println("Error opening connection to database:", err)
return
}
defer db.Close()
if err = scrapeGlobalStatus(db, ch); err != nil {
log.Println("Error scraping global state:", err)
return
}
if err = scrapeGlobalVariables(db, ch); err != nil {
log.Println("Error scraping global variables:", err)
return
}
if err = scrapeSlaveStatus(db, ch); err != nil {
log.Println("Error scraping slave state:", err)
return
}
if *autoIncrementColumns {
if err = scrapeInformationSchema(db, ch); err != nil {
log.Println("Error scraping information schema:", err)
return
}
}
if *binlogSize {
if err = scrapeBinlogSize(db, ch); err != nil {
log.Println("Error scraping binlog size:", err)
return
}
}
if *perfTableIOWaits {
if err = scrapePerfTableIOWaits(db, ch); err != nil {
log.Println("Error scraping performance schema:", err)
return
}
}
if *perfTableIOWaitsTime {
if err = scrapePerfTableIOWaitsTime(db, ch); err != nil {
log.Println("Error scraping performance schema:", err)
return
}
}
if *perfIndexIOWaits {
if err = scrapePerfIndexIOWaits(db, ch); err != nil {
log.Println("Error scraping performance schema:", err)
return
}
}
if *perfIndexIOWaitsTime {
if err = scrapePerfIndexIOWaitsTime(db, ch); err != nil {
log.Println("Error scraping performance schema:", err)
return
}
}
if *perfTableLockWaits {
if err = scrapePerfTableLockWaits(db, ch); err != nil {
log.Println("Error scraping performance schema:", err)
return
}
}
if *perfTableLockWaitsTime {
if err = scrapePerfTableLockWaitsTime(db, ch); err != nil {
log.Println("Error scraping performance schema:", err)
return
}
}
if *perfEventsStatements {
if err = scrapePerfEventsStatements(db, ch); err != nil {
log.Println("Error scraping performance schema:", err)
return
}
}
if *userStat {
if err = scrapeUserStat(db, ch); err != nil {
log.Println("Error scraping user stat:", err)
return
}
}
}
func scrapeGlobalStatus(db *sql.DB, ch chan<- prometheus.Metric) error {
globalStatusRows, err := db.Query(globalStatusQuery)
if err != nil {
return err
}
defer globalStatusRows.Close()
var key string
var val sql.RawBytes
for globalStatusRows.Next() {
if err := globalStatusRows.Scan(&key, &val); err != nil {
return err
}
if floatVal, ok := parseStatus(val); ok { // Unparsable values are silently skipped.
key = strings.ToLower(key)
match := globalStatusRE.FindStringSubmatch(key)
if match == nil {
ch <- prometheus.MustNewConstMetric(
newDesc(globalStatus, key, "Generic metric from SHOW GLOBAL STATUS."),
prometheus.UntypedValue,
floatVal,
)
continue
}
switch match[1] {
case "com":
ch <- prometheus.MustNewConstMetric(
globalCommandsDesc, prometheus.CounterValue, floatVal, match[2],
)
case "connection_errors":
ch <- prometheus.MustNewConstMetric(
globalConnectionErrorsDesc, prometheus.CounterValue, floatVal, match[2],
)
case "innodb_rows":
ch <- prometheus.MustNewConstMetric(
globalInnoDBRowOpsDesc, prometheus.CounterValue, floatVal, match[2],
)
case "performance_schema":
ch <- prometheus.MustNewConstMetric(
globalPerformanceSchemaLostDesc, prometheus.CounterValue, floatVal, match[2],
)
}
}
}
return nil
}
func scrapeGlobalVariables(db *sql.DB, ch chan<- prometheus.Metric) error {
globalVariablesRows, err := db.Query(globalVariablesQuery)
if err != nil {
return err
}
defer globalVariablesRows.Close()
var key string
var val sql.RawBytes
for globalVariablesRows.Next() {
if err := globalVariablesRows.Scan(&key, &val); err != nil {
return err
}
key = strings.ToLower(key)
if floatVal, ok := parseStatus(val); ok {
ch <- prometheus.MustNewConstMetric(
newDesc(globalVariables, key, "Generic gauge metric from SHOW GLOBAL VARIABLES."),
prometheus.GaugeValue,
floatVal,
)
continue
}
}
return nil
}
func scrapeSlaveStatus(db *sql.DB, ch chan<- prometheus.Metric) error {
slaveStatusRows, err := db.Query(slaveStatusQuery)
if err != nil {
return err
}
defer slaveStatusRows.Close()
if slaveStatusRows.Next() {
// There is either no row in SHOW SLAVE STATUS (if this is not a
// slave server), or exactly one. In case of multi-source
// replication, things work very much differently. This code
// cannot deal with that case.
slaveCols, err := slaveStatusRows.Columns()
if err != nil {
return err
}
// As the number of columns varies with mysqld versions,
// and sql.Scan requires []interface{}, we need to create a
// slice of pointers to the elements of slaveData.
scanArgs := make([]interface{}, len(slaveCols))
for i := range scanArgs {
scanArgs[i] = &sql.RawBytes{}
}
if err := slaveStatusRows.Scan(scanArgs...); err != nil {
return err
}
for i, col := range slaveCols {
if value, ok := parseStatus(*scanArgs[i].(*sql.RawBytes)); ok { // Silently skip unparsable values.
ch <- prometheus.MustNewConstMetric(
newDesc(slaveStatus, strings.ToLower(col), "Generic metric from SHOW SLAVE STATUS."),
prometheus.UntypedValue,
value,
)
}
}
}
return nil
}
func scrapeInformationSchema(db *sql.DB, ch chan<- prometheus.Metric) error {
autoIncrementRows, err := db.Query(infoSchemaAutoIncrementQuery)
if err != nil {
return err
}
defer autoIncrementRows.Close()
var (
schema, table, column string
value, max uint64
)
for autoIncrementRows.Next() {
if err := autoIncrementRows.Scan(
&schema, &table, &column, &value, &max,
); err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(
globalInfoSchemaAutoIncrementDesc, prometheus.GaugeValue, float64(value),
schema, table, column,
)
ch <- prometheus.MustNewConstMetric(
globalInfoSchemaAutoIncrementMaxDesc, prometheus.GaugeValue, float64(max),
schema, table, column,
)
}
return nil
}
func scrapeBinlogSize(db *sql.DB, ch chan<- prometheus.Metric) error {
masterLogRows, err := db.Query(binlogQuery)
if err != nil {
return err
}
defer masterLogRows.Close()
var (
size uint64
count uint64
filename string
filesize uint64
)
size = 0
count = 0
for masterLogRows.Next() {
if err := masterLogRows.Scan(&filename, &filesize); err != nil {
return nil
}
size += filesize
count++
}
ch <- prometheus.MustNewConstMetric(
binlogSizeDesc, prometheus.GaugeValue, float64(size),
)
ch <- prometheus.MustNewConstMetric(
binlogFilesDesc, prometheus.GaugeValue, float64(count),
)
return nil
}
func scrapePerfTableIOWaits(db *sql.DB, ch chan<- prometheus.Metric) error {
perfSchemaTableWaitsRows, err := db.Query(perfTableIOWaitsQuery)
if err != nil {
return err
}
defer perfSchemaTableWaitsRows.Close()
var (
objectSchema, objectName string
countFetch, countInsert, countUpdate, countDelete uint64
)
for perfSchemaTableWaitsRows.Next() {
if err := perfSchemaTableWaitsRows.Scan(
&objectSchema, &objectName, &countFetch, &countInsert, &countUpdate, &countDelete,
); err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(
performanceSchemaTableWaitsDesc, prometheus.CounterValue, float64(countFetch),
objectSchema, objectName, "fetch",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaTableWaitsDesc, prometheus.CounterValue, float64(countInsert),
objectSchema, objectName, "insert",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaTableWaitsDesc, prometheus.CounterValue, float64(countUpdate),
objectSchema, objectName, "update",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaTableWaitsDesc, prometheus.CounterValue, float64(countDelete),
objectSchema, objectName, "delete",
)
}
return nil
}
func scrapePerfTableIOWaitsTime(db *sql.DB, ch chan<- prometheus.Metric) error {
// Timers here are returned in picoseconds.
perfSchemaTableWaitsTimeRows, err := db.Query(perfTableIOWaitsTimeQuery)
if err != nil {
return err
}
defer perfSchemaTableWaitsTimeRows.Close()
var (
objectSchema, objectName string
timeFetch, timeInsert, timeUpdate, timeDelete uint64
)
for perfSchemaTableWaitsTimeRows.Next() {
if err := perfSchemaTableWaitsTimeRows.Scan(
&objectSchema, &objectName, &timeFetch, &timeInsert, &timeUpdate, &timeDelete,
); err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(
performanceSchemaTableWaitsTimeDesc, prometheus.CounterValue, float64(timeFetch)/picoSeconds,
objectSchema, objectName, "fetch",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaTableWaitsTimeDesc, prometheus.CounterValue, float64(timeInsert)/picoSeconds,
objectSchema, objectName, "insert",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaTableWaitsTimeDesc, prometheus.CounterValue, float64(timeUpdate)/picoSeconds,
objectSchema, objectName, "update",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaTableWaitsTimeDesc, prometheus.CounterValue, float64(timeDelete)/picoSeconds,
objectSchema, objectName, "delete",
)
}
return nil
}
func scrapePerfIndexIOWaits(db *sql.DB, ch chan<- prometheus.Metric) error {
perfSchemaIndexWaitsRows, err := db.Query(perfIndexIOWaitsQuery)
if err != nil {
return err
}
defer perfSchemaIndexWaitsRows.Close()
var (
objectSchema, objectName, indexName string
countFetch, countInsert, countUpdate, countDelete uint64
)
for perfSchemaIndexWaitsRows.Next() {
if err := perfSchemaIndexWaitsRows.Scan(
&objectSchema, &objectName, &indexName, &countFetch, &countInsert, &countUpdate, &countDelete,
); err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(
performanceSchemaIndexWaitsDesc, prometheus.CounterValue, float64(countFetch),
objectSchema, objectName, indexName, "fetch",
)
// We only update write columns when indexName is NONE.
if indexName == "NONE" {
ch <- prometheus.MustNewConstMetric(
performanceSchemaIndexWaitsDesc, prometheus.CounterValue, float64(countInsert),
objectSchema, objectName, indexName, "insert",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaIndexWaitsDesc, prometheus.CounterValue, float64(countUpdate),
objectSchema, objectName, indexName, "update",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaIndexWaitsDesc, prometheus.CounterValue, float64(countDelete),
objectSchema, objectName, indexName, "delete",
)
}
}
return nil
}
func scrapePerfIndexIOWaitsTime(db *sql.DB, ch chan<- prometheus.Metric) error {
// Timers here are returned in picoseconds.
perfSchemaIndexWaitsTimeRows, err := db.Query(perfIndexIOWaitsTimeQuery)
if err != nil {
return err
}
defer perfSchemaIndexWaitsTimeRows.Close()
var (
objectSchema, objectName, indexName string
timeFetch, timeInsert, timeUpdate, timeDelete uint64
)
for perfSchemaIndexWaitsTimeRows.Next() {
if err := perfSchemaIndexWaitsTimeRows.Scan(
&objectSchema, &objectName, &indexName, &timeFetch, &timeInsert, &timeUpdate, &timeDelete,
); err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(
performanceSchemaIndexWaitsTimeDesc, prometheus.CounterValue, float64(timeFetch)/picoSeconds,
objectSchema, objectName, indexName, "fetch",
)
// We only update write columns when indexName is NONE.
if indexName == "NONE" {
ch <- prometheus.MustNewConstMetric(
performanceSchemaIndexWaitsTimeDesc, prometheus.CounterValue, float64(timeInsert)/picoSeconds,
objectSchema, objectName, indexName, "insert",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaIndexWaitsTimeDesc, prometheus.CounterValue, float64(timeUpdate)/picoSeconds,
objectSchema, objectName, indexName, "update",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaIndexWaitsTimeDesc, prometheus.CounterValue, float64(timeDelete)/picoSeconds,
objectSchema, objectName, indexName, "delete",
)
}
}
return nil
}
func scrapePerfTableLockWaits(db *sql.DB, ch chan<- prometheus.Metric) error {
perfSchemaTableLockWaitsRows, err := db.Query(perfTableLockWaitsQuery)
if err != nil {
return err
}
defer perfSchemaTableLockWaitsRows.Close()
var (
objectSchema, objectName string
countReadNormal, countReadWithSharedLocks uint64
countReadHighPriority, countReadNoInsert uint64
countWriteNormal, countWriteAllowWrite uint64
countWriteConcurrentInsert, countWriteDelayed uint64
countWriteLowPriority uint64
countReadExternal, countWriteExternal uint64
)
for perfSchemaTableLockWaitsRows.Next() {
if err := perfSchemaTableLockWaitsRows.Scan(
&objectSchema, &objectName, &countReadNormal, &countReadWithSharedLocks,
&countReadHighPriority, &countReadNoInsert, &countWriteNormal,
&countWriteAllowWrite, &countWriteConcurrentInsert, &countWriteDelayed,
&countWriteLowPriority, &countReadExternal, &countWriteExternal,
); err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(
performanceSchemaSQLTableLockWaitsDesc, prometheus.CounterValue, float64(countReadNormal),
objectSchema, objectName, "read_normal",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaSQLTableLockWaitsDesc, prometheus.CounterValue, float64(countReadWithSharedLocks),
objectSchema, objectName, "read_with_shared_locks",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaSQLTableLockWaitsDesc, prometheus.CounterValue, float64(countReadHighPriority),
objectSchema, objectName, "read_high_priority",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaSQLTableLockWaitsDesc, prometheus.CounterValue, float64(countReadNoInsert),
objectSchema, objectName, "read_no_insert",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaSQLTableLockWaitsDesc, prometheus.CounterValue, float64(countWriteNormal),
objectSchema, objectName, "write_normal",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaSQLTableLockWaitsDesc, prometheus.CounterValue, float64(countWriteAllowWrite),
objectSchema, objectName, "write_allow_write",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaSQLTableLockWaitsDesc, prometheus.CounterValue, float64(countWriteConcurrentInsert),
objectSchema, objectName, "write_concurrent_insert",
)
ch <- prometheus.MustNewConstMetric(
performanceSchemaSQLTableLockWaitsDesc, prometheus.CounterValue, float64(countWriteDelayed),
objectSchema, objectName, "write_delayed",
)