check_local_webservice.sh: add parameter 'CONFLICTING_SCRIPTS' - don't run this script if anny given conflicting scripts are running.

This commit is contained in:
Christoph 2021-10-22 14:20:44 +02:00
parent 62db293784
commit 9a9e862782

View File

@ -11,68 +11,6 @@ RESTART_CHECK_FILE="/tmp/${script_name%%.*}.NEED-RESTART"
declare -a alert_email_arr declare -a alert_email_arr
# -------------
# --- Read Configurations from $conf_file
# -------------
# Some default values
#
DEFAULT_NUMBER_LINES=20
DEFAULT_TIME_OUT=20
if [[ ! -f "$conf_file" ]]; then
echo ""
echo -e " [ Fatal ] Configuration file '$(basename ${conf_file})' not found!"
echo ""
echo -e "\tScript terminated.."
echo ""
exit 1
else
source "$conf_file"
fi
for _email in $alert_email_addresses ; do
alert_email_arr+=("$_email")
done
[[ -n "$sender_address" ]] || sender_address="check_local_webservice@$(hostname -f)"
[[ -n "$content_type" ]] || content_type='Content-Type: text/plain;\n charset="utf-8"'
[[ -n "$TIME_OUT" ]] || TIME_OUT=$DEFAULT_TIME_OUT
TIME_OUT_MAX="$(expr ${TIME_OUT} + 5)"
# -------------
# - 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
# ------------- # -------------
@ -185,21 +123,128 @@ is_number() {
reboot_system() { reboot_system() {
content_type='Content-Type: text/plain;\n charset="utf-8"' # content_type='Content-Type: text/plain;\n charset="utf-8"'
datum="$(date +"%d.%m.%Y")" # datum="$(date +"%d.%m.%Y")"
from_address="root@$(hostname --long)" # from_address="root@$(hostname --long)"
# msg="S*"
#
for _email in ${alert_email_arr[@]} ; do #
# for _email in ${alert_email_arr[@]} ; do
#
# echo -e "To:${_email}\n${content_type}\nSubject:[Fatal: Local Webservice NOT rsponding] - Reboot System\n${msg}" \
# | sendmail -F "Error `hostname -f`" -f $sender_address $_email
# done
echo -e "To:${_email}\n${content_type}\nSubject:[Fatal: Local Webservice NOT rsponding] - Reboot System\n$*" \ sleep 10
/sbin/reboot -f > /dev/null 2>&1
}
# -------------
# --- Read Configurations from $conf_file
# -------------
# Some default values
#
DEFAULT_NUMBER_LINES=20
DEFAULT_TIME_OUT=20
DEFAULT_CONFLICTING_SCRIPTS="/root/bin/monitoring/check_webservice_load.sh"
if [[ ! -f "$conf_file" ]]; then
echo ""
echo -e " [ Fatal ] Configuration file '$(basename ${conf_file})' not found!"
echo ""
echo -e "\tScript terminated.."
echo ""
exit 1
else
source "$conf_file"
fi
for _email in $alert_email_addresses ; do
alert_email_arr+=("$_email")
done
[[ -n "$sender_address" ]] || sender_address="check_local_webservice@$(hostname -f)"
[[ -n "$content_type" ]] || content_type='Content-Type: text/plain;\n charset="utf-8"'
[[ -n "$TIME_OUT" ]] || TIME_OUT=$DEFAULT_TIME_OUT
TIME_OUT_MAX="$(expr ${TIME_OUT} + 5)"
[[ -n "$CONFLICTING_SCRIPTS" ]] || CONFLICTING_SCRIPTS="$DEFAULT_CONFLICTING_SCRIPTS"
# -------------
# - 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 | sendmail -F "Error `hostname -f`" -f $sender_address $_email
done done
sleep 10 exit 1
/sbin/reboot -f
} fi
# - Stop here, if these give scripts are running
# -
if [[ ${#CONFLICTING_SCRIPTS} -gt 0 ]] ; then
_stop_running=false
for _val in $CONFLICTING_SCRIPTS ; do
IFS=':' read -a _val_arr <<< "${_val}"
_script_name="$(basename ${_val_arr[0]})"
if [[ -n "${_val_arr[1]}" ]] && [[ -d "${_val_arr[1]}" ]] ; then
_stop_running=true
else
check_string_ps="${_val_arr[0]}"
if ps -e f | grep -E "\s+${check_string_ps}" | grep -v grep | grep -v -E "\s+vim\s+" > /dev/null ; then
_stop_running=true
fi
fi
if $_stop_running ; then
echo ""
echo "[ Error ]: The \"${_script_name}\" script is currently running, but it conflicts with this script."
echo ""
echo " Exiting now.."
echo ""
clean_up 1
fi
done
fi
@ -364,17 +409,24 @@ else
err_msg="${err_msg} ${_site}\n" err_msg="${err_msg} ${_site}\n"
done done
if $LOGGING ; then
echo -e "\n\033[1mGoing to restart the system NOW..\033[m"
else
echo ""
echo "Going to restart the system NOW.."
fi
err_msg="${err_msg}\nGoing to restart the system..\n" err_msg="${err_msg}\nGoing to restart the system..\n"
datum="$(date +"%d.%m.%Y %H:%M")" datum="$(date +"%d.%m.%Y %H:%M")"
for _email in ${alert_email_arr[@]} ; do for _email in ${alert_email_arr[@]} ; do
echo -e "To:${_email}\n${content_type}\nSubject:[Error: Local Webservice IS STILL NOT rsponding] - A system restart is triggered.\n$err_msg\nFilesystem usage:\n=================\n$(df -h)\n\n${msg_user_defined}\n${msg}" \ echo -e "To:${_email}\n${content_type}\nSubject:[ REBOOT System ]: Local Webservice IS STILL NOT rsponding.\n$err_msg\nFilesystem usage:\n=================\n$(df -h)\n\n${msg_user_defined}\n${msg}" \
| sendmail -F "Error `hostname -f`" -f $sender_address $_email | sendmail -F "Error `hostname -f`" -f $sender_address $_email
done done
rm -f "$RESTART_CHECK_FILE" rm -f "$RESTART_CHECK_FILE"
reboot_system "The local web service is still down - a system restart is triggered." reboot_system
fi fi