check_local_webservice.sh: optional number of lines to be printed and some changes on script output.

This commit is contained in:
Christoph 2021-09-24 10:26:07 +02:00
parent cf29c039c5
commit b1557f866d
2 changed files with 50 additions and 12 deletions

View File

@ -15,6 +15,11 @@ 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!"
@ -33,7 +38,7 @@ 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=30
[[ -n "$TIME_OUT" ]] || TIME_OUT=$DEFAULT_TIME_OUT
TIME_OUT_MAX="$(expr ${TIME_OUT} + 5)"
@ -168,6 +173,16 @@ echo_skipped() {
fi
}
is_number() {
return $(test ! -z "${1##*[!0-9]*}" > /dev/null 2>&1);
# - also possible
# -
#[[ ! -z "${1##*[!0-9]*}" ]] && return 0 || return 1
#return $([[ ! -z "${1##*[!0-9]*}" ]])
}
reboot_system() {
content_type='Content-Type: text/plain;\n charset="utf-8"'
@ -286,8 +301,21 @@ msg="${msg_head}\n${msg00}\n${msg01}\n${msg02}\n\n${msg03}\n${msg04}\n${msg05}\n
if [[ ${#LOG_FILES_TO_MONITOR} -gt 0 ]] ; then
msg_user_defined="\n==========\nUser defined logfiles\n==========\n"
for _log_file in $LOG_FILES_TO_MONITOR ; do
msg_user_defined="${msg_user_defined}\n---\nLast entries (20 lines) of \"${_log_file}\":\n---\n$(tail -n 20 ${_log_file})\n"
for _val in $LOG_FILES_TO_MONITOR ; do
IFS=':' read -a _val_arr <<< "${_val}"
_log_file="${_val_arr[0]}"
if [[ -n "${_val_arr[0]}" ]] && is_number "${_val_arr[1]}" ; then
_number_lines=${_val_arr[1]}
else
_number_lines=$DEFAULT_NUMBER_LINES
fi
if [[ -s "${_log_file}" ]] ; then
msg_user_defined="${msg_user_defined}\n---\nLast entries (${_number_lines} lines) of \"${_log_file}\":\n---\n$(tail -n ${_number_lines} ${_log_file})\n"
else
msg_user_defined="${msg_user_defined}\n---\nLast entries (${_number_lines} lines) of \"${_log_file}\":\n---\n-- FILE IS EMPTY --\n"
fi
done
msg_user_defined="${msg_user_defined}\n\n"
else
@ -303,7 +331,7 @@ if [[ ! -f "$RESTART_CHECK_FILE" ]]; then
err_msg="\n[ Warning ]: The local webservice seems to be down.\n\n Some Websites do not respond as expected:\n"
for _site in $LOCAL_WEBSITES_TO_CHECK ; do
err_msg+=" $_site\n"
if $LOGGING ; then
echo -e "\t \033[1m$_site\033[m"
else
@ -317,7 +345,7 @@ if [[ ! -f "$RESTART_CHECK_FILE" ]]; then
datum="$(date +"%d.%m.%Y %H:%M")"
for _email in ${alert_email_arr[@]} ; do
echo -e "To:${_email}\n${content_type}\nSubject:[Warn: Local Webservice NOT rsponding] - Do nothing for now\n$err_msg\n${msg_user_defined}\n${msg}" \
echo -e "To:${_email}\n${content_type}\nSubject:[Warn: Local Webservice NOT rsponding] - Do nothing for now\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
done
@ -328,7 +356,6 @@ else
err_msg="\n[ Error ]: The local webservice seems to be down.\n\n Some Websites still not responding:\n"
for _site in $LOCAL_WEBSITES_TO_CHECK ; do
err_msg+=" $_site\n"
if $LOGGING ; then
echo -e "\t \033[1m$_site\033[m"
else
@ -342,12 +369,12 @@ else
datum="$(date +"%d.%m.%Y %H:%M")"
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\n${msg_user_defined}\n${msg}" \
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}" \
| sendmail -F "Error `hostname -f`" -f $sender_address $_email
done
rm -f "$RESTART_CHECK_FILE"
reboot_system "The local web service is still down - a system restart is triggered."
#reboot_system "The local web service is still down - a system restart is triggered."
fi

View File

@ -17,10 +17,21 @@
# -
LOCAL_WEBSITES_TO_CHECK=""
# - LOG_FILES_TO_MONITOR
# - LOG_FILES_TO_MONITOR[:<n>]
# -
# - Print out the last 20 lines from theses logfiles
# - Print out the last lines from theses given logfiles
# -
# - its possible to give a number of lines, which should be printed out
# - by appending it seperated ba a colon ':'.
# -
# - The default number of printed out lines is 20.
#
# - Example:
# - LOG_FILES_TO_MONITOR="
# - /var/log/apache2/ip_requests.log:40
# - /var/log/php-7.4-fpm/php_errors.log
# - /var/log/mysql/mysql.err:15
# - "
# -
LOG_FILES_TO_MONITOR=""