#!/bin/sh
#
# mail: Sun Enterprise Server check - BB external script test
#
# version 1.0
# version 1.1 - properly uses $CAT instead of cat
# version 1.2 - properly uses $MACHINE instead of `uname -n` in bb-hosts check
# version 1.3 - properly uses $THIS_HOST instead of $MACHINE due to fqdn using comma in name - thanks Craig Cook
# version 1.4 - updated the "bad words" list 
# version 1.5 - updated the "bad words" to do does case insensitive now
# version 1.6 - updated the "bad words" to do exclude for "transferred", which had matched "err"
# version 1.7 - updated the "bad words" to do exclude for "hour.errlog", which had matched "err"
# version 1.8 - updated the "bad words" to do exclude for "day.errlog", which had matched "err"
#
# BIG BROTHER / XXXXXXXXXXXXXXXX status
#
# Written by Paul A. Luzzi
#  on March 22, 2000
#

########################################
# NOTE
# This has been tested with BB 1.2b, 1.4h2, 1.5, 1.5a
# 
# Tested on 
#   Sun Ultra 60, E220R/420R, E250/450, E3000/3500, E4500
#   AIX 43P
#   UnixWare 7
#   RedHat 5.2, 6.0 and 6.1
#   Linux Turbo Cluster Server 4.0
#   Caldera Open Linux 1.1 and 2.3
########################################

########################################
# INSTALLATION
#  step 1  - update the EXT section of the runbb.sh script to include this
#  requires read access to the /var/[spool/]mail/root file
#  ...
########################################

##################################
# CONFIGURE IT HERE
##################################
MAILX=/usr/bin/mailx 
MAIL_DIR=/var/mail
ERROR_WATCH="not responding|fail|error|warning|WARN|ERR|FAIL|Cannot|Can't"
ERROR_EXCLUDE="ERRORLOG|ERRLOG|Legato|standard error|^a |Grepping|htdig|ransferred|hour.errlog|day.errlog"
export MAIL_DIR ERROR_WATCH ERROR_EXCLUDE

##################################
# 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.
  #####

  #####
  #####    Modified extensively on 03-22-2000 by Paul A. Luzzi
  #####    for use with Big Brother
  #####
  #####  Purpose is to report back to a central server, all "standard"
  #####    detailed information about network, disk, volume, cpu, memory
  #####    which is not reported thru regular "easy" checks.
  #####
  #####  Any problem areas should be highlighted in :
  #####    bold, italics, and red font.
  #####

  #####
  #####  Setup some variables for use later
  #####
  COLOR="clear"
  
  #####
  #####  Grep for any error conditions on the current user's mail
  #####
  echo "<P><DIV ALIGN=\"CENTER\"><HR>" 
  echo "<B>============== Bad Root Mail ==============</B>"
  echo "<B>--- (grep errors $MAIL_DIR/root) ---</B>"
  echo "<HR></DIV>" 
  echo "<BLOCKQUOTE>" 
  CATCH_COMMAND=`$MAILX -f $MAIL_DIR/root -H | $WC`
  if [ -z "$CATCH_COMMAND" ]
    then
      CATCH_COMMAND=0
    fi
  $EGREP -i "$ERROR_WATCH" $MAIL_DIR/root | $EGREP -v "$ERROR_EXCLUDE" | $UNIQ > /tmp/$THIS_HOST.mail.log 
  RESULT_FLAG="$?"
  echo "There are currently $CATCH_COMMAND unread messages"
  echo ""
  if [ -s "/tmp/$THIS_HOST.mail.log" ]
    then
      COLOR="red"
      echo "Potential problems with root's mail - reported errors/warnings of :"
      echo ""
      $CAT /tmp/$THIS_HOST.mail.log
     else
      COLOR="green"
      echo "No reported problems in root's mail."
      echo ""
    fi
  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.mail
$GREP $THIS_HOST $BBHOSTS | $GREP "mail" | 
while read line
  do
    if [ ! -z "$line" ]
      then
        echo "<BR><DIV ALIGN=\"CENTER\"><HR></DIV>" > /tmp/$THIS_HOST.mail
        echo "Status of root mail on $THIS_HOST " >> /tmp/$THIS_HOST.mail
        echo "Captured : `$DATE` " >> /tmp/$THIS_HOST.mail
        get_status >> /tmp/$THIS_HOST.mail
        $BB $BBDISP "status $THIS_HOST.mail $COLOR `$DATE` `$CAT /tmp/$THIS_HOST.mail` "
      else
        COLOR="clear"
        $BB $BBDISP "status $THIS_HOST.mail $COLOR `$DATE` `$CAT /tmp/$THIS_HOST.mail` "
      fi
    done

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