cups-exporter/main.go

81 lines
2.3 KiB
Go

/*
** Copyright 2022 Dolysis Consulting Limited
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
** For changes see the git log
*/
package main
import (
"flag"
"net/http"
"os"
"git.st8l.com/dolysis/cups-exporter/pkg"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
)
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 occurred 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)
}