#!/bin/bash
# 
# Check the status of a proxy server by downloading a sample URL directly and through the proxy and then comparing the two files
# 
# -- Written 2008/03/13 TA
# -- modified Thomas Wagner

# establish the default settings
#
# this can be overridden on the command line, but as long as the Nagios instance is up, this URL should work
SAMPLE_URL=http://uptime.uhi.ac.uk/

# This is always overriden with a single host URL and is only provided as an example
HTTP_PROXY=http://squid2.uhi.ac.uk:8080/

# This needs to be high enough to weather network glitches, but not so high that we end up with multiple instances waiting to finish
TIMEOUT=30

# The default error code is unknown in case I haven't thought of something
ERROR_CODE=-1

# comment this out to see the output from wget on the command line (and in the cron output)
LOG="-o /dev/null"

# parse the command line options
#
if [ -z "$1" ]; then 
    echo "Usage: $0 PROXY_URL [TEST_URL] [TIMEOUT]";
    echo "  PROXY_URL: The full URL (including port number and protocol) of the proxy server to be tested";
    echo "  TEST_URL:  The URL to attempt to load";
    echo "  TIMEOUT:   The time (in seconds) to wait before giving up on loading the test URL";
    echo;
    exit $ERROR_CODE;
else
    HTTP_PROXY=$1
fi

if [ ! -z "$2" ]; then
    SAMPLE_URL=$2
fi

if [ ! -z "$3" ]; then
    TIMEOUT=$3
fi


# required binaries
#
WGET=/usr/bin/wget
DIFF=/usr/bin/diff
DATE=/bin/date
BC=/usr/bin/bc

# The base timestamp used to uniquely tag the filenames as a matching set
TIMESTAMP=`$DATE +%s%N`

# the filenames we will create
#
PROXIED_FILE="/tmp/proxied_$TIMESTAMP.html"
UNPROXIED_FILE="/tmp/unproxied_$TIMESTAMP.html"

# Remove the temporary files if they already exist
#
if [ -e $UNPROXIED_FILE ]; then rm $PROXIED_FILE; fi
if [ -e $PROXIED_FILE ]; then rm $PROXIED_FILE; fi

# Get the content directly
TIMESTAMP2=`$DATE +%s%N`
$WGET -t 1 --timeout=$TIMEOUT $SAMPLE_URL -O $UNPROXIED_FILE $LOG
if [ $? -ne 0 ]; then
	echo "UNKNOWN - wget for direct link exited with $?"
	exit 3
fi

# Get the content through the proxy
TIMESTAMP3=`$DATE +%s%N`
$WGET -t 1 --timeout=$TIMEOUT --proxy --execute=http_proxy=$HTTP_PROXY -O $PROXIED_FILE $SAMPLE_URL $LOG
if [ $? -ne 0 ]; then
	echo "UNKNOWN - wget for proxied link exited with $?"
	exit 3
fi
TIMESTAMP4=`$DATE +%s%N`


# Debug
#echo "<<<<<<<<<<<<<<<<<< Proxied >>>>>>>>>>>>>>>>>>>>>>>>"
#cat $PROXIED_FILE
#echo "<<<<<<<<<<<<<<<<<< Unproxied >>>>>>>>>>>>>>>>>>>>>>"
#cat $UNPROXIED_FILE

if [ ! -s "$UNPROXIED_FILE" -o ! -s "$PROXIED_FILE" ]; then
	# at least one file is missing or empty
	if [ ! -s "$UNPROXIED_FILE" -a ! -s "$PROXIED_FILE" ]; then
		# both are missing or empty
		echo "CRITICAL - Could not load file trough both proxy and direct link"
		ERROR_CODE=2
	elif [ ! -s "$PROXIED_FILE" ]; then
		# proxied file is missing or empty
		echo "CRITICAL - Could not load the content through the specified proxy server."
		ERROR_CODE=2	
	elif [ ! -s "$UNPROXIED_FILE" ]; then
		# direct link download is missind or empty
		echo "CRITICAL - Could not load the content through the direct link."
		ERROR_CODE=2
	fi
else 
	# both files are here, compare them
    	
    	# Compare the two files
	DIFF_OUTPUT=`$DIFF $PROXIED_FILE $UNPROXIED_FILE`;
	#echo $DIFF_OUTPUT

    	DIRECT_TIME=`echo "scale=3; ($TIMESTAMP3-$TIMESTAMP2)/1000000000" | $BC`;
	PROXIED_TIME=`echo "scale=3; ($TIMESTAMP4-$TIMESTAMP3)/1000000000" | $BC`;
	ELAPSED_TIME=`echo "scale=3; ($TIMESTAMP4-$TIMESTAMP2)/1000000000" | $BC`;
	FILE_SIZE=`du $PROXIED_FILE | awk '{ print $1 }'`;
	PERF_DATA=`echo "| direct_time="$DIRECT_TIME"s;;;; proxied_time="$PROXIED_TIME"s;;;; file_size="$FILE_SIZE";;;;"`
	
	if [ ! -z "$DIFF_OUTPUT" ]; then 
	    echo "WARNING - The content downloaded through the proxy does not match the live content. $PERF_DATA";
	    echo `date` > /tmp/check_proxy.log
	    echo "Proxied file" `du $PROXIED_FILE` >> /tmp/check_proxy.log	  
	    echo "Unproxied file" `du $UNPROXIED_FILE` >> /tmp/check_proxy.log
	    echo $DIFF_OUTPUT >> /tmp/check_proxy.log
	    echo "EOF" >> /tmp/check_proxy.log
	    # debug
	    echo "Proxied file:\n" >> /tmp/check_proxy.log
	    cat $PROXIED_FILE  >> /tmp/check_proxy.log
	    echo "Unproxied file:\n"  >> /tmp/check_proxy.log
	    cat $UNPROXIED_FILE >> /tmp/check_proxy.log
	    
	    cat /tmp/check_proxy.log
	    ERROR_CODE=1;
	else 
	    echo "OK - Proxy server accessible and serving up the same content as a direct link. $PERF_DATA";
	    ERROR_CODE=0;
	fi
    
fi



if [ -e $UNPROXIED_FILE ]; then rm $UNPROXIED_FILE; fi
if [ -e $PROXIED_FILE ]; then rm $PROXIED_FILE; fi

exit $ERROR_CODE;

# Predefined exit codes for Nagios/NetSaint
#	UNKNOWN  = -1
#	OK       =  0
#	WARNING	 =  1
#	CRITICAL =  2
