[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

ppptime v0.3.1 Aho



Hello,

Voici le script avec le bug, reporte par plusieurs personnes hier, corrige.
De plus, j'ai ajouter un second grep ce qui a pour effet d'accelerer grandement
l'execution du script.

grep Month file | grep pppd | appel au script awk
		 ^^^^^^^^^^^
		grep selectionne directement les lignes du mois qui
		contiennent le mot pppd.

Bonne semaine!

--
Life is hard but the root password helps.
		-- Anonymous

Gregoire Welraeds
<gregoire (at) welraeds (dot) be>
#!/bin/sh

# ppptime by Grégoire Welraeds <gregoire@welraeds.be>
# v0.3.1 Aho
#
# calculate time spent by pppd connection
#
# Juin 2001

VERBOSE=0
LIST=0

usage()
{
cat << _END_
Usage: ppptime [-vl] [-f file] -m month 
Try 'ppptime -h' for more information.
_END_
}

help()
{
cat << _EOF_
Usage: ppptime [-vl] [-f file] -m month 
	-f file		path to pppd log file
	-h		display help message and exit successfully
	-l		list detail for each connection
	-m month	search file for month. REQUIRED.
	-v		verbose mode
	if no file is given, ppptime will read from stdin.
_EOF_
}

test $# -eq 0 && usage && exit -1

TEMP=`getopt f:hlm:v "$@"` # > /dev/null`

if [ $? != 0 ] ; then usage >&2 ; exit 1 ; fi

eval set -- "$TEMP"

while test 1
do
	case $1 in
		-f) FILE=$2; shift 2;;
		-l) LIST=1; shift ;;
		-m) MONTH=$2; shift 2 ;;
		-v) VERBOSE=1; shift ;;
		-h) help ; exit 1 ;; 
		--) shift ; test $1 || break ;;
		 *) echo "invalid option $1"; usage ; exit -1 ;;
	esac	
done	

# test if $FILE exist and is readable
if test ${FILE}; then
	if ! test -r ${FILE}; then
		echo Can\'t read ${FILE}; 
		usage;
		exit -1
	fi
else
	FILE="-"
fi

if test ! ${MONTH}; then
	usage ; exit -1
fi	

grep "^${MONTH}" ${FILE} | grep pppd | awk -v verbose=${VERBOSE} -v list=${LIST} '

BEGIN {
	total= 0
	daycount= 0
	day= ""
	connectcount= 0
}
	
	$6 == "Serial" && $8 == "established." {

		# new connection

		if ( day != $2 ) { 
			day= $2 ; daycount++ 
		}
		# store time
		start_hour= substr($3,1,2)
		start_min= substr($3,4,5)
		start_sec= substr($3,7,8)
		next
	}

	$6 == "Connection" && $7 == "terminated." {

		# end of the connection
		connectcount++

		# store time
		stop_hour= substr($3,1,2)
		stop_min= substr($3,4,5)
		stop_sec= substr($3,7,8)

		if ( start_hour )
			start= start_hour*3600 + start_min*60 + start_sec
		else
			start= ($2 * day)
		stop= stop_hour*3600 + stop_min*60 + stop_sec

		if ( list )
		{
			printf("%2d", day);
			printf("\t%8s", time2str(start));
			printf("\t%8s", time2str(stop));
		}
			
		if ( day != $2 ) { 
			daydiff = $2 - day
			daycount += daydiff ; day= $2 
			tmp= (stop + ((86400 * daydiff) - start))
		}
		else
			tmp = stop - start
		if ( list )
			printf("\t%8s\n", time2str(tmp))
		total += tmp
		next
	}

END {
	spent= time2str(total)
	if ( list )
	{
		printf("==\t========\t========\t========\n")
		printf("%2d\t        \t        \t%8s\n", daycount, spent)
	}
	if ( verbose )
	{	
		if ( list )
			printf("\n");
		printf("spent: ");
	}	
	if ( verbose || ! list ) 
		print spent
	if (verbose ) 
	{
		print "avg time/day (" daycount "):", time2str(int (total / daycount) )  
		print "avg time/connect (" connectcount "):", time2str(int (total / connectcount))
		print "avg connect/day:", connectcount / daycount
	}
}

function time2str(total)
{
	# recieve an amount of seconds and 
	# convert it to a string format
	hour= int ( total / 3600 )
	tmp= total % 3600
	minute= int ( tmp / 60 )
	second= tmp % 60

	minute= ( minute < 10 ? 0 minute : minute )

	second= ( second < 10 ? 0 second : second )

	return hour ":" minute ":" second

}
'

Reply to: