forked from prometheus-community/systemd_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
92 lines (79 loc) · 2.5 KB
/
main.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
package main
import (
"net/http"
_ "net/http/pprof"
"github.com/povilasv/prommod"
"github.com/povilasv/systemd_exporter/systemd"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
)
func main() {
var (
listenAddress = kingpin.Flag(
"web.listen-address",
"Address on which to expose metrics and web interface.",
).Default(":9558").String()
metricsPath = kingpin.Flag(
"web.telemetry-path",
"Path under which to expose metrics.",
).Default("/metrics").String()
disableExporterMetrics = kingpin.Flag(
"web.disable-exporter-metrics",
"Exclude metrics about the exporter itself (promhttp_*, process_*, go_*).",
).Bool()
maxRequests = kingpin.Flag(
"web.max-requests",
"Maximum number of parallel scrape requests. Use 0 to disable.",
).Default("40").Int()
)
log.AddFlags(kingpin.CommandLine)
kingpin.Version(prommod.Print(version.Print("systemd_exporter")))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
log.Infoln("Starting systemd_exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
exporterMetricsRegistry := prometheus.NewRegistry()
r := prometheus.NewRegistry()
r.MustRegister(version.NewCollector("systemd_exporter"))
r.MustRegister(prommod.NewCollector("systemd_exporter"))
collector, err := systemd.NewCollector(log.Base())
if err != nil {
log.Fatalf("couldn't create collector: %s", err)
}
if err := r.Register(collector); err != nil {
log.Fatalf("couldn't register systemd collector: %s", err)
}
handler := promhttp.HandlerFor(
prometheus.Gatherers{exporterMetricsRegistry, r},
promhttp.HandlerOpts{
ErrorLog: log.NewErrorLogger(),
ErrorHandling: promhttp.ContinueOnError,
MaxRequestsInFlight: *maxRequests,
},
)
if !*disableExporterMetrics {
handler = promhttp.InstrumentMetricHandler(
exporterMetricsRegistry, handler,
)
}
http.Handle(*metricsPath, handler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(`<html>
<head><title>Systemd Exporter</title></head>
<body>
<h1>Systemd Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
if err != nil {
log.Errorf("couldn't write response: %s", err)
}
})
log.Infoln("Listening on", *listenAddress)
if err := http.ListenAndServe(*listenAddress, nil); err != nil {
log.Fatal(err)
}
}