:
# @(#)errsum 5.3b of Feb 8, 1988
:
#
# This script keeps a running summary of the streams error log
# files.  The date and other leading information is stripped off,
# and a count of the number of times each message occured is
# maintained.  The summary file is maintained in sorted order
# on the messages.
# To obtain a list sorted on frequency, use the command:
#	sort -nr Error.summary | more
# This command should be run nightly on the same command line
# as strclean via cron.  It should be given the same age parameter
# as strclean, and currently defaults to the same value (3 days)
# 	Created by Dave Olson at Altos, 1/88

AGE=3 #default files to summarize, should match strclean default
SUMDIR=/usr/adm/streams 	# directory summary maintained in
SUMFILE=Error.summary # file summary maintained in
USAGE='eval echo "Usage: $0 [-a age-in-days] [-d directory]"; exit 1'

# check to see if options have been given
if [ $# -gt 0 ]
then	set -- `getopt d:a: $@`
	if [ $? -ne 0 ]
	then	$USAGE
	fi
	while [ $# -gt 0 ]; do
	case "$1" {
		-a)	AGE="$2"
			if [ "$AGE" -le 0 ]
			then	$USAGE # age must be numeric and > 0
			fi
			shift 2
			;;
		-d)	SUMDIR="$2"
			shift 2
			;;
		--)	shift
			;;
	}
	done
fi

if cd $SUMDIR
	then	:
	else	echo "$0: Unable to cd to $SUMDIR"; exit 1
fi


# get list of files to process
# find has a little bit funny idea of mtime +#, so decrement
# AGE by one
OAGE=$AGE
AGE=`expr $AGE - 1`
files="`find . -mtime +$AGE -name 'error.[01][0-9]-[0-3][0-9]' -print`"

# if no files, exit early
if [ "$files" = "" ]
then echo "$0: No log files older than $OAGE days to summarize"; exit 0
fi

# if the summary file doesn't exist, create it with 0 size to
# simplify later code
if [ ! -f $SUMFILE ]
then	if [ -d $SUMFILE -o -b $SUMFILE -o -c $SUMFILE -o -p $SUMFILE ]
	then echo Summary-file is not a file!; exit 1
	else	if cp /dev/null $SUMFILE
		then	echo Created new log summary file $SUMFILE
		else	echo Unable to create new log summary file $SUMFILE; exit 1
		fi
	fi
fi

tmpsort=$$.sort
sumnew=$$.SUM.new
awkcmds=$$.awkin

# remove tmp files on exit and signals
trap "rm -f $$.*; exit 1" 0 2 3 15

# create the awk cmd files on the fly, instead of keeping them
# around as separate files
cat > $awkcmds << \EOF
	{
		if( NR == 1) { lastmsg = $0 ; cnt = 0 }
		if ( $0 == lastmsg ) cnt++
		else  {
			printf("%4d\t%s\n", cnt, lastmsg);
			cnt = 1;
			lastmsg = $0
		}
	}
	END { printf("%4d\t%s\n", cnt, lastmsg); }
EOF

cat > $awkcmds.2 << \EOF
	{
		ncnt = $1
		msg = $2
		for (i = 3 ; i <= NF; i++)
			msg = msg " " $i
		if ( NR == 1) { lastmsg = msg ; cnt = 0 }
		if ( msg == lastmsg )
			cnt += ncnt
		else  {
			printf("%4d\t%s\n", cnt, lastmsg);
			cnt = ncnt;
			lastmsg = msg
		}
	}
	END { printf("%4d\t%s\n", cnt, lastmsg); }
EOF

# strip off the id #'s and dates, and sort on the messages

sed -e 's/[ 	]*[^ 	]*[ 	]*[^ 	]*[ 	]*[^ 	]*[ 	]*[^ 	]*[ 	]*[^ 	]*[ 	]*[^ 	]*[ 	]*//' $files | sort -o $tmpsort


# first awk puts counts on lines from file being summarized.
# sort sorts on the message for the new file and the existing summary
# second awk combines the counts for messages that are the same,
# and writes one copy of the 'duplicate' lines with the new counts
awk -f $awkcmds $tmpsort | sort -t'	' +1 - $SUMFILE | awk -f $awkcmds.2 > $SUMFILE
