forked from prometheus/mysqld_exporter
/
mysqld_exporter.go
333 lines (279 loc) · 7.86 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
package main
import (
"database/sql"
"flag"
_ "github.com/go-sql-driver/mysql"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/log"
)
const (
namespace = "mysql"
slaveStatus = "slave_status"
globalStatus = "global_status"
)
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.")
)
type Exporter struct {
dsn string
mutex sync.RWMutex
duration, error prometheus.Gauge
totalScrapes prometheus.Counter
metrics map[string]prometheus.Gauge
commands *prometheus.CounterVec
connectionErrors *prometheus.CounterVec
innodbRows *prometheus.CounterVec
performanceSchema *prometheus.CounterVec
}
// return new empty exporter
func NewMySQLExporter(dsn string) *Exporter {
return &Exporter{
dsn: dsn,
duration: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "exporter_last_scrape_duration_seconds",
Help: "The last scrape duration.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_scrapes_total",
Help: "Current total mysqld scrapes.",
}),
error: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "exporter_last_scrape_error",
Help: "The last scrape error status.",
}),
metrics: map[string]prometheus.Gauge{},
commands: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: globalStatus,
Name: "commands_total",
Help: "Number of executed mysql commands.",
}, []string{"command"}),
connectionErrors: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: globalStatus,
Name: "connection_errors_total",
Help: "Number of mysql connection errors.",
}, []string{"error"}),
innodbRows: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: globalStatus,
Name: "innodb_rows_total",
Help: "Mysql Innodb row operations.",
}, []string{"operation"}),
performanceSchema: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: globalStatus,
Name: "performance_schema_total",
Help: "Mysql instrumentations that could not be loaded or created due to memory constraints",
}, []string{"instrumentation"}),
}
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
for _, m := range e.metrics {
m.Describe(ch)
}
e.commands.Describe(ch)
e.connectionErrors.Describe(ch)
e.innodbRows.Describe(ch)
e.performanceSchema.Describe(ch)
ch <- e.duration.Desc()
ch <- e.totalScrapes.Desc()
ch <- e.error.Desc()
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
scrapes := make(chan metric)
go e.scrape(scrapes)
e.mutex.Lock()
defer e.mutex.Unlock()
e.setMetrics(scrapes)
ch <- e.duration
ch <- e.totalScrapes
ch <- e.error
e.collectMetrics(ch)
e.commands.Collect(ch)
e.connectionErrors.Collect(ch)
e.innodbRows.Collect(ch)
e.performanceSchema.Collect(ch)
}
type metric struct {
source string
key string
value string
}
func (e *Exporter) scrape(scrapes chan<- metric) {
defer close(scrapes)
now := time.Now().UnixNano()
e.totalScrapes.Inc()
db, err := sql.Open("mysql", e.dsn)
if err != nil {
log.Println("error opening connection to database:", err)
e.error.Set(1)
e.duration.Set(float64(time.Now().UnixNano()-now) / 1000000000)
return
}
defer db.Close()
// fetch database status
rows, err := db.Query("SHOW GLOBAL STATUS")
if err != nil {
log.Println("error running status query on database:", err)
e.error.Set(1)
e.duration.Set(float64(time.Now().UnixNano()-now) / 1000000000)
return
}
defer rows.Close()
var key, val []byte
for rows.Next() {
// get RawBytes from data
err = rows.Scan(&key, &val)
if err != nil {
log.Println("error getting result set:", err)
return
}
res := metric{
source: globalStatus,
key: string(key),
value: string(val),
}
scrapes <- res
}
// fetch slave status
rows, err = db.Query("SHOW SLAVE STATUS")
if err != nil {
log.Println("error running show slave query on database:", err)
e.error.Set(1)
e.duration.Set(float64(time.Now().UnixNano()-now) / 1000000000)
return
}
defer rows.Close()
var slaveCols []string
slaveCols, err = rows.Columns()
if err != nil {
log.Println("error retrieving column list:", err)
e.error.Set(1)
e.duration.Set(float64(time.Now().UnixNano()-now) / 1000000000)
return
}
var slaveData = make([]sql.RawBytes, len(slaveCols))
// 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 slaveData {
scanArgs[i] = &slaveData[i]
}
for rows.Next() {
err = rows.Scan(scanArgs...)
if err != nil {
log.Println("error retrieving result set:", err)
e.error.Set(1)
e.duration.Set(float64(time.Now().UnixNano()-now) / 1000000000)
return
}
}
for i, col := range slaveCols {
res := metric{
source: slaveStatus,
key: col,
value: parseStatus(slaveData[i]),
}
scrapes <- res
}
e.error.Set(0)
e.duration.Set(float64(time.Now().UnixNano()-now) / 1000000000)
}
func (e *Exporter) setGenericMetric(subsystem string, name string, value float64) {
if _, ok := e.metrics[name]; !ok {
e.metrics[name] = prometheus.NewUntyped(prometheus.UntypedOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
})
}
e.metrics[name].Set(value)
}
var globalStatusRegexp = regexp.MustCompile(`^(com|connection_errors|innodb_rows|performance_schema)_(.*)$`)
func (e *Exporter) setGlobalStatusMetric(name string, value float64) {
match := globalStatusRegexp.FindStringSubmatch(name)
if match == nil {
e.setGenericMetric(globalStatus, name, value)
return
}
switch match[1] {
case "com":
e.commands.With(prometheus.Labels{"command": match[2]}).Set(value)
case "connection_errors":
e.connectionErrors.With(prometheus.Labels{"error": match[2]}).Set(value)
case "innodb_rows":
e.innodbRows.With(prometheus.Labels{"operation": match[2]}).Set(value)
case "performance_schema":
e.performanceSchema.With(prometheus.Labels{"instrumentation": match[2]}).Set(value)
}
}
func (e *Exporter) setMetrics(scrapes <-chan metric) {
for m := range scrapes {
name := strings.ToLower(m.key)
value, err := strconv.ParseFloat(m.value, 64)
if err != nil {
// convert/serve text values here ?
continue
}
switch m.source {
case slaveStatus:
e.setGenericMetric(m.source, name, value)
case globalStatus:
e.setGlobalStatusMetric(name, value)
}
}
}
func (e *Exporter) collectMetrics(metrics chan<- prometheus.Metric) {
for _, m := range e.metrics {
m.Collect(metrics)
}
}
func parseStatus(data []byte) string {
logRexp := regexp.MustCompile(`\.([0-9]+$)`)
logNum := logRexp.Find(data)
switch {
case string(data) == "Yes":
return "1"
case string(data) == "No":
return "0"
case len(logNum) > 1:
return string(logNum[1:])
default:
return string(data)
}
}
func main() {
flag.Parse()
dsn := os.Getenv("DATA_SOURCE_NAME")
if len(dsn) == 0 {
log.Fatal("couldn't find environment variable DATA_SOURCE_NAME")
}
exporter := NewMySQLExporter(dsn)
prometheus.MustRegister(exporter)
http.Handle(*metricPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>MySQLd exporter</title></head>
<body>
<h1>MySQLd exporter</h1>
<p><a href='` + *metricPath + `'>Metrics</a></p>
</body>
</html>
`))
})
log.Infof("Starting Server: %s", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}