forked from prometheus/mysqld_exporter
/
global_status.go
189 lines (171 loc) · 5.82 KB
/
global_status.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
// Scrape `SHOW GLOBAL STATUS`.
package collector
import (
"context"
"database/sql"
"regexp"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
)
const (
// Scrape query.
globalStatusQuery = `SHOW GLOBAL STATUS`
// Subsystem.
globalStatus = "global_status"
)
var replLatencyMap = []string{
"Minimum",
"Average",
"Maximum",
"Standard Deviation",
"Sample Size",
}
// Regexp to match various groups of status vars.
var globalStatusRE = regexp.MustCompile(`^(com|handler|connection_errors|innodb_buffer_pool_pages|innodb_rows|performance_schema)_(.*)$`)
var (
globalCommandsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, globalStatus, "commands_total"),
"Total number of executed MySQL commands.",
[]string{"command"}, nil,
)
globalHandlerDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, globalStatus, "handlers_total"),
"Total number of executed MySQL handlers.",
[]string{"handler"}, nil,
)
globalConnectionErrorsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, globalStatus, "connection_errors_total"),
"Total number of MySQL connection errors.",
[]string{"error"}, nil,
)
globalBufferPoolPagesDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, globalStatus, "buffer_pool_pages"),
"Innodb buffer pool pages by state.",
[]string{"state"}, nil,
)
globalBufferPoolPageChangesDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, globalStatus, "buffer_pool_page_changes_total"),
"Innodb buffer pool page state changes.",
[]string{"operation"}, nil,
)
globalInnoDBRowOpsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, globalStatus, "innodb_row_ops_total"),
"Total number of MySQL InnoDB row operations.",
[]string{"operation"}, 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,
)
)
// ScrapeGlobalStatus collects from `SHOW GLOBAL STATUS`.
type ScrapeGlobalStatus struct{}
// Name of the Scraper.
func (ScrapeGlobalStatus) Name() string {
return globalStatus
}
// Help returns additional information about Scraper.
func (ScrapeGlobalStatus) Help() string {
return "Collect from SHOW GLOBAL STATUS"
}
// Version of MySQL from which scraper is available.
func (ScrapeGlobalStatus) Version() float64 {
return 5.1
}
// Scrape collects data.
func (ScrapeGlobalStatus) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error {
globalStatusRows, err := db.QueryContext(ctx, globalStatusQuery)
if err != nil {
return err
}
defer globalStatusRows.Close()
var key string
var val sql.RawBytes
textItems := map[string]string{
"wsrep_local_state_uuid": "",
"wsrep_cluster_state_uuid": "",
"wsrep_provider_version": "",
"wsrep_evs_repl_latency": "",
}
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 = validPrometheusName(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 "handler":
ch <- prometheus.MustNewConstMetric(
globalHandlerDesc, prometheus.CounterValue, floatVal, match[2],
)
case "connection_errors":
ch <- prometheus.MustNewConstMetric(
globalConnectionErrorsDesc, prometheus.CounterValue, floatVal, match[2],
)
case "innodb_buffer_pool_pages":
switch match[2] {
case "data", "dirty", "free", "misc", "old", "total":
ch <- prometheus.MustNewConstMetric(
globalBufferPoolPagesDesc, prometheus.GaugeValue, floatVal, match[2],
)
default:
ch <- prometheus.MustNewConstMetric(
globalBufferPoolPageChangesDesc, 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],
)
}
} else if _, ok := textItems[key]; ok {
textItems[key] = string(val)
}
}
// mysql_galera_variables_info metric.
if textItems["wsrep_local_state_uuid"] != "" {
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(prometheus.BuildFQName(namespace, "galera", "status_info"), "PXC/Galera status information.",
[]string{"wsrep_local_state_uuid", "wsrep_cluster_state_uuid", "wsrep_provider_version"}, nil),
prometheus.GaugeValue, 1, textItems["wsrep_local_state_uuid"], textItems["wsrep_cluster_state_uuid"], textItems["wsrep_provider_version"],
)
}
if textItems["wsrep_evs_repl_latency"] != "" {
galeraReplLatencyArray := strings.Split(textItems["wsrep_evs_repl_latency"], "/")
// check if galeraReplLatencyArray contains all needed values
if len(galeraReplLatencyArray) == len(replLatencyMap) {
galeraReplLatencyDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, globalStatus, "wsrep_evs_repl_latency"),
"PXC/Galera replication latency on group communication.",
[]string{"aggregator"}, nil,
)
for index, label := range replLatencyMap {
if floatVal, err := strconv.ParseFloat(galeraReplLatencyArray[index], 64); err == nil {
ch <- prometheus.MustNewConstMetric(
galeraReplLatencyDesc, prometheus.GaugeValue, floatVal, label,
)
}
}
}
}
return nil
}