forked from camptocamp/cups_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (54 loc) · 1.66 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
package main
import (
"flag"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/phin1x/cups-exporter/pkg"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
"net/http"
"os"
)
type options struct {
Address string
MetricsPath string
CupsUri string
}
func main() {
log := getLogger()
opts := options{}
flag.StringVar(&opts.Address, "web.listen-address", ":9628", "address on which to expose metrics and web interface")
flag.StringVar(&opts.MetricsPath, "web.telemetry-path", "/metrics", "path under which to expose metrics")
flag.StringVar(&opts.CupsUri, "cups.uri", "https://localhost:631", "uri under which the cups server is available, including username and password it required")
flag.Parse()
log.Info("starting cups exporter")
exporter, err := pkg.NewExporter(opts.CupsUri, log)
if err != nil {
log.Error(err, "failed to create the exporter")
os.Exit(1)
}
prometheus.MustRegister(exporter)
http.Handle(opts.MetricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(`<html>
<head><title>Cups Exporter</title></head>
<body>
<h1>Cups Exporter</h1>
<p><a href="` + opts.MetricsPath + `">Metrics</a></p>
</body>
</html>`))
if err != nil {
log.Info("An error occured while writing http response: %v", err)
}
})
log.Info("listening on " + opts.Address)
log.Error(http.ListenAndServe(opts.Address, nil), "failed to start the http server")
}
func getLogger() logr.Logger {
zapLog, err := zap.NewDevelopment()
if err != nil {
panic(err)
}
return zapr.NewLogger(zapLog)
}