I needed to write a quick script to monitor the availability & performance of a web application cluster. The following quick script is set so that it follows redirects (HTTP Location header), which was required to measure it properly. It optionally searches for text in the output to measure whether or not the results were as expected, and returns -1 if the text is not output.
The following was written and tested under Solaris 10. You may need to change the time and or awk command flags on other UNIX distributions so that it displays the same. If ksh is not available, switch it to bash.
Here is an example of it being used as a UserParameter:
UserParameter=checkurl[reciprocal],/opt/local/bin/checkurl 'http://www.reciprocalnet.org/recipnet/search.jsp?browse=1' "Next page"
The following was written and tested under Solaris 10. You may need to change the time and or awk command flags on other UNIX distributions so that it displays the same. If ksh is not available, switch it to bash.
Code:
#!/bin/ksh
# syntax: checkurl URL TEXT
URL=$1
TEXT=$2
TEMP_FILE=/tmp/checkurl.$$
WEB_CMD="curl -L -o $TEMP_FILE"
TIME="/usr/bin/time -p"
elapsed=`$TIME $WEB_CMD $URL 2>&1 | grep real | awk '{ print $2 }'`
# if we are supposed to check for some text
# instead of just saying "ya, it works".
if [ "$TEXT" != "" ]; then
if [ "`grep "$TEXT" $TEMP_FILE`" = "" ]; then
elapsed=-1
fi
fi
rm $TEMP_FILE
echo $elapsed
UserParameter=checkurl[reciprocal],/opt/local/bin/checkurl 'http://www.reciprocalnet.org/recipnet/search.jsp?browse=1' "Next page"
Comment