#!/bin/sh
#------------------------------------------------------------------------------
#  Copyright (c) 1992 Regents of the University of Michigan.
#  All rights reserved.
# 
#  Redistribution and use in source and binary forms are permitted
#  provided that this notice is preserved and that due credit is given
#  to the University of Michigan at Ann Arbor. The name of the University
#  may not be used to endorse or promote products derived from this
#  software without specific prior written permission. This software
#  is provided ``as is'' without express or implied warranty.
#------------------------------------------------------------------------------
PATH=/bin:/etc:/usr/bin:.:$HOME/src/rovers/bin:$HOME/bin:
TMP=/tmp/.is_clean$$
LINKS=/rtdata/rover/logs/InetRover/nsfnett3.LINKS
CLEAN="YES"
SNMP="snmptable2 -t 30 -r 10 -c lookatit"

#------------------------------------------------------------------------------
#  Check() does most of the work in this script
#------------------------------------------------------------------------------
Check()
{
	# echo $1 $2 $3
	# return

	$SNMP -h $1 ds3Interval$2s.$3 | 
	awk '/[0-9].*/ {
			if ( $3 > max )
   				max=$3;
			total+=$3;
			count++;
		}
		END { 
			if ( count == 96 ) {
				print "count=" count " max=" max " 24hr="total
				if ( max > 0 ) print "_ES "
				if ( max > 6 ) print "_SES_ _SEF_ "
				if ( max > 60 ) print "_CSS_ _UAS_"
				if ( total > 16 ) print "_SES_ _SEFS_ "
				if ( total > 160 ) print "_ES_ "
				if ( total > 259 ) print "_CSS_ "
			}
			else
				print "+++++ NODATA RECORD COUNT="count
		}' > $TMP
	grep NODATA $TMP >/dev/null
	if [ $? -eq 0 ] ; then
		echo " "
		echo "$RemoteNode DSU $RemoteDSUNumber $var  - NO ANSWER"
		exit 2
	fi
	grep _${var}_ $TMP >/dev/null
	if [ $? -eq 0 ] ; then
		echo " "
		echo '******* '$LocalNode DSU $LocalDSUNumber $var Thresholds still exceeded #$answer
		return 1
	fi
	rm -f $TMP
	return 0
}

#------------------------------------------------------------------------------
#  Can be invoked as:
#
#	isclean LocalNode LocalDSUNumber RemoteNode RemoteDSUNumber
#  or
#	isclean LocalNode RemoteNode
#------------------------------------------------------------------------------
if [ $# -eq 2 ] ; then
	grep '^22' $LINKS | grep $1 | grep $2 >$TMP
	numlines=`wc -l $TMP | awk '{ print $1 }'`
	if [ $numlines -eq 1 ] ; then
		LocalNode=$1
		RemoteNode=$2
		LocalDSUNumber=`awk '{ print $5 }' $TMP`
		RemoteDSUNumber=`awk '{ print $6 }' $TMP`
	else
		echo " 0 or Multiple LINKS lines in $LINKS ... aborting"
		echo "Run manually specifying node tty numbers."
		echo "Example: "
		echo "Usage: $0 LocalNode LocalDSUNumber RemoteNode RemoteDSUNumber"
		rm -f $TMP
		exit
	fi
else 
	if [ $# -ne 4 ] ; then
		echo "Usage: $0 LocalNode LocalDSUNumber RemoteNode RemoteDSUNumber"
		echo "----- OR ------"
		echo "       $0 LocalNode RemoteNode"
		rm -f $TMP
		exit
	fi
	LocalNode=$1
	LocalDSUNumber=$2
	RemoteNode=$3
	RemoteDSUNumber=$4
fi

echo -n "Checking.  Please be patient . . . "
for var in ES SES SEFS UAS CSS ; do
	echo -n "#"
	Check $LocalNode $var $LocalDSUNumber 
	if [ $? -eq 1 ] ; then
		CLEAN="NO"
	fi
	echo -n "#"
	Check $RemoteNode $var $RemoteDSUNumber 
	if [ $? -eq 1 ] ; then
		CLEAN="NO"
	fi
done

if [ "$CLEAN" = "YES" ] ; then
	echo ""
	echo CLEAN
	exit 0
fi
echo ""
echo DIRTY
exit 1
