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