monitoring/check_wwsympa_service.sh

377 lines
8.2 KiB
Bash
Executable File

#!/usr/bin/env bash
working_dir="$(dirname $(realpath $0))"
# -------------
# - Some Variables
# -------------
LOCK_DIR="/tmp/$(basename $0).LOCK"
service_name="wwsympa"
alert_email_arr="root"
sender_address="check_${service_name}@$(hostname -f)"
content_type='Content-Type: text/plain;\n charset="utf-8"'
wwsympa_commands="/usr/local/sympa/bin/wwsympa.fcgi"
check_string_ps="$wwsympa_commands"
restart_needed=false
# -------------
# - Job is already running?
# -------------
# - If job already runs, stop execution..
# -
if mkdir "$LOCK_DIR" 2> /dev/null ; then
## - Remove lockdir when the script finishes, or when it receives a signal
trap clean_up SIGHUP SIGINT SIGTERM
else
datum="$(date +"%d.%m.%Y %H:%M")"
msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\n Exiting now.."
echo ""
echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running."
echo ""
echo -e " Exiting now.."
echo ""
for _email in ${alert_email_arr[@]} ; do
echo -e "To:${_email}\n${content_type}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \
| sendmail -F "Error `hostname -f`" -f $sender_address $_email
done
exit 1
fi
# -------------
# - Some functions
# -------------
clean_up() {
# Perform program exit housekeeping
rm -rf "$LOCK_DIR"
exit $1
}
echononl(){
if $terminal ; then
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
fi
}
fatal(){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
echo ""
echo -e " \033[31m\033[1mScript was interupted\033[m!"
else
echo " [ Fatal ]: $*"
echo ""
echo " Script was terminated...."
fi
echo ""
clean_up 1
}
error (){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mError\033[m ]: $*"
else
echo "[ Error ]: $*"
fi
echo ""
}
warn (){
echo ""
if $terminal ; then
echo -e " [ \033[33m\033[1mWarning\033[m ]: $*"
else
echo "[ Warning ]: $*"
fi
echo ""
}
info (){
if $terminal ; then
echo ""
echo -e " [ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
fi
}
echo_done() {
if $terminal ; then
echo -e "\033[75G[ \033[32mdone\033[m ]"
fi
}
echo_failed(){
if $terminal && $LOGGING ; then
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
fi
}
echo_skipped() {
if $terminal && $LOGGING ; then
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
fi
}
detect_os () {
if $(which lsb_release > /dev/null 2>&1) ; then
os_dist="$(lsb_release -i | awk '{print tolower($3)}')"
os_version="$(lsb_release -r | awk '{print tolower($2)}')"
os_codename="$(lsb_release -c | awk '{print tolower($2)}')"
if [[ "$os_dist" = "debian" ]]; then
if $(echo "$os_version" | grep -q '\.') ; then
os_version=$(echo "$os_version" | cut --delimiter='.' -f1)
fi
fi
elif [[ -e "/etc/os-release" ]]; then
. /etc/os-release
os_dist=$ID
os_version=${VERSION_ID}
fi
# remove whitespace from os_dist and os_version
os_dist="${os_dist// /}"
os_version="${os_version// /}"
}
# -------------
# - Check some prerequisites
# -------------
# - Running in a terminal?
# -
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
fi
# - Detect linux distribution
# -
detect_os
# - Systemd supported ?
# -
systemd=$(which systemd)
systemctl=$(which systemctl)
systemd_supported=false
if [[ -n "$systemd" ]] && [[ -n "$systemctl" ]] ; then
systemd_supported=true
fi
# - Check How to start/stop service
# -
SYSTEMD_UNIT_FILE=""
SYSY_INIT_SCRIPT=""
if $systemd_supported ; then
SYSTEMD_UNIT_FILE="$(systemctl list-unit-files | grep -E "${service_name}[^@]*\.service" | grep "enabled" | cut -d' ' -f1)"
fi
if [[ -z "$SYSTEMD_UNIT_FILE" ]]; then
SYSY_INIT_SCRIPT="$(service --status-all | awk '{print$4}' | grep ${service_name} | head -1)"
fi
if [[ -z "$SYSTEMD_UNIT_FILE" ]] && [[ -z "$SYSY_INIT_SCRIPT" ]] ; then
fatal "Neither an init-script nor a service file for ${service_name} service found!"
fi
# -------------
# - Main Part of Script
# -------------
if $terminal ; then
echo -e "\n Check if ${service_name} service is running.."
echo -e " ==================================="
fi
# - Note:
# -
# - first we will check '/usr/local/symba/bin/wwsympa.fcgi'
# - second we will check '$(realpath "/usr/local/symba/bin/wwsympa.fcgi"'
# - i.e /usr/local/sympa-6.2.36/bin/wwsympa.fcgi
# -
for _command in $wwsympa_commands ; do
PIDS="$(ps ax | grep -E "${_command}" | grep -v grep | awk '{print$1}')"
if [[ -z "$PIDS" ]]; then
_command="$(realpath "${_command}")"
PIDS="$(ps ax | grep -E "${_command}" | grep -v grep | awk '{print$1}')"
if [[ -z "$PIDS" ]]; then
restart_needed=true
break
fi
fi
done
if $restart_needed ;then
error "${service_name} service seems to be down. Try to (re)start .."
echononl " Stop service '${service_name}' first .."
if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then
if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \
&& $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then
echo_skipped
warn "It seems that package management (apt,dpkg) is running. Omit starting the service .."
else
systemctl stop $SYSTEMD_UNIT_FILE > /dev/null 2>&1
if [[ $? -eq 0 ]] ; then
echo_done
sleep 2
echononl " Start Service '${service_name}' .."
if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \
&& $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then
echo_skipped
warn "It seems that package management (apt,dpkg) is running. Omit starting the service .."
else
systemctl start $SYSTEMD_UNIT_FILE > /dev/null 2>&1
if [[ $? -eq 0 ]] ; then
echo_done
else
echo_failed
fi
fi
else
echo_failed
fi
fi
else
if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \
&& $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then
echo_skipped
warn "It seems that package management (apt,dpkg) is running. Omit starting the service .."
else
/etc/init.d/$SYSY_INIT_SCRIPT stop > /dev/null 2>&1
if [[ $? -eq 0 ]] ; then
echo_done
sleep 2
echononl " Start Service '${service_name}' .."
if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \
&& $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then
echo_skipped
warn "It seems that package management (apt,dpkg) is running. Omit starting the service .."
else
/etc/init.d/$SYSY_INIT_SCRIPT start > /dev/null 2>&1
if [[ $? -eq 0 ]] ; then
echo_done
else
echo_failed
fi
fi
else
echo_failed
fi
fi
fi
declare -i count=0
restart_sucessfully=false
while [[ $count -lt 10 ]] && ! $restart_sucessfully ; do
((count++))
sleep 1
PIDS=""
_NEW_PIDS=""
declare -i count_2=0
for _command in $wwsympa_commands ; do
((count_2++))
PIDS="$(ps ax | grep -E "${_command}" | grep -v grep | awk '{print$1}')"
if [[ -z "$PIDS" ]]; then
continue
else
if [[ $count_2 -eq 1 ]] ; then
_NEW_PIDS="$PIDS"
else
_NEW_PIDS="$_NEW_PIDS $PIDS"
fi
restart_sucessfully=true
fi
done
break
done
if ! $restart_sucessfully ; then
error "Restarting service '${service_name}' failed."
else
count=0
for _pid in $_NEW_PIDS ; do
((count++))
if [[ $count -eq 1 ]] ; then
NEW_PIDS="$_pid"
else
NEW_PIDS="$NEW_PIDS $_pid"
fi
done
info "Service '${service_name}' was restarted and is now up and running.
The new PIDs are $NEW_PIDS"
if ! $terminal ; then
echo ""
echo "[ Success ]: Service '${service_name}' was restarted and is now up and running."
echo " The new PIDs are $NEW_PIDS"
echo ""
fi
fi
else
info "Service ${service_name} is up and running."
fi
clean_up 0