An Apache Watchgoose

In the past, people have used geese in the place of watchdogs because they honk and flap rather a lot. Usually, the domestic Greylag Goose is the preferred choice as it’s half a kilo heavier than the Swan Goose. It was also a goose that God chose to infuse with his divine will in 1096 as part of the People’s Crusade, according to the Germans that followed him around the Rhine region, killing any Jews they found. Those pesky Germans, eh?

So, with that in mind, when I found myself looking for a watchdog script to monitor and fix Apache and nothing fitted my needs exactly, I decided to infuse my script with the power and will of the goose. I don’t know if this will be of use to anyone, but my previous script was well received by one or two people so here you go.

#!/bin/bash
#
# watchgoose.sh - an Apache restartinator.
# Put it in your crontab every five minutes
#
export site="http://cat5.org/"        # replace with a site running on your webserver
export tempfile=~jearle/bin/goose.txt # This can be anywhere
export logfile=~jearle/bin/goose.log  # Logfile
export email=jearle@example.com       # Your email address
if [ "`curl -sIm 5 ${site} | head -1 | tr -d '\r'`" != "HTTP/1.1 200 OK" ]; then
	echo `date +%F\ %T` > $tempfile
	/usr/bin/top -b -n1 >> $tempfile
	ps wwaux | awk '/httpd/{print "kill -9 " $2}' | sh
	ipcs -s | grep apache | awk ' { print $2 } ' | xargs -n 1 ipcrm -s
	/usr/sbin/apachectl graceful >> $tempfile
	echo "Attempted Apache restart at `date +%F\ %T`" >> $tempfile
	echo "Attempted Apache restart at `date +%F\ %T`" >> $logfile
	cat ${tempfile} | /bin/mail $email -s "[CAT5] Restarted Apache"
	rm ${tempfile}
fi
echo "`date +%F\ %T` and all is well ..." >> $logfile

Apologies to those who came here to see how the native Americans used waterfowl to guard their teepees. This isn’t the post you’re looking for. Honk, honk! Oh, and if you’re looking for someone to cripple your goose, try Pet Connection NI instead.

Update: Sometimes Apache decides to be a little bitch and not start up because it loves hoarding semaphores. I’ve added a ipcs line to fix that. Thanks to outofcontrol for something to copy/paste.

Jared Earle is a writer, photographer and systems administrator. You can find him on Twitter most of the time.

Posted in geek Tagged with: , ,