###### tops ######
#
#	$Header: tops,v 4.300 91/06/09 00:59:33 root Rel41 $
#	SNMP
#	Copyright 1990 Sony Corp.
#	Copyright 1990 Performance Systems International Inc.
#       copyright 1988 NYSERNet, Inc.
#	All Rights Reserved.
#       wpc

#	argument is date in mmddyy format

source ../../config/report-env
if (-e $ROOT/config/daily-env) then
  source $ROOT/config/daily-env
else
  echo "$0 : daily-env missing. Can't continue. Exit."
  exit(1)
endif

if ($#argv >= 1) then
  set ext=$argv[1]
else
  echo "Usage : $0 mmddyy."
  exit(1)
endif

#	following define referenced files/directories (also see OUT2)

alias	echo $BIN/echo

if (! $?DAILYREPORTS) then
  set	DIR=$ROOT/daily/report/$ext
else
  set	DIR=$DAILYREPORTS/$ext
endif
set	DAILY=$DIR/Daily.Stats
set	LINKDOWN=$DIR/LinkDowns
set	REBOOTS=$DIR/Reboots
set	EGP=$DIR/EgpLoss

#	final output files

set	OUT=$DIR/Tops
set	SUM=$DIR/Summary

#	intermediate files

set	TMP=/tmp/s$ext
mkdir	$TMP
set	file=$TMP/topsin
set	LAN=$TMP/tops.lan
set	LINK=$TMP/tops.link
set	Tens=$TMP/tops.tens
set	TMPFILES=( $file $LAN $LINK $Tens )
set	Out=$TMP/tops
set	Sum=$TMP/sum

#

set	Date=(`$BIN/prsdate $ext`)

#	Get data, and modify format + annotate DS0 lines, then split lans/lines

grep	Total $DAILY	| sed -f $SEDAWK/tops.sed >! $file

#
# ---------IMPORTANT----------
# All the entities in a local area network that are not gateways should
# have '-lan' or '.eth' or both in the data file names in the snmppoll.cf.
# Since they are not linked with any other network entities, thses
# entities are not links.
#
egrep -v "(\-lan|\.eth)"	$file >! $LINK
egrep	"(\-lan|\.eth)"	$file >! $LAN

#	do top ten link reports

echo "NYSERNet SNMP Top Ten Link Report for $Date      Page 1" >! $Tens

sort -n -r +3 -4 $LINK	| head |$BIN/topspr 3 " Output KBytes"  >> $Tens
sort -n -r +4 -5 $LINK	| head |$BIN/topspr 4 " Output Pkts "	>> $Tens
sort -n -r +5 -6 $LINK |head |$BIN/topspr 5 " Output Errors"	>> $Tens

#sort -n -r +0 -1 $LINK	|head | $BIN/topspr 0 " Input KBytes"	>> $Tens
#sort -n -r +1 -2 $LINK	|head | $BIN/topspr 1 " Input Pkts"	>> $Tens

sort -n -r +2 -3 $LINK |head |$BIN/topspr 2 " Input Errors"	>> $Tens

#sort -n -r +8 -9 $LAN |head -10 | $BIN/topspr 8 "Total I/O KBytes"  >> $Tens

echo "\fNYSERNet SNMP Top Ten Link Report for $Date      Page 2" >> $Tens

sort -n -r +6 -7 $LINK |head |$BIN/topspr 6 " Interface Maintenance Failures"  >> $Tens
sort -n -r +7 -8 $LINK |head |$BIN/topspr 7 " Interface Selftest Failures"  >> $Tens


echo "\n\nTop Ten Reboots\n"	>> $Tens
echo "___Gateway___		 Reloads	Restarts\n" >> $Tens
grep Boot $REBOOTS | sort -n -r +3 -5 | head -10 >> $Tens

echo "\n\nTop Ten Links Out of Service\n" >> $Tens
echo "___Gateway___	_ifc_	Traps Received Fail(Up) __Total Downtime__\n" >> $Tens
grep Total $LINKDOWN | sort -n -r +7 | head -10 >> $Tens

echo "\fNYSERNet SNMP Top Ten Link Report for $Date      Page 3" >> $Tens

echo "\n\nTop Ten Links Outages\n" >> $Tens
echo "___Gateway___	_ifc_		Start_Time    Duration\n" >> $Tens
grep down $LINKDOWN | sort -r +6 | head -10 >> $Tens

echo "\n\nTop Ten EGP Neighbors Lost\n" >> $Tens
echo "___Gateway___			__Neighbor__	Losses\n" >> $Tens
grep "t Neighbor" $EGP | sort +4 -n -r >> $Tens

#	Do top lan reports

echo "\fNYSERNet SNMP Top Lan Report for $Date      Page 1" >> $Tens

sort -n -r +8 -9 $LAN |head -10 | $BIN/topspr 8 "Top Ten Lan KBytes"	 >> $Tens

sort -n -r +0 -1 $LAN |head -5 | $BIN/topspr 0 "Top Five Lan Input KBytes"	 >> $Tens
sort -n -r +1 -2 $LAN |head -5 | $BIN/topspr 1 "Top Five Lan Input Pkts"	 >> $Tens
sort -n -r +2 -3 $LAN |head -5 |$BIN/topspr 2 "Top Five Lan Input Errors"	 >> $Tens

sort -n -r +3 -4 $LAN | head -5 |$BIN/topspr 3 "Top Five Lan Output KBytes"  >> $Tens
sort -n -r +4 -5 $LAN | head -5 |$BIN/topspr 4 "Top Five Lan Output Pkts "	 >> $Tens
sort -n -r +5 -6 $LAN |head -5 |$BIN/topspr 5 "Top Five Lan Output Errors"  >> $Tens

echo "\fNYSERNet SNMP Top Lan Report for $Date      Page 2" >> $Tens

sort -n -r +6 -7 $LAN |head -5 |$BIN/topspr 6 "Top Five Lan Maintenance Failures"  >> $Tens
sort -n -r +7 -8 $LAN |head -5 |$BIN/topspr 7 "Top Five Lan Selftest Failures"  >> $Tens

# multiple spaces before 0 is key... otherwise you take page headers out...

grep	-v '  0$'	$Tens >! $Out

# lan errors start on same page as lan stuff

echo "\n\n SNMP  The Top Lan Error Hours Report for $Date\n\n\tNumber of Hours where errors exceeded:\n\t>100\t>10\t>0\tInterface\n\n" >> $Out

awk '{printf "\t%2d\t%2d\t%2d\t%s\n", $12, $11, $10, $13}' $LAN |	\
	grep -v "0	 0	 0" | sort -r >> $Out

#	link errors report on new page

echo "\f\n SNMP  The Top Link Error Hours Report for $Date\n\n\tNumber of Hours where errors exceeded:\n\t>100\t>10\t>0\tInterface\n\n" >> $Out

awk '{printf "\t%2d\t%2d\t%2d\t%s\n", $12, $11, $10, $13}' $LINK |	\
	grep -v "0	 0	 0" | sort -r >> $Out

#	print cumulatives for all interfaces (three pages, rotated)
set temp=`wc $file`
@ linenum = $temp[1]
@ page = 1
@ start = 1

/bin/rm -f $Sum
echo "SNMP  Daily Summary for $Date         Page ${page}\n______incoming_______   _____outgoing________  __failures__ Total \t_error hours_\n(KB)  (pkts)   (errs)   (KB)  (pkts)   (errs)  maint slftst KBytes\t>0 >10 >100\n\n"  > $Sum

while (1)
  
  tail +$start $file | head -40 >> $Sum
  @ start += 40
  @ page ++
  @ linenum -= 40

  if ($linenum <= 0) then
    break
  endif

  echo "\fSNMP  Daily Summary for $Date         Page ${page}\n______incoming_______   _____outgoing________  __failures__ Total \t_error hours_\n(KB)  (pkts)   (errs)   (KB)  (pkts)   (errs)  maint slftst KBytes\t>0 >10 >100\n\n"  >> $Sum

end

#	Move temp files to report directory

mv	$Out	$OUT
mv	$Sum	$SUM

/bin/rm -rf $TMP
