2021-02-05 23:49:47 +01:00
|
|
|
package main
|
|
|
|
|
2021-02-06 00:01:00 +01:00
|
|
|
import (
|
2021-02-06 16:17:35 +01:00
|
|
|
"fmt"
|
2021-02-06 00:01:00 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2022-02-25 22:53:38 +01:00
|
|
|
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/auth"
|
|
|
|
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/cfg"
|
|
|
|
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/collector/f2b"
|
|
|
|
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/collector/textfile"
|
2022-02-20 09:17:06 +01:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2021-02-06 00:01:00 +01:00
|
|
|
)
|
|
|
|
|
2021-09-21 10:34:23 +02:00
|
|
|
const (
|
|
|
|
metricsPath = "/metrics"
|
|
|
|
)
|
|
|
|
|
2021-02-06 12:45:46 +01:00
|
|
|
var (
|
2021-02-06 16:17:35 +01:00
|
|
|
version = "dev"
|
|
|
|
commit = "none"
|
|
|
|
date = "unknown"
|
|
|
|
builtBy = "unknown"
|
2021-02-06 00:01:00 +01:00
|
|
|
)
|
|
|
|
|
2021-02-06 16:17:35 +01:00
|
|
|
func printAppVersion() {
|
|
|
|
fmt.Println(version)
|
|
|
|
fmt.Printf(" build date: %s\r\n commit hash: %s\r\n built by: %s\r\n", date, commit, builtBy)
|
|
|
|
}
|
|
|
|
|
2021-09-25 23:23:28 +02:00
|
|
|
func rootHtmlHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, err := w.Write([]byte(
|
|
|
|
`<html>
|
|
|
|
<head><title>Fail2Ban Exporter</title></head>
|
|
|
|
<body>
|
|
|
|
<h1>Fail2Ban Exporter</h1>
|
|
|
|
<p><a href="` + metricsPath + `">Metrics</a></p>
|
|
|
|
</body>
|
|
|
|
</html>`))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error handling root url: %v", err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:32:08 +02:00
|
|
|
func metricHandler(w http.ResponseWriter, r *http.Request, collector *textfile.Collector) {
|
2021-10-12 22:38:26 +02:00
|
|
|
promhttp.Handler().ServeHTTP(w, r)
|
|
|
|
collector.WriteTextFileMetrics(w, r)
|
|
|
|
}
|
|
|
|
|
2021-02-05 23:49:47 +01:00
|
|
|
func main() {
|
2021-02-06 16:17:35 +01:00
|
|
|
appSettings := cfg.Parse()
|
|
|
|
if appSettings.VersionMode {
|
|
|
|
printAppVersion()
|
|
|
|
} else {
|
2022-02-20 09:17:06 +01:00
|
|
|
handleGracefulShutdown()
|
2022-02-19 12:21:58 +01:00
|
|
|
log.Printf("fail2ban exporter version %s", version)
|
2022-02-19 18:01:49 +01:00
|
|
|
log.Printf("starting server at %s", appSettings.MetricsAddress)
|
2021-02-06 00:01:00 +01:00
|
|
|
|
2021-10-13 22:33:49 +02:00
|
|
|
f2bCollector := f2b.NewExporter(appSettings, version)
|
|
|
|
prometheus.MustRegister(f2bCollector)
|
2021-02-06 00:01:00 +01:00
|
|
|
|
2021-10-18 20:32:08 +02:00
|
|
|
textFileCollector := textfile.NewCollector(appSettings)
|
2021-10-12 22:38:26 +02:00
|
|
|
prometheus.MustRegister(textFileCollector)
|
|
|
|
|
2022-01-14 22:36:49 +01:00
|
|
|
http.HandleFunc("/", auth.BasicAuthMiddleware(rootHtmlHandler, appSettings.BasicAuthProvider))
|
|
|
|
http.HandleFunc(metricsPath, auth.BasicAuthMiddleware(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
metricHandler(w, r, textFileCollector)
|
|
|
|
},
|
|
|
|
appSettings.BasicAuthProvider,
|
|
|
|
))
|
2021-09-21 10:34:23 +02:00
|
|
|
log.Printf("metrics available at '%s'", metricsPath)
|
2022-02-19 12:21:58 +01:00
|
|
|
if appSettings.BasicAuthProvider.Enabled() {
|
|
|
|
log.Printf("basic auth enabled")
|
|
|
|
}
|
2021-09-21 10:34:23 +02:00
|
|
|
|
|
|
|
svrErr := make(chan error)
|
|
|
|
go func() {
|
2022-02-19 18:01:49 +01:00
|
|
|
svrErr <- http.ListenAndServe(appSettings.MetricsAddress, nil)
|
2021-09-21 10:34:23 +02:00
|
|
|
}()
|
|
|
|
log.Print("ready")
|
|
|
|
|
|
|
|
err := <-svrErr
|
|
|
|
log.Print(err)
|
2021-02-06 16:17:35 +01:00
|
|
|
}
|
2021-02-05 23:49:47 +01:00
|
|
|
}
|
2022-02-20 09:17:06 +01:00
|
|
|
|
|
|
|
func handleGracefulShutdown() {
|
|
|
|
var signals = make(chan os.Signal)
|
|
|
|
|
|
|
|
signal.Notify(signals, syscall.SIGTERM)
|
|
|
|
signal.Notify(signals, syscall.SIGINT)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
sig := <-signals
|
|
|
|
log.Printf("caught signal: %+v", sig)
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
}
|