check_webservice_load.sh: add support for PostgreSQL Service

This commit is contained in:
Christoph 2018-04-23 17:49:42 +02:00
parent 96bdb83da0
commit 51a38fc061

View File

@ -14,6 +14,7 @@ conf_file="${working_dir}/conf/check_webservice_load.conf"
# -
check_load=false
check_mysql=false
check_postgresql=false
check_apache=false
check_nginx=false
check_php_fpm=false
@ -243,6 +244,35 @@ if $check_mysql ; then
fi
if $check_postgresql ; then
PSQL_SERVICE_FILE=""
PSQL_INIT_SCRIPT=""
if $systemd_supported ; then
# - Is Service exclusive controlled by systemd
# -
if systemctl -t service list-unit-files \
| grep -e "^ postgresql" \
| grep -q -E "(enabled|disabled)" 2> /devnull ; then
PSQL_SERVICE_FILE=$(systemctl -t service list-unit-files \
| grep -e "^postgresql" \
| awk '{print$1}')
fi
fi
if [[ -z "$PSQL_SERVICE_FILE" ]] ; then
if [ -x "/etc/init.d/postgresql" ]; then
PSQL_INIT_SCRIPT="/etc/init.d/postgresql"
fi
fi
if [[ -z "$PSQL_INIT_SCRIPT" ]] && [[ -z "$PSQL_SERVICE_FILE" ]] ; then
fatal "Neither an init-script nor a service file for 'postgesql' databaseservice found!"
fi
fi
if $check_apache ; then
HTTPD=`realpath $(which httpd) 2>/dev/null`
@ -739,6 +769,91 @@ stop_mysql() {
fi
}
stop_postgresql() {
send_msg=$1
send_msg=${send_msg:=true}
## - Stop PostgreSQL database service
## -
if [[ -n "$PSQL_SERVICE_FILE" ]] ; then
systemctl stop "$PSQL_SERVICE_FILE" > /dev/null 2>&1
else
$PSQL_INIT_SCRIPT stop > /dev/null 2>&1
fi
sleep 12
declare -i i=0
CHILD_PID="$(ps aux | grep -E "\ postgres:\ " | grep -v grep | tail -n 1 | awk '{print$2}')"
PSQL_MAIN_PID="$(ps -o ppid= -p $CHILD_PID 2> /dev/null)"
while [ "X$PSQL_MAIN_PID" != "X" ]; do
if [ $i -eq 0 ]; then
echo "" >> $LOCK_DIR/extra_msg.txt
echo -e "\t[ Error ]: Stopping PostgreSQL database service failed !!!" >> $LOCK_DIR/extra_msg.txt
echo "" >> $LOCK_DIR/extra_msg.txt
echo -e "\t[ Info ]; Going to kill remaining postgres-processes.." >> $LOCK_DIR/extra_msg.txt
echo "" >> $LOCK_DIR/extra_msg.txt
error "Stopping PostgreSQL database service failed !!!"
info "Going to kill remaining postgres-processes.."
fi
if [ $i -gt 10 ]; then
echo "" >> $LOCK_DIR/extra_msg.txt
echo -e "\t[ Error ]: Killing remaining postgres-processes failed !!!" >> $LOCK_DIR/extra_msg.txt
echo "" >> $LOCK_DIR/extra_msg.txt
error "Killing remaining postgres-processes failed !!!"
if $send_msg ; then
subject="[ Error ]: Stopping PostgreSQL database 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
kill $PSQL_MAIN_PID > /dev/null 2>&1
sleep 2
CHILD_PID="$(ps aux | grep -E "\ postgres:\ " | grep -v grep | tail -n 1 | awk '{print$2}')"
PSQL_MAIN_PID="$(ps -o ppid= -p $CHILD_PID)"
i=i+1
done
CHILD_PIDS=$(ps aux | grep -E "postgres:" | grep -v grep | awk '{print$2}')
if [[ "X${CHILD_PIDS}X" != 'XX' ]]; then
echo "" >> $LOCK_DIR/extra_msg.txt
echo -e "\t[ Error ]: Found processes 'postgres:'." >> $LOCK_DIR/extra_msg.txt
echo "" >> $LOCK_DIR/extra_msg.txt
echo -e "\t[ Info ]; Going to kill remaining postgres-processes.." >> $LOCK_DIR/extra_msg.txt
echo "" >> $LOCK_DIR/extra_msg.txt
error "Found processes 'postgres:'."
info "Going to kill processes 'postgres:'.."
for _PID in $CHILD_PIDS ; do
kill $_PID > /dev/null 2>&1
done
fi
}
stop_php_fpm() {
__version=$1
@ -974,6 +1089,64 @@ start_mysql() {
fi
}
start_postgresql() {
send_msg=$1
send_msg=${send_msg:=true}
## - Start PostgreSQL Webservice
## -
if [[ -n "$PSQL_SERVICE_FILE" ]] ; then
systemctl start "$PSQL_SERVICE_FILE" > /dev/null 2>&1
else
$PSQL_INIT_SCRIPT start > /dev/null 2>&1
fi
sleep 2
CHILD_PID="$(ps aux | grep -E "\ postgres:\ " | grep -v grep | tail -n 1 | awk '{print$2}')"
NEWPID="$(ps -o ppid= -p $CHILD_PID 2> /dev/null)"
if [ "X${NEWPID}X" = "XX" ]; then
echo "" >> $LOCK_DIR/extra_msg.txt
echo -e "\t[ Error ]: I started the 'PostgreSQL' database server, but service is not running !!!" >> $LOCK_DIR/extra_msg.txt
echo "" >> $LOCK_DIR/extra_msg.txt
error "I started the 'PostgreSQL' database server, but service is not running !!!"
datum=`date +"%d.%m.%Y %H:%Mh"`
subject="[ Error ]: PostgreSQL database 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 'PostgreSQL' database server. the new PID is $NEWPID" >> $LOCK_DIR/extra_msg.txt
echo "" >> $LOCK_DIR/extra_msg.txt
ok "I started the 'PostgreSQL' database server. the new PID is $NEWPID"
fi
if $send_msg ; then
datum=`date +"%d.%m.%Y %H:%Mh"`
subject="Restarting PostgreSQL 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_apache() {
send_msg=$1
@ -1241,7 +1414,7 @@ restart_nginx() {
restart_mysql(){
## - Stop MySQL Servive
## - Stop MySQL Service
## -
stop_mysql
@ -1252,6 +1425,19 @@ restart_mysql(){
start_mysql
}
restart_postgresql(){
## - Stop PostgreSQL Service
## -
stop_postgresql
sleep 2
## - Start MySQL Service
## -
start_postgresql
}
restart_php_fpm(){
## - Stop MySQL Servive
@ -1532,6 +1718,42 @@ if $check_mysql ; then
fi
#---------------------------------------
#-----------------------------
# Check Postgresql database service..
#-----------------------------
#---------------------------------------
if $check_postgresql ; then
echo "" > $LOCK_DIR/extra_msg.txt
if $LOGGING ; then
echo -e "\nChecking PostgreSQL database service on \"`hostname -f`\".."
fi
CHILD_PID="$(ps aux | grep -E "\ postgres:\ " | grep -v grep | tail -n 1 | awk '{print$2}')"
PSQL_MAIN_PID="$(ps -o ppid= -p $CHILD_PID 2> /dev/null)"
if [ "X${PSQL_MAIN_PID}X" = "XX" ];then
echo -e "\nPostgreSQL database service is not running! - Try to start it.." > $LOCK_DIR/extra_msg.txt
echo -e "===============================================================" >> $LOCK_DIR/extra_msg.txt
error "PostgeSQL database service is not running! - Try to start it.."
restart_postgresql
else
if $LOGGING ; then
ok "PostgreSQL database server on \"`hostname -f`\" seems to be running.."
fi
fi
fi
#---------------------------------------
#-----------------------------
# Check Apache webservice..