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"
|
2022-02-20 09:17:06 +01:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2023-06-21 12:31:33 +02:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"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-06 00:01:00 +01:00
|
|
|
)
|
|
|
|
|
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-02-05 23:49:47 +01:00
|
|
|
func main() {
|
2021-02-06 16:17:35 +01:00
|
|
|
appSettings := cfg.Parse()
|
|
|
|
if appSettings.VersionMode {
|
|
|
|
printAppVersion()
|
2023-06-22 18:09:36 +02:00
|
|
|
return
|
|
|
|
}
|
2021-02-06 00:01:00 +01:00
|
|
|
|
2023-06-22 18:09:36 +02:00
|
|
|
handleGracefulShutdown()
|
|
|
|
log.Printf("fail2ban exporter version %s", version)
|
|
|
|
log.Printf("starting server at %s", appSettings.MetricsAddress)
|
2021-02-06 00:01:00 +01:00
|
|
|
|
2023-06-22 18:09:36 +02:00
|
|
|
f2bCollector := f2b.NewExporter(appSettings, version)
|
|
|
|
prometheus.MustRegister(f2bCollector)
|
2021-10-12 22:38:26 +02:00
|
|
|
|
2023-06-22 18:09:36 +02:00
|
|
|
textFileCollector := textfile.NewCollector(appSettings)
|
|
|
|
prometheus.MustRegister(textFileCollector)
|
|
|
|
|
|
|
|
if !appSettings.DryRunMode {
|
2023-09-09 08:15:23 +02:00
|
|
|
svrErr := server.StartServer(appSettings, f2bCollector, textFileCollector)
|
2021-09-21 10:34:23 +02:00
|
|
|
err := <-svrErr
|
2022-09-18 22:26:22 +02:00
|
|
|
log.Fatal(err)
|
2023-06-22 18:09:36 +02:00
|
|
|
} else {
|
|
|
|
log.Print("running in dry-run mode - exiting")
|
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() {
|
2023-06-22 20:24:15 +02:00
|
|
|
var signals = make(chan os.Signal, 1)
|
2022-02-20 09:17:06 +01:00
|
|
|
|
|
|
|
signal.Notify(signals, syscall.SIGTERM)
|
|
|
|
signal.Notify(signals, syscall.SIGINT)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
sig := <-signals
|
|
|
|
log.Printf("caught signal: %+v", sig)
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
}
|