Imagine your server is an office building, and Fail2ban is the security guard standing at the front door. Anyone who enters the wrong password more than 5 times gets their badge revoked and is escorted out immediately. That sounds reassuring, except for one blind spot: there are no security monitors at the reception desk. You won’t know whether last night only 2 employees forgot their passwords, or a botnet of 500 IPs took turns trying to pick the SSH lock.
Previously, whenever I wanted to check, I had to SSH into each VPS and run commands to inspect the logs. Now, I just open a single browser tab and see the whole picture. If your infrastructure already has Prometheus and Grafana, adding observability to Fail2ban takes only about 5 minutes.
Quick start: Set up Fail2ban monitoring in 5 minutes
The workflow consists of 3 lightweight steps: run an exporter to read data from Fail2ban, configure Prometheus to scrape it, and build charts in Grafana.
Step 1: Run Fail2ban Exporter with Docker
On the server you want to monitor, run the exporter container and mount the Fail2ban socket file so it can read the status of currently banned IPs:
docker run -d \
--name fail2ban-exporter \
--restart unless-stopped \
-p 9191:9191 \
-v /var/run/fail2ban/fail2ban.sock:/var/run/fail2ban/fail2ban.sock:ro \
registry.gitlab.com/hctrdev/fail2ban-prometheus-exporter:latest
Once that’s done, test port 9191 using curl:
curl -s http://localhost:9191/metrics | grep fail2ban_
If you see lines like fail2ban_banned_current{jail="sshd"} 3, the exporter is successfully reading data.
Step 2: Connect to Prometheus
Switch to your Prometheus server, open prometheus.yml, and add a new job pointing to the internal IP of the server where you just installed the exporter:
scrape_configs:
- job_name: 'fail2ban'
scrape_interval: 15s
static_configs:
- targets: ['192.168.1.50:9191']
Next, reload the Prometheus configuration (note that Prometheus needs the --web.enable-lifecycle flag enabled; otherwise, you can send a SIGHUP signal or restart the service):
curl -X POST http://localhost:9090/-/reload
Step 3: Display charts on Grafana
Go to Grafana, create a new Panel with Prometheus as the data source, and paste the following query into the Metrics field:
sum by (jail, instance) (fail2ban_banned_current)
The chart will immediately display the number of currently banned IPs per service, such as sshd or nginx-http-auth, updating continuously every 15 seconds.
Why combine all three tools?
Many newcomers wonder why not just read the logs directly for speed. The answer lies in how each component divides the work:
- Fail2ban (The security guard): Does a great job blocking bad actors, but logs everything locally on each machine. Managing 10 VPSs means having 10 scattered notebooks, making it impossible to see the big picture at once.
- Fail2ban Exporter (The translator): Prometheus cannot read Fail2ban’s socket file directly. The exporter reads
fail2ban.sockand exposes standardized metrics over HTTP on port 9191. - Prometheus (The time-series database): Every 15 seconds, Prometheus scrapes metrics from your servers and stores their historical trends over time.
- Grafana (The dashboard): Pulls data from Prometheus to build line graphs, summary tables, and send alerts when anomalies occur.
3 core metrics to monitor
The exporter’s /metrics endpoint returns quite a few parameters, but early on you only need to focus on these 3 metrics:
fail2ban_banned_current: The number of IPs currently banned in each jail (for example:sshdis currently blocking 12 IPs).fail2ban_failed_current: The number of failed login attempts accumulating before reaching the ban threshold. Tracking this helps you spot early signs of password guessing.fail2ban_up: The connection status to the Fail2ban daemon (1means normal,0means the exporter cannot reach the socket or the service is down).
Detecting Brute-force attacks and configuring alerts
Having 3 to 5 IPs blocked per day on a public-facing server is completely normal—automated port-scanning bots roam the internet 24/7. What you need to catch is the moment a chart spikes abnormally, signaling a concentrated brute-force wave.
Use PromQL to catch sudden spikes
Instead of looking only at the current total, measure the rate of change over a short time window. Because fail2ban_failed_current is a Gauge (it can go up and then drop when findtime expires), you should use delta() instead of increase() to avoid inaccuracies:
sum by (instance, jail) (delta(fail2ban_failed_current[5m])) > 30
Additionally, you can set an upper threshold for the total number of IPs blocked simultaneously on a single server:
sum by (instance) (fail2ban_banned_current) > 20
Set up Alert Rules in Prometheus
Once you have the right queries, simply add them to your Prometheus rules file so the system automatically notifies you via Slack or Telegram when an attack lasts longer than 2 minutes:
groups:
- name: fail2ban_alerts
rules:
- alert: HighBruteForceActivity
expr: sum by (instance, jail) (fail2ban_banned_current) > 20
for: 2m
labels:
severity: warning
annotations:
summary: "Brute-force attack detected on {{ $labels.instance }}"
description: "Jail {{ $labels.jail }} is currently blocking {{ $value }} IPs simultaneously."
- alert: Fail2banServiceDown
expr: fail2ban_up == 0
for: 1m
labels:
severity: critical
annotations:
summary: "Fail2ban on {{ $labels.instance }} has stopped responding!"
description: "Exporter could not connect to the Fail2ban socket for more than 1 minute."
The second rule (Fail2banServiceDown) is especially useful after OS upgrades, helping you immediately catch cases where the Fail2ban service accidentally fails to restart.
Practical tips from real-world operations
Deploying this across multiple server clusters has taught me 4 small lessons to save you debugging time:
- Socket permission errors: Running the exporter directly on the host (without Docker) as a non-root user easily triggers a
permission deniederror on/var/run/fail2ban/fail2ban.sock. The cleanest fix is to add that user to a group with socket read access, or adjust the socket permissions in/etc/fail2ban/fail2ban.conf. - Keep scrape_interval at 15s–30s: This interval is smooth enough to track attacks. Forcing it down to 1–2 seconds just makes the exporter hammer the socket unnecessarily.
- Separate key jails on your dashboard: Don’t lump everything into a single line chart. Separating
sshdandnginx-http-authimmediately tells you whether attackers are targeting the infrastructure layer or the web application. - Combine with log inspection during major alerts: A dashboard only tells you something is happening; to find the exact cause, you still need to check the server. When you see hundreds of IPs banned at once, run
fail2ban-client status sshdor inspect/var/log/auth.log. Knowing whether they are tryingroot,admin, orubuntuhelps you tighten your SSH configuration in time.

