Skip to content
Snippets Groups Projects
Verified Commit 0d9e1624 authored by Jiří Prokop's avatar Jiří Prokop
Browse files

feat: check_php_syntax

parent e77cbc09
No related branches found
No related tags found
1 merge request!39feat: check_php_syntax .sh -> .py
Pipeline #310893 passed
#!/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
#!/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()
...@@ -37,6 +37,7 @@ setuptools.setup( ...@@ -37,6 +37,7 @@ setuptools.setup(
"check_rpc_status=perun.proxy.utils.nagios.check_rpc_status:main", "check_rpc_status=perun.proxy.utils.nagios.check_rpc_status:main",
"check_saml=perun.proxy.utils.nagios.check_saml:main", "check_saml=perun.proxy.utils.nagios.check_saml:main",
"check_user_logins=perun.proxy.utils.nagios.check_user_logins: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=" "check_webserver_availability="
"perun.proxy.utils.nagios.webserver_availability:main", "perun.proxy.utils.nagios.webserver_availability:main",
"metadata_expiration=perun.proxy.utils.metadata_expiration:main", "metadata_expiration=perun.proxy.utils.metadata_expiration:main",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment