#!/bin/sh

# bb-baan2.sh
#
# Large juicy chunks of code shamelessly stolen from the
# BB Proc script.
#
# BB-Proc portions Copyright (c) 1997-2001
# BB4 Technologies Inc
# All Rights Reserved
#
# Hacks to customize for BaaN data by
# Ed Hintz <ehintz@natus.com>, Copyright (c) 2001
# Natus Medical, Inc. and freely redistributable,
# as long as attribution is given to the author.
#
# To add this script to your installation, change the user defined variables
# below to match your envrironment and taste, then put it in
# $BBHOME/ext (on the machine where BaaN runs-NOT your BBDISPLAY machine-
# unless, of course, they happen to be one and the same in your case...)
# and add it to your $BBHOME/etc/bbdef.sh file. Also, on your BBPAGER
# system remember to update the svcerrlist token in the bbwarnsetup.cfg file.
# Restart the bb client and be happy.
#
# This was tested and functions on a Solaris 2.6 box with bb15d2 client,
# bbdisplay is Mandrake 7.2 with bb16d install. Works for me. YMMV.
#
# USER DEFINED VARIABLES:
#
#
# Set BaaN temp dir here
BSETEMP="/u01/app/baan/bse/tmp"

# Enter space available (in k) for yellow warning
BSEYEL="200000"

# Enter space available for red warning
BSERED="100000"

# Enter name of shmtimer proc on local system
SHMTIMER="shmtimer6.1"

# Enter name of pdaemon proc on local system
PDAEMON="pdaemon6.1"

#-----------------------------
#End of user defined variables
#-----------------------------
TEST="baan"

if test ! "$BBTMP"                      # GET DEFINITIONS IF NEEDED
then
         # echo "*** LOADING BBDEF ***"
        . $BBHOME/etc/bbdef.sh          # INCLUDE STANDARD DEFINITIONS
fi


PROCS=""
PAGEPROC="$SHMTIMER $PDAEMON"                         # (RED) PAGE IF NOT RUNNING
export PROCS PAGEPROC

$PS > $BBTMP/bb.$$                      # GET A PS LISTING

if test ! "$MACHINEDOTS"                # THIS SHOULD ALREADY BE SET...
then
        # Setup the name of the local host
        # Save a version of the machine name with '.' 
        # instead of ',', if any are there
        MACHINEDOTS=`echo $MACHINE | $SED 's/,/\./g'`
        if [ "$FQDN" != "TRUE" ]
        then
                OLDIFS=$IFS
                IFS='.'
                set $MACHINEDOTS >/dev/null
                IFS=$OLDIFS
                MACHINEMASK="${1}"
        else
                MACHINEMASK="${MACHINEDOTS}"
        fi
        export MACHINEDOTS MACHINEMASK
fi

#=====================================================================
# PROCESSES THAT MUST EXIST
#=====================================================================
#
# echo "*** PROCESSES TEST ***"

GREENLINES=""
YELLOWLINES=""
REDLINES=""
STATLINE="No processes to check"
COLOR="green"


#
# All this crap is to be able to handle processes specified
# with "":  snmmpd "syslogd -s" xntpd
# The string "syslogd -s" will be matched against the PS output
#
# The ' ' in the "" construct is replaced with '&_' temporarely
# until the string is actually used which is reverted back to ' '
#
# That's because a constrcut like "syslogd -s" in BBPROCS would
# appear as "syslogd and -s" in the for loop if no substitution
# was done:
# for proc in $BBPROCS  -> would become for proc in "syslogd -s"
# but proc instead of being "syslogd -s" there would be 2 proc
# iteration: one for "syslogd and the other -s"   
#
# Any clearer ?
#
# Must inverse !"   " to "!    " also

BBPROCS=`echo $PROCS $PAGEPROC | $SED 's/\![    ]*"/"\!/g'`
if [ -n "$BBPROCS" ]
then
        OLDIFS=$IFS
        IFS='"'
        set " "$BBPROCS >/dev/null 2>&1
        IFS=$OLDIFS
        BBPROCS=""
        while [ $# -gt 0 ]
        do
                BBPROCS="$BBPROCS $1"
                if [ $# -gt 1 ]
                then
                        BBPROCS="$BBPROCS "`echo $2 | $SED 's/ /\&_/g'`
                        shift
                fi
                shift
        done
fi

for proc in $BBPROCS
do
        proc=`echo "$proc" | $SED 's/\&_/ /g'`
        case $proc
        in
                *!* )
                        proc=`echo "$proc" | $SED 's/!//g'`
                        TESTREVERSED=TRUE
                        OK=DOWN
                        NOTOK=UP
                        ;;
                *)      
                        TESTREVERSED=FALSE
                        OK=UP
                        NOTOK=DOWN
                        ;;
        esac

                                # No need to exclude grep from
                                # process list
                                # thanks to Paul Gluhosky
                                # <paul.gluhosky@yale.edu>
        $GREP "$proc" $BBTMP/bb.$$ > /dev/null 2>&1
        RC=$?
        # Check if service must NOT be running
        if [ "$TESTREVERSED" = "TRUE" ]
        then    
                if [ "$RC" -ne 0 ]
                then
                        RC=0
                else
                        RC=1
                fi
        fi   
        if test "$RC" != "0"
        then
                STATLINE="Some processes are in error"
                #
                # NOW SEE IF THIS IS A PROCESS WE SHOULD NOTIFY ON...
                #
                echo "$PAGEPROC" | $GREP "$proc" > /dev/null 2>&1
                if test "$?" = "0"
                then
                        COLOR="red"
                        REDLINES="&red $proc  $NOTOK
$REDLINES"
                else
                        if test "$COLOR" != "red"       # WASN'T A PANIC
                        then
                                COLOR="yellow"          # SO JUST WARN
                        fi
                        YELLOWLINES="&yellow $proc  $NOTOK
$YELLOWLINES"
                fi
        else
                if [ "$COLOR" = "green" ]
                then
                        STATLINE="All processes are OK"
                fi
                GREENLINES="&green $proc  $OK
$GREENLINES"
        fi
done

# *****BEGIN HACKS TO CUSTOMIZE FOR BaaN *****
#
# Change statline variable to proc statline, so we can add 
# bse temp space to the resulting statline.

PROCSTATLINE="$STATLINE"

# Procs checked, now let's look into bse temp space...
#
set `df -k $BSETEMP |sed -e 1d`
BSEAVAIL="$4"

	if test "$BSEAVAIL" -le "$BSERED"
	then
		COLOR="red" BSESTATLINE="BSE Temp Space is less than $BSERED"
	else
		if test "$BSEAVAIL" -le "$BSEYEL"
		then
			if COLOR="red"
				then BSESTATLINE="BSE Temp Space is less than $BSEYEL"
			else
			COLOR="yellow" BSESTATLINE="BSE Temp Space is less than $BSEYEL"
			fi
	else
		if test "$COLOR" != "green"
			then BSESTATLINE="BSE Temp Space OK"
		else
		COLOR="green" BSESTATLINE="BSE Temp Space OK"
		fi
	fi
fi



# Combine the 2 statlines:

STATLINE="$PROCSTATLINE $BSESTATLINE"

#
# *****END HACKS TO CUSTOMIZE FOR BaaN *****
#
# NOW SEND THIS INFORMATION TO THE BIG BROTHER DISPLAY UNIT
#
$BB $BBDISP "status $MACHINE.$TEST $COLOR `date` $STATLINE

${REDLINES}${YELLOWLINES}${GREENLINES} $BSETEMP has $BSEAVAIL k available"

$RM -f $BBTMP/bb.$$
