cups-exporter/main.go

78 lines
2.3 KiB
Go
Raw Normal View History

/*
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
*/
2019-12-26 20:52:54 +00:00
package main
import (
"flag"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
2022-11-17 13:20:46 +00:00
"git.st8l.com/dolysis/cups-exporter/pkg"
2019-12-26 20:52:54 +00:00
"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)
}