Skip to content

Instantly share code, notes, and snippets.

@tikenn
Last active August 30, 2018 09:53
Show Gist options
  • Save tikenn/a035748efd91368cee4515ee6713b1ad to your computer and use it in GitHub Desktop.
Save tikenn/a035748efd91368cee4515ee6713b1ad to your computer and use it in GitHub Desktop.
Monitors the HTTP status of a web server and sends and alert email to an administrator if the server goes down
#!/bin/bash
#
# --------------------------------------------------------------------------------------------
# Automatic Residency Wiki Web Server Status Checkup
# --------------------------------------------------------------------------------------------
# This file checks the status of the residency wiki database at
# https://resdb.physicianscientists.org/Main_Page in order to determine if the server has gone
# down. Basic "curl" is performed looking for HTTP 200 status code. An email alert is sent
# to an administrator if a 200 status code is not received.
#
# --------------------------------------------------------------------------------------------
# Author Info
# --------------------------------------------------------------------------------------------
# Name :: Tim Kennell Jr. ~ tikenn
#
# --------------------------------------------------------------------------------------------
# Config
# --------------------------------------------------------------------------------------------
# URL :: The web address of residency wiki database to check the status of
# EMAIL_FROM :: The fake email address that the system should use to send the alert
# EMAIL_TO :: The email address that the alert should be sent to
# EMAIL_SUBJECT :: The subject of the email alert
# EMAIL_BODY :: The body of the email alert
#
# --------------------------------------------------------------------------------------------
# Setting up crontab
# --------------------------------------------------------------------------------------------
# - Create a file in /etc/cron.d/
# - Suggested to run the file once a week
# - Example line (runs at midnight): "0 0 * * 1 /path/to/wiki_server_status"
#
# ~ tikenn
# Setting GLOBAL variables for the script
URL=https://resdb.physicianscientists.org/Main_Page
EMAIL_FROM=resdb@$HOSTNAME.server
EMAIL_TO=resdb@physicianscientists.org
EMAIL_SUBJECT="Residency Wiki Database Down"
EMAIL_BODY="The residency wiki server is down as of checking at $(date +"%F %T") "
# --------------------------------------------------------------------------------------------
# Core App
# --------------------------------------------------------------------------------------------
# Setting up the cron job file
cron_job_file="/etc/cron.d/wiki_server_status"
cron_job_intro="# Sets up the cron job for checking the server\n"
cron_job_intro+="# Main script at /opt/server_status\n\n"
cron_job_intro+="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\n\n"
cron_job_schedule="0 * * * * root /opt/server_status/server_status"
cron_job_schedule_down="0 8 * * * root /opt/server_status/server_status"
if ! [[ -f "$cron_job_file" ]] ; then
echo "cron file not created"
printf "$cron_job_intro" > "$cron_job_file"
printf "$cron_job_schedule" >> "$cron_job_file"
fi
# echo -e "The last cron job was run at $(date +"%F %T")\n" >> /opt/server_status/cron_job_test
# Check the status of the server for 200 status code and send alert email if 200 not returned
server_status=$(curl -sI "$URL" | head -n 1 | cut -d ' ' -f2)
if ! [[ "$server_status" = "200" ]] ; then
EMAIL_BODY+="and is throwing a $server_status status code"
# email error
echo -e "$EMAIL_BODY" | \
mutt -e "set from=$EMAIL_FROM realname='Residency Wiki Database Status Script'" \
-e "set crypt_use_gpgme=no" \
-s "$EMAIL_SUBJECT" \
-- "$EMAIL_TO"
# slow down cron job to avoid email bombardment
echo "$cron_job_intro" > "$cron_job_file"
echo "$cron_job_schedule_down" >> "$cron_job_file"
else
# speed up cron job if previously slowed down
if ! [[ $(awk 'NR==6' "$cron_job_file" | cut -d' ' -f2) = '*' ]] ; then
printf "$cron_job_intro" > "$cron_job_file"
printf "$cron_job_schedule" >> "$cron_job_file"
fi
fi
# ---------------------------------------------------------------------------------------------
# End of Core App
# ---------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment