forked from percona/proxysql_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxysql_exporter.go
398 lines (360 loc) · 10.6 KB
/
proxysql_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
package main
import (
"crypto/tls"
"database/sql"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"gopkg.in/yaml.v2"
)
var (
version = "1.0.1"
showVersion = flag.Bool(
"version", false,
"Print version information.",
)
listenAddress = flag.String(
"web.listen-address", ":42004",
"Address to listen on for web interface and telemetry.",
)
metricPath = flag.String(
"web.telemetry-path", "/metrics",
"Path under which to expose metrics.",
)
webAuthFile = flag.String(
"web.auth-file", "",
"Path to YAML file with server_user, server_password options for http basic auth (overrides HTTP_AUTH env var).",
)
sslCertFile = flag.String(
"web.ssl-cert-file", "",
"Path to SSL certificate file.",
)
sslKeyFile = flag.String(
"web.ssl-key-file", "",
"Path to SSL key file.",
)
collectMySQLStatus = flag.Bool(
"collect.mysql_status", true,
"Collect from SHOW MYSQL STATUS",
)
collectMySQLConnectionPool = flag.Bool(
"collect.mysql_connection_pool", true,
"Collect from stats_mysql_connection_pool",
)
)
const (
namespace = "proxysql"
mysqlStatusQuery = "SHOW MYSQL STATUS"
mysqlConnectionPoolQuery = `
SELECT
hostgroup, srv_host, srv_port, status,
ConnUsed, ConnFree, ConnOK, ConnERR, Queries,
Bytes_data_sent, Bytes_data_recv, Latency_us
FROM stats_mysql_connection_pool
`
)
var landingPage = []byte(`<html>
<head><title>ProxySQL exporter</title></head>
<body>
<h1>ProxySQL exporter</h1>
<p><a href='` + *metricPath + `'>Metrics</a></p>
</body>
</html>
`)
type webAuth struct {
User string `yaml:"server_user,omitempty"`
Password string `yaml:"server_password,omitempty"`
}
type basicAuthHandler struct {
handler http.HandlerFunc
user string
password string
}
func (h *basicAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
user, password, ok := r.BasicAuth()
if !ok || password != h.password || user != h.user {
w.Header().Set("WWW-Authenticate", "Basic realm=\"metrics\"")
http.Error(w, "Invalid username or password", http.StatusUnauthorized)
return
}
h.handler(w, r)
}
type exporter struct {
dsn string
duration prometheus.Gauge
error prometheus.Gauge
totalScrapes prometheus.Counter
scrapeErrors *prometheus.CounterVec
proxysqlUP prometheus.Gauge
}
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 ProxySQL.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "exporter",
Name: "scrapes_total",
Help: "Total number of times ProxySQL was scraped for metrics.",
}),
scrapeErrors: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "exporter",
Name: "scrape_errors_total",
Help: "Total number of times an error occurred scraping a ProxySQL.",
}, []string{"collector"}),
error: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "exporter",
Name: "last_scrape_error",
Help: "Whether the last scrape of metrics from ProxySQL resulted in an error (1 for error, 0 for success).",
}),
proxysqlUP: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "up",
Help: "Whether ProxySQL is up.",
}),
}
}
func (e *exporter) Describe(ch chan<- *prometheus.Desc) {
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
}
func (e *exporter) Collect(ch chan<- prometheus.Metric) {
e.scrape(ch)
ch <- e.duration
ch <- e.totalScrapes
ch <- e.error
e.scrapeErrors.Collect(ch)
ch <- e.proxysqlUP
}
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.Errorln("Error opening connection to database:", err)
return
}
defer db.Close()
if err = db.Ping(); err != nil {
log.Errorln("Error pinging ProxySQL:", err)
e.proxysqlUP.Set(0)
return
}
e.proxysqlUP.Set(1)
if *collectMySQLStatus {
if err = scrapeMySQLStatus(db, ch); err != nil {
log.Errorln("Error scraping for collect.mysql_status:", err)
e.scrapeErrors.WithLabelValues("collect.mysql_status").Inc()
}
}
if *collectMySQLConnectionPool {
if err = scrapeMySQLConnectionPool(db, ch); err != nil {
log.Errorln("Error scraping for collect.mysql_connection_pool:", err)
e.scrapeErrors.WithLabelValues("collect.mysql_connection_pool").Inc()
}
}
}
// scrapeMySQLStatus collects `SHOW MYSQL STATUS`.
func scrapeMySQLStatus(db *sql.DB, ch chan<- prometheus.Metric) error {
rows, err := db.Query(mysqlStatusQuery)
if err != nil {
return err
}
defer rows.Close()
var (
key string
val float64
)
for rows.Next() {
if err := rows.Scan(&key, &val); err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, "mysql_status", strings.ToLower(key)),
"Global status metric.",
nil, nil,
),
prometheus.UntypedValue, val,
)
}
return nil
}
func newConnPoolMetric(name, hostgroup, endpoint string, value float64, valueType prometheus.ValueType, ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, "connection_pool", name),
"Connection pool usage statistic.",
[]string{"hostgroup", "endpoint"}, nil,
),
valueType, value,
hostgroup, endpoint,
)
}
// scrapeMySQLConnectionPool collects from `stats_mysql_connection_pool`.
func scrapeMySQLConnectionPool(db *sql.DB, ch chan<- prometheus.Metric) error {
rows, err := db.Query(mysqlConnectionPoolQuery)
if err != nil {
return err
}
defer rows.Close()
var (
hostgroup string
srv_host string
srv_port string
status string
statusNum float64
ConnUsed float64
ConnFree float64
ConnOK float64
ConnERR float64
Queries float64
Bytes_data_sent float64
Bytes_data_recv float64
Latency_us float64
)
for rows.Next() {
if err := rows.Scan(&hostgroup, &srv_host, &srv_port, &status, &ConnUsed, &ConnFree, &ConnOK, &ConnERR, &Queries, &Bytes_data_sent, &Bytes_data_recv, &Latency_us); err != nil {
return err
}
// Map status to ids.
switch status {
case "ONLINE":
statusNum = 1
case "SHUNNED":
statusNum = 2
case "OFFLINE_SOFT":
statusNum = 3
case "OFFLINE_HARD":
statusNum = 4
}
endpoint := srv_host + ":" + srv_port
newConnPoolMetric("status", hostgroup, endpoint, statusNum, prometheus.GaugeValue, ch)
newConnPoolMetric("conn_used", hostgroup, endpoint, ConnUsed, prometheus.GaugeValue, ch)
newConnPoolMetric("conn_free", hostgroup, endpoint, ConnFree, prometheus.GaugeValue, ch)
newConnPoolMetric("conn_ok", hostgroup, endpoint, ConnOK, prometheus.CounterValue, ch)
newConnPoolMetric("conn_err", hostgroup, endpoint, ConnERR, prometheus.CounterValue, ch)
newConnPoolMetric("queries", hostgroup, endpoint, Queries, prometheus.CounterValue, ch)
newConnPoolMetric("bytes_data_sent", hostgroup, endpoint, Bytes_data_sent, prometheus.CounterValue, ch)
newConnPoolMetric("bytes_data_recv", hostgroup, endpoint, Bytes_data_recv, prometheus.CounterValue, ch)
newConnPoolMetric("latency_us", hostgroup, endpoint, Latency_us, prometheus.GaugeValue, ch)
}
return nil
}
func main() {
flag.Parse()
if *showVersion {
fmt.Fprintln(os.Stdout, version)
os.Exit(0)
}
log.Infoln("Starting proxysql_exporter", version)
dsn := os.Getenv("DATA_SOURCE_NAME")
if dsn == "" {
dsn = "stats:stats@tcp(localhost:6032)/"
}
cfg := &webAuth{}
httpAuth := os.Getenv("HTTP_AUTH")
if *webAuthFile != "" {
bytes, err := ioutil.ReadFile(*webAuthFile)
if err != nil {
log.Fatal("Cannot read auth file: ", err)
}
if err := yaml.Unmarshal(bytes, cfg); err != nil {
log.Fatal("Cannot parse auth file: ", err)
}
} else if httpAuth != "" {
data := strings.SplitN(httpAuth, ":", 2)
if len(data) != 2 || data[0] == "" || data[1] == "" {
log.Fatal("HTTP_AUTH should be formatted as user:password")
}
cfg.User = data[0]
cfg.Password = data[1]
}
exporter := newExporter(dsn)
prometheus.MustRegister(exporter)
handler := prometheus.Handler()
if cfg.User != "" && cfg.Password != "" {
handler = &basicAuthHandler{handler: handler.ServeHTTP, user: cfg.User, password: cfg.Password}
log.Infoln("HTTP basic authentication is enabled")
}
if *sslCertFile != "" && *sslKeyFile == "" || *sslCertFile == "" && *sslKeyFile != "" {
log.Fatal("One of the flags -web.ssl-cert or -web.ssl-key is missed to enable HTTPS/TLS")
}
ssl := false
if *sslCertFile != "" && *sslKeyFile != "" {
if _, err := os.Stat(*sslCertFile); os.IsNotExist(err) {
log.Fatal("SSL certificate file does not exist: ", *sslCertFile)
}
if _, err := os.Stat(*sslKeyFile); os.IsNotExist(err) {
log.Fatal("SSL key file does not exist: ", *sslKeyFile)
}
ssl = true
log.Infoln("HTTPS/TLS is enabled")
}
log.Infoln("Listening on", *listenAddress)
if ssl {
// https
mux := http.NewServeMux()
mux.Handle(*metricPath, handler)
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
w.Write(landingPage)
})
tlsCfg := &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
},
}
srv := &http.Server{
Addr: *listenAddress,
Handler: mux,
TLSConfig: tlsCfg,
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler), 0),
}
log.Fatal(srv.ListenAndServeTLS(*sslCertFile, *sslKeyFile))
} else {
// http
http.Handle(*metricPath, handler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(landingPage)
})
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}
}