#!/bin/sh
#  OVERVIEW:	This script will check the TSM fileexit file for entries, 
#  ---------	color code any entries found, and then report them to the 
#		Big Brother display server.
#
#		The fileexit file is a user exit file, that is activated
#		by TSM.  You can route specific messages to this file using
#		the TSM "enable event" command.  It is this file that this
#		script will interrogate.  If any entries are found in this
#		file they are reported upon to BB.  Theory is that you should
#		then clear out the offending message from this file when
#		you have resolved the condition that gives rise to the error.
#		I have another script that allows you to individually clear
#		out these messages.  Mail me at sbrennan@eircom.ie for a copy.
#	
#		Regards: Sean B
#
#  HISTORY:	
#  --------
#  16/10/2001	SB	Initial design
#

####=================================####
##  Script variables
####=================================####
TMPOUT=/tmp/`basename $0.$$`
MSGLOG=/var/log/TSM/filetext_msg.log
MACHINE="`uname -n`"			# HAS TO BE IN A,B,C FORM

####=================================####
##  Big Brother specific values
####=================================####
BBHOME=/home/bb/bb; export BBHOME	# FOR TESTING
BBPROG=`basename $0`; 

TEST="TSM"

if test ! "$BBTMP"                      # GET DEFINITIONS IF NEEDED
then
	 # echo "*** LOADING BBDEF ***"
        . $BBHOME/etc/bbdef.sh          # INCLUDE STANDARD DEFINITIONS
fi

#####===================================================#####
##-  The TSM message file is a result of user exit.
##   If there are entries in this file then we should
##   act upon them.  Color code depends on severity.  
##   See TSM admin guide Appendix for field definitions.
#####===================================================#####

COLOR="green"
PRUNED=/tmp/pruned.errors

	cat $MSGLOG | cut -c 8-10,33-46,420-620 > $PRUNED
	if test -s "$PRUNED"
	then
		COLOR="yellow"
		HIGHMARK="WARNING"

	 	cat $PRUNED  | sort -d |  while read LINE 
		do
			SEVERITY=`echo $LINE | cut -b 1-3`
			YY=`echo $LINE | cut -b 4-7`
			MM=`echo $LINE | cut -b 8-9`
			DD=`echo $LINE | cut -b 10-11`
			TIME=`echo $LINE | cut -b 12-15`
			EVENTTEXT=`echo $LINE | cut -c 18-`

			# Reformat the date
			DATE="$DD-$MM-$YY $TIME"

			echo "$SEVERITY $DATE $EVENTTEXT" >> $TMPOUT
		
			# Set the alarm bells if errors are severe
			if [ $SEVERITY -gt 004 ] 
			then
				COLOR="red"
				REDLINE=`echo "SEVERE ERRORS DETECTED $EVENTTEXT" `
				HIGHMARK="PANIC"
			fi
			
		done
	fi

	# Sort the final output file by severity
	sort -d $TMPOUT 

	# Build the results for display
	if [ "$COLOR" = "green" ]
	then
		LINE="status $MACHINE.$TEST $COLOR `date`- No TSM errors logged"
	else
		LINE="status $MACHINE.$TEST $COLOR `date`- TSM on $MACHINE is at $HIGHMARK level
${REDLINE}
`cat $TMPOUT`"

	fi

	# File no longer required
	$RM -f $TMPOUT 
	$RM -f $PRUNED

	# Send the output to BB server
	$BBHOME/bin/bb $BBDISP "$LINE"

# Were done now lets get out of here
exit 0
