Add script 'check_remote_websites.sh'.
This commit is contained in:
247
check_remote_websites.sh
Executable file
247
check_remote_websites.sh
Executable file
@@ -0,0 +1,247 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
|
||||
working_dir="$(dirname $(realpath $0))"
|
||||
conf_file="${working_dir}/conf/check_remote_websites.conf"
|
||||
|
||||
LOCK_DIR="/tmp/check_remote_websites.LOCK"
|
||||
|
||||
declare -a alert_email_arr
|
||||
|
||||
# -------------
|
||||
# --- Read Configurations from $conf_file
|
||||
# -------------
|
||||
|
||||
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_websites@$(hostname -f)"
|
||||
[[ -n "$content_type" ]] || content_type='Content-Type: text/plain;\n charset="utf-8"'
|
||||
|
||||
[[ -n "$TIME_OUT" ]] || TIME_OUT=240
|
||||
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
|
||||
|
||||
|
||||
# -------------
|
||||
# --- Some functions
|
||||
# -------------
|
||||
|
||||
clean_up() {
|
||||
|
||||
# Perform program exit housekeeping
|
||||
rm -rf "$LOCK_DIR"
|
||||
exit $1
|
||||
}
|
||||
|
||||
echononl(){
|
||||
if $terminal && $LOGGING ; 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[1mError\033[m ]: $*"
|
||||
echo ""
|
||||
echo -e "\t\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 "\t[ \033[31m\033[1mError\033[m ]: $*"
|
||||
else
|
||||
echo "[ Error ]: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
warn (){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
|
||||
else
|
||||
echo "[ Warning ]: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
echo_done() {
|
||||
if $terminal && $LOGGING ; then
|
||||
echo -e "\033[75G[ \033[32mdone\033[m ]"
|
||||
else
|
||||
if $LOGGING ; then
|
||||
echo " [ done ]"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
echo_ok() {
|
||||
if $terminal && $LOGGING ; then
|
||||
echo -e "\033[75G[ \033[32mok\033[m ]"
|
||||
else
|
||||
if $LOGGING ; then
|
||||
echo " [ ok ]"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
echo_failed(){
|
||||
if $terminal && $LOGGING ; then
|
||||
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
|
||||
else
|
||||
if $LOGGING ; then
|
||||
echo " [ failed ]"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
echo_skipped() {
|
||||
if $terminal && $LOGGING ; then
|
||||
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
|
||||
else
|
||||
if $LOGGING ; then
|
||||
echo " [ skipped ]"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
# -------------
|
||||
# --- Check some prerequisites
|
||||
# -------------
|
||||
|
||||
# - Running in a terminal?
|
||||
# -
|
||||
if [[ -t 1 ]] ; then
|
||||
terminal=true
|
||||
LOGGING=true
|
||||
else
|
||||
terminal=false
|
||||
LOGGING=false
|
||||
fi
|
||||
|
||||
|
||||
# - Systemd supported ?
|
||||
# -
|
||||
systemd=$(which systemd)
|
||||
systemctl=$(which systemctl)
|
||||
|
||||
systemd_supported=false
|
||||
if [[ -n "$systemd" ]] && [[ -n "$systemctl" ]] ; then
|
||||
systemd_supported=true
|
||||
fi
|
||||
|
||||
|
||||
if $LOGGING ; then
|
||||
echo ""
|
||||
fi
|
||||
|
||||
if [[ -n "$WEBSITES_TO_CHECK" ]] ; then
|
||||
for _site in $WEBSITES_TO_CHECK ; do
|
||||
echononl " Check site \033[1m$_site\033[m .."
|
||||
response="$(curl --max-time $TIME_OUT --connect-timeout 30 \
|
||||
-I -k -L --write-out %{http_code} --silent --output /dev/null $_site 2> ${LOCK_DIR}/err.msg)"
|
||||
if [[ "$response" -eq 200 ]]; then
|
||||
echo_ok
|
||||
else
|
||||
echo_failed
|
||||
if [[ -s "$(cat ${LOCK_DIR}/err.msg)" ]] ; then
|
||||
error "$(cat ${LOCK_DIR}/err.msg)"
|
||||
fi
|
||||
websites_failed_arr+=("$_site")
|
||||
fi
|
||||
done
|
||||
else
|
||||
warn "No Website to check given (empty var 'WEBSITES_TO_CHECK')"
|
||||
fi
|
||||
|
||||
if [[ ${#websites_failed_arr[@]} -gt 0 ]] ; then
|
||||
#if [[ ${#websites_failed_arr[@]} -eq 1 ]] ; then
|
||||
# err_msg="\n[ Error ]: Website ${websites_failed_arr[0]} does NOT respond as exspected\n"
|
||||
# error "Website ${websites_failed_arr[0]} does NOT respond as exspected"
|
||||
#else
|
||||
error "Some Websites does not respond as expected:"
|
||||
err_msg="\n[ Error ]: Some Websites does not respond as expected:\n\n"
|
||||
for _site in ${websites_failed_arr[@]} ; do
|
||||
err_msg+=" $_site\n"
|
||||
if $LOGGING ; then
|
||||
echo -e "\t \033[1m$_site\033[m"
|
||||
#echo -e "\n\tWebsite \033[1m$_site\033[m does NOT respond as exspected"
|
||||
else
|
||||
echo " $_site"
|
||||
fi
|
||||
done
|
||||
#fi
|
||||
|
||||
datum="$(date +"%d.%m.%Y %H:%M")"
|
||||
for _email in ${alert_email_arr[@]} ; do
|
||||
echo -e "To:${_email}\n${content_type}\nSubject:Error: Website(s) not reachable - ${datum}\n$err_msg" \
|
||||
| /usr/sbin/sendmail -F "$company - Check Websites" -f $sender_address $_email
|
||||
done
|
||||
fi
|
||||
|
||||
if $LOGGING ; then
|
||||
echo ""
|
||||
fi
|
||||
clean_up 0
|
||||
|
||||
#curl --max-time 35 --connect-timeout 30 -L --write-out %{http_code} --silent --output /dev/null https:/ats.warenform.de
|
||||
Reference in New Issue
Block a user