#!/bin/sh
# check_servers0.sh: Check web server by dowonloading some files.
# For use with crontab every day.
# Before using, you should modifiy the proxy setting to your proxy (or set PROXY=off),
# modify the list of files for downloading, HTTPURLS, and create the subdirectory
# with the original files, originals.
# Example for the subdirectory originals with one file: 
# HTTPURLS="http://www.true-random.com/pictures/logos/us_eflag.gif"
# mkdir -p originals/http://www.true-random.com/pictures/logos
# cp -i /home/user/html/pictures/logos/us_eflag.gif originals/http://www.true-random.com/pictures/logos/
#
# Version 1.3, 2009-06-17
#
# ----------------------------------------------------------------------------
# "THE BEERWARE LICENSE" (Revision 44):
# Dr. Rolf Freitag (rolf dot freitag at email dot de) wrote this file.
# As long as you retain this notice you can do whatever
# the GPL (GNU Public License version 3) allows with this stuff.
# If you think this stuff is worth it, you can send me money via
# paypal, and get a contribution receipt if you wish, or if we met some day
# you can buy me a beer in return.
# ----------------------------------------------------------------------------

# uncomment the next line for debugging
#set -x

# user for receiving the emails: The actual user.
MAILUSER="$USER"

# integer i for counting
typeset -i i=0 

# Dowonload sub-directories: Do not change them to get a correct error message.
FTPDIR="ftp://"
HTTPDIR="http://"
HTTPSDIR="https://"

# Proxy settings
export http_proxy="192.168.1.1:3128"
export https_proxy="$http_proxy"
export ftp_proxy="$http_proxy"

# user agent version
USER_AGENT=Mozilla/5.0\ \(compatible\;\ Googlebot/2.2\;\ +http://www.google.com/bot.html\)
#USER_AGENT=Mozilla/4.0\ \(compatible\;\ MSIE\ 7.0\;\ Windows\ NT\ 6.0\;\ SLCC1\;\ .NET\ CLR\ 2.0.50727\;\ .NET\ CLR\ 3.0.04506\)
#USER_AGENT=Mozilla/4.0\ \(compatible\;\ MSIE\ 6.0\;\ Windows\ NT\ 5.1\;\ SV1\;\ .NET\ CLR\ 1.1.4322\)
#USER_AGENT=Mozilla/5.0\ \(compatible\;\ MSIE\ 7.0\;\ Windows\ NT\ 6.0\;\ MSIECrawler\)

# with or without proxy (on/off)
PROXY=on

# make clean
rm -rf "$FTPDIR" "$HTTPDIR" "$HTTPSDIR"

# Check the connection to the internet with httpings to big and important domains at port 80,
# because in a network without a default gateway ping does not work while httping does the same way the 
# downloads do work (with a proxy).
# httping with one packet, show HTTP status code, proxy, url, port.
for URL in http://www.ebay.com http://www.msnbc.msn.com http://www.whitehouse.gov http://freshmeat.net http://sourceforge.net/; do
{ httping -c 1 -s -x $http_proxy -g $URL -p 80 &> /dev/null ; }&& i=i+1 && break
done


if [ $i -eq 0 ] ; then
  # not connected: wait 2 minutes for a new connection
  sleep 120
fi

# set i to zero because it will be next used as exit value
i=0


# for debugging, ftp section
echo "line A"


# download test files
# With http wget returns 0 even when the file does not exist!

#FTPURLS=""


# for debugging, http section
echo "line B"


HTTPURLS="http://www.true-random.com/pictures/logos/us_eflag.gif http://true-random.com/homepage/projects/vc840/README http://true-random.com/homepage/projects/servers/httpd.conf.local http://true-random.com/homepage/projects/freq_counter0/Makefile"
# http://127.0.0.1/pictures/logos/us_eflag.gif"

# http downloads
for FILE in $HTTPURLS ; do
  wget -e robots=off --proxy=$PROXY --user-agent="$USER_AGENT" --timeout=60 --dns-timeout=50 --connect-timeout=50 --read-timeout=50 --limit-rate=10k -np -r -q "$FILE" --directory-prefix=$HTTPDIR
done


# for debugging, https section
echo "line C"

#HTTPSURLS=""

# https downloads
#  wget --no-check-certificat "$FILE" --directory-prefix=$HTTPSDIR


# for debugging, downloaded http files checking section
echo "line D"

# check if the downloaded test files exist
for FILE in $HTTPURLS ; do
  if [ ! -e "$FILE" ];
  then
    echo "$FILE could not be downloaded"
    mail -s "$FILE could not be downloaded" "$MAILUSER"@localhost </dev/null ;
    i=i+1
  else
    # compare the file and the original
    diff $FILE originals/$FILE
    if [ $? -ne 0 ];
    then
      mail -s "$FILE could be downloaded but is a fake" "$MAILUSER"@localhost </dev/null ;
      i=i+1
    fi
  fi
done


# echo the number of files which could not be downloaded correct
echo "$i files could not be downloaded correct."


# exit with the number of not downloadable or faked files
exit $i

