Merge branch 'print-ready-message-with-host-and-port-on-startup' into 'main'

Print ready message with host and port on startup

See merge request hectorjsmith/fail2ban-prometheus-exporter!34
This commit is contained in:
Hector 2021-09-22 12:34:39 +00:00
commit a1a0aa03a4

View file

@ -12,6 +12,10 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
) )
const (
metricsPath = "/metrics"
)
var ( var (
version = "dev" version = "dev"
commit = "none" commit = "none"
@ -29,12 +33,23 @@ func main() {
if appSettings.VersionMode { if appSettings.VersionMode {
printAppVersion() printAppVersion()
} else { } else {
log.Print("starting fail2ban exporter") addr := fmt.Sprintf("0.0.0.0:%d", appSettings.MetricsPort)
log.Printf("starting fail2ban exporter at %s", addr)
exporter := export.NewExporter(appSettings, version) exporter := export.NewExporter(appSettings, version)
prometheus.MustRegister(exporter) prometheus.MustRegister(exporter)
http.Handle("/metrics", promhttp.Handler()) http.Handle(metricsPath, promhttp.Handler())
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", appSettings.MetricsPort), nil)) log.Printf("metrics available at '%s'", metricsPath)
svrErr := make(chan error)
go func() {
svrErr <- http.ListenAndServe(addr, nil)
}()
log.Print("ready")
err := <-svrErr
log.Print(err)
} }
} }