From d5e42ce26aa23849773649ddc1c5ac94e1575719 Mon Sep 17 00:00:00 2001
From: Nicolas KAROLAK <nicolas@karolak.fr>
Date: Tue, 18 Sep 2018 10:12:45 +0200
Subject: [PATCH] add(test_fail2ban): check currently banned

---
 tests/test_fail2ban.py | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/tests/test_fail2ban.py b/tests/test_fail2ban.py
index 0c652c0d..16f15db2 100755
--- a/tests/test_fail2ban.py
+++ b/tests/test_fail2ban.py
@@ -75,9 +75,23 @@ def get_jails() -> list:
     return jails
 
 
-def check_jail(name: str) -> int:
-    _, output = u.exec_cmd("fail2ban-client status {} | grep 'Jail list'".format(name))
-    print(output)
+def check_jail_banned(name: str) -> int:
+    """Check if there is currently banned hosts.
+
+    :param name: Jail name
+    :type name: str
+    :return: Number of banned hosts
+    :rtype: int
+    """
+
+    _, output = u.exec_cmd(
+        "fail2ban-client status {} | grep 'Currently banned'".format(name)
+    )
+    banned = output.split(":")[1].replace(" ", "")
+
+    if banned:
+        return banned
+
     return 0
 
 
@@ -95,7 +109,14 @@ def main():
     else:
         u.success("fail2ban is running")
 
-    # print("Checking fail2ban jails:")
+    print("Checking fail2ban jails:")
+    jails = get_jails()
+    for jail in jails:
+        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
 
     if errors:
         sys.exit(1)
-- 
GitLab