#!/bin/sh
#
# top: Sun Enterprise Server check - BB external script test
#
# version 1.0
# version 2.0 - now supports aix
# version 2.1 - now supports RedHat 6.1
# version 2.2 - properly uses $CAT instead of cat
# version 2.3 - properly uses $MACHINE instead of `uname -n` in bb-hosts check
# version 2.4 - properly uses $THIS_HOST instead of $MACHINE due to fqdn using comma in name - thanks Craig Cook
# version 2.5 - handles 100% cpu usage properly as red now - had been reporting green
#
# 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
#   IBM 43P
#   SCO Unixware 7
#   RedHat 6.1
########################################

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

##################################
# CONFIGURE IT HERE
##################################

##################################
# 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="green"
  ## COLOR="clear"

  #####
  #####  Top Processes
  #####
  echo "<P><DIV ALIGN=\"CENTER\"><HR>"
  echo "<B>============== Top Process Info ==============</B>"
  if [ -z "$LINUX_TOP" ]
    then
      #####
      #####  Unix box
      #####
      echo "<B>--- ($TOP -b 30) ---</B>"
      CATCH_COMMAND=` $TOP -b 30 > /tmp/$THIS_HOST.top.log `
     else
      #####
      #####  Linux box
      #####
      echo "<B>--- ($TOP -b $LINUX_TOP | $HEAD -30) ---</B>"
      CATCH_COMMAND=` $TOP -b $LINUX_TOP | $HEAD -30 > /tmp/$THIS_HOST.top.log `
    fi
  echo "<HR></DIV>"
  echo "<BLOCKQUOTE>"
  RESULT_FLAG="$?"
  if [ -s "/tmp/$THIS_HOST.top.log" ]
    then
      $CAT /tmp/$THIS_HOST.top.log
     else
      COLOR="yellow"
      echo "Problem with top command."
    fi
  #####
  #####  Having trouble passing Threshold variable into awk,
  #####    so it is currently setup as a literal, 20.00%.
  #####
  if [ -z "$LINUX_TOP" ]
    then
      #####
      #####  Unix box
      #####
      YELLOW_STATUS=`$TOP -b 5 | $TAIL -6 | $AWK ' $10 ~ /[2-3][0-9].[0-9][0-9]/ { print $2" job, pid "$1" is at "$10" and is using "$6" of memory." } ' `
      RED_STATUS=`$TOP -b 5 | $TAIL -6 | $AWK ' $10 ~ /1[0-9][0-9]/ { print $2" job, pid "$1" is at "$10" and is using "$6" of memory." } $10 ~ /[4-9][0-9].[0-9][0-9]/ { print $2" job, pid "$1" is at "$10" and is using "$6" of memory." } ' `
     else
      #####
      #####  Linux box - seems to be head unix amount + 9
      #####
      YELLOW_STATUS=`$TOP -b $LINUX_TOP | $HEAD -14 | $TAIL -6 | $AWK ' $10 ~ /[2-3][0-9].[0-9][0-9]/ { print $2" job, pid "$1" is at "$10" and is using "$6" of memory." } ' `
      RED_STATUS=`$TOP -b $LINUX_TOP | $HEAD -14 | $TAIL -6 | $AWK ' $10 ~ /1[0-9][0-9]/ ~ { print $2" job, pid "$1" is at "$10" and is using "$6" of memory." } $10 ~ /[4-9][0-9].[0-9][0-9]/ { print $2" job, pid "$1" is at "$10" and is using "$6" of memory." } ' `
    fi
  if [ ! -z "$YELLOW_STATUS" ]
    then
      COLOR="yellow"
      echo "<CENTER><P><HR><P></CENTER>"
      echo "<P>Jobs running at greater than 20% :<P>"
      echo "$YELLOW_STATUS"
    fi
  if [ ! -z "$RED_STATUS" ]
    then
      COLOR="red"
      echo "<CENTER><P><HR><P></CENTER>"
      echo "<P>Jobs running at greater than 40% :<P>"
      echo "$YELLOW_STATUS"
    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.top
$GREP $THIS_HOST $BBHOSTS | $GREP "top" |
while read line
  do
    if [ ! -z "$line" ]
      then
        echo "<BR><DIV ALIGN=\"CENTER\"><HR></DIV>" > /tmp/$THIS_HOST.top
        echo "Status of processes on $THIS_HOST " >> /tmp/$THIS_HOST.top
        echo "Captured : `$DATE` " >> /tmp/$THIS_HOST.top
        get_status >> /tmp/$THIS_HOST.top
        $BB $BBDISP "status $THIS_HOST.top $COLOR `$DATE` `$CAT /tmp/$THIS_HOST.top` "
      else
        COLOR="clear"
        $BB $BBDISP "status $THIS_HOST.top $COLOR `$DATE` `$CAT /tmp/$THIS_HOST.top` "
      fi
    done

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

##############################################
# end of script
##############################################
