From 0d9e162433d54e72dfd6b19dd11c4da6715bc077 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ji=C5=99=C3=AD=20Prokop?= <jprokop.ofc@gmail.com>
Date: Tue, 15 Aug 2023 13:02:53 +0200
Subject: [PATCH] feat: check_php_syntax

---
 nagios/php_syntax_check.sh                   | 25 --------
 perun/proxy/utils/nagios/check_php_syntax.py | 66 ++++++++++++++++++++
 setup.py                                     |  1 +
 3 files changed, 67 insertions(+), 25 deletions(-)
 delete mode 100755 nagios/php_syntax_check.sh
 create mode 100755 perun/proxy/utils/nagios/check_php_syntax.py

diff --git a/nagios/php_syntax_check.sh b/nagios/php_syntax_check.sh
deleted file mode 100755
index b6dd7d2..0000000
--- a/nagios/php_syntax_check.sh
+++ /dev/null
@@ -1,25 +0,0 @@
-#!/bin/bash
-
-#The root directory to check
-dir="/etc/simplesamlphp"
-
-cd $dir
-
-paths=$(find . -type f -name "*.php")
-globalResult=""
-
-for path in $paths
-do
-    if [[ -f $path ]] ; then
-        result=$(php -l $path 2>&1)
-        if [[ ! $result =~ ^No.syntax.errors.*$ ]] ; then
-            globalResult+="$result  |  "
-        fi
-    fi
-done
-
-if [[ -z $globalResult ]] ; then
-    echo "0 php_syntax_check - OK"
-else
-    echo "2 php_syntax_check - $globalResult"
-fi
diff --git a/perun/proxy/utils/nagios/check_php_syntax.py b/perun/proxy/utils/nagios/check_php_syntax.py
new file mode 100755
index 0000000..56d0efe
--- /dev/null
+++ b/perun/proxy/utils/nagios/check_php_syntax.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python3
+
+import os
+import subprocess
+import sys
+import argparse
+
+
+# nagios return codes
+UNKNOWN = -1
+OK = 0
+WARNING = 1
+CRITICAL = 2
+
+
+def get_args():
+    parser = argparse.ArgumentParser(
+        description=(
+            "Checks whether PHP files have valid syntax, primarily used for checking"
+            " automatically generated files"
+        )
+    )
+    parser.add_argument(
+        "-d",
+        "--directory",
+        required=True,
+        help="path which will be scanned for PHP files, including subdirectories",
+    )
+    return parser.parse_args()
+
+
+def main():
+    dir = get_args().directory
+    os.chdir(dir)
+    paths = []
+    for (
+        dirpath,
+        dirname,
+        filenames,
+    ) in os.walk("."):
+        for f in filenames:
+            if f.endswith(".php"):
+                paths.append(
+                    os.path.join(
+                        dirpath,
+                        f,
+                    )
+                )
+    global_result = ""
+
+    for path in paths:
+        if os.path.isfile(path):
+            result = subprocess.getoutput(f"php -l {path}")
+            if not result.startswith("No syntax errors"):
+                global_result += f"{result}  |  "
+
+    if not global_result:
+        print(f"{OK} check_php_syntax - OK")
+        sys.exit(OK)
+    else:
+        print(f"{CRITICAL} check_php_syntax - {global_result}")
+        sys.exit(CRITICAL)
+
+
+if __name__ == "__main__":
+    main()
diff --git a/setup.py b/setup.py
index 7339cde..7bf8c55 100644
--- a/setup.py
+++ b/setup.py
@@ -37,6 +37,7 @@ setuptools.setup(
             "check_rpc_status=perun.proxy.utils.nagios.check_rpc_status:main",
             "check_saml=perun.proxy.utils.nagios.check_saml:main",
             "check_user_logins=perun.proxy.utils.nagios.check_user_logins:main",
+            "check_php_syntax=perun.proxy.utils.nagios.check_php_syntax:main",
             "check_webserver_availability="
             "perun.proxy.utils.nagios.webserver_availability:main",
             "metadata_expiration=perun.proxy.utils.metadata_expiration:main",
-- 
GitLab