#!/bin/sh
# check_php v1.0.1  (c) 6.12.2022 by Andreas Ley  (u) 18.4.2024
# Check PHP FPM for availability

fqdn='www.kit.edu'
pathinfo='/phpinfo.php'

PATH="/usr/bin:/bin:/usr/sbin:/sbin"
export PATH

usage()
{
	echo "Usage: ${0##*/} [-v]" >&2
	echo "-v  verbose mode" >&2
	exit 1
}

trace=false
verbose=false
while getopts f:hxv OPTION; do
	case ${OPTION} in
		h)	sed '1d;s/^# *//;/^$/q' "$0" >&2; usage;;
		x)	set -x; trace=true;;
		v)	verbose=true;;
		f)	file="${OPTARG}";;
		?)	usage;;
	esac
done
_XPG=1 shift $((${OPTIND} - 1))
test $# -ne 0 && usage

if echo "GET ${pathinfo} HTTP/1.0\r\nHost: ${fqdn}\r\nUser-Agent: check_php/1.0.1\r\n\r" | openssl s_client -ign_eof -connect "${fqdn}:443" -servername "${fqdn}" 2>/dev/null | grep -q '503 Service Unavailable'; then
	unit="php$(phpquery -V)-fpm.service"
	systemctl reload "${unit}"
fi

exit 0
