check_webservice_load.sh: add support for 'nginx' and 'redis'. Change output on terminal.

This commit is contained in:
Christoph 2018-04-23 02:24:38 +02:00
parent 36389fdc02
commit 9689157121
2 changed files with 470 additions and 10 deletions

View File

@ -10,6 +10,16 @@ conf_file="${working_dir}/conf/check_webservice_load.conf"
#-----------------------------
#---------------------------------------
# - Defaults
# -
check_load=false
check_mysql=false
check_apache=false
check_nginx=false
check_php_fpm=false
check_redis=false
check_website=false
if [[ ! -f "$conf_file" ]]; then
echo ""
echo -e " [ Fatal ] Configuration file '$(basename ${conf_file})' not found!"
@ -40,34 +50,53 @@ LOCK_DIR=`mktemp -d`
fatal(){
echo ""
echo -e " [ Fatal ] $*"
if $terminal ; then
echo -e " [ \033[31m\033[1mFatal\033[m ] $*"
else
echo -e " [ Fatal ] $*"
fi
echo ""
echo -e "\tScript terminated.."
echo ""
exit 1
rm -rf $LOCK_DIR
exit 1
}
error (){
echo ""
echo -e " [ Error ] $*"
if $terminal ; then
echo -e " [ \033[31m\033[1mError\033[m ] $*"
else
echo " [ Error ] $*"
fi
echo ""
}
warn (){
echo ""
echo -e " [ Warn ] $*"
if $terminal ; then
echo -e " [ \033[33m\033[1mWarn\033[m ] $*"
else
echo " [ Warn ] $*"
fi
echo ""
}
info (){
echo ""
echo -e " [ Info ] $*"
if $terminal ; then
echo -e " [ \033[32m\033[1mInfo\033[m ] $*"
else
echo " [ Info ] $*"
fi
echo ""
}
ok (){
echo ""
echo -e " [ Ok ] $*"
if $terminal ; then
echo -e " [ \033[32m\033[1mOk\033[m ] $*"
else
echo " [ Ok ] $*"
fi
echo ""
}
@ -86,8 +115,8 @@ trim() {
#---------------------------------------
if [[ -t 1 ]] ; then
terminal=true
LOGGING=true
terminal=true
LOGGING=true
else
terminal=false
LOGGING=false
@ -113,6 +142,12 @@ if [ -z "`which realpath`" ]; then
fatal 'It seems "realpath" is not installed, but needed!'
fi
if $check_apache || $check_nginxA ; then
if [ -z "$(which curl)" ]; then
fatal 'It seems "curls" is not installed, but needed!'
fi
fi
if $check_load ; then
if [ -z "`which bc`" ]; then
fatal 'It seems "bc" is not installed, but needed!'
@ -244,6 +279,46 @@ if $check_apache ; then
fi
fi
if $check_nginx ; then
NGINXD="$(realpath $(which nginx))"
if [ -z "$NGINXD" ]; then
if [ -x "/usr/local/nginx/bin/nginx" ]; then
NGINXD="$(realpath "/usr/local/nginx/bin/nginx")"
elif [ -x "/usr/local/nginx/sbin/nginx" ]; then
NGINXD="$(realpath "/usr/local/nginx/sbin/nginx")"
else
fatal 'Command \"nginx\" not found!'
fi
fi
NGINX_SERVICE_FILE=""
NGINX_INIT_SCRIPT=""
if $systemd_supported ; then
# - Is Service exclusive controlled by systemd
# -
if systemctl -t service list-unit-files \
| grep -e "^nginx" \
| grep -q -E "(enabled|disabled)" 2> /devnull ; then
NGINX_SERVICE_FILE=$(systemctl -t service list-unit-files \
| grep -e "^nginx" \
| awk '{print$1}')
fi
fi
if [[ -z "$NGINX_SERVICE_FILE" ]] ; then
if [ -x "/etc/init.d/nginx" ]; then
NGINX_INIT_SCRIPT="/etc/init.d/nginx"
fi
fi
if [[ -z "$NGINX_INIT_SCRIPT" ]] && [[ -z "$NGINX_SERVICE_FILE" ]] ; then
fatal "Neither an init-script nor a service file for 'nginx' webservice found!"
fi
fi
declare -A start_stop_method_php_fpm
declare -A start_stop_file_php_fpm
declare -A php_fpm_ping_path
@ -319,6 +394,44 @@ if $check_php_fpm ; then
done
fi
if $check_redis ; then
REDISD="$(realpath $(which redis-server))"
if [ -z "$REDISD" ]; then
if [ -x "/usr/sbin/redis-server" ]; then
REDISD="/usr/sbin/redis-server"
else
fatal 'Command \"redis-server\" not found!'
fi
fi
REDIS_SERVICE_FILE=""
REDIS_INIT_SCRIPT=""
if $systemd_supported ; then
# - Is Service exclusive controlled by systemd
# -
if systemctl -t service list-unit-files \
| grep -e "^redis-server" \
| grep -q -E "(enabled|disabled)" 2> /devnull ; then
REDIS_SERVICE_FILE=$(systemctl -t service list-unit-files \
| grep -e "^redis-server" \
| awk '{print$1}')
fi
fi
if [[ -z "$REDIS_SERVICE_FILE" ]] ; then
if [ -x "/etc/init.d/redis-server" ]; then
REDIS_INIT_SCRIPT="/etc/init.d/redis-server"
fi
fi
if [[ -z "$REDIS_INIT_SCRIPT" ]] && [[ -z "$REDIS_SERVICE_FILE" ]] ; then
fatal "Neither an init-script nor a service file for 'nginx' webservice found!"
fi
fi
if $check_website ; then
if [[ -n "$php_version_of_working_url" ]] && [[ "$php_version_of_working_url" != "None" ]] ; then
@ -468,6 +581,69 @@ stop_apache() {
i=i+1
done
}
stop_nginx() {
send_msg=$1
send_msg=${send_msg:=true}
## - Stop Nginx Webservice
## -
if [[ -n "$NGINX_SERVICE_FILE" ]] ; then
systemctl stop "$NGINX_SERVICE_FILE" > /dev/null 2>&1
else
$NGINX_INIT_SCRIPT stop > /dev/null 2>&1
fi
sleep 12
declare -i i=0
PIDS=$(ps aux | grep "$NGINXD" | grep -v grep | awk '{print$2}')
while [ "X$PIDS" != "X" ]; do
if [ $i -eq 0 ]; then
echo "" >> $LOCK_DIR/extra_msg.txt
echo -e "\t[ Error ]: Stopping Nginx Webservice failed !!!" >> $LOCK_DIR/extra_msg.txt
echo "" >> $LOCK_DIR/extra_msg.txt
echo -e "\t[ Info ]; Going to kill remaining nginx-processes.." >> $LOCK_DIR/extra_msg.txt
echo "" >> $LOCK_DIR/extra_msg.txt
error "Stopping Nginx Webservice failed !!!"
info "Going to kill remaining nginx-processes.."
fi
if [ $i -gt 10 ]; then
echo "" >> $LOCK_DIR/extra_msg.txt
echo -e "\t[ Error ]: Killing remaining nginx-processes failed !!!" >> $LOCK_DIR/extra_msg.txt
echo "" >> $LOCK_DIR/extra_msg.txt
error "Killing remaining nginx-processes failed !!!"
if $send_msg ; then
subject="[ Error ]: Stopping Nginx Webservice on `hostname -f` failed -- $datum"
msg="\n\n`cat $LOCK_DIR/extra_msg.txt`\n"
for _to_address in $to_addresses ; do
echo -e "To:${_to_address}\n${content_type}\nSubject:$subject\n\n${msg}\n" \
| /usr/sbin/sendmail -F "Webservice Monitor" -f $from_address $_to_address
done
fi
break
fi
for _PID in $PIDS ; do
kill -9 $_PID > /dev/null 2>&1
done
sleep 2
PIDS=$(ps aux | grep "$NGINXD" | grep -v grep | awk '{print$2}')
i=i+1
done
}
@ -654,6 +830,70 @@ stop_php_fpm() {
}
stop_redis() {
send_msg=$1
send_msg=${send_msg:=true}
## - Stop Redis Caching Service
## -
if [[ -n "$REDIS_SERVICE_FILE" ]] ; then
systemctl stop "$REDIS_SERVICE_FILE" > /dev/null 2>&1
else
$REDIS_INIT_SCRIPT stop > /dev/null 2>&1
fi
sleep 12
declare -i i=0
PIDS=$(ps aux | grep "$REDISD" | grep -v grep | awk '{print$2}')
while [ "X$PIDS" != "X" ]; do
if [ $i -eq 0 ]; then
echo "" >> $LOCK_DIR/extra_msg.txt
echo -e "\t[ Error ]: Stopping Redis Caching Sservice failed !!!" >> $LOCK_DIR/extra_msg.txt
echo "" >> $LOCK_DIR/extra_msg.txt
echo -e "\t[ Info ]; Going to kill remaining 'redis-server'-processes.." >> $LOCK_DIR/extra_msg.txt
echo "" >> $LOCK_DIR/extra_msg.txt
error "Stopping Redis Caching Service failed !!!"
info "Going to kill remaining 'redis-server'-processes.."
fi
if [ $i -gt 10 ]; then
echo "" >> $LOCK_DIR/extra_msg.txt
echo -e "\t[ Error ]: Killing remaining 'redis-server'-processes failed !!!" >> $LOCK_DIR/extra_msg.txt
echo "" >> $LOCK_DIR/extra_msg.txt
error "Killing remaining 'redis-server'-processes failed !!!"
if $send_msg ; then
subject="[ Error ]: Stopping Redis Caching Service on `hostname -f` failed -- $datum"
msg="\n\n`cat $LOCK_DIR/extra_msg.txt`\n"
for _to_address in $to_addresses ; do
echo -e "To:${_to_address}\n${content_type}\nSubject:$subject\n\n${msg}\n" \
| /usr/sbin/sendmail -F "Webservice Monitor" -f $from_address $_to_address
done
fi
break
fi
for _PID in $PIDS ; do
kill -9 $_PID > /dev/null 2>&1
done
sleep 2
PIDS=$(ps aux | grep "$REDISD" | grep -v grep | awk '{print$2}')
i=i+1
done
}
start_mysql() {
send_msg=$1
@ -765,6 +1005,63 @@ start_apache() {
fi
}
start_nginx() {
send_msg=$1
send_msg=${send_msg:=true}
## - Start Nginx Webservice
## -
if [[ -n "$NGINX_SERVICE_FILE" ]] ; then
systemctl start "$NGINX_SERVICE_FILE" > /dev/null 2>&1
else
$NGINX_INIT_SCRIPT start > /dev/null 2>&1
fi
sleep 2
NEWPID=$(ps aux | grep "$NGINXD" | grep -v grep | grep root | awk '{print$2}')
if [ "X${NEWPID}X" = "XX" ]; then
echo "" >> $LOCK_DIR/extra_msg.txt
echo -e "\t[ Error ]: I started the 'Nginx' webserver, but service is not running !!!" >> $LOCK_DIR/extra_msg.txt
echo "" >> $LOCK_DIR/extra_msg.txt
error "I started the 'Nginx' webserver, but service is not running !!!"
datum=`date +"%d.%m.%Y %H:%Mh"`
subject="[ Error ]: Nginx Webservice on `hostname -f` NOT RUNNING -- $datum"
msg="\n\n`cat $LOCK_DIR/extra_msg.txt`\n"
for _to_address in $to_addresses ; do
echo -e "To:${_to_address}\n${content_type}\nSubject:$subject\n\n${msg}\n" \
| /usr/sbin/sendmail -F "Webservice Monitor" -f $from_address $_to_address
done
else
echo "" >> $LOCK_DIR/extra_msg.txt
echo -e "\tI started the 'Nginx' webserver. the new PID is $NEWPID" >> $LOCK_DIR/extra_msg.txt
echo "" >> $LOCK_DIR/extra_msg.txt
ok "I started the 'NGINX' webserver. the new PID is $NEWPID"
fi
if $send_msg ; then
datum=`date +"%d.%m.%Y %H:%Mh"`
subject="Restarting Nginx on `hostname -f` invoked -- $datum"
msg=`cat $LOCK_DIR/extra_msg.txt`
for _to_address in $to_addresses ; do
echo -e "To:${_to_address}\n${content_type}\nSubject:$subject\n\n${msg}\n" \
| /usr/sbin/sendmail -F "Webservice Monitor" -f $from_address $_to_address
done
fi
}
start_php_fpm() {
__version=$1
@ -833,6 +1130,63 @@ start_php_fpm() {
fi
}
start_redis() {
send_msg=$1
send_msg=${send_msg:=true}
## - Start Redis Caching Service
## -
if [[ -n "$REDIS_SERVICE_FILE" ]] ; then
systemctl start "$REDIS_SERVICE_FILE" > /dev/null 2>&1
else
$REDIS_INIT_SCRIPT start > /dev/null 2>&1
fi
sleep 2
NEWPID=$(ps aux | grep "$REDISD" | grep -v grep | awk '{print$2}')
if [ "X${NEWPID}X" = "XX" ]; then
echo "" >> $LOCK_DIR/extra_msg.txt
echo -e "\t[ Error ]: I started the 'Redis' Server, but service is not running !!!" >> $LOCK_DIR/extra_msg.txt
echo "" >> $LOCK_DIR/extra_msg.txt
error "I started the 'Redis' Server, but service is not running !!!"
datum=`date +"%d.%m.%Y %H:%Mh"`
subject="[ Error ]: Redis Caching Service on `hostname -f` NOT RUNNING -- $datum"
msg="\n\n`cat $LOCK_DIR/extra_msg.txt`\n"
for _to_address in $to_addresses ; do
echo -e "To:${_to_address}\n${content_type}\nSubject:$subject\n\n${msg}\n" \
| /usr/sbin/sendmail -F "Webservice Monitor" -f $from_address $_to_address
done
else
echo "" >> $LOCK_DIR/extra_msg.txt
echo -e "\tI started the 'Redis' Server. the new PID is $NEWPID" >> $LOCK_DIR/extra_msg.txt
echo "" >> $LOCK_DIR/extra_msg.txt
ok "I started the 'Redis' Server. the new PID is $NEWPID"
fi
if $send_msg ; then
datum=`date +"%d.%m.%Y %H:%Mh"`
subject="Restarting Redis Server on `hostname -f` invoked -- $datum"
msg=`cat $LOCK_DIR/extra_msg.txt`
for _to_address in $to_addresses ; do
echo -e "To:${_to_address}\n${content_type}\nSubject:$subject\n\n${msg}\n" \
| /usr/sbin/sendmail -F "Webservice Monitor" -f $from_address $_to_address
done
fi
}
restart_apache() {
## - Stop Apache Webservice
@ -846,6 +1200,19 @@ restart_apache() {
start_apache
}
restart_nginx() {
## - Stop Apache Webservice
## -
stop_nginx
sleep 2
## - Start Apache Webservice
## -
start_nginx
}
restart_mysql(){
## - Stop MySQL Servive
@ -872,6 +1239,19 @@ restart_php_fpm(){
start_php_fpm $1
}
restart_redis(){
## - Stop MySQL Servive
## -
stop_redis
sleep 2
## - Start MySQL Service
## -
start_redis
}
graceful_restart_php_fpm() {
__version=$1
@ -1176,6 +1556,50 @@ if $check_apache ; then
fi
#---------------------------------------
#-----------------------------
# Check Nginx webservice..
#-----------------------------
#---------------------------------------
if $check_nginx ; then
echo "" > $LOCK_DIR/extra_msg.txt
if $LOGGING ; then
echo -e "\nChecking Nginx webservice on \"`hostname -f`\".."
fi
PID=`ps aux | grep "$NGINXD " | grep -e "^root" | grep -v grep | awk '{print$2}'`
if [ "X${PID}X" = "XX" ];then
echo -e "\nNginx Webservice is not running! - Try to start it.." > $LOCK_DIR/extra_msg.txt
echo -e "====================================================" >> $LOCK_DIR/extra_msg.txt
error "Nginx webservice is not running! - Try to start it.."
restart_nginx
else
if ! $(curl -Is -m 10 http://$curl_check_host | head -n 1 | grep "200 OK" > /dev/null) ; then
echo -e "\nNginx Webservice seems to be running, but is NOT responding! - Try to restart service.." >> $LOCK_DIR/extra_msg.txt
echo -e "=======================================================================================" >> $LOCK_DIR/extra_msg.txt
error "Nginx Webservice seems to be running, but is NOT responding! - Try to restart service.."
restart_nginx
else
if $LOGGING ; then
ok "Nginx Webserver on \"`hostname -f`\" seems to be running.."
fi
fi
fi
fi
#---------------------------------------
#-----------------------------
# Check PHP-FPM engine..
@ -1223,6 +1647,40 @@ if $check_php_fpm ; then
fi
#---------------------------------------
#-----------------------------
# Check Redis Caching Service..
#-----------------------------
#---------------------------------------
if $check_nginx ; then
echo "" > $LOCK_DIR/extra_msg.txt
if $LOGGING ; then
echo -e "\nChecking Redis Caching Service on \"`hostname -f`\".."
fi
PID=`ps aux | grep "$REDISD " | grep -v grep | awk '{print$2}'`
if [ "X${PID}X" = "XX" ];then
echo -e "\nRedis Caching Service is not running! - Try to start it.." > $LOCK_DIR/extra_msg.txt
echo -e "=========================================================" >> $LOCK_DIR/extra_msg.txt
error "Redis Caching Service is not running! - Try to start it.."
restart_redis
else
if $LOGGING ; then
ok "Redis Caching Service on \"`hostname -f`\" seems to be running.."
fi
fi
fi
#---------------------------------------
#-----------------------------
# Check website..

View File

@ -19,7 +19,9 @@
check_load=true
check_mysql=true
check_apache=true
check_nginx=false
check_php_fpm=true
check_redis=false
check_website=false