#!/bin/sh
# 
# A tool for compressing the logs so that
# mailers do not have to cope with huge files (300k)
# Use post_mail to convert the result of this into
# a "stat_line" table.

if test $# = 0
then
	echo "We need a log file..."
	exit
fi
file="$1"
if [ ! -f "$file" ]
then
	file="$file"_log
fi
if [ ! -f "$file" ]
then
	echo "We have no log file $file"
	exit 1
fi

awk -F# '
/^------/ { next; }
NF == 5	{
		if (address[$5] == 0) {
			best[$5] = 999999
			name[$5] = $1
		}
		if (length ($1) > maxlen)
			maxlen = length($1)
		address[$5] ++
		if ($2 == 1) {
			avgtime[$5] = (avgtime[$5] * up[$5] + $3) \
						 / (1 + up[$5])
			up[$5] ++
			if ($3 < best[$5])
				best[$5] = $3
			if ($3 > worst[$5])
				worst[$5] = $3
		}
		else {
			down[$5] ++
		}
	}
END {
	for (i in address) 
	{
	    if (up[i] != 0)
	    {
		if (best[i] == 999999)
		{
			best[i] = 0
		}
		printf("%s:%d:%d:%f:%f:%f:%f:%s\n",\
		name[i], address[i], down[i], (100 * up[i]) / address[i], \
		best[i], worst[i], avgtime[i], i )
	    }
	    else
	    {
			printf("%s:%d:%d:%f:0:0:0:%s\n",\
				name[i], address[i], down[i], \
 				(100 * up[i]) / address[i], i)
	    }
	}
}
' "$file" | sort -t: +3nr +0 | sed -e "s/  :/ :/" 
