-
Notifications
You must be signed in to change notification settings - Fork 12
/
maxctrl_exporter.go
418 lines (357 loc) · 13 KB
/
maxctrl_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
// Copyright 2019, Vitaly Bezgachev, vitaly.bezgachev [the_at_symbol] gmail.com, Kadir Tugan, kadir.tugan [the_at_symbol] gmail.com
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/yaml.v2"
)
const (
metricsPath = "/metrics"
localIP = "0.0.0.0"
)
var (
maxScaleUrl string // URL of maxscale instance
maxScaleUsername string // Username for maxscale REST API authentication
maxScalePassword string // Password for maxscale REST API authentication
maxScaleExporterPort string // Port for this exporter to run on
maxScaleCACertificate string // File containing CA certificate
maxctrlExporterConfigFile string // File containing exporter config
)
type ConfigValues struct {
Url string `yaml:"url"`
Username string `yaml:"username"`
Password string `yaml:"password"`
ExporterPort string `yaml:"exporter_port"`
CACertificate string `yaml:"caCertificate"`
}
// MaxScale contains connection parameters to the server and metric maps
type MaxScale struct {
url string
username string
password string
transport *http.Transport
up prometheus.Gauge
totalScrapes prometheus.Counter
serverMetrics map[string]Metric
serviceMetrics map[string]Metric
maxscaleStatusMetrics map[string]Metric
statusMetrics map[string]Metric
}
// NewExporter creates a new instance of the MaxScale
func NewExporter(url string, username string, password string, caCertificate string) (*MaxScale, error) {
rootCAs, _ := x509.SystemCertPool()
if rootCAs == nil {
rootCAs = x509.NewCertPool()
}
if len(caCertificate) > 0 {
// Read in the cert file
certs, err := os.ReadFile(caCertificate)
if err != nil {
log.Fatalf("Failed to open CA certificate file %q: %v", caCertificate, err)
}
// Append our cert to the system pool
if ok := rootCAs.AppendCertsFromPEM(certs); !ok {
log.Fatalf("Could not append certificate to the root store from file %s", caCertificate)
}
}
transport := &http.Transport{TLSClientConfig: &tls.Config{
RootCAs: rootCAs,
}}
return &MaxScale{
url: url,
username: username,
password: password,
transport: transport,
up: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: Namespace,
Name: "up",
Help: "Was the last scrape of MaxScale successful?",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: Namespace,
Name: "exporter_total_scrapes",
Help: "Current total MaxScale scrapes",
}),
serverMetrics: ServerMetrics,
serviceMetrics: ServiceMetrics,
maxscaleStatusMetrics: MaxscaleStatusMetrics,
statusMetrics: StatusMetrics,
}, nil
}
// Describe describes all the metrics ever exported by the MaxScale exporter. It
// implements prometheus.Collector.
func (m *MaxScale) Describe(ch chan<- *prometheus.Desc) {
for _, m := range m.serverMetrics {
ch <- m.Desc
}
for _, m := range m.serviceMetrics {
ch <- m.Desc
}
for _, m := range m.maxscaleStatusMetrics {
ch <- m.Desc
}
for _, m := range m.statusMetrics {
ch <- m.Desc
}
ch <- m.up.Desc()
ch <- m.totalScrapes.Desc()
}
// Collect fetches the stats from configured MaxScale location and delivers them
// as Prometheus metrics. It implements prometheus.Collector.
func (m *MaxScale) Collect(ch chan<- prometheus.Metric) {
m.totalScrapes.Inc()
var parseErrors = false
if err := m.parseServers(ch); err != nil {
parseErrors = true
log.Print(err)
}
if err := m.parseServices(ch); err != nil {
parseErrors = true
log.Print(err)
}
if err := m.parseMaxscaleStatus(ch); err != nil {
parseErrors = true
log.Print(err)
}
if err := m.parseThreadStatus(ch); err != nil {
parseErrors = true
log.Print(err)
}
if parseErrors {
m.up.Set(0)
} else {
m.up.Set(1)
}
ch <- m.up
ch <- m.totalScrapes
}
func (m *MaxScale) getStatistics(path string, v interface{}) error {
var err error
req, err := http.NewRequest("GET", m.url+"/v1"+path, nil)
if err != nil {
return err
}
req.SetBasicAuth(m.username, m.password)
client := &http.Client{Transport: m.transport}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error while getting %v: %v", path, err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
err = fmt.Errorf("the MaxScale statistic request failed with a status: %s", resp.Status)
return err
}
return json.NewDecoder(resp.Body).Decode(v)
}
func serverUp(status string) int {
if strings.Contains(status, ",Down,") {
return 0
}
if strings.Contains(status, ",Running,") {
return 1
}
return 0
}
func (m *MaxScale) createMetricForPrometheus(metricsMap map[string]Metric, metricKey string,
value int, ch chan<- prometheus.Metric, labelValues ...string) {
metric := metricsMap[metricKey]
ch <- prometheus.MustNewConstMetric(
metric.Desc,
metric.ValueType,
float64(value),
labelValues...,
)
}
func (m *MaxScale) parseServers(ch chan<- prometheus.Metric) error {
var servers Servers
err := m.getStatistics("/servers", &servers)
if err != nil {
return err
}
for _, server := range servers.Data {
serverID := server.ID
serverAddress := server.Attributes.Parameters.Address
m.createMetricForPrometheus(m.serverMetrics, "server_connections",
server.Attributes.Statistics.Connections, ch, serverID, serverAddress)
// We surround the separated list with the separator as well. This way regular expressions
// in labeling don't have to consider satus positions.
normalizedStatus := "," + strings.Replace(server.Attributes.State, ", ", ",", -1) + ","
m.createMetricForPrometheus(m.serverMetrics, "server_up",
serverUp(normalizedStatus), ch, serverID, serverAddress, normalizedStatus)
}
return nil
}
func (m *MaxScale) parseServices(ch chan<- prometheus.Metric) error {
var services Services
err := m.getStatistics("/services", &services)
if err != nil {
return err
}
for _, service := range services.Data {
m.createMetricForPrometheus(m.serviceMetrics, "service_current_sessions",
service.Attributes.Connections, ch, service.ID, service.Attributes.Router)
m.createMetricForPrometheus(m.serviceMetrics, "service_sessions_total",
service.Attributes.Connections, ch, service.ID, service.Attributes.Router)
}
return nil
}
func (m *MaxScale) parseMaxscaleStatus(ch chan<- prometheus.Metric) error {
var maxscaleStatus MaxscaleStatus
err := m.getStatistics("/maxscale", &maxscaleStatus)
if err != nil {
return err
}
m.createMetricForPrometheus(m.maxscaleStatusMetrics, "status_uptime",
maxscaleStatus.Data.Attributes.Uptime, ch)
m.createMetricForPrometheus(m.maxscaleStatusMetrics, "status_threads",
maxscaleStatus.Data.Attributes.Parameters.Threads, ch)
passiveMode := 0
if maxscaleStatus.Data.Attributes.Parameters.Passive {
passiveMode = 1
}
m.createMetricForPrometheus(m.maxscaleStatusMetrics, "status_passive", passiveMode, ch)
return nil
}
func (m *MaxScale) parseThreadStatus(ch chan<- prometheus.Metric) error {
var threadStatus ThreadStatus
err := m.getStatistics("/maxscale/threads", &threadStatus)
if err != nil {
return err
}
for _, threadStatus := range threadStatus.Data {
m.createMetricForPrometheus(m.statusMetrics, "status_read_events",
threadStatus.Attributes.Stats.Reads, ch, threadStatus.ID)
m.createMetricForPrometheus(m.statusMetrics, "status_write_events",
threadStatus.Attributes.Stats.Writes, ch, threadStatus.ID)
m.createMetricForPrometheus(m.statusMetrics, "status_error_events",
threadStatus.Attributes.Stats.Errors, ch, threadStatus.ID)
m.createMetricForPrometheus(m.statusMetrics, "status_hangup_events",
threadStatus.Attributes.Stats.Hangups, ch, threadStatus.ID)
m.createMetricForPrometheus(m.statusMetrics, "status_accept_events",
threadStatus.Attributes.Stats.Accepts, ch, threadStatus.ID)
m.createMetricForPrometheus(m.statusMetrics, "status_avg_event_queue_length",
threadStatus.Attributes.Stats.AvgEventQueueLength, ch, threadStatus.ID)
m.createMetricForPrometheus(m.statusMetrics, "status_max_event_queue_length",
threadStatus.Attributes.Stats.MaxEventQueueLength, ch, threadStatus.ID)
m.createMetricForPrometheus(m.statusMetrics, "status_max_event_exec_time",
threadStatus.Attributes.Stats.MaxExecTime, ch, threadStatus.ID)
m.createMetricForPrometheus(m.statusMetrics, "status_max_event_queue_time",
threadStatus.Attributes.Stats.MaxQueueTime, ch, threadStatus.ID)
m.createMetricForPrometheus(m.statusMetrics, "status_current_descriptors",
threadStatus.Attributes.Stats.CurrentDescriptors, ch, threadStatus.ID)
m.createMetricForPrometheus(m.statusMetrics, "status_total_descriptors",
threadStatus.Attributes.Stats.TotalDescriptors, ch, threadStatus.ID)
m.createMetricForPrometheus(m.statusMetrics, "status_load_last_second",
threadStatus.Attributes.Stats.Load.LastSecond, ch, threadStatus.ID)
m.createMetricForPrometheus(m.statusMetrics, "status_load_last_minute",
threadStatus.Attributes.Stats.Load.LastMinute, ch, threadStatus.ID)
m.createMetricForPrometheus(m.statusMetrics, "status_load_last_hour",
threadStatus.Attributes.Stats.Load.LastHour, ch, threadStatus.ID)
m.createMetricForPrometheus(m.statusMetrics, "status_query_classifier_cache_size",
threadStatus.Attributes.Stats.QueryClassifierCache.Size, ch, threadStatus.ID)
m.createMetricForPrometheus(m.statusMetrics, "status_query_classifier_cache_inserts",
threadStatus.Attributes.Stats.QueryClassifierCache.Inserts, ch, threadStatus.ID)
m.createMetricForPrometheus(m.statusMetrics, "status_query_classifier_cache_hits",
threadStatus.Attributes.Stats.QueryClassifierCache.Hits, ch, threadStatus.ID)
m.createMetricForPrometheus(m.statusMetrics, "status_query_classifier_cache_misses",
threadStatus.Attributes.Stats.QueryClassifierCache.Misses, ch, threadStatus.ID)
m.createMetricForPrometheus(m.statusMetrics, "status_query_classifier_cache_evictions",
threadStatus.Attributes.Stats.QueryClassifierCache.Evictions, ch, threadStatus.ID)
}
return nil
}
// GetEnvVar - retrieves values of environment variables. If nothing is set, return the default value
func GetEnvVar(envName string, defaultValue string) string {
envVal := os.Getenv(envName)
if envVal == "" {
return defaultValue
}
return envVal
}
func readConfigFile(fname string) {
yamlFile, err := os.ReadFile(fname)
if err != nil {
// If the file doesn't exist, just return
if errors.Is(err, fs.ErrNotExist) {
return
} else {
log.Printf("Could not open configuration file '%s': %v", maxctrlExporterConfigFile, err)
}
}
parseConfigFile(yamlFile)
}
func parseConfigFile(contents []byte) {
var config ConfigValues
err := yaml.Unmarshal(contents, &config)
if err != nil {
log.Fatalf("Could not parse config file contents: %v", err)
}
if config.Url != "" {
maxScaleUrl = config.Url
}
if config.Username != "" {
maxScaleUsername = config.Username
}
if config.Password != "" {
maxScalePassword = config.Password
}
if config.ExporterPort != "" {
maxScaleExporterPort = config.ExporterPort
}
if config.CACertificate != "" {
maxScaleCACertificate = config.CACertificate
}
}
func setConfigFromEnvironmentVars() {
maxScaleUrl = GetEnvVar("MAXSCALE_URL", "http://127.0.0.1:8989")
maxScaleUsername = GetEnvVar("MAXSCALE_USERNAME", "admin")
maxScalePassword = GetEnvVar("MAXSCALE_PASSWORD", "mariadb")
maxScaleExporterPort = GetEnvVar("MAXSCALE_EXPORTER_PORT", "8080")
maxScaleCACertificate = GetEnvVar("MAXSCALE_CA_CERTIFICATE", "")
maxctrlExporterConfigFile = GetEnvVar("MAXCTRL_EXPORTER_CFG_FILE", "maxctrl_exporter.yaml")
}
func main() {
setConfigFromEnvironmentVars()
readConfigFile(maxctrlExporterConfigFile)
log.Print("Starting MaxScale exporter")
log.Printf("Scraping MaxScale JSON API at: %s", maxScaleUrl)
exporter, err := NewExporter(maxScaleUrl, maxScaleUsername, maxScalePassword, maxScaleCACertificate)
if err != nil {
log.Fatalf("Failed to start maxscale exporter: %v\n", err)
}
prometheus.MustRegister(exporter)
http.Handle(metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>MaxScale Exporter</title></head>
<body>
<h1>MaxScale Exporter</h1>
<p><a href="` + metricsPath + `">Metrics</a></p>
</body>
</html>`))
})
log.Printf("Started MaxScale exporter, listening on port: %v", maxScaleExporterPort)
log.Fatal(http.ListenAndServe(localIP+":"+maxScaleExporterPort, nil))
}