:
#
# aodsum (Online Diagnostics)
#


# check if super-user 

if [ `logname` != "root" ]
then
	echo "Must be super-user to execute the Online Summary function"
	exit 1
fi


clear
echo "\nOnline Diagnostics Summary working, be patient...\n"

# preset default number of days to summarize
PAST=3
# preset usage invocation message
USAGE="aodsum usage: aodsum [-a 'arg'][-hsv]"

# determine invocation option(s)
set -- `getopt hsva: $*`
if [ $? != 0 ]
then
	echo $USAGE
	exit 2
fi
for i in $*
do
	case $i in
	-a) PAST=$2; shift; shift;;
	-h) HEADINGS="on"; shift;;
	-s) INCLSUM="on"; shift;;
	-v) VERBOSE="on"; shift;;
	--) shift; break;;
	esac
done

#define file equates to shorten shell script lines
xfile=/usr/lib/err.xref
tfile=/usr/lib/err.thresh
tmpfile=$$.tmpfile
if [ -f $tmpfile ]
then
	rm $tmpfile
fi
bbscan=/usr/lib/bbscan.awk
tmpsort=$$.th.srt
if [ -f $tmpsort ]
then
	rm $tmpsort
fi

################################################################################

# we need to get device/error definitions and current thresholds settings from 
#	err.xref (error-desc cross-reference) and err.thresh (settings)
#	files.

# check first if cross-ref files is present
if [ ! -f $xfile ]
then
	# cross-reference file doesn't exist; make default one
	echo
	echo "No cross-reference file present;\n	 run aodinit, setthresh or aodmon to create it."
	exit 1
fi

# now check if threshold settings file is present
if [ ! -f $tfile ]
then
	# the thresholds settings file doesn't exist, notify user.
	echo
	echo "No existing threshold settings file;\n	run aodinit, setthresh, or aodmon to create it."
fi

# now read the error types for each kind of device being monitored

#
# get current settings for all types of devices on system
#
if [ "$VERBOSE" ]
then
	echo; echo "Reading error definitions & descriptions file...\c"
fi

#
# get hard drive settings
#
hdfields=`awk 'BEGIN{FS=";";cnt=0}{if($1=="hd"){cnt++;printf("%d ",cnt);}}' $xfile`; 
for i in $hdfields
do
	eval "hdval$i='`awk BEGIN\{FS=\\"\;\\"\;cnt=0\}\{if\(\\$1==\\"hd\\"\)\{cnt++\;if\(cnt=="$i"\)\{printf\(\\"%s\\",\\$2\)\;\}\}\} $xfile`'"
	eval "hdstr$i=\"`awk BEGIN\{FS=\\"\;\\"\;cnt=0\}\{if\(\\$1==\\"hd\\"\)\{cnt++\;if\(cnt=="$i"\)\{printf\(\\"%s\\",\\$3\)\;\}\}\} $xfile`\""
done; 


#
# get floppy drive settings
#
fdfields=`awk 'BEGIN{FS=";";cnt=0}{if($1=="fd"){cnt++;printf("%d ",cnt);}}' $xfile`; 
for i in $fdfields
do
	eval "fdval$i='`awk BEGIN\{FS=\\"\;\\"\;cnt=0\}\{if\(\\$1==\\"fd\\"\)\{cnt++\;if\(cnt=="$i"\)\{printf\(\\"%s\\",\\$2\)\;\}\}\} $xfile`'"
	eval "fdstr$i=\"`awk BEGIN\{FS=\\"\;\\"\;cnt=0\}\{if\(\\$1==\\"fd\\"\)\{cnt++\;if\(cnt=="$i"\)\{printf\(\\"%s\\",\\$3\)\;\}\}\} $xfile`\""
done; 


#
# get tape drive settings
#
tpfields=`awk 'BEGIN{FS=";";cnt=0}{if($1=="tp"){cnt++;printf("%d ",cnt);}}' $xfile`; 
for i in $tpfields
do
	eval "tpval$i='`awk BEGIN\{FS=\\"\;\\"\;cnt=0\}\{if\(\\$1==\\"tp\\"\)\{cnt++\;if\(cnt=="$i"\)\{printf\(\\"%s\\",\\$2\)\;\}\}\} $xfile`'"
	eval "tpstr$i=\"`awk BEGIN\{FS=\\"\;\\"\;cnt=0\}\{if\(\\$1==\\"tp\\"\)\{cnt++\;if\(cnt=="$i"\)\{printf\(\\"%s\\",\\$3\)\;\}\}\} $xfile`\""
done; 

#
# get multidrop board settings
#
mdcfields=`awk 'BEGIN{FS=";";cnt=0}{if($1=="mdc"){cnt++;printf("%d ",cnt);}}' $xfile`; 
for i in $mdcfields
do
	eval "mdcval$i='`awk BEGIN\{FS=\\"\;\\"\;cnt=0\}\{if\(\\$1==\\"mdc\\"\)\{cnt++\;if\(cnt=="$i"\)\{printf\(\\"%s\\",\\$2\)\;\}\}\} $xfile`'"
	eval "mdcstr$i=\"`awk BEGIN\{FS=\\"\;\\"\;cnt=0\}\{if\(\\$1==\\"mdc\\"\)\{cnt++\;if\(cnt=="$i"\)\{printf\(\\"%s\\",\\$3\)\;\}\}\} $xfile`\""
done; 

#
# get sio board settings
#
siofields=`awk 'BEGIN{FS=";";cnt=0}{if($1=="sio"){cnt++;printf("%d ",cnt);}}' $xfile`; 
for i in $siofields
do
	eval "sioval$i='`awk BEGIN\{FS=\\"\;\\"\;cnt=0\}\{if\(\\$1==\\"sio\\"\)\{cnt++\;if\(cnt=="$i"\)\{printf\(\\"%s\\",\\$2\)\;\}\}\} $xfile`'"
	eval "siostr$i=\"`awk BEGIN\{FS=\\"\;\\"\;cnt=0\}\{if\(\\$1==\\"sio\\"\)\{cnt++\;if\(cnt=="$i"\)\{printf\(\\"%s\\",\\$3\)\;\}\}\} $xfile`\""
done; 


#
# get Kernel settings
#
krnfields=`awk 'BEGIN{FS=";";cnt=0}{if($1=="krn"){cnt++;printf("%d ",cnt);}}' $xfile`; 
for i in $krnfields
do
	eval "krnval$i='`awk BEGIN\{FS=\\"\;\\"\;cnt=0\}\{if\(\\$1==\\"krn\\"\)\{cnt++\;if\(cnt=="$i"\)\{printf\(\\"%s\\",\\$2\)\;\}\}\} $xfile`'"
	eval "krnstr$i=\"`awk BEGIN\{FS=\\"\;\\"\;cnt=0\}\{if\(\\$1==\\"krn\\"\)\{cnt++\;if\(cnt=="$i"\)\{printf\(\\"%s\\",\\$3\)\;\}\}\} $xfile`\""
done; 

#
# get Miscellaneous settings
#
misfields=`awk 'BEGIN{FS=";";cnt=0}{if($1=="mis"){cnt++;printf("%d ",cnt);}}' $xfile`; 
for i in $misfields
do
	eval "misval$i='`awk BEGIN\{FS=\\"\;\\"\;cnt=0\}\{if\(\\$1==\\"mis\\"\)\{cnt++\;if\(cnt=="$i"\)\{printf\(\\"%s\\",\\$2\)\;\}\}\} $xfile`'"
	eval "misstr$i=\"`awk BEGIN\{FS=\\"\;\\"\;cnt=0\}\{if\(\\$1==\\"mis\\"\)\{cnt++\;if\(cnt=="$i"\)\{printf\(\\"%s\\",\\$3\)\;\}\}\} $xfile`\""
done; 

if [ "$VERBOSE" ]
then
	echo "done";echo
fi


rm -f /usr/adm/streams/error.[01][0-9]-[0-3][0-9] 

# create error files in error.mm-dd format

cp /usr/adm/messages /usr/adm/streams/$$.tmpmsg
egrep "^WARNING|^NOTICE|^PANIC|^ERROR|^DANGER|^Mon|^Tue|^Wed|^Thu|^Fri|^Sat|^Sun|^status" /usr/adm/streams/$$.tmpmsg > /usr/adm/streams/$$.msgs

# remove temporary error files on exit and interrupts
trap "rm -f /usr/adm/streams/$$.msgs  /usr/adm/streams/$$.tmpmsg; exit 1" 0 2 3 15
cat /usr/adm/streams/$$.msgs | awk -f /usr/lib/mparse.awk

rm -f /usr/adm/streams/$$.msgs /usr/adm/streams/$$.tmpmsg

# get error lists for last $PAST days

Sdays="`find /usr/adm/streams -name 'error.[01][0-9]-[0-3][0-9]' -print`"


# if no files to check, exit with error status (4)
if [ ! "$Sdays" ]
then
	# the above error file lists could not be generated (i.e. there are
	# no system errorlog files for the last $PAST days).
	echo "No errors were logged for the past $PAST days"
	exit 4
fi

# set up temporary file aliases
SUMFILE=$$.old.sum

# in case of premature exit, remove temporary files
trap "rm -f $tmpfile; rm -f $SUMFILE; exit 5" 0 2 3 15

# summarize error activity in file lists
if [ -f $Sdays ]
then
	# if summary error activity file does not exist, create with
	# size 0 to simplify following awk|sort|awk command line
	if [ ! -f $SUMFILE ]
	then
		cp /dev/null $SUMFILE 
	fi 

	cat $Sdays | sort -o $tmpsort

	if [ "$INCLSUM" ]
	then
		cat /usr/adm/streams/Error.summary >> $tmpsort
	fi

	awk -f /usr/lib/segpref.awk $tmpsort | awk -f /usr/lib/segdev.awk | \
	awk -f /usr/lib/segmulti.awk | awk -f /usr/lib/segkernel.awk | \
	awk -f /usr/lib/segmisc.awk > $tmpfile

	mv $tmpfile $tmpsort

	cat $SUMFILE >> $tmpsort
	sort +1 $tmpsort | awk -f /usr/lib/msgcount.awk > $SUMFILE

	#echo "****************************************************************"
	#cat $SUMFILE
	#echo "****************************************************************"

	rm $tmpsort

	for device in hd fd tp mdc sio krn mis
	do
		case $device in
		hd) devtype="Hard Disk Drive";;
		fd) devtype="Floppy Disk Drive";;
		tp) devtype="Streaming Tape Drive";;
		mdc) devtype="Multidrop Slot";;
		sio) devtype="SIO Board";;
		krn) devtype="Kernel";;
		mis) devtype="Miscellaneous";;
		esac
		if [ "$HEADINGS" ]
		then
			echo "\nEvaluating $devtype errors"
		fi
		eval fieldlist=$"$device""fields"
		for fieldtype in $fieldlist
		do
			eval fieldname=$""$device"val"$fieldtype""
			eval fullname="$device""$fieldname"
			# using echo in next 2 lines gets rid of trailing blanks
			eval fieldstr=`echo $""$device"str"$fieldtype""`
			#eval fieldstr=$"$device"str"$fieldtype"
			fldstr="`echo $fieldstr`"
			eval "awkstr='{printf(\"$fullname	$devtype: $fldstr = %d\\n\", \$1);}'"
			estr=`grep -s "$fullname" $SUMFILE`
			if [ "$estr" ]
			then
				case $device in
				hd|fd|tp|krn|mis)
					eval "awkstr='{printf(\"$fullname	$devtype: $fldstr = %d\\n\", \$1);}'"
					;;
				mdc|sio)
					eval "awkstr='{printf(\"$fullname	$devtype # %d, addr %d: $fldstr = %d\\n\", \$4, \$5, \$1);}'"
					;;
				esac
				echo "$estr" | awk "$awkstr" - | sort -u
			else
				if [ "$VERBOSE" ]
				then
					echo "$fullname	$devtype: $fldstr = 0"
				fi
			fi
		done
	done
	echo
	rm $SUMFILE
fi

# exit with code reflecting error/threshold status
trap 0 2 3 15
exit $exceeded

# done, so get out of here
exit 0

