forked from liv-io/monit_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonit_exporter.go
561 lines (514 loc) · 16.3 KB
/
monit_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
package main
import (
"bytes"
"crypto/tls"
"encoding/xml"
"errors"
"flag"
"fmt"
"io"
"net/http"
"strings"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"golang.org/x/net/html/charset"
)
const (
namespace = "monit" // Prefix for Prometheus metrics.
SERVICE_TYPE_FILESYSTEM = 0
SERVICE_TYPE_DIRECTORY = 1
SERVICE_TYPE_FILE = 2
SERVICE_TYPE_PROCESS = 3
SERVICE_TYPE_HOST = 4
SERVICE_TYPE_SYSTEM = 5
SERVICE_TYPE_FIFO = 6
SERVICE_TYPE_PROGRAM = 7
SERVICE_TYPE_NET = 8
)
var serviceTypes = map[int]string{
SERVICE_TYPE_FILESYSTEM: "filesystem",
SERVICE_TYPE_DIRECTORY: "directory",
SERVICE_TYPE_FILE: "file",
SERVICE_TYPE_PROCESS: "process",
SERVICE_TYPE_HOST: "host",
SERVICE_TYPE_SYSTEM: "system",
SERVICE_TYPE_FIFO: "fifo",
SERVICE_TYPE_PROGRAM: "program",
SERVICE_TYPE_NET: "network",
}
type monitXML struct {
MonitServer monitServer `xml:"server"`
MonitServices []monitService `xml:"service"`
}
type monitServer struct {
Hostname string `xml:"localhostname"`
Uptime int64 `xml:"uptime"`
Version string `xml:"version"`
}
// Simplified structure of monit check.
type monitService struct {
CPU monitServiceCPU `xml:"cpu"`
DiskRead monitServiceDisk `xml:"read"`
DiskWrite monitServiceDisk `xml:"write"`
Link monitServiceLink `xml:"link"`
Memory monitServiceMem `xml:"memory"`
Monitored string `xml:"monitor"`
Name string `xml:"name"`
Ports []monitServicePort `xml:"port"`
ServiceTimes monitServiceTime `xml:"servicetime"`
Status int `xml:"status"`
Type int `xml:"type,attr"`
UnixSockets []monitServicePort `xml:"unix"`
Uptime int64 `xml:"uptime"`
Icmp monitServiceIcmp `xml:"icmp"`
}
type monitServiceMem struct {
Percent float64 `xml:"percent,attr"`
PercentTotal float64 `xml:"percenttotal"`
Kilobyte int `xml:"kilobyte"`
KilobyteTotal int `xml:"kilobytetotal"`
}
type monitServiceCPU struct {
Percent float64 `xml:"percent,attr"`
PercentTotal float64 `xml:"percenttotal"`
}
type monitServiceDisk struct {
Bytes monitBytes `xml:"bytes"`
}
type monitServiceTime struct {
Read float64 `xml:"read"`
Write float64 `xml:"write"`
Wait float64 `xml:"wait"`
Run float64 `xml:"run"`
}
type monitServicePort struct {
Hostname string `xml:"hostname"`
Path string `xml:"path"`
Portnumber string `xml:"portnumber"`
Protocol string `xml:"protocol"`
Type string `xml:"type"`
Responsetime float64 `xml:"responsetime"`
}
type monitServiceLink struct {
State int `xml:"state"`
Speed int `xml:"speed"`
Duplex int `xml:"duplex"`
Download monitServiceLinkDirection `xml:"download"`
Upload monitServiceLinkDirection `xml:"upload"`
}
type monitServiceLinkDirection struct {
Packets monitNetworkCount `xml:"packets"`
Bytes monitNetworkCount `xml:"bytes"`
Errors monitNetworkCount `xml:"errors"`
}
type monitServiceIcmp struct {
Type string `xml:"type"`
Responsetime float64 `xml:"responsetime"`
}
type monitBytes struct {
Count int `xml:"count"`
Total int `xml:"total"`
}
type monitNetworkCount struct {
Now int `xml:"now"`
Total int `xml:"total"`
}
// Exporter collects monit stats from the given URI and exports them using
// the prometheus metrics package.
type Exporter struct {
config *Config
mutex sync.RWMutex
client *http.Client
up prometheus.Gauge
version *prometheus.GaugeVec
checkUptime *prometheus.GaugeVec
checkStatus *prometheus.GaugeVec
checkMem *prometheus.GaugeVec
checkCPU *prometheus.GaugeVec
checkDiskWrite *prometheus.GaugeVec
checkDiskRead *prometheus.GaugeVec
checkPortRespTimes *prometheus.GaugeVec
checkLinkState *prometheus.GaugeVec
checkLinkStats *prometheus.GaugeVec
}
type Config struct {
listen_address string
metrics_path string
ignore_ssl bool
monit_scrape_uri string
monit_user string
monit_password string
}
// FetchMonitStatus gather metrics from Monit API
func FetchMonitStatus(e *Exporter) ([]byte, error) {
req, err := http.NewRequest("GET", e.config.monit_scrape_uri, nil)
if err != nil {
log.Errorf("Unable to create request: %v", err)
}
req.SetBasicAuth(e.config.monit_user, e.config.monit_password)
resp, err := e.client.Do(req)
if err != nil {
log.Error("Unable to fetch monit status")
return nil, err
}
switch resp.StatusCode {
case 200:
case 401:
return nil, errors.New("authentication with monit failed")
default:
return nil, fmt.Errorf("monit returned %s", resp.Status)
}
data, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
log.Fatal("Unable to read monit status")
return nil, err
}
return data, nil
}
// ParseMonitStatus parse XML data and return it to struct
func ParseMonitStatus(data []byte) (monitXML, error) {
var statusChunk monitXML
reader := bytes.NewReader(data)
decoder := xml.NewDecoder(reader)
// Parsing status results to structure
decoder.CharsetReader = charset.NewReaderLabel
err := decoder.Decode(&statusChunk)
return statusChunk, err
}
// ParseConfig parse exporter binary options from command line
func ParseConfig() *Config {
flag.String("conf", "./config.toml", "Configuration file for exporter")
flag.Parse()
v := viper.New()
// Provide all configurations as environment variable as well.
v.SetEnvPrefix(strings.ToUpper(namespace))
v.AutomaticEnv()
v.SetDefault("listen_address", "0.0.0.0:9388")
v.SetDefault("metrics_path", "/metrics")
v.SetDefault("ignore_ssl", false)
v.SetDefault("monit_scrape_uri", "http://localhost:2812/_status?format=xml&level=full")
v.SetDefault("monit_user", "")
v.SetDefault("monit_password", "")
v.SetConfigFile(flag.Lookup("conf").Value.String())
v.SetConfigType("toml")
err := v.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
log.Printf("Error reading config file: %s. Using defaults.", err)
}
return &Config{
listen_address: v.GetString("listen_address"),
metrics_path: v.GetString("metrics_path"),
ignore_ssl: v.GetBool("ignore_ssl"),
monit_scrape_uri: v.GetString("monit_scrape_uri"),
monit_user: v.GetString("monit_user"),
monit_password: v.GetString("monit_password"),
}
}
// Returns an initialized Exporter.
func NewExporter(c *Config) (*Exporter, error) {
return &Exporter{
config: c,
client: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.ignore_ssl},
},
},
up: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "up",
Help: "Monit status availability",
}),
version: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "version",
Help: "Monit version",
},
[]string{"version"},
),
checkUptime: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "service_uptime",
Help: "Monit service and server uptime",
},
[]string{"check_name", "type"},
),
checkStatus: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "service_check",
Help: "Monit service check info",
},
[]string{"check_name", "type", "monitored"},
),
checkMem: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "service_mem_bytes",
Help: "Monit service mem info",
},
[]string{"check_name", "type"},
),
checkCPU: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "service_cpu_perc",
Help: "Monit service CPU info",
},
[]string{"check_name", "type"},
),
checkDiskWrite: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "service_write_bytes",
Help: "Monit service Disk Writes Bytes",
},
[]string{"check_name", "type"},
),
checkDiskRead: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "service_read_bytes",
Help: "Monit service Disk Read Bytes",
},
[]string{"check_name", "type"},
),
checkPortRespTimes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "service_port_response_times",
Help: "Monit service port and unix socket checks response times",
},
[]string{"check_name", "hostname", "path", "port", "protocol", "type", "uri"},
),
checkLinkState: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "service_network_link_state",
Help: "Monit service link states",
},
[]string{"check_name"},
),
checkLinkStats: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "service_network_link_statistics",
Help: "Monit service link statistics",
},
[]string{"check_name", "direction", "unit", "type"},
),
}, nil
}
// Describe describes all the metrics ever exported by the monit exporter. It
// implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.up.Describe(ch)
e.version.Describe(ch)
e.checkUptime.Describe(ch)
e.checkStatus.Describe(ch)
e.checkCPU.Describe(ch)
e.checkMem.Describe(ch)
e.checkDiskWrite.Describe(ch)
e.checkDiskRead.Describe(ch)
e.checkPortRespTimes.Describe(ch)
e.checkLinkState.Describe(ch)
e.checkLinkStats.Describe(ch)
}
func (e *Exporter) scrape() error {
data, err := FetchMonitStatus(e)
if err != nil {
// set "monit_exporter_up" gauge to 0, remove previous metrics from e.checkStatus vector
e.up.Set(0)
e.checkStatus.Reset()
log.Errorf("Error getting monit status: %v", err)
return err
} else {
parsedData, err := ParseMonitStatus(data)
if err != nil {
e.up.Set(0)
e.checkStatus.Reset()
log.Errorf("Error parsing data from monit: %v\n%s", err, data)
} else {
e.up.Set(1)
e.checkUptime.With(
prometheus.Labels{
"check_name": parsedData.MonitServer.Hostname,
"type": "server",
}).Set(float64(parsedData.MonitServer.Uptime))
e.version.With(
prometheus.Labels{
"version": parsedData.MonitServer.Version,
}).Set(1)
// Constructing metrics
for _, service := range parsedData.MonitServices {
e.checkStatus.With(prometheus.Labels{"check_name": service.Name, "type": serviceTypes[service.Type], "monitored": service.Monitored}).Set(float64(service.Status))
e.checkStatus.With(
prometheus.Labels{
"check_name": service.Name,
"type": serviceTypes[service.Type],
"monitored": service.Monitored,
}).Set(float64(service.Status))
// Memory + CPU + Uptime only for specifiy status types (cf. monit/xml.c)
if service.Type == SERVICE_TYPE_PROCESS || service.Type == SERVICE_TYPE_SYSTEM {
e.checkMem.With(
prometheus.Labels{
"check_name": service.Name,
"type": "kilobyte",
}).Set(float64(service.Memory.Kilobyte * 1024))
e.checkMem.With(
prometheus.Labels{
"check_name": service.Name,
"type": "kilobyte_total",
}).Set(float64(service.Memory.KilobyteTotal * 1024))
e.checkCPU.With(
prometheus.Labels{
"check_name": service.Name,
"type": "percentage",
}).Set(float64(service.CPU.Percent))
e.checkCPU.With(
prometheus.Labels{
"check_name": service.Name,
"type": "percentage_total",
}).Set(float64(service.CPU.PercentTotal))
e.checkUptime.With(
prometheus.Labels{
"check_name": service.Name,
"type": serviceTypes[service.Type],
}).Set(float64(service.Uptime))
}
if service.Type == SERVICE_TYPE_PROCESS || service.Type == SERVICE_TYPE_FILESYSTEM {
e.checkDiskWrite.With(
prometheus.Labels{
"check_name": service.Name,
"type": "write_count",
}).Set(float64(service.DiskWrite.Bytes.Count))
e.checkDiskWrite.With(
prometheus.Labels{
"check_name": service.Name,
"type": "write_count_total",
}).Set(float64(service.DiskWrite.Bytes.Total))
e.checkDiskRead.With(
prometheus.Labels{
"check_name": service.Name,
"type": "read_count",
}).Set(float64(service.DiskRead.Bytes.Count))
e.checkDiskRead.With(
prometheus.Labels{
"check_name": service.Name,
"type": "read_count_total",
}).Set(float64(service.DiskRead.Bytes.Total))
}
// Link (only relevant for network checks)
if service.Type == SERVICE_TYPE_NET {
e.checkLinkState.With(
prometheus.Labels{
"check_name": service.Name,
}).Set(float64(service.Link.State))
e.addNetLinkElement(&service, "download", &service.Link.Download)
e.addNetLinkElement(&service, "upload", &service.Link.Upload)
}
// ICMP checks
if service.Type == SERVICE_TYPE_HOST && service.Icmp.Type != "" {
e.checkPortRespTimes.With(
prometheus.Labels{
"check_name": service.Name,
"type": "ICMP",
"hostname": "",
"path": "",
"port": "",
"protocol": strings.ToUpper(service.Icmp.Type),
"uri": "",
}).Set(float64(service.Icmp.Responsetime))
}
// Port checks
for _, port := range service.Ports {
var uri = fmt.Sprintf("%s://%s:%s", strings.ToLower(port.Type), port.Hostname, port.Portnumber)
e.checkPortRespTimes.With(
prometheus.Labels{
"check_name": service.Name,
"type": port.Type,
"hostname": port.Hostname,
"path": "",
"port": port.Portnumber,
"protocol": port.Protocol,
"uri": uri,
}).Set(float64(port.Responsetime))
}
// Unix socket checks
for _, port := range service.UnixSockets {
var uri = fmt.Sprintf("unix://%s", port.Path)
e.checkPortRespTimes.With(
prometheus.Labels{
"check_name": service.Name,
"type": "UNIX",
"hostname": "",
"path": port.Path,
"port": "",
"protocol": port.Protocol,
"uri": uri,
}).Set(float64(port.Responsetime))
}
}
}
return err
}
}
func (e *Exporter) addNetLinkElement(service *monitService, direction string, lnk *monitServiceLinkDirection) {
e.addNetLinkUnitElement(service, direction, "packets", &lnk.Packets)
e.addNetLinkUnitElement(service, direction, "bytes", &lnk.Bytes)
e.addNetLinkUnitElement(service, direction, "errors", &lnk.Errors)
}
func (e *Exporter) addNetLinkUnitElement(service *monitService, direction string, unit string, lnk *monitNetworkCount) {
e.checkLinkStats.With(
prometheus.Labels{
"check_name": service.Name,
"direction": direction,
"unit": unit,
"type": "now",
}).Set(float64(lnk.Now))
e.checkLinkStats.With(
prometheus.Labels{
"check_name": service.Name,
"direction": direction,
"unit": unit,
"type": "total",
}).Set(float64(lnk.Total))
}
// Collect fetches the stats from configured monit location and delivers them
// as Prometheus metrics. It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock() // Protect metrics from concurrent collects.
defer e.mutex.Unlock()
e.checkStatus.Reset()
if err := e.scrape(); err == nil {
e.up.Collect(ch)
e.version.Collect(ch)
e.checkUptime.Collect(ch)
e.checkStatus.Collect(ch)
e.checkMem.Collect(ch)
e.checkCPU.Collect(ch)
e.checkDiskWrite.Collect(ch)
e.checkDiskRead.Collect(ch)
e.checkPortRespTimes.Collect(ch)
e.checkLinkState.Collect(ch)
e.checkLinkStats.Collect(ch)
}
}
func main() {
config := ParseConfig()
exporter, err := NewExporter(config)
if err != nil {
log.Fatal(err)
}
prometheus.MustRegister(exporter)
log.Printf("Starting monit_exporter: %s", config.listen_address)
http.Handle(config.metrics_path, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(`<html>
<head><title>Monit Exporter</title></head>
<body>
<h1>Monit Exporter</h1>
<p><a href="` + config.metrics_path + `">Metrics</a></p>
</body>
</html>`))
if err != nil {
log.Fatal(err)
}
})
log.Fatal(http.ListenAndServe(config.listen_address, nil))
}