forked from lumasepa/prometheus_checkman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.go
44 lines (37 loc) · 1.04 KB
/
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
package main
import (
"github.com/prometheus/client_golang/prometheus"
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"strconv"
)
func buildGauge(check Check) prometheus.Gauge {
return prometheus.NewGauge(prometheus.GaugeOpts{
Name: check.Name,
Help: check.Help,
ConstLabels: check.Labels,
})
}
func metricsUpdater(gauges map[string]prometheus.Gauge, resultsChan chan CheckResult){
for {
checkResult := <- resultsChan
gauge := gauges[checkResult.name]
if checkResult.err == nil {
gauge.Set(float64(checkResult.exitCode))
}else{
gauge.Set(255)
}
}
}
func mainExporter(conf *Configuration, resultsChan chan CheckResult) {
gauges := make(map[string]prometheus.Gauge, len(conf.Checks))
for _, check := range conf.Checks {
gauge := buildGauge(check)
prometheus.MustRegister(gauge)
gauges[check.Name] = gauge
}
go metricsUpdater(gauges, resultsChan)
http.Handle(conf.ExporterPath, promhttp.Handler())
log.Fatal(http.ListenAndServe(conf.ListenIP + ":" + strconv.Itoa(conf.ListenPort), nil))
}