Skip to content
Snippets Groups Projects
Verified Commit adefcf59 authored by Nicolas KAROLAK's avatar Nicolas KAROLAK
Browse files

check that fail2ban is installed, disable warning on bans

parent ad0a97b7
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
"""
Criticality: Low
Checks the current state of the fail2ban service.
"""
from pathlib import Path
import subprocess
import sys
sys.path.append(str(Path(__file__).parents[1].resolve()))
# pylint: disable=wrong-import-position
......@@ -45,7 +46,9 @@ def get_service_state(name: str) -> tuple:
else:
u.log("Using dbus to get current state.")
bus = dbus.SystemBus()
systemd = bus.get_object("org.freedesktop.systemd1", "/org/freedesktop/systemd1")
systemd = bus.get_object(
"org.freedesktop.systemd1", "/org/freedesktop/systemd1"
)
manager = dbus.Interface(systemd, "org.freedesktop.systemd1.Manager")
unit = manager.LoadUnit("{}.service".format(name))
proxy = bus.get_object("org.freedesktop.systemd1", str(unit))
......@@ -120,6 +123,10 @@ def check_jail_banned(name: str) -> int:
def main():
"""Run all checks and exits with corresponding exit code."""
if subprocess.call(["which", "fail2ban-server"], stdout=subprocess.DEVNULL) != 0:
u.info("fail2ban not installed, skipping test")
exit(2)
# init
errors = 0
warnings = 0
......@@ -129,7 +136,7 @@ def main():
u.warning("fail2ban is not running")
warnings += 1
# warning exit if not running
sys.exit(3)
exit(3)
else:
u.success("fail2ban is running")
......@@ -139,15 +146,14 @@ def main():
u.info("{} jail is running".format(jail))
banned = check_jail_banned(jail)
if banned > 0:
u.warning("there is {} banned host in {} jail".format(banned, jail))
warnings += 1
u.info("there is {} banned host in {} jail".format(banned, jail))
if errors:
sys.exit(1)
exit(1)
elif warnings:
sys.exit(3)
else:
sys.exit(0)
exit(3)
exit(0)
if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment