#!/bin/bash # Check status of a list of servers # # Any servers in URLS that yield a bad response are collated and sent via email # to all RECIPIENTS. # # Dependencies # - curl: Polling the servers # - msmtp: Sending email; requires separate configuration # NOTE: Currently only useful for services running over HTTP #set -eux LOG_FILE="/media/workshop/Projects/openmedianetwork/.omn_watchdog.log" RECIPIENTS=( saunders@openworlds.info witchescauldron@openworlds.info ) URLS=( unite.openworlds.info # NOTE: [Currently down] openworlds.info pad.openworlds.info visionon.tv activism.openworlds.info campaign.openworlds.info #indymedia.openworlds.info #roadstonowhere.openworlds.info #indymedia.hs2rebellion.earth ) # TODO: How to check these sensibly? SERVICES=( chat.openworlds.info conference.openworlds.info # Not much point checking the mail server # as this script relies on it to send reports # TODO: Separate local check just for this? #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 } send_mail() { local -n error_list=$1 local error_string="" for i in "${!error_list[@]}"; do # Use bash's awful syntax to escape any / in the server string code=${i//\//\\\/} server=${error_list[$i]} error_string+=" ${code}: ${server}\n" done cat mail_template \ | sed "s/{ERR_LIST}/${error_string}/g" \ | msmtp -a omn_server_watch ${RECIPIENTS[@]} \ || echo "[ERROR] Failed to send mail!" } main