2021-02-05 22:49:47 +00:00
|
|
|
# Fail2Ban Prometheus Exporter
|
|
|
|
|
2022-02-20 16:44:27 +00:00
|
|
|
[![Pipeline](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/badges/main/pipeline.svg)](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter)
|
2023-06-23 20:42:46 +00:00
|
|
|
[![Go Report Card](https://goreportcard.com/badge/gitlab.com/hectorjsmith/fail2ban-prometheus-exporter)](https://goreportcard.com/report/gitlab.com/hectorjsmith/fail2ban-prometheus-exporter)
|
2021-02-09 21:45:19 +00:00
|
|
|
|
2022-02-20 16:44:27 +00:00
|
|
|
Collect metrics from a running fail2ban instance.
|
2021-08-31 12:44:58 +00:00
|
|
|
|
2022-02-20 16:44:27 +00:00
|
|
|
## Table of Contents
|
2023-09-04 19:38:16 +00:00
|
|
|
|
2022-02-20 16:44:27 +00:00
|
|
|
1. Quick Start
|
|
|
|
2. Metrics
|
|
|
|
3. Configuration
|
|
|
|
4. Building from source
|
|
|
|
5. Textfile metrics
|
2022-01-30 21:32:48 +00:00
|
|
|
|
2022-02-20 16:44:27 +00:00
|
|
|
## 1. Quick Start
|
2021-08-31 12:44:58 +00:00
|
|
|
|
2022-02-20 16:44:27 +00:00
|
|
|
The exporter can be run as a standalone binary or a docker container.
|
2022-01-30 21:32:48 +00:00
|
|
|
|
2022-02-20 16:44:27 +00:00
|
|
|
### 1.1. Standalone
|
2021-08-31 12:44:58 +00:00
|
|
|
|
2023-09-04 19:40:09 +00:00
|
|
|
The following command will start collecting metrics from the
|
|
|
|
`/var/run/fail2ban/fail2ban.sock` file and expose them on port `9191`.
|
2021-08-31 12:44:58 +00:00
|
|
|
|
2023-09-04 19:38:16 +00:00
|
|
|
```bash
|
2022-02-20 16:44:27 +00:00
|
|
|
$ fail2ban_exporter --collector.f2b.socket=/var/run/fail2ban/fail2ban.sock --web.listen-address=":9191"
|
2021-08-31 12:44:58 +00:00
|
|
|
|
2023-06-20 16:34:25 +00:00
|
|
|
2022/02/20 09:54:06 fail2ban exporter version 0.8.1
|
2022-02-20 16:44:27 +00:00
|
|
|
2022/02/20 09:54:06 starting server at :9191
|
|
|
|
2022/02/20 09:54:06 reading metrics from fail2ban socket: /var/run/fail2ban/fail2ban.sock
|
|
|
|
2022/02/20 09:54:06 metrics available at '/metrics'
|
|
|
|
2022/02/20 09:54:06 ready
|
2021-02-09 21:45:19 +00:00
|
|
|
```
|
|
|
|
|
2023-09-04 19:40:09 +00:00
|
|
|
Binary files for each release can be found on the
|
|
|
|
[releases](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/releases)
|
|
|
|
page.
|
2021-02-09 21:45:19 +00:00
|
|
|
|
2023-09-04 19:40:09 +00:00
|
|
|
There is also an [example systemd service
|
|
|
|
file](/_examples/systemd/fail2ban_exporter.service) included in the repository.
|
2023-06-22 14:22:31 +00:00
|
|
|
This is a starting point to run the exporter as a service.
|
|
|
|
|
2022-02-20 16:44:27 +00:00
|
|
|
### 1.2. Docker
|
2021-02-09 21:45:19 +00:00
|
|
|
|
2023-09-04 19:38:16 +00:00
|
|
|
#### Docker run
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ docker run -d \
|
2021-02-09 21:45:19 +00:00
|
|
|
--name "fail2ban-exporter" \
|
2021-08-31 12:44:58 +00:00
|
|
|
-v /var/run/fail2ban:/var/run/fail2ban:ro \
|
|
|
|
-p "9191:9191" \
|
2021-02-09 21:45:19 +00:00
|
|
|
registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter:latest
|
|
|
|
```
|
|
|
|
|
2023-09-04 19:38:16 +00:00
|
|
|
#### Docker compose
|
2021-02-09 21:45:19 +00:00
|
|
|
|
2023-09-04 19:38:16 +00:00
|
|
|
```yaml
|
2021-02-09 21:45:19 +00:00
|
|
|
version: "2"
|
|
|
|
services:
|
|
|
|
exporter:
|
2021-02-10 18:56:25 +00:00
|
|
|
image: registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter:latest
|
2021-02-09 21:45:19 +00:00
|
|
|
volumes:
|
2021-08-31 12:44:58 +00:00
|
|
|
- /var/run/fail2ban/:/var/run/fail2ban:ro
|
2021-02-09 21:45:19 +00:00
|
|
|
ports:
|
|
|
|
- "9191:9191"
|
|
|
|
```
|
|
|
|
|
2023-09-04 19:40:09 +00:00
|
|
|
Use the `:latest` tag to get the latest stable release. See the [registry
|
|
|
|
page](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/container_registry)
|
|
|
|
for all available tags.
|
2021-08-31 12:44:58 +00:00
|
|
|
|
2023-09-04 19:40:09 +00:00
|
|
|
**NOTE:** While it is possible to mount the `fail2ban.sock` file directly, it is
|
|
|
|
recommended to mount the parent folder instead. The `.sock` file is deleted by
|
|
|
|
fail2ban on shutdown and re-created on startup and this causes problems for the
|
|
|
|
docker mount. See [this
|
|
|
|
reply](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/issues/11#note_665003499)
|
|
|
|
for more details.
|
2021-08-31 12:44:58 +00:00
|
|
|
|
2022-02-20 16:44:27 +00:00
|
|
|
## 2. Metrics
|
2021-08-31 12:44:58 +00:00
|
|
|
|
2022-02-20 16:44:27 +00:00
|
|
|
The exporter exposes the following metrics:
|
2021-08-31 12:44:58 +00:00
|
|
|
|
2022-02-20 16:44:27 +00:00
|
|
|
*All metric names are prefixed with `f2b_`*
|
2021-02-09 21:45:19 +00:00
|
|
|
|
2022-02-20 16:44:27 +00:00
|
|
|
| Metric | Description | Example |
|
|
|
|
|------------------------------|------------------------------------------------------------------------------------|-----------------------------------------------------|
|
|
|
|
| `up` | Returns 1 if the exporter is up and running | `f2b_up 1` |
|
|
|
|
| `errors` | Count the number of errors since startup by type | |
|
|
|
|
| `errors{type="socket_conn"}` | Errors connecting to the fail2ban socket (e.g. connection refused) | `f2b_errors{type="socket_conn"} 0` |
|
|
|
|
| `errors{type="socket_req"}` | Errors sending requests to the fail2ban server (e.g. invalid responses) | `f2b_errors{type="socket_req"} 0` |
|
|
|
|
| `jail_count` | Number of jails configured in fail2ban | `f2b_jail_count 2` |
|
|
|
|
| `jail_banned_current` | Number of IPs currently banned per jail | `f2b_jail_banned_current{jail="sshd"} 15` |
|
|
|
|
| `jail_banned_total` | Total number of banned IPs since fail2ban startup per jail (includes expired bans) | `f2b_jail_banned_total{jail="sshd"} 31` |
|
|
|
|
| `jail_failed_current` | Number of current failures per jail | `f2b_jail_failed_current{jail="sshd"} 6` |
|
|
|
|
| `jail_failed_total` | Total number of failures since fail2ban startup per jail | `f2b_jail_failed_total{jail="sshd"} 125` |
|
|
|
|
| `jail_config_ban_time` | How long an IP is banned for in this jail (in seconds) | `f2b_config_jail_ban_time{jail="sshd"} 600` |
|
|
|
|
| `jail_config_find_time` | How far back the filter will look for failures in this jail (in seconds) | `f2b_config_jail_find_time{jail="sshd"} 600` |
|
|
|
|
| `jail_config_max_retry` | The max number of failures allowed before banning an IP in this jail | `f2b_config_jail_max_retries{jail="sshd"} 5` |
|
|
|
|
| `version` | Version string of the exporter and fail2ban | `f2b_version{exporter="0.5.0",fail2ban="0.11.1"} 1` |
|
2021-02-09 21:45:19 +00:00
|
|
|
|
2023-09-04 19:40:09 +00:00
|
|
|
The metrics above correspond to the matching fields in the `fail2ban-client
|
|
|
|
status <jail>` command:
|
2023-09-04 19:38:16 +00:00
|
|
|
|
|
|
|
```text
|
2022-02-20 16:44:27 +00:00
|
|
|
Status for the jail: sshd
|
|
|
|
|- Filter
|
2021-08-31 12:44:58 +00:00
|
|
|
| |- Currently failed: 6
|
|
|
|
| |- Total failed: 125
|
|
|
|
| `- File list: /var/log/auth.log
|
|
|
|
`- Actions
|
|
|
|
|- Currently banned: 15
|
|
|
|
|- Total banned: 31
|
|
|
|
`- Banned IP list: ...
|
2021-02-09 21:45:19 +00:00
|
|
|
```
|
|
|
|
|
2022-02-20 16:44:27 +00:00
|
|
|
### 2.1. Grafana
|
|
|
|
|
2023-09-04 19:40:09 +00:00
|
|
|
The metrics exported by this tool are compatible with Prometheus and Grafana. A
|
|
|
|
sample grafana dashboard can be found in the
|
|
|
|
[grafana.json](/_examples/grafana/dashboard.json) file. Just import the contents
|
|
|
|
of this file into a new Grafana dashboard to get started.
|
2022-02-20 16:44:27 +00:00
|
|
|
|
2023-09-04 19:40:09 +00:00
|
|
|
The dashboard supports displaying data from multiple exporters. Use the
|
|
|
|
`instance` dashboard variable to select which ones to display.
|
2022-10-13 08:36:43 +00:00
|
|
|
|
|
|
|
*(Sample dashboard is compatible with Grafana `9.1.8` and above)*
|
2022-02-20 16:44:27 +00:00
|
|
|
|
|
|
|
## 3. Configuration
|
|
|
|
|
|
|
|
The exporter is configured with CLI flags and environment variables.
|
|
|
|
There are no configuration files.
|
|
|
|
|
2023-09-04 19:38:16 +00:00
|
|
|
### CLI flags
|
|
|
|
|
|
|
|
```text
|
2023-06-20 20:16:23 +00:00
|
|
|
🚀 Collect prometheus metrics from a running Fail2Ban instance
|
2022-02-20 16:44:27 +00:00
|
|
|
|
|
|
|
Flags:
|
2023-06-20 20:16:23 +00:00
|
|
|
-h, --help Show context-sensitive help.
|
|
|
|
-v, --version Show version info and exit
|
2023-06-22 18:37:01 +00:00
|
|
|
--dry-run Attempt to connect to the fail2ban socket then exit
|
|
|
|
before starting the server
|
2023-06-20 20:16:23 +00:00
|
|
|
--web.listen-address=":9191" Address to use for the metrics server
|
|
|
|
($F2B_WEB_LISTEN_ADDRESS)
|
|
|
|
--collector.f2b.socket="/var/run/fail2ban/fail2ban.sock"
|
|
|
|
Path to the fail2ban server socket
|
|
|
|
($F2B_COLLECTOR_SOCKET)
|
|
|
|
--collector.f2b.exit-on-socket-connection-error
|
|
|
|
When set to true the exporter will immediately
|
|
|
|
exit on a fail2ban socket connection error
|
|
|
|
($F2B_EXIT_ON_SOCKET_CONN_ERROR)
|
|
|
|
--collector.textfile.directory=STRING
|
|
|
|
Directory to read text files with metrics from
|
|
|
|
($F2B_COLLECTOR_TEXT_PATH)
|
|
|
|
--web.basic-auth.username=STRING
|
|
|
|
Username to use to protect endpoints with basic auth
|
|
|
|
($F2B_WEB_BASICAUTH_USER)
|
|
|
|
--web.basic-auth.password=STRING
|
|
|
|
Password to use to protect endpoints with basic auth
|
|
|
|
($F2B_WEB_BASICAUTH_PASS)
|
2022-02-20 16:44:27 +00:00
|
|
|
```
|
|
|
|
|
2023-09-04 19:38:16 +00:00
|
|
|
### Environment variables
|
2022-02-20 16:44:27 +00:00
|
|
|
|
|
|
|
Each environment variable corresponds to a CLI flag.
|
|
|
|
If both are specified, the CLI flag takes precedence.
|
|
|
|
|
2022-06-19 07:20:45 +00:00
|
|
|
| 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` |
|
2022-02-20 16:44:27 +00:00
|
|
|
|
|
|
|
## 4. Building from source
|
|
|
|
|
2023-06-20 16:34:25 +00:00
|
|
|
Building from source has the following dependencies:
|
2023-09-04 19:38:16 +00:00
|
|
|
|
2023-06-20 16:34:25 +00:00
|
|
|
- Go v1.20
|
|
|
|
- Make
|
2022-02-20 16:44:27 +00:00
|
|
|
|
2023-06-20 16:34:25 +00:00
|
|
|
From there, simply run `make build`
|
|
|
|
|
2023-09-04 19:40:09 +00:00
|
|
|
This will download the necessary dependencies and build a `fail2ban_exporter`
|
|
|
|
binary in the root of the project.
|
2022-02-20 16:44:27 +00:00
|
|
|
|
|
|
|
## 5. Textfile metrics
|
2021-10-12 20:38:26 +00:00
|
|
|
|
2023-09-04 19:40:09 +00:00
|
|
|
For more flexibility the exporter also allows exporting metrics collected from a
|
|
|
|
text file.
|
2021-10-12 20:38:26 +00:00
|
|
|
|
2023-09-04 19:40:09 +00:00
|
|
|
To enable textfile metrics provide the directory to read files from with the
|
|
|
|
`--collector.textfile.directory` flag.
|
2021-10-12 20:38:26 +00:00
|
|
|
|
2023-09-04 19:40:09 +00:00
|
|
|
Metrics collected from these files will be exposed directly alongside the other
|
|
|
|
metrics without any additional processing. This means that it is the
|
|
|
|
responsibility of the file creator to ensure the format is correct.
|
2021-10-12 20:38:26 +00:00
|
|
|
|
2023-09-04 19:40:09 +00:00
|
|
|
By exporting textfile metrics an extra metric is also exported with an error
|
|
|
|
count for each file:
|
2021-10-12 20:38:26 +00:00
|
|
|
|
2023-09-04 19:38:16 +00:00
|
|
|
```text
|
2021-10-12 20:38:26 +00:00
|
|
|
# HELP textfile_error Checks for errors while reading text files
|
|
|
|
# TYPE textfile_error gauge
|
|
|
|
textfile_error{path="file.prom"} 0
|
|
|
|
```
|
|
|
|
|
|
|
|
**NOTE:** Any file not ending with `.prom` will be ignored.
|
2022-02-19 14:10:36 +00:00
|
|
|
|
2023-09-04 19:38:16 +00:00
|
|
|
### Running in Docker
|
2022-02-19 14:10:36 +00:00
|
|
|
|
2023-09-04 19:40:09 +00:00
|
|
|
To collect textfile metrics inside a docker container, a couple of things need
|
|
|
|
to be done:
|
2023-09-04 19:38:16 +00:00
|
|
|
|
2022-02-19 17:01:49 +00:00
|
|
|
1. Mount the folder with the metrics files
|
|
|
|
2. Set the `F2B_COLLECTOR_TEXT_PATH` environment variable
|
2022-02-19 14:10:36 +00:00
|
|
|
|
|
|
|
*For example:*
|
2023-09-04 19:38:16 +00:00
|
|
|
|
|
|
|
```bash
|
|
|
|
$ docker run -d \
|
2022-02-19 14:10:36 +00:00
|
|
|
--name "fail2ban-exporter" \
|
|
|
|
-v /var/run/fail2ban:/var/run/fail2ban:ro \
|
|
|
|
-v /path/to/metrics:/app/metrics/:ro \
|
|
|
|
-e F2B_COLLECTOR_TEXT_PATH=/app/metrics \
|
|
|
|
-p "9191:9191" \
|
|
|
|
registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter:latest
|
|
|
|
```
|