#!/bin/bash

# DSPAM processing time statistics

# Optionally prints the detail of the last "n" lines in system.log,
# Then print general statistics and distribution

# Written by     Michel Bouissou on 03/05/2005
# Modified by    Michel Bouissou on 14/05/2005
# Copyright (c)  Michel Bouissou 2005

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# my_starttime="1116057131" Passage à MySQL
my_starttime="1116149760"

# Validate optional parameter
(( $# > 1 )) \
	|| ( (( $# == 1 )) && ! echo "$1" | egrep -q "^-*(all|[0-9]+)$" ) && {
		echo "Usage: $0 [ [-]all | [-]nb_lines ]"
		exit 1
}

LC_NUMERIC=C
export LC_NUMERIC

logfile="/var/dspam/system.log"

[ -f "${logfile}" ] || {
	echo "Cannot find ${logfile} log file."
	echo "Please check \"logfile=\" parameter value"
	echo "defined in $0"
	exit 1
}

# First gawk pass to determine total rec nr and avg time
gawkresult="`gawk \"BEGIN { FS=\\\"\t\\\"; t=0; n=0 }
	/^[0-9]+/ && \\\$1 >= ${my_starttime} && \\\$6 > 0 && \\\$6 < 1800 { n++; t=t+\\\$6 }
	END { print n \\\"/\\\" t/n  }\" < ${logfile}`"

nbr1="${gawkresult%%/*}"; moy1="${gawkresult##*/}"

# Determine line number from which to print optional detail
if echo "$1" | egrep -q "^-*all$"; then
	(( start=1 ))
elif echo "$1" | egrep -q "^-*[0-9]+$"; then
	(( start=nbr1+1-${1##*-} ))
else
	(( start=nbr1+1 ))
fi

# Main gawk loop
gawk "BEGIN { FS=\"\t\"; t=0; n=0; l=99999; h=0; v=0 }
	/^[0-9]+/ && \$1 >= ${my_starttime} && \$6 > 0 && \$6 < 1800 {
		n++; t=t+\$6
		if (\$6 < l) l=\$6
		if (\$6 > h) h=\$6
		v=v+(\$6-${moy1})^2
		# if (n >= ${start}) print \"N°\", n \"   \" \$2 \"   t:\", \$6 \"\tT:\", t \"\tm:\", l, \"M:\", h, \"Moy:\", t/n, \"Std:\", sqrt(v/n)
		if (n >= ${start}) print \"N°\", n \"   \" \$2 \"   t:\", \$6
	}
	END {
	# print \"Nbr. 1st pass:\", ${nbr1} \"\tAvg.T 1st pass:\", ${moy1}
	if (${start}<=n) print \"\"
	print \"Nb.Recs:\", n \"; Tot.Time:\", t \"; Min.Time:\", l \"; Max.Time:\", h \"; Avg.Time:\", t/n \"; Std.dev:\", sqrt(v/n) }" < ${logfile}

# Another gawk loop to get maw value and std dev
gawkresult="`gawk \"BEGIN { FS=\\\"\t\\\"; t=0; n=0; h=0; v=0 }
	/^[0-9]+/ && \\\$1 >= ${my_starttime} && \\\$6 > 0 && \\\$6 < 1800 {
		n++; t=t+\\\$6
		if (\\\$6 > h) h=\\\$6
		v=v+(\\\$6-${moy1})^2
	}
	END {
	print h \\\"/\\\" sqrt(v/n) }\" < ${logfile}`"

max2="${gawkresult%%/*}"; std2="${gawkresult##*/}"

# Distribution printout
gawk "BEGIN { FS=\"\t\"; print \"\"; print \"Distribution:\" }
	/^[0-9]+/ && \$1 >= ${my_starttime} && \$6 > 0 && \$6 < 1800 {
		pos=\$6-${moy1}; if (pos<0) pos=0-pos
		pos=int(pos/${std2}); dist[pos]++
	}
	END {
		max=${max2}-${moy1}; if (max<0) max=0-max
		max=int(max/${std2})
		for ( i=0; i<=max; i++ ) {
			if (dist[i]>=1) print \"Dev. between +/-\", i \"-\" i+1, \"Std.Dev:\", dist[i], \"(\" int(dist[i]/${nbr1}*10000)/100 \"%)\"
		}
	}" < ${logfile}

