ansible-roles/nagios-nrpe/files/plugins/check_phpfpm_multi
William Hirigoyen 1a034af944
All checks were successful
gitea/ansible-roles/pipeline/head This commit looks good
nagios-nrpe: Print pool config path in check_phpfpm_multi output
2022-12-30 10:45:09 +01:00

88 lines
2.5 KiB
Bash

#!/bin/bash
function detect_pool_dir() {
# Try to autodetect pool directory
php_version=$(php --version |head -n1 | sed -E 's/^PHP ([0-9]\.[0-9]).*/\1/g')
if [[ $php_version =~ ^5. ]]; then
echo "/etc/php5/fpm/pool.d/";
else
echo "/etc/php/$php_version/fpm/pool.d/";
fi
}
return=0
nb_crit=0
nb_warn=0
nb_ok=0
nb_unchk=0
output=""
readonly POOL_FOLDER=${1:-$(detect_pool_dir)}
if [[ ! -d "$POOL_FOLDER" ]]; then
echo "CRITICAL - $POOL_FOLDER does not exists"
exit 2
fi;
readonly POOL_FILES=$(find "$POOL_FOLDER" -name "*.conf")
for pool_file in $POOL_FILES; do
pool_name=$(grep "^\[" "$pool_file" | sed -E 's/^\[(.*)\].*$/\1/g')
pool_status_path=$(grep -E "^pm.status_path\s?=" "$pool_file" | sed -E "s/.*=\s?'?([^']*)'?\s?$/\1/g")
pool_listen=$(grep -E "^listen\s?=" "$pool_file" | sed -E 's/.*=\s?(.*)\s?$/\1/g')
pool_max_children=$(grep -E "^pm.max_children" "$pool_file" | sed -E 's/.*=\s?(.*)\s?$/\1/g' )
pool_crit_procs=$(expr $pool_max_children \* 85 / 100)
pool_warn_procs=$(expr $pool_max_children \* 55 / 100)
if [[ "$pool_status_path" == '' ]]; then
nb_unchk=$((nb_unchk + 1))
output="${output}UNCHK - ${pool_name} (missing pm.status_path definition)\n"
continue;
fi;
if [[ -S "$pool_listen" ]] || [[ ! "$pool_listen" =~ ':' ]]; then
target=(-H 127.0.0.1 --unix "$pool_listen")
else
target=(-H "$(echo "$pool_listen" | cut -d':' -f1)" -p "$(echo "$pool_listen" | cut -d':' -f2 )")
fi
result=$(perl /usr/local/lib/nagios/plugins/check_phpfpm_status.pl -t 5 "${target[@]}" -u "$pool_status_path" -c "$pool_crit_procs" -w "$pool_warn_procs" )
ret="${?}"
if [ "${ret}" -ge 2 ]; then
nb_crit=$((nb_crit + 1))
[ "${return}" -le 2 ] && return=2
elif [ "${ret}" -ge 1 ]; then
nb_warn=$((nb_warn + 1))
[ "${return}" -le 1 ] && return=1
else
nb_ok=$((nb_ok + 1))
[ "${return}" -le 0 ] && return=0
fi
result_status=$(echo ${result} | awk -F' - ' '{ print $1}')
result_content=$(echo ${result} | awk -F' - ' '{ print $2}')
output="${output}${result_status} - ${pool_file} - ${result_content}\n"
done
[ "${return}" -ge 0 ] && header="OK"
[ "${return}" -ge 1 ] && header="WARNING"
[ "${return}" -ge 2 ] && header="CRITICAL"
printf "%s - %s UNCHK / %s CRIT / %s WARN / %s OK\n\n" "${header}" "${nb_unchk}" "${nb_crit}" "${nb_warn}" "${nb_ok}"
printf "%b" "${output}" | grep -E "(CRITICAL|UNKNOWN)"
printf "%b" "${output}" | grep -E "WARNING"
printf "%b" "${output}" | grep -E "OK"
printf "%b" "${output}" | grep -E "^UNCHK"
exit "${return}"