#!/bin/sh
# This script checks the 'ps ax' output to see if squid is still running.  If
# not, then it will attempt ($NUMTRIES times) to restart Squid,
# else it will send mail to $MAILTO.  $LOCKFILE shouldn't generally need to be
# changed.  $SQUIDDIR is the location of the squid cache files.
#
# Copyright (c) 2003 by Paul Kreiner

LOCKFILE=/var/lock/subsys/squid-grep
NUMTRIES=3
MAILTO=root
SQUIDDIR=/var/spool/squid
IPTABLES=/usr/local/sbin/iptables

# SCRIPT BEGINS #
# Is the lock set?  If so, then exit.
if [ -e $LOCKFILE ]; then
##  echo "DEBUG: lock already set!"
  exit 0
fi
touch $LOCKFILE

COUNT=$NUMTRIES
SQUID=`ps ax | grep squid | grep -v grep | wc -l`
##echo "DEBUG: variables COUNT,SQUID = $COUNT, $SQUID"

# Check if squid is running.  If so, then remove the lock & exit.
if [ $SQUID -ge 1 ]; then
  rm -f $LOCKFILE
##  echo "DEBUG: Squid already running(1).  Exiting..."
  exit 0
fi

##echo "DEBUG: Squid not running(2).  Trying to restart it!"
# Squid wasn't running, so loop a few times, trying to start it:
while [ $COUNT -gt 0 ] && [ $SQUID -eq 0 ]; do
##  echo "DEBUG:   Trying (attempt $(( 1+$NUMTRIES-$COUNT )) of $NUMTRIES)..."
  COUNT=$(( $COUNT-1 ))
  killall -q squid; killall -q RunCache; sleep 2
  RunCache > /dev/null 2>&1 &
  sleep 6
  SQUID=`ps ax | grep squid | grep -v grep | wc -l`
done

# Exit if squid got started eventually...
if [ $SQUID -ge 1 ]; then
  rm -f $LOCKFILE
##  echo "DEBUG: Squid is running(3).  Exiting successfully..."
  exit 0
fi

# Squid still didn't get started, so try nuking the cache directories, then
# restarting squid.
##echo "DEBUG: last-ditch effort to clear squid cache & restart! (4)"
killall -q -9 -w squid; killall -q -9 -w RunCache
rm -rf $SQUIDDIR/0*
rm -f $SQUIDDIR/squid.pid $SQUIDDIR/cache.log $SQUIDDIR/swap.state
sync
##echo "DEBUG: (4) recreating squid cache dirs..."
squid -z
sync
##echo "DEBUG: (4) restarting Squid..."
RunCache > /dev/null 2>&1 &
sleep 8

SQUID=`ps ax | grep squid | grep -v grep | wc -l`
rm -f $LOCKFILE

# One final check to see if squid restarted correctly:
if [ $SQUID -ge 1 ]; then
##  echo "DEBUG: Squid started successfully! (5)"
  exit 0
fi

# OK, we've done all we can, and squid still doesn't want to start.  So we
# bail out, sending an email to whoever is configured in $MAILTO...
##echo "DEBUG: Failure! Sending mail notification..."
cat << EOL | sendmail $MAILTO
Subject: Squid failure at `hostname -f`!
X-Priority: 1
Priority: Urgent
From: root@`hostname -f`
To: root@`hostname -f`

$0: Failed $NUMTRIES times to restart squid.
Also failed trying to delete squid directories and restart.

GIVING UP!
I've automatically deleted the Squid redirector firewalling rule,
so you'll need to re-run the firewall scripts (or reboot) after
fixing Squid.
EOL

killall -9 -q squid; killall -9 -q RunCache


# Here we list all iptables rules, and find the rule number with the
# port 3128 redirect, and delete it (the Squid redirect rule):
RULENUM=$(( `$IPTABLES -L -v -n -t nat | grep -n 3128 | cut -d: -f1` -2 ))
$IPTABLES -t nat -D PREROUTING $RULENUM
exit 127


