Merge branch '21-exit-exporter-when-fail2ban-is-restarted' into 'main'
Exit exporter when fail2ban is restarted Closes #21 See merge request hectorjsmith/fail2ban-prometheus-exporter!69
This commit is contained in:
commit
77b69da93c
3 changed files with 35 additions and 18 deletions
19
README.md
19
README.md
|
@ -112,7 +112,7 @@ There are no configuration files.
|
|||
|
||||
**CLI flags**
|
||||
```
|
||||
usage: fail2ban_exporter [<flags>]
|
||||
usage: exporter [<flags>]
|
||||
|
||||
Flags:
|
||||
-h, --help Show context-sensitive help (also try --help-long and --help-man).
|
||||
|
@ -127,6 +127,8 @@ Flags:
|
|||
username to use to protect endpoints with basic auth
|
||||
--web.basic-auth.password=""
|
||||
password to use to protect endpoints with basic auth
|
||||
--collector.f2b.exit-on-socket-connection-error
|
||||
when set to true the exporter will immediately exit on a fail2ban socket connection error
|
||||
```
|
||||
|
||||
**Environment variables**
|
||||
|
@ -134,13 +136,14 @@ Flags:
|
|||
Each environment variable corresponds to a CLI flag.
|
||||
If both are specified, the CLI flag takes precedence.
|
||||
|
||||
| Environment variable | Corresponding CLI flag |
|
||||
|---------------------------|----------------------------------|
|
||||
| `F2B_COLLECTOR_SOCKET` | `--collector.f2b.socket` |
|
||||
| `F2B_COLLECTOR_TEXT_PATH` | `--collector.textfile.directory` |
|
||||
| `F2B_WEB_LISTEN_ADDRESS` | `--web.listen-address` |
|
||||
| `F2B_WEB_BASICAUTH_USER` | `--web.basic-auth.username` |
|
||||
| `F2B_WEB_BASICAUTH_PASS` | `--web.basic-auth.password` |
|
||||
| Environment variable | Corresponding CLI flag |
|
||||
|---------------------------------|---------------------------------------------------|
|
||||
| `F2B_COLLECTOR_SOCKET` | `--collector.f2b.socket` |
|
||||
| `F2B_COLLECTOR_TEXT_PATH` | `--collector.textfile.directory` |
|
||||
| `F2B_WEB_LISTEN_ADDRESS` | `--web.listen-address` |
|
||||
| `F2B_WEB_BASICAUTH_USER` | `--web.basic-auth.username` |
|
||||
| `F2B_WEB_BASICAUTH_PASS` | `--web.basic-auth.password` |
|
||||
| `F2B_EXIT_ON_SOCKET_CONN_ERROR` | `--collector.f2b.exit-on-socket-connection-error` |
|
||||
|
||||
## 4. Building from source
|
||||
|
||||
|
|
|
@ -7,19 +7,21 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
socketEnvName = "F2B_COLLECTOR_SOCKET"
|
||||
fileCollectorPathEnvName = "F2B_COLLECTOR_TEXT_PATH"
|
||||
addressEnvName = "F2B_WEB_LISTEN_ADDRESS"
|
||||
basicAuthUserEnvName = "F2B_WEB_BASICAUTH_USER"
|
||||
basicAuthPassEnvName = "F2B_WEB_BASICAUTH_PASS"
|
||||
socketEnvName = "F2B_COLLECTOR_SOCKET"
|
||||
fileCollectorPathEnvName = "F2B_COLLECTOR_TEXT_PATH"
|
||||
addressEnvName = "F2B_WEB_LISTEN_ADDRESS"
|
||||
basicAuthUserEnvName = "F2B_WEB_BASICAUTH_USER"
|
||||
basicAuthPassEnvName = "F2B_WEB_BASICAUTH_PASS"
|
||||
exitOnSocketConnErrorEnvName = "F2B_EXIT_ON_SOCKET_CONN_ERROR"
|
||||
)
|
||||
|
||||
type AppSettings struct {
|
||||
VersionMode bool
|
||||
MetricsAddress string
|
||||
Fail2BanSocketPath string
|
||||
FileCollectorPath string
|
||||
BasicAuthProvider *hashedBasicAuth
|
||||
VersionMode bool
|
||||
MetricsAddress string
|
||||
Fail2BanSocketPath string
|
||||
FileCollectorPath string
|
||||
BasicAuthProvider *hashedBasicAuth
|
||||
ExitOnSocketConnError bool
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
@ -64,6 +66,11 @@ func readParamsFromCli(settings *AppSettings) {
|
|||
Default("").
|
||||
Envar(basicAuthPassEnvName).
|
||||
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()
|
||||
|
||||
|
@ -72,6 +79,7 @@ func readParamsFromCli(settings *AppSettings) {
|
|||
settings.Fail2BanSocketPath = *socketPath
|
||||
settings.FileCollectorPath = *fileCollectorPath
|
||||
settings.setBasicAuthValues(*rawBasicAuthUsername, *rawBasicAuthPassword)
|
||||
settings.ExitOnSocketConnError = *rawExitOnSocketConnError
|
||||
}
|
||||
|
||||
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/socket"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Collector struct {
|
||||
|
@ -13,6 +14,7 @@ type Collector struct {
|
|||
lastError error
|
||||
socketConnectionErrorCount int
|
||||
socketRequestErrorCount int
|
||||
exitOnSocketConnError bool
|
||||
}
|
||||
|
||||
func NewExporter(appSettings *cfg.AppSettings, exporterVersion string) *Collector {
|
||||
|
@ -23,6 +25,7 @@ func NewExporter(appSettings *cfg.AppSettings, exporterVersion string) *Collecto
|
|||
lastError: nil,
|
||||
socketConnectionErrorCount: 0,
|
||||
socketRequestErrorCount: 0,
|
||||
exitOnSocketConnError: appSettings.ExitOnSocketConnError,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,6 +44,9 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
|
|||
if err != nil {
|
||||
log.Printf("error opening socket: %v", err)
|
||||
c.socketConnectionErrorCount++
|
||||
if c.exitOnSocketConnError {
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
defer s.Close()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue