#!/bin/sh

# Web server monitor.  Copyright (C) Zeus Technology, 1997.

# --- configurables ----
# the localtion of the monitor program
MONITOR=./monitor

# the interval in seconds between checking the webserver
CHECK_PERIOD=600



# alter this function to notify the administrators in the way you require.
# This example mails a report to the webmaster, but it would be possible,
# for example, to modify it to call a page.

# the mail addresses to email
ADMINS="webmaster@localhost"

# the location of the sendmail program
SENDMAIL="/usr/lib/sendmail"

report_error()
{
	HOST=$1
	MSG=$2
	printf "To: $ADMINS\nFrom: web-monitor\nSubject: problem with %s\n\nThe Web monitor detected a failure, the error was:\n  %s\n"  $HOST "$MSG" | $SENDMAIL $ADMINS
}



if [ $# -eq 0 ] 
then
	echo "Usage $0 <hostname>[:port] <hostname>[:port] ..."
	exit 1
fi

HOSTS=$*


while [ 1 ]
do
	for HOST in $HOSTS
	do
		error="`$MONITOR $HOST`"
		if [ $? -eq 1 ]
		then
		   report_error $HOST "$error"
		fi
	done
	sleep $CHECK_PERIOD
done




