Improve readability

main
mj-saunders 2022-11-03 16:18:06 +04:00
parent 01295e851f
commit 0bffafcc82
1 changed files with 47 additions and 40 deletions

View File

@ -30,18 +30,63 @@ SERVICES=(
#mail.openworlds.info
)
main()
{
declare -A FLAGGED
rotate_log LOG_FILE
# Redirect all output to LOG_FILE but preserve a link to stdout should we
# still want to use it
exec 3>&1 1>>${LOG_FILE} 2>&1
echo ">> $(date)"
echo "== Checking:"
for uri in "${URLS[@]}"; do \
full_uri="https://${uri}"
echo -n " ${full_uri} ... ";
stat="$(curl -s -o /dev/null -w "%{http_code}%{stdout}" ${full_uri})"
echo "${stat}"
# All other HTTP responses should not technically be errors
if ((${stat} >= 400 && ${stat} <= 599)); then
FLAGGED[${full_uri}]=${stat}
fi
done
ret=0
if ((${#FLAGGED[@]} > 0)); then
echo "== Error(s) detected. Sending mail to:"
for rcp in ${RECIPIENTS[@]}; do
echo " ${rcp}"
done
send_mail FLAGGED
ret=1
else
echo "== OK"
fi
exit ${ret}
}
rotate_log()
{
local -n log=$1
local max_log=4096 # 4M as we get size in K below
touch ${log}
local log_size=$(du -k "${log}" | cut -f 1)
if ((${log_size} > ${max_log})); then
# Clobber any previous backup
mv -f ${log} ${log}.1
touch ${log}
fi
touch ${log}
}
send_mail()
@ -62,42 +107,4 @@ send_mail()
|| echo "[ERROR] Failed to send mail!"
}
declare -A FLAGGED
rotate_log LOG_FILE
# Redirect all output to LOG_FILE but preserve a link to stdout should we
# still want to use it
exec 3>&1 1>>${LOG_FILE} 2>&1
echo ">> $(date)"
echo "== Checking:"
for uri in "${URLS[@]}"; do \
full_uri="https://${uri}"
echo -n " ${full_uri} ... ";
stat="$(curl -s -o /dev/null -w "%{http_code}%{stdout}" ${full_uri})"
echo "${stat}"
# All other HTTP responses should not technically be errors
if ((${stat} >= 400 && ${stat} <= 599)); then
FLAGGED[${full_uri}]=${stat}
fi
done
ret=0
if ((${#FLAGGED[@]} > 0)); then
echo "== Error(s) detected. Sending mail to:"
for rcp in ${RECIPIENTS[@]}; do
echo " ${rcp}"
done
send_mail FLAGGED
ret=1
else
echo "== OK"
fi
exit ${ret}
main