File: //proc/thread-self/root/etc/zabbix/monitoring/check_fail2ban_banned.py
#!/usr/bin/env python3
import subprocess
import sys
def get_total_banned_ips(jail_name):
try:
command = f"sudo fail2ban-client status {jail_name}"
result = subprocess.run(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True
)
for line in result.stdout.split('\n'):
if "Total banned" in line:
return line.split(':')[1].strip()
return "0"
except Exception as e:
return "0"
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: check_fail2ban_banned.py <jail_name>")
sys.exit(1)
jail_name = sys.argv[1]
print(get_total_banned_ips(jail_name))