feat: add new dry run mode (!98)
* Add a new *dry-run* mode to exit just before running the server * This allows testing that the socket is working before starting the server https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/merge_requests/98
This commit is contained in:
parent
5275d280be
commit
9c1a10e309
@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
var cliStruct struct {
|
var cliStruct struct {
|
||||||
VersionMode bool `name:"version" short:"v" help:"Show version info and exit"`
|
VersionMode bool `name:"version" short:"v" help:"Show version info and exit"`
|
||||||
|
DryRunMode bool `name:"dry-run" help:"Attempt to connect to the fail2ban socket then exit before starting the server"`
|
||||||
ServerAddress string `name:"web.listen-address" env:"F2B_WEB_LISTEN_ADDRESS" help:"Address to use for the metrics server" default:"${default_address}"`
|
ServerAddress string `name:"web.listen-address" env:"F2B_WEB_LISTEN_ADDRESS" help:"Address to use for the metrics server" default:"${default_address}"`
|
||||||
F2bSocketPath string `name:"collector.f2b.socket" env:"F2B_COLLECTOR_SOCKET" help:"Path to the fail2ban server socket" default:"${default_socket}"`
|
F2bSocketPath string `name:"collector.f2b.socket" env:"F2B_COLLECTOR_SOCKET" help:"Path to the fail2ban server socket" default:"${default_socket}"`
|
||||||
ExitOnSocketError bool `name:"collector.f2b.exit-on-socket-connection-error" env:"F2B_EXIT_ON_SOCKET_CONN_ERROR" help:"When set to true the exporter will immediately exit on a fail2ban socket connection error"`
|
ExitOnSocketError bool `name:"collector.f2b.exit-on-socket-connection-error" env:"F2B_EXIT_ON_SOCKET_CONN_ERROR" help:"When set to true the exporter will immediately exit on a fail2ban socket connection error"`
|
||||||
@ -34,6 +35,7 @@ func Parse() *AppSettings {
|
|||||||
validateFlags(ctx)
|
validateFlags(ctx)
|
||||||
settings := &AppSettings{
|
settings := &AppSettings{
|
||||||
VersionMode: cliStruct.VersionMode,
|
VersionMode: cliStruct.VersionMode,
|
||||||
|
DryRunMode: cliStruct.DryRunMode,
|
||||||
MetricsAddress: cliStruct.ServerAddress,
|
MetricsAddress: cliStruct.ServerAddress,
|
||||||
Fail2BanSocketPath: cliStruct.F2bSocketPath,
|
Fail2BanSocketPath: cliStruct.F2bSocketPath,
|
||||||
FileCollectorPath: cliStruct.TextFileExporterPath,
|
FileCollectorPath: cliStruct.TextFileExporterPath,
|
||||||
|
@ -4,6 +4,7 @@ import "gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/auth"
|
|||||||
|
|
||||||
type AppSettings struct {
|
type AppSettings struct {
|
||||||
VersionMode bool
|
VersionMode bool
|
||||||
|
DryRunMode bool
|
||||||
MetricsAddress string
|
MetricsAddress string
|
||||||
Fail2BanSocketPath string
|
Fail2BanSocketPath string
|
||||||
FileCollectorPath string
|
FileCollectorPath string
|
||||||
|
@ -30,7 +30,9 @@ func main() {
|
|||||||
appSettings := cfg.Parse()
|
appSettings := cfg.Parse()
|
||||||
if appSettings.VersionMode {
|
if appSettings.VersionMode {
|
||||||
printAppVersion()
|
printAppVersion()
|
||||||
} else {
|
return
|
||||||
|
}
|
||||||
|
|
||||||
handleGracefulShutdown()
|
handleGracefulShutdown()
|
||||||
log.Printf("fail2ban exporter version %s", version)
|
log.Printf("fail2ban exporter version %s", version)
|
||||||
log.Printf("starting server at %s", appSettings.MetricsAddress)
|
log.Printf("starting server at %s", appSettings.MetricsAddress)
|
||||||
@ -41,9 +43,12 @@ func main() {
|
|||||||
textFileCollector := textfile.NewCollector(appSettings)
|
textFileCollector := textfile.NewCollector(appSettings)
|
||||||
prometheus.MustRegister(textFileCollector)
|
prometheus.MustRegister(textFileCollector)
|
||||||
|
|
||||||
|
if !appSettings.DryRunMode {
|
||||||
svrErr := server.StartServer(appSettings, textFileCollector)
|
svrErr := server.StartServer(appSettings, textFileCollector)
|
||||||
err := <-svrErr
|
err := <-svrErr
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
} else {
|
||||||
|
log.Print("running in dry-run mode - exiting")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user