#!/bin/ksh
#
# mailq: Server mail queue check - BB external script test
#
# version 1.0
# version 1.1 (10-03-02) - now color coded errors, and proper error check on port 25
#
# BIG BROTHER / XXXXXXXXXXXXXXXX status
#
# Written by Paul A. Luzzi
#  on July 15, 2002
#

########################################
# NOTE
# This has been tested with BB 1.2b, 1.4h2, 1.5, 1.5a
# 
# Tested on :
#   HP K460 - HPUX 10.20
########################################

########################################
# INSTALLATION
#  step 1  - update the EXT section of the runbb.sh script to include this
#  ...
########################################

##################################
# CONFIGURE IT HERE
##################################
TEST="mailq"                            # THE NAME OF OUR TEST
MAILQ="/usr/bin/mailq"                  # REQUIRED COMMAND
WARN="10"                               # WARNING LEVEL (yellow)
PANIC="100"                             # PANIC LEVEL (red)
NEXTHOP="pershing.com"                  # SMART MAIL HOST

##################################
# Start of script
##################################

if test ! "$BBHOME"
then
	echo "template: BBHOME is not set"
	exit 1
fi

if test ! -d "$BBHOME"
then
	echo "template: BBHOME is invalid"
	exit 1
fi

if test ! "$BBTMP"                      # GET DEFINITIONS IF NEEDED
then
	echo "template: The BB environment is not set"
	exit 1
fi

#####
#####  Start of custom section added by Paul A. Luzzi
#####

#####
#####  Get Status proc - used to get all responses
#####
get_status() 
{

  #####
  #####  Setup any and all variables before beginning.
  #####

  #####
  #####  Setup some variables for use later
  #####
  COLOR="green"
  
  #####
  #####  Mail queue information
  #####
  echo "<P><DIV ALIGN=\"CENTER\"><HR>" 
  echo "<B>============== Mail Queue Status ==============</B>"
  echo "<B>--- ($MAILQ  ....) ---</B>"
  echo "<HR></DIV>" 
  echo "<BLOCKQUOTE>" 

  #####
  #####  Find out how many messages are in the queue
  #####
  MSGCNT=`$MAILQ | $GREP -v "/var/spool/mqueue is empty" | $WC -l`
  LEVEL=`$EXPR $MSGCNT / 3 `
  LEVEL=`echo $LEVEL`                     # GET RID OF SPACES

  if [ "$LEVEL" -ge "$PANIC" ]
    then
      COLOR="red"
      echo "<FONT COLOR=$COLOR>Problem with mail queue - at panic level. Too many messages queued!"
      echo "Actually reported $LEVEL queued messages.</FONT>"
     elif [ "$LEVEL" -ge "$WARN" ]
    then
      COLOR="yellow"
      echo "<FONT COLOR=$COLOR>Problem with mail queue - at warning level."
      echo "Actually reported $LEVEL queued messages.</FONT>"
     else
      echo "No reported problems with mail queue - no queued messages."
    fi

  #####
  #####  Since its such a critical server, check the next hop 
  #####    for connectivity status.
  #####
  echo "</BLOCKQUOTE>" 
  echo "<P><DIV ALIGN=\"CENTER\"><HR>" 
  echo "<B>============== Next Hop Status ==============</B>"
  echo "<B>--- (telnet $NEXTHOP 25  ....) ---</B>"
  echo "<HR></DIV>" 
  echo "<BLOCKQUOTE>" 
  CATCH_COMMAND=`/usr/bin/telnet $NEXTHOP 25 <<EOMT

quit
EOMT`
  echo "\nStatus of $NEXTHOP smtp server :\n"
  SMART_STATUS=`echo $CATCH_COMMAND | $GREP ready | $GREP -v not`
  if [ -z "$SMART_STATUS" ]
    then
      COLOR="red"
      echo "<FONT COLOR=$COLOR>Problem with next hop mailserver - aka smarthost - of $NEXTHOP"
      echo "It is not accepting our requests."
      echo "Actually reported : \n"
    fi
    echo "$CATCH_COMMAND</FONT>"

  #####
  #####  Close up the block quoting
  #####
  echo "</BLOCKQUOTE>" 

  #####
  #####  Make sure to export COLOR so that it gets back to "central"
  #####
  export COLOR

#####
#####  End of get_status proc
#####
}

#####
#####  Main body
#####
touch /tmp/$THIS_HOST.mailq
$GREP $THIS_HOST $BBHOSTS | $GREP "mailq" | 
while read line
  do
    if [ ! -z "$line" ]
      then
        echo "<BR><DIV ALIGN=\"CENTER\"><HR></DIV>" > /tmp/$THIS_HOST.mailq
        echo "Mail queue check on $THIS_HOST " >> /tmp/$THIS_HOST.mailq
        echo "Captured : `$DATE` " >> /tmp/$THIS_HOST.mailq
        get_status >> /tmp/$THIS_HOST.mailq
        $BB $BBDISP "status $THIS_HOST.mailq $COLOR `$DATE` `$CAT /tmp/$THIS_HOST.mailq` "
      else
        COLOR="clear"
        $BB $BBDISP "status $THIS_HOST.mailq $COLOR `$DATE` `$CAT /tmp/$THIS_HOST.mailq` "
      fi
    done

#####
#####  End of custom section added by Paul A. Luzzi
#####
  
##############################################
# end of script
##############################################
