#!/bin/sh
#
# queue: 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 - switched from "lpstat -p" to "lpstat -v" for speed and Sun prob
#
# 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
#    HP-UX 10.20 and 11.0
#
# Could not make my table code appear as
#   nice as I normally do with all the 
#   indents, because the spaces were taking
#   up very precious data area I needed.
########################################

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

##################################
# CONFIGURE IT HERE
##################################
LPSTAT=/usr/bin/lpstat
export LPSTAT 

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

  #####
  #####  Section written 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"
  echo "0" > /tmp/$THIS_HOST.queue.color
  
  #####
  #####  Printer Information
  #####
  echo "<DIV ALIGN=\"CENTER\"><HR>"
  echo "<B>==== ($LPSTAT -o ptr | $WC) ====</B>"
  echo "<HR></DIV>" 

  #####
    echo "<FONT FACE=\"Arial\">"
    echo "<TABLE BORDER=1 WIDTH=\"100%\" CELLPADDING=2>"
  
    echo "<TR>"
    echo "<TD><B><I>Printer Name</I></B></TD>"
    echo "<TD><B><I>Queued Jobs</I></B></TD>"
    echo "</TR>"
   
  #####
  #####  I used the "cat print.output" line for testing, since my test box 
  #####    doesnt support the hpnpadmin command.  Normally, the hpnpadmin
  #####    command will be called directly.
  #####
    cp /dev/null /tmp/$THIS_HOST.queue.out 2> /dev/null
    for PRT in `$LPSTAT -v | $GREP \^device | $AWK '{print $3}' | $AWK -F: '{print $1}' | $SORT `
      do
        echo "\n====================\n" >> /tmp/$THIS_HOST.queue.out
        echo "Checking : $PRT\n" >> /tmp/$THIS_HOST.queue.out
        echo "Queued Jobs : `$LPSTAT -o $PRT | $WC -l` " >> /tmp/$THIS_HOST.queue.out
        JOB_LIST=` $LPSTAT -o $PRT | $AWK -F- '{print $2}' | $AWK '{print $1}' `
        for EACH_JOB in $JOB_LIST
        do
          LONG_JOBS=` $FIND /var/spool/lp/temp/${EACH_JOB}* -mtime +1 -print `
          if [ ! -z "$LONG_JOBS" ]
            then
              echo "Long Wait : ${EACH_JOB}" >> /tmp/$THIS_HOST.queue.out
            fi
        done

      done
  
    $CAT /tmp/$THIS_HOST.queue.out | while read FULL_LINE
    do
  
    #####
    #####     Checking       : - Printer Name
    #####     Queued Jobs    : - Jobs in queue
    #####
       if [ ! -z "$FULL_LINE" ]
         then
  
           echo "$FULL_LINE" | $NAWK -F: '
             {
                 { 
                 if ( $1 == "====================" ) 
                   {
                     print "<TR>"
                   }
                 else if ( $1 == "Checking " ) 
                   {
                     print "<TD>"$2"</TD>"
                   }
                 else if ( $1 == "Queued Jobs " ) 
                   {
                     if ( $2 >= 5 )
                       {
                         if ( $2 >= 10 )
                           {
                             print "<TD ALIGN=CENTER><B><I>"$2"</I></B></TD>"
                             print "</TR>"
                             system( "echo 2 >> /tmp/$THIS_HOST.queue.color" )
                           }
                         else
                           {
                             print "<TD ALIGN=CENTER><I>"$2"</I></TD>"
                             print "</TR>"
                             system( "echo 1 >> /tmp/$THIS_HOST.queue.color" )
                           }
                       }
                     else
                       {
                         print "<TD ALIGN=CENTER>"$2"</TD>"
                         print "</TR>"
                       }
                   }
                 else if ( $1 == "Long Wait " ) 
                   {
                     system( "echo 2 >> /tmp/$THIS_HOST.queue.color" )
                   }
               } 
             } '  
  
           fi
  
    done
  
  #####
  #####  Close up the table, and the font tags - we're done.
  #####
  echo "</TABLE>"
  echo "</FONT>"

  #####
  #####  Make sure to export COLOR so that it gets back to "central"
  #####
  if [ -s "/tmp/$THIS_HOST.queue.color" ]
    then
      COLOR_NUMBER=` $CAT /tmp/$THIS_HOST.queue.color | $UNIQ | $SORT | $TAIL -1 `
     else
      COLOR_NUMBER=3
    fi

  case "$COLOR_NUMBER" in
    2)  COLOR="red";;
    1)  COLOR="yellow" ;;
    0)  COLOR="green" ;;
    *)  COLOR="clear" ;;
  esac

  export COLOR

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

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

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