feat: option to exit on socket conn error (#21)
Add a new startup option to exit the exporter when an error occurs when connecting to the fail2ban socket file. This option is set to "false" by default.
This commit is contained in:
parent
951ceccf67
commit
eb04cc7845
@ -12,6 +12,7 @@ const (
|
|||||||
addressEnvName = "F2B_WEB_LISTEN_ADDRESS"
|
addressEnvName = "F2B_WEB_LISTEN_ADDRESS"
|
||||||
basicAuthUserEnvName = "F2B_WEB_BASICAUTH_USER"
|
basicAuthUserEnvName = "F2B_WEB_BASICAUTH_USER"
|
||||||
basicAuthPassEnvName = "F2B_WEB_BASICAUTH_PASS"
|
basicAuthPassEnvName = "F2B_WEB_BASICAUTH_PASS"
|
||||||
|
exitOnSocketConnErrorEnvName = "F2B_EXIT_ON_SOCKET_ERROR"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AppSettings struct {
|
type AppSettings struct {
|
||||||
@ -20,6 +21,7 @@ type AppSettings struct {
|
|||||||
Fail2BanSocketPath string
|
Fail2BanSocketPath string
|
||||||
FileCollectorPath string
|
FileCollectorPath string
|
||||||
BasicAuthProvider *hashedBasicAuth
|
BasicAuthProvider *hashedBasicAuth
|
||||||
|
ExitOnSocketConnError bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -64,6 +66,11 @@ func readParamsFromCli(settings *AppSettings) {
|
|||||||
Default("").
|
Default("").
|
||||||
Envar(basicAuthPassEnvName).
|
Envar(basicAuthPassEnvName).
|
||||||
String()
|
String()
|
||||||
|
rawExitOnSocketConnError := kingpin.
|
||||||
|
Flag("collector.f2b.exit-on-socket-connection-error", "when set to true the exporter will immediately exit on a fail2ban socket connection error").
|
||||||
|
Default("false").
|
||||||
|
Envar(exitOnSocketConnErrorEnvName).
|
||||||
|
Bool()
|
||||||
|
|
||||||
kingpin.Parse()
|
kingpin.Parse()
|
||||||
|
|
||||||
@ -72,6 +79,7 @@ func readParamsFromCli(settings *AppSettings) {
|
|||||||
settings.Fail2BanSocketPath = *socketPath
|
settings.Fail2BanSocketPath = *socketPath
|
||||||
settings.FileCollectorPath = *fileCollectorPath
|
settings.FileCollectorPath = *fileCollectorPath
|
||||||
settings.setBasicAuthValues(*rawBasicAuthUsername, *rawBasicAuthPassword)
|
settings.setBasicAuthValues(*rawBasicAuthUsername, *rawBasicAuthPassword)
|
||||||
|
settings.ExitOnSocketConnError = *rawExitOnSocketConnError
|
||||||
}
|
}
|
||||||
|
|
||||||
func (settings *AppSettings) setBasicAuthValues(rawUsername, rawPassword string) {
|
func (settings *AppSettings) setBasicAuthValues(rawUsername, rawPassword string) {
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/cfg"
|
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/cfg"
|
||||||
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/socket"
|
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/socket"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Collector struct {
|
type Collector struct {
|
||||||
@ -13,6 +14,7 @@ type Collector struct {
|
|||||||
lastError error
|
lastError error
|
||||||
socketConnectionErrorCount int
|
socketConnectionErrorCount int
|
||||||
socketRequestErrorCount int
|
socketRequestErrorCount int
|
||||||
|
exitOnSocketConnError bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewExporter(appSettings *cfg.AppSettings, exporterVersion string) *Collector {
|
func NewExporter(appSettings *cfg.AppSettings, exporterVersion string) *Collector {
|
||||||
@ -23,6 +25,7 @@ func NewExporter(appSettings *cfg.AppSettings, exporterVersion string) *Collecto
|
|||||||
lastError: nil,
|
lastError: nil,
|
||||||
socketConnectionErrorCount: 0,
|
socketConnectionErrorCount: 0,
|
||||||
socketRequestErrorCount: 0,
|
socketRequestErrorCount: 0,
|
||||||
|
exitOnSocketConnError: appSettings.ExitOnSocketConnError,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,6 +44,9 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error opening socket: %v", err)
|
log.Printf("error opening socket: %v", err)
|
||||||
c.socketConnectionErrorCount++
|
c.socketConnectionErrorCount++
|
||||||
|
if c.exitOnSocketConnError {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user