diff --git a/status-check.sh b/status-check.sh index d72491c..6050306 100755 --- a/status-check.sh +++ b/status-check.sh @@ -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