#!/bin/sh # intended to provide easy stats for pppd 2.3.10 and up # # (c) 1999 by Yves Lafon printBytes() { ko=`expr $1 / 1024` mo=`expr $ko / 1024` ro=`expr $1 % 1024` if [ $mo -gt 0 ]; then echo "$mo Mo, `expr $ko % 1024` Ko, $ro bytes" elif [ $ko -gt 0 ]; then echo "$ko Ko, $ro bytes" else echo "$ro bytes" fi } printTime() { day=`expr $1 / 86400` hour=`expr $1 / 3600 % 24` mins=`expr $1 / 60 % 60` secs=`expr $1 % 60` if [ $hour -le 9 ]; then hour=`echo 0$hour` fi if [ $mins -le 9 ]; then mins=`echo 0$mins` fi if [ $secs -le 9 ]; then secs=`echo 0$secs` fi if [ $day -gt 0 ]; then echo "$day Days, $hour:$mins:$secs" else echo "$hour:$mins:$secs" fi } recv=0 lastrecv=0 sent=0 lastsent=0 conntime=0 started=0 r=0 maxtime=0 maxrecv=0 maxsent=0 # the bytes received/sent are cumulative # while the time is not... so lame! # delimiter are: # Nov 27 23:09:00 arachne pppd[14534]: pppd 2.3.10 started by root, uid 0 # and # Nov 15 18:55:01 arachne pppd[11662]: Exit. if [ -f /var/log/ppp.log ]; then logfile=/var/log/ppp.log elif [ -f /var/log/messages ]; then logfile=/var/log/messages elif [ -f /var/adm/messages ]; then logfile=/var/adm/messages else echo "Unable to find a logfile" exit 1 fi for j in `ls $logfile*`; do ext=`echo $j | sed 's/.*\.//'` if [ $ext = "gz" ]; then gcmd="gzip -cd $j" elif [ $ext = "bz2" ]; then gcmd="bzip2 -cd $j" else gcmd="cat $j" fi for i in `$gcmd | grep pppd | awk '{ if ($6 == "Sent" ) { print $7 " " $10 } else { if ( $9 == "minutes." ) { print "minutes " $8 } else { if ( $6 == "Exit.") { print "exit" } else { if ( $6 == "Serial") { print "Starting" } } } } }' | sed 's/\.//'`; do # a kind of state machine there # msg are: # "Starting" the link starts # "exit" pppd exited (used to reset some values) # "minutes " minutes of last connection # bytes received/sent, beware of cumulative mode ;) if [ $i == "exit" ]; then recv=`expr $recv + $lastrecv` lastrecv=0 sent=`expr $sent + $lastsent` lastsent=0 elif [ $i == "Starting" ]; then started=`expr $started + 1` elif [ $i == "minutes" ]; then r=2 else if [ $r -eq 2 ]; then lasttime=`expr $i \* 6` if [ $lasttime -gt $maxtime ]; then maxtime=$lasttime fi conntime=`expr $conntime + $lasttime` r=0 elif [ $r -eq 1 ]; then currrecv=`expr $i - $lastrecv` if [ $currrecv -gt $maxrecv ]; then maxrecv=$currrecv fi lastrecv=$i r=0 else currsent=`expr $i - $lastsent` if [ $currsent -gt $maxsent ]; then maxsent=$currsent fi lastsent=$i r=1 fi fi done done # add the latest received/sent recv=`expr $recv + $lastrecv` sent=`expr $sent + $lastsent` if [ $conntime -gt 0 ]; then avg_sent=`expr $sent / $conntime` avg_recv=`expr $recv / $conntime` else avg_sent=0 avg_recv=0 fi if [ $started -gt 0 ]; then sess_sent=`expr $sent / $started` sess_recv=`expr $recv / $started` sess_time=`expr $conntime / $started` else sess_sent=0 sess_recv=0 sess_time=0 fi echo PPP Usage Statistics echo echo Cumulative Statistics: echo echo Connection Time `printTime $conntime` echo Received: `printBytes $recv` echo Sent: `printBytes $sent` echo Sessions: $started echo echo Average Byte Rate: echo echo Received per sec: `printBytes $avg_recv` echo Sent per sec: `printBytes $avg_sent` echo echo Average per Session: echo echo Agv Session Time: `printTime $sess_time` echo Avg Bytes Received per Session: `printBytes $sess_recv` echo Avg Bytes Sent per Session: `printBytes $sess_sent` echo echo Maximum per Session: echo echo Max Session Time: `printTime $maxtime` echo Max Session Bytes Received: `printBytes $maxrecv` echo Max Session Bytes Sent: `printBytes $maxsent`