#!/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.
#
#  Invoked from within the 'dsuchk' program.
#------------------------------------------------------------------------------
PATH=/bin:/etc:/usr/bin:/usr/local/bin:.:$HOME/src/rovers/bin:$HOME/bin:
TMP=/tmp/.notify.$$

INFO="$3 $4 $5 $6 $7 $8 $9"

PERMITTED=rover
CLOSESTO="circuittt@noc.ans.net circuitttverbose@noc.ans.net trouble@noc.ans.net"
UPDATESTO="circuitttverbose@noc.ans.net"
OPENSTO="trouble@noc.ans.net circuittt@noc.ans.net circuitttverbose@noc.ans.net"

#-----------------------------------------------------------------------------
#  Used to send informative mail to the Network Operations Center
#-----------------------------------------------------------------------------
OpenTTMessage()
{
#-----------------------------------------------------------------------------
#	Get the Node numbers - in form of e131 and c40 etc.
#-----------------------------------------------------------------------------
LocalNodeNum=`echo $3 | awk -F. '{ if ($3 > 127) print "e"$3 ; else print "c"$3 ; }'`
RemoteNodeNum=`echo $7 | awk -F. '{ if ($3 > 127) print "e"$3 ; else print "c"$3 ; }'`
#-----------------------------------------------------------------------------
#	Send out a nice message
#-----------------------------------------------------------------------------
echo '**********************************************************************'
echo 'The link from '$LocalNodeNum'<>'$RemoteNodeNum' is experiencing an excessive number of errors. '
if [ $2 = $8 ] ; then
	echo 'INTRAPOP Link Problem: Inform the NNAF the following threshold(s)'
else
	echo 'If there is no known activity that could account for this please'
	echo 'open a priority 4 ticket with the NASC on the following circuit:'
	echo '   '
	echo ' '$5
	echo '   '
	echo 'Inform the NASC the following threshold(s)'
fi
echo ''
echo "POP Reporting Node	Threshold Exceeded      t1     t2      t3      Thresh"
echo "==============================================================================="
grep '\[' $1 | sed -e '1,$s/\[//g' \
  	-e '1,$s/ds3IntervalESs/Errored_Seconds          /g' \
	-e '1,$s/ds3IntervalSESs/Severely_Errored_Seconds /g' \
	-e '1,$s/ds3IntervalCSSs/Control_Slip_Seconds     /g' \
	-e '1,$s/ds3IntervalSEFS/Severly_Errored_Frames    /g' \
	-e '1,$s/ds3IntervalUASs/Unavailable_Seconds      /g' |
	awk '{ print $1"\t"$2"\t"$3"\t"$5"\t"$6"\t"$7"\t"$8 }'

echo ''
echo 'were exceeded at '`date`'.'
echo ""
#echo '       The threshold was exceeded during the last two 15 minute intervals'
echo '   The threshold was exceeded during one of the last 15 minute intervals'
echo '    '
if [ $2 != $8 ] ; then
	echo 'The NASC will preform non-intrusive monitoring of this circuit'
	echo 'for up to 24 hours and get back with us.'
else
	echo "The NNAF or IE on call will perform diagnosis of the link problem"
fi
echo '*********************************************************************'
echo '                                    - Details -'
echo 'See the following file for circuit statistics:'
echo $1
echo ' '
echo 'Here are all of the counters including ones that did not exceed the threshold:'
cat $1 
}

echo `date` $0 running $* >/tmp/notify
echo `date` $0 running $* 

#-----------------------------------------------------------------------------
#  Main
#-----------------------------------------------------------------------------
if [ $USER"" != $PERMITTED ] ; then
	echo "Only $PERMITTED may execute this program."
	exit
fi
if [ $# -lt 9 ] ; then
	echo "Usage: $0 OPEN|UPDATE|CLOSE historyfile LocalPOP LocalNode LocalDSU Circuit RemoteDSU RemoteNode RemotePOP"
	exit
fi

case "$1" in

	"OPEN")
	if [ $3 != $9 ] ; then
		MSG="OPEN CIRCUIT TICKET"
	else
		MSG="INTRAPOP OPEN CIRCUIT TICKET"
	fi
	OpenTTMessage $2 $3 $4 $5 $6 $7 $8 $9 | mail -s "$MSG: $INFO" $OPENSTO
	;;

	"UPDATE")
	if [ -r $2 ] ; then
		if [ $3 != $9 ] ; then
			MSG="UPDATE CIRCUIT TICKET"
		else
			MSG="INTRAPOP UPDATE CIRCUIT TICKET"
		fi
		tail -60 $2  | reversecat | mail -s "$MSG: $INFO" $UPDATESTO
	else
		echo "$0: history file $2 does not exist"
	fi
	;;

	"CLOSE")
	if [ $3 != $9 ] ; then
		MSG="CLOSE CIRCUIT TICKET"
	else
		MSG="INTRAPOP CLOSE CIRCUIT TICKET"
	fi
	echo `date` "$MSG" | mail -s "$MSG: $INFO" $CLOSESTO
	;;

	*)
	echo "Type must be one of OPEN, CLOSE or UPDATE"
	;;
esac
