cups-exporter/main.go

63 lines
1.7 KiB
Go
Raw Permalink Normal View History

2019-12-26 20:52:54 +00:00
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
2021-01-07 10:36:09 +00:00
CupsUri string
2019-12-26 20:52:54 +00:00
}
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")
2021-01-07 10:36:09 +00:00
flag.StringVar(&opts.CupsUri, "cups.uri", "https://localhost:631", "uri under which the cups server is available, including username and password it required")
2019-12-26 20:52:54 +00:00
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) {
2021-01-07 11:04:03 +00:00
_, err := w.Write([]byte(`<html>
2019-12-26 20:52:54 +00:00
<head><title>Cups Exporter</title></head>
<body>
<h1>Cups Exporter</h1>
<p><a href="` + opts.MetricsPath + `">Metrics</a></p>
</body>
</html>`))
2021-01-07 11:04:03 +00:00
if err != nil {
log.Info("An error occured while writing http response: %v", err)
}
2019-12-26 20:52:54 +00:00
})
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)
}