2021-02-05 22:49:47 +00:00
|
|
|
package main
|
|
|
|
|
2021-02-05 23:01:00 +00:00
|
|
|
import (
|
2021-02-06 15:17:35 +00:00
|
|
|
"fmt"
|
2022-02-20 08:17:06 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2023-06-21 10:31:33 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/cfg"
|
|
|
|
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/collector/f2b"
|
|
|
|
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/collector/textfile"
|
|
|
|
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/server"
|
2021-02-05 23:01:00 +00:00
|
|
|
)
|
|
|
|
|
2021-09-21 08:34:23 +00:00
|
|
|
const (
|
|
|
|
metricsPath = "/metrics"
|
|
|
|
)
|
|
|
|
|
2021-02-06 11:45:46 +00:00
|
|
|
var (
|
2021-02-06 15:17:35 +00:00
|
|
|
version = "dev"
|
|
|
|
commit = "none"
|
|
|
|
date = "unknown"
|
|
|
|
builtBy = "unknown"
|
2021-02-05 23:01:00 +00:00
|
|
|
)
|
|
|
|
|
2021-02-06 15:17:35 +00: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 21:23:28 +00: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 18:32:08 +00:00
|
|
|
func metricHandler(w http.ResponseWriter, r *http.Request, collector *textfile.Collector) {
|
2021-10-12 20:38:26 +00:00
|
|
|
promhttp.Handler().ServeHTTP(w, r)
|
|
|
|
collector.WriteTextFileMetrics(w, r)
|
|
|
|
}
|
|
|
|
|
2021-02-05 22:49:47 +00:00
|
|
|
func main() {
|
2021-02-06 15:17:35 +00:00
|
|
|
appSettings := cfg.Parse()
|
|
|
|
if appSettings.VersionMode {
|
|
|
|
printAppVersion()
|
|
|
|
} else {
|
2022-02-20 08:17:06 +00:00
|
|
|
handleGracefulShutdown()
|
2022-02-19 11:21:58 +00:00
|
|
|
log.Printf("fail2ban exporter version %s", version)
|
2022-02-19 17:01:49 +00:00
|
|
|
log.Printf("starting server at %s", appSettings.MetricsAddress)
|
2021-02-05 23:01:00 +00:00
|
|
|
|
2021-10-13 20:33:49 +00:00
|
|
|
f2bCollector := f2b.NewExporter(appSettings, version)
|
|
|
|
prometheus.MustRegister(f2bCollector)
|
2021-02-05 23:01:00 +00:00
|
|
|
|
2021-10-18 18:32:08 +00:00
|
|
|
textFileCollector := textfile.NewCollector(appSettings)
|
2021-10-12 20:38:26 +00:00
|
|
|
prometheus.MustRegister(textFileCollector)
|
|
|
|
|
2023-06-21 10:31:33 +00:00
|
|
|
http.HandleFunc("/", server.BasicAuthMiddleware(rootHtmlHandler, appSettings.AuthProvider))
|
|
|
|
http.HandleFunc(metricsPath, server.BasicAuthMiddleware(
|
2022-01-14 21:36:49 +00:00
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
metricHandler(w, r, textFileCollector)
|
|
|
|
},
|
2023-06-21 10:31:33 +00:00
|
|
|
appSettings.AuthProvider,
|
2022-01-14 21:36:49 +00:00
|
|
|
))
|
2021-09-21 08:34:23 +00:00
|
|
|
log.Printf("metrics available at '%s'", metricsPath)
|
|
|
|
|
|
|
|
svrErr := make(chan error)
|
|
|
|
go func() {
|
2022-02-19 17:01:49 +00:00
|
|
|
svrErr <- http.ListenAndServe(appSettings.MetricsAddress, nil)
|
2021-09-21 08:34:23 +00:00
|
|
|
}()
|
|
|
|
log.Print("ready")
|
|
|
|
|
|
|
|
err := <-svrErr
|
2022-09-18 20:26:22 +00:00
|
|
|
log.Fatal(err)
|
2021-02-06 15:17:35 +00:00
|
|
|
}
|
2021-02-05 22:49:47 +00:00
|
|
|
}
|
2022-02-20 08:17:06 +00: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)
|
|
|
|
}()
|
|
|
|
}
|