#!/bin/sh # connect-time-summary -- find ppp connect time for this month so far # Copyright : http://www.fsf.org/copyleft/gpl.html # New Author 2/02 : Jacques L'helgoualc'h -- http://lhh.free.fr/pub/ppp/ # Old Author : Dan Jacobson -- http://www.geocities.com/jidanni/ # Created On : Feb 2001 # Last Modified By: lhh # Last Modified On: Sun Mar 10 17:43:33 CET 2002 # Update Count : 63 # Status : hacky # aside from getting a general idea of how much I've called this month, # my ISP allows 20 hours of 'free' connect time... I'd like to know how # close I am to that limit so I might switch to another ISP for the # rest of the month. # to use: be root [to read the logfile], assumes you didn't let # logfile grow for more than 11 months long as logfile has no year # info... # hmmm didn't distinguish between ISPs, ok, at least one can think of phone co. $ #### Configure here # Main ISP phone regexp (numbers after ATDT): : ${phones='Not configured!'} # Examples: "^0412", "0868929898", "^0(868|25158)" # max hours (through phone(s) matching regexp): : ${limit=20} # Modem song duration, if billed # (seconds, see the logs and substract ~ 3s for dialup) : ${delay=24} # > but what if ISP even rounds up fractional minutes to 1 full min? # - add 30s average ; # - or patch... # Where are the rotated logs, or perhaps single log : : ${logs=/var/log/ppp.log*} # See also /var/log/messages or syslog -- of course, # logs must show the whole month :) #### Hack below function usage () { cat 1>&2 <&2; usage; exit 23;} if [ $# -ne 0 ]; then case "$1" in -d|--day|--daily) daily=1 ;; -l|--long) verbose=1 ;; -?|-h|--help) usage;exit 0 ;; *) echo "$0: Bad parameter \`$1'" 1>&2 usage exit 23 esac fi set `LC_ALL=C date` thismonth=$2 date=$3 time=$4 year=$6 : ${month=$thismonth} locmonth=`date -d $month\ 1 +%B` # Logs rotated weekly => up to _6_ logs. { for log in `ls -tr $logs` do case $log in *.gz) zcat $log;; *) cat $log;; esac done } | awk " BEGIN{ day=1 date=\"$date\" month=\"$month\" time=\"$time\" delay=$delay/60 # (minutes) if modem song time is billed } # convert string HH:MM:SS to minutes function s2t(string) { split(string,A,/:/) return( A[1]*60 + A[2] + A[3]/60 ) } !/^$month/ {next} \$7~/^\\(ATDT/ { dialtime=\$3\"\" # flag dialin dialdate=\$2\"\" phone_no=\$7\"\";sub(/\(ATDT/,\"\",phone_no);sub(/\^M\)/,\"\",phone_no) } /^$month.*: Connect time / { connecttime = \$8 if(dialtime) { total[phone_no] += connecttime += delay } else { if(\$2==1) { finaltime = s2t(\$3\"\") ; if(finaltime <= connecttime + 0.05) { total[phone_no] += finaltime ; connecttime = finaltime ; } else { print \"$0: Error at the first connection of $month, no dialin found.\"; exit ; } } else { print \"$0: More than 24 hours at the first connection of $month ?!\" ; exit ; } } # Acquit dialin dialtime=0 if ($daily) { if(\$2 > day && dailytotal[day]){ printf \"%3s %2d:\t%3d h %4.1f mn \t%4d connection\", month, day, dailytotal[day]/60, dailytotal[day]%60, n if(n>1) { print \"s\" } else { print\"\" } n=0 } n++ day=\$2 dailytotal[day] += connecttime } if($verbose) { printf \"%3s %2d %8s %6.1f mn\n\", \$1,\$2, \$3, \$8 + delay } } END{ if(! phone_no) { print \"No data yet this month\"; exit } # different month lengths lenmonth=31 if(month == \"Feb\") { if( $year%4 \ || ( !$year%100 && int(year/100)%4 ) \ ) { # 2100 ready! 2400 ready!! lenmonth=28 } else { lenmonth=29 } } if(month ~ /(Apr|Jun|Sep|Nov)/) {lenmonth=30} # close last dialin (if online now, or end of last month) if(dialtime) { if(month==\"$thismonth\") { now = 1440*date + s2t(time) dial = 1440*dialdate + s2t(dialtime) connecttime = now - dial } else if (dialdate==lenmonth) { connecttime = 1440 - s2t(dialtime) } else { print \"$0: Internal error, or connection time > 24h.\" ; exit } total[phone_no] += connecttime if($daily) { dailytotal[day] += connecttime } } # flush last day if($daily && dailytotal[day]) { printf \"%3s %2d:\t%3d h %4.1f mn \t%4d connection\", month, day, dailytotal[day]/60, dailytotal[day]%60, n if(n>1) { print \"s\" } else { print\"\" } } if($verbose||$daily) { print \"\" } limit=$limit #the cheap or free hours my ISP allows me per month for (i in total) { p++ greattotal += total[i] if(i~/$phones/){ maintotal += total[i] ff++ } } if(p>1) { # several phone_nos detailed for (i in total) { # summarizing on phones_no printf \"\t%3d h %4.1f mn on %s\", total[i]/60, total[i]%60, i if(i~/$phones/) { print \" (main ISP)\" } else { print\"\" } } } # if main ISP is called on several phone_no, # and other phones are used, we need a subtotal if(ff>1 && maintotal != greattotal){ printf \"\n\t%3d h %4.1f mn on main ISP.\n\", maintotal/60, maintotal%60 } # General total printf \"Total: \t%3d h %4.1f mn online this month.\n\", greattotal/60, greattotal%60 if($verbose||$daily){print\"\"} # stay terse without options if(\"$thismonth\"==month) { printf \"Today is the %dth day of $month ($locmonth), %d days long.\n\", date, lenmonth if(maintotal){ printf \"We are %2d%% thru the month, and used %2d%% of %d hours.\n\",\ $date/lenmonth*100, maintotal/60/limit*100, limit } else if (p!=1 || \"$phones\"!=\"Not configured!\") { print \"Main ISP not yet used thru phones matching: \`$phones'\" } } else { printf \"In %s ($locmonth), we used %2d%% of %d hours.\n\", month, maintotal/60/limit*100, limit } } "