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 #!/usr/bin/env python3
""" """
Criticality: Low Criticality: Low
Checks the current state of the fail2ban service. Checks the current state of the fail2ban service.
""" """
from pathlib import Path from pathlib import Path
import subprocess
import sys import sys
sys.path.append(str(Path(__file__).parents[1].resolve())) sys.path.append(str(Path(__file__).parents[1].resolve()))
# pylint: disable=wrong-import-position # pylint: disable=wrong-import-position
...@@ -45,7 +46,9 @@ def get_service_state(name: str) -> tuple: ...@@ -45,7 +46,9 @@ def get_service_state(name: str) -> tuple:
else: else:
u.log("Using dbus to get current state.") u.log("Using dbus to get current state.")
bus = dbus.SystemBus() 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") manager = dbus.Interface(systemd, "org.freedesktop.systemd1.Manager")
unit = manager.LoadUnit("{}.service".format(name)) unit = manager.LoadUnit("{}.service".format(name))
proxy = bus.get_object("org.freedesktop.systemd1", str(unit)) proxy = bus.get_object("org.freedesktop.systemd1", str(unit))
...@@ -120,6 +123,10 @@ def check_jail_banned(name: str) -> int: ...@@ -120,6 +123,10 @@ def check_jail_banned(name: str) -> int:
def main(): def main():
"""Run all checks and exits with corresponding exit code.""" """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 # init
errors = 0 errors = 0
warnings = 0 warnings = 0
...@@ -129,7 +136,7 @@ def main(): ...@@ -129,7 +136,7 @@ def main():
u.warning("fail2ban is not running") u.warning("fail2ban is not running")
warnings += 1 warnings += 1
# warning exit if not running # warning exit if not running
sys.exit(3) exit(3)
else: else:
u.success("fail2ban is running") u.success("fail2ban is running")
...@@ -139,15 +146,14 @@ def main(): ...@@ -139,15 +146,14 @@ def main():
u.info("{} jail is running".format(jail)) u.info("{} jail is running".format(jail))
banned = check_jail_banned(jail) banned = check_jail_banned(jail)
if banned > 0: if banned > 0:
u.warning("there is {} banned host in {} jail".format(banned, jail)) u.info("there is {} banned host in {} jail".format(banned, jail))
warnings += 1
if errors: if errors:
sys.exit(1) exit(1)
elif warnings: elif warnings:
sys.exit(3) exit(3)
else:
sys.exit(0) exit(0)
if __name__ == "__main__": 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