-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
95 lines (84 loc) · 3.05 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
93
94
95
package main
import (
"net/http"
"os"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus/common/promlog/flag"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/version"
"github.com/vareversat/digicert_exporter/collector"
)
var (
app = kingpin.New(
"digicert_exporter",
"🔥 A Prometheus exporter to monitor Digicert certificates.",
)
digicertURL = app.Flag(
"digicert.url",
"Digicert API URL used to fetch data.",
).Default("https://www.digicert.com/services/v2/order/certificate").Envar("DIGICERT_URL").String()
sandboxMode = app.Flag(
"digicert.sandbox-mode",
"Use the mock.json file to test the exporter",
).Default("false").Envar("SANDBOX_MODE").Bool()
digicertAPIKey = app.Flag("digicert.api-key",
"Digicert API Key used to authentication.").Envar("DIGICERT_API_KEY").String()
digicertShowExpiredCertificates = app.Flag(
"digicert.show-expired-certificates",
"Show expired certificate.",
).Default("false").Envar("DIGICERT_SHOW_EXPIRED_CERTIFICATES").Bool()
listenAddress = app.Flag("web.listen-port",
"Port used to run the exporter.").Default(":10005").Envar("EXPORTER_PORT").String()
metricPath = app.Flag("web.metrics-path",
"Path under which to expose metrics.").Default("/metrics").Envar("EXPORTER_PATH").String()
webMetrics = app.Flag(
"web.exporter-metrics",
"Show the go and http system metrics for this exporter.",
).Default("false").Envar("EXPORTER_ENABLE_EXPORTER_METRICS").Bool()
)
func main() {
promlogConfig := &promlog.Config{}
flag.AddFlags(app, promlogConfig)
kingpin.CommandLine.UsageWriter(os.Stdout)
app.Version(version.Print("digicert_exporter"))
app.VersionFlag.Short('v')
app.HelpFlag.Short('h')
kingpin.MustParse(app.Parse(os.Args[1:]))
logger := promlog.New(promlogConfig)
level.Info(logger).
Log("msg", "Starting digicert_exporter", "port", listenAddress, "path", metricPath, "version", version.Info())
level.Debug(logger).Log("msg", "Build context", "build_context", version.BuildContext())
digicertCollector, err := collector.NewDigicertCollector(
logger,
*digicertURL,
*digicertAPIKey,
*sandboxMode,
*digicertShowExpiredCertificates,
)
if err != nil {
level.Error(logger).Log("err", err)
os.Exit(1)
}
promRegistry := prometheus.NewRegistry()
promRegistry.MustRegister(digicertCollector)
httpHandler := promhttp.HandlerFor(promRegistry, promhttp.HandlerOpts{})
if *webMetrics {
httpHandler = promhttp.InstrumentMetricHandler(promRegistry, httpHandler)
}
http.Handle(*metricPath, httpHandler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<head><title>Digicert Exporter Metrics</title></head>
<body>
<h1>Digicert Exporter Metrics</h1>
<p><a href='` + *metricPath + `'>Metrics</a></p>
</body>
</html>
`))
})
level.Error(logger).Log("err", http.ListenAndServe(*listenAddress, nil))
}