#!/usr/local/bin/bash


# updated to check port 80 and 443

rm /index.html

echo $1 > /usr/local/bin/hosts
var1=`/usr/local/bin/portmon --timeout=10 --config=/usr/local/bin/hosts --report='The host %h:%p is %w' | awk '{print $5}'`



# www.weather.com
if [ "$var1" != "normal" ]; then
sleep 60
var2=`/usr/local/bin/portmon --timeout=10 --config=/usr/local/bin/hosts --report='The host %h:%p is %w' | awk '{print $5}'`
if [ "$var2" != "normal" ]; then

if [[ "$2" == "NOEMAIL" ]]
then
sleep 1
else
			echo PORT FAIL $1 |  mailx -s "$1 PORT FAIL" $2
fi
echo port fail $1 `date`
		else
			echo $1 2nd check OK
		fi
#	sleep 1
fi

#content check
if [[ $1 == *44* ]]
then

links -dump -receive-timeout  10 $4 > /index.html 
varcontent=`grep "$3" /index.html`
if [[ "$varcontent" == "" ]]; then
if [[ "$2" == "NOEMAIL" ]]
then
sleep 1
else
echo CONTENT FAIL L $1  |  mailx -s "$1 CONTENT FAIL" $2
fi
echo CONTENT FAIL $4 `date`
cp /index.html /data/stuff/temp/`date "+FAIL_%m-%d-%Y-%H-%M-%S.html"`
#sleep 1
fi


else

wget --timeout=10 --waitretry=3 -q -O /index.html $4
#wget -q -O /index.html $4
varcontent=`grep "$3" /index.html`
if [[ "$varcontent" == "" ]]; then
if [[ "$2" == "NOEMAIL" ]]
then
sleep 1
else
echo CONTENT FAIL W $1  |  mailx -s "$1 CONTENT FAIL" $2
fi
echo CONTENT FAIL $4 `date`
cp /index.html /data/stuff/temp/`date "+FAIL_%m-%d-%Y-%H-%M-%S.html"`
#sleep 1
fi



fi



