monitoring/check_zombies_php-cgi.sh

133 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env bash
#---------------------------------------
#-----------------------------
# Settings
#-----------------------------
#---------------------------------------
# - Is this script running on terminal ?
# -
if [[ -t 1 ]] ; then
terminal=true
LOGGING=true
else
terminal=false
LOGGING=false
fi
KILL_SIGNAL=HUP
#---------------------------------------
#-----------------------------
# Base Function(s)
#-----------------------------
#---------------------------------------
fatal(){
echo ""
echo -e " [ Fatal ] $*"
echo ""
echo -e "\tScript terminated.."
echo ""
exit 1
}
error (){
echo ""
echo -e " [ Error ] $*"
echo ""
}
warn (){
echo ""
echo -e " [ Warn ] $*"
echo ""
}
info (){
echo ""
echo -e " [ Info ] $*"
echo ""
}
ok (){
echo ""
echo -e " [ Ok ] $*"
echo ""
}
#---------------------------------------
#-----------------------------
# Check some prerequisites
#-----------------------------
#---------------------------------------
if [ -z "`which basename`" ]; then
fatal 'It seems "basename" is not installed, but needed!'
fi
if [ -z "`which realpath`" ]; then
fatal 'It seems "realpath" is not installed, but needed!'
fi
php_cgi=$(realpath $(which php-cgi) 2>/dev/null ) > /dev/null 2>&1
if [[ -z "$php_cgi" ]]; then
if [[ -x "/usr/local/php/bin/php-cgi" ]]; then
php_cgi="/usr/local/php/bin/php-cgi"
else
fatal "No 'php-cgi' binary found."
fi
fi
#---------------------------------------
#-----------------------------
# Jobhandling
#-----------------------------
#---------------------------------------
#LOCK_DIR=`mktemp -d`
## - Remove lockdir when the script finishes, or when it receives a signal
#trap 'rm -rf "$LOCK_DIR"' 0 2 15
#---------------------------------------
#-----------------------------
# Checking ..
#-----------------------------
#---------------------------------------
if $LOGGING ; then
echo -e "\nChecking for zombie processes \"$php_cgi\" on \"`hostname -f`\".."
fi
if `ps -Ao"stat,command" | grep "$(basename $php_cgi)" | grep -v grep | grep -e '^[zZ]' > /dev/null 2>&1` ; then
warn "Found Zombie Processes \"$php_cgi\" on \"`hostname -f`\":\n"
ps -Ao"stat,user,pid,ppid,command" | grep "$(basename $php_cgi)" | grep -v grep | grep -e '^[zZ]'
membefore=`cat /proc/meminfo | grep Committed_AS | awk '{print $2}'`
info "Trying a graceful kill to the concerning parents with signal \"$KILL_SIGNAL\".."
ps -Ao"stat,pid,ppid,command" | grep "$(basename $php_cgi)" | grep -v grep | grep -e '^[zZ]' \
| awk '{ print $3 }' | sort -u | xargs --no-run-if-empty kill -$KILL_SIGNAL
if [ "$?" = "0" ];then
sleep 10
ok "Cleaning up zombie processes \"$php_cgi\" seems succsessfully finisched"
memafter=`cat /proc/meminfo | grep Committed_AS | awk '{print $2}'`
info "`expr $membefore - $memafter` KB RAM has been freed"
else
error "Cleaning up zombie processes \"$php_cgi\" failed!"
fi
else
if $LOGGING ; then
ok "No zombie prossses found"
fi
fi
exit 0