ServerCheck (v0.6)

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

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:

  • mail
  • curl

Code:

#!/bin/bash
##Set Default Options
CHECKFOR="<meta name=\"ServerCheck\" content=\"Success\" />" ##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 "\033[1;32mOK!"
		echo -e "\033[0m"
		echo -e "Recovery successful.\n\nNothing is needed for ${CHECKURL}\n\n" | mail -s "ServerCheck" "${EMAIL}"
	else
		echo -n -e "\033[1;31mFailed!"
		echo -e "\033[0m"
		echo -e "Recovery failed.\n\nManual fix may be needed for ${CHECKURL}.\n\n" | mail -s "ServerCheck" "${EMAIL}"
	fi
}
 
USAGE="Usage: ${0} -s <WEB_ADDRESS> -e <EMAIL> -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 "\033[1;32mOK!"
		echo -e "\033[0m"
	else
		echo -n -e "\033[1;31mFailed!"
		echo -e "\033[0m"
		echo "String not found."
 
		if [ "$RESTORECMD" ]
		then
			Restore	
		else
			echo -e "Wrong output recieved.\n\nPlease check ${CHECKURL}.\n\n" | mail -s "ServerCheck" "${EMAIL}"
		fi
	fi
else
	echo -n -e "\033[1;31mFailed!"
	echo -e "\033[0m"
	echo "Some other error occurred."
 
	if [ "$RESTORECMD" ]
	then
		Restore
	else
		echo -e "Server may be down.\n\nPlease check ${CHECKURL}.\n\n" | 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.

0 Responses to “ServerCheck (v0.6)”


  1. No Comments

Leave a Reply





User Agent: CCBot/1.0 (+http://www.commoncrawl.org/bot.html)
Character Encoding: UTF-8
Generated: 09/07/2010 22:52:27 UTC
Rendering Time: 0.775 seconds.