[Preston] Script to check Apache
Kevin Porter
kev at 9ballpool.co.uk
Thu Apr 17 09:54:02 BST 2008
Bollocks. I thought this would be a nice pre-breakfast exercise in bash
scripting, so I knocked up a script, but I guess it's now surplus
to requirements since people have pointed you to a good open source
solution.
Oh well, I wrote it so I'll post it (below). You never know, it might be
useful. You would use it like:
./check_apache.sh "http://billiardsearch.net"
although you would probably just create a tiny webpage that returns just
a '1' if server is alive, rather than a full web page.
regards,
- Kev
----- BASH SCRIPT START -----
#!/bin/bash
# START Editable variables
EMAIL="kev at 9ballpool.co.uk"
# Don't know / no time to figure out how to get a HTTP header directly
from curl,
# so we'll use the --dump-header option to output headers to a file,
then we will
# check the first line of the file for the status of the request
HEADERS_DUMP_FILE="/tmp/headers.$$"
# END Editable variables
# Which web page to request (first command line arg)
if [ -z $1 ]; then
echo "Usage: check_apache.sh URL"
exit 1
else
URL=$1
fi
# Check we have mail
MAILPROG=`which mail 2>/dev/null || echo arse`
#echo MAILPROG = $MAILPROG
if [ $MAILPROG == 'arse' ]; then
echo "Arse! No curl installed"
exit 1
fi
# Check we have curl
CURL=`which curl 2>/dev/null || echo arse`
if [ $CURL == 'arse' ]; then
echo "Arse! No curl installed"
exit 1
fi
# Run curl
$CURL "$URL" --dump-header "$HEADERS_DUMP_FILE" >/dev/null 2>/dev/null
# If the headers dump file doesn't exist, we know we couldn't contact
apache for
# some reason
if [ ! -f "$HEADERS_DUMP_FILE" ]; then
REPORT_STR="Couldn't connect to web server"
echo "$REPORT_STR"
echo "$REPORT_STR" | $MAILPROG -s "$URL status report" "$EMAIL"
exit 1
fi
# The headers dump file does exist, so check the first line. Report
success if
# the status is 200, failure otherwise
FIRST_LINE=`head -1 "$HEADERS_DUMP_FILE"`
#echo FIRST_LINE = $FIRST_LINE
HTTP_STATUS=`echo $FIRST_LINE | awk '{print $2}'`
#echo HTTP_STATUS = $HTTP_STATUS
# Clean up
rm -rf "$HEADERS_DUMP_FILE"
# Report success/failure and exit
if [ $HTTP_STATUS == 200 ]; then
echo "HTTP status OK (200)"
exit 0
else
REPORT_STR="HTTP status not OK ($HTTP_STATUS)"
echo "$REPORT_STR"
echo "$REPORT_STR" | $MAILPROG -s "$URL status report" "$EMAIL"
exit 1
fi
----- BASH SCRIPT END -----
Deepan wrote:
> Hi All,
> I am looking for a script to check if Apache is up
> and running, if not the script should send an
> email automatically. The script has to be run from
> a different machine (not from the machine that
> runs apache). So it has to query for a http
> address and if it receives any other reply that
> 200 OK, it should send the email. Can you guys
> suggest on how to go about ? Should I use curl ?
> or wget ? or ping ? I can handle the email part
> and cron part. If you guys have a similar script
> please share it if possible.
> Regards
> Deepan
> Sudoku Solver: http://www.sudoku-solver.net/
>
>
>
> _______________________________________________
> Preston mailing list
> Preston at mailman.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/preston
>
>
>
>
--
Kevin Porter
Advanced Web Construction Ltd
http://www.9ballpool.co.uk
http://billiardsearch.net
More information about the Preston
mailing list