Summary: This script will check to see if a web server is functioning properly by checking the output of a specific location. If this output is wrong, or unavailable, a message will be emailed. Additionally, if the server returns output which the script was not expecting, it has the ability to run a custom restore command and hopefully get the server functioning again.
Requirements:
- curl
Code:
#!/bin/bash ##Set Default Options CHECKFOR="" ##This is the what the script expects to find in the result. TIMEOUT="10" ##Max amount of time to wait for a connection. MAXTIME="30" ##Max time to spend trying to load page. ##Do not edit anything below this comment! function Restore { echo echo -n "Attempting Recovery..." ${RESTORECMD} > /dev/null 2>&1 if [ $? == 0 ] then echo -n -e "33[1;32mOK!" echo -e "33[0m" echo -e "Recovery successful.nnNothing is needed for ${CHECKURL}nn" | mail -s "ServerCheck" "${EMAIL}" else echo -n -e "33[1;31mFailed!" echo -e "33[0m" echo -e "Recovery failed.nnManual fix may be needed for ${CHECKURL}.nn" | mail -s "ServerCheck" "${EMAIL}" fi } USAGE="Usage: ${0} -s -e -f [CHECKFOR] -d [POST_DATA] -r [RESTORECMD]" echo "ServerCheck" echo "Scripted by Blake Johnson" echo "http://www.blakeanthonyjohnson.com/" echo while getopts ':s:f:r:e:d:h' OPTION; do case ${OPTION} in s) CHECKURL="${OPTARG}";; f) CHECKFOR="${OPTARG}";; r) RESTORECMD="${OPTARG}";; e) EMAIL="${OPTARG}";; d) POSTDATA="${OPTARG}";; h) echo ${USAGE};; ?) echo "Unknown option "-${OPTARG}"." >&2 echo ${USAGE};; :) echo "Option "-${OPTARG}" needs an argument." >&2 echo ${USAGE} exit 1;; *) echo ${USAGE} exit 1;; esac done if [ -z "$CHECKURL" ] then echo "One or more parameters are missing." echo "${USAGE}" exit 1 fi if [ -z "$EMAIL" ] then echo "One or more parameters are missing." echo "${USAGE}" exit 1 fi echo "Checking URL: "${CHECKURL}"" echo "Email: "${EMAIL}"" echo "Checking for: "${CHECKFOR}"" echo echo -n "Checking if server is working properly... " CURLCOMMAND="curl -s ${CHECKURL} --connect-timeout ${TIMEOUT} --max-time ${MAXTIME}" if [ $POSTDATA ] then CURLCOMMAND="${CURLCOMMAND} -d ${POSTDATA}" fi CURLOUTPUT=`${CURLCOMMAND}` if [ $? == 0 ] then if [[ "$CURLOUTPUT" =~ "$CHECKFOR" ]] then echo -n -e "33[1;32mOK!" echo -e "33[0m" else echo -n -e "33[1;31mFailed!" echo -e "33[0m" echo "String not found." if [ "$RESTORECMD" ] then Restore else echo -e "Wrong output recieved.nnPlease check ${CHECKURL}.nn" | mail -s "ServerCheck" "${EMAIL}" fi fi else echo -n -e "33[1;31mFailed!" echo -e "33[0m" echo "Some other error occurred." if [ "$RESTORECMD" ] then Restore else echo -e "Server may be down.nnPlease check ${CHECKURL}.nn" | mail -s "ServerCheck" "${EMAIL}" fi fi echo echo "This script will self-destruct in 6 seconds." sleep 6 exit 0
Note: Mac OS X users may have to change the line endings to the Unix equivalent.
Changes:
v0.6: Adds the ability to run a custom restore command.
v0.5: Adds the ability to add post data to a page request.
v0.3: Restructured how the parameters are read in.
v0.2: Allowed parameters to be set at the command line.
v0.1: Initial release.