<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl

use warnings;
use strict;
use Parse::Syslog;
use DateTime;

my $VERSION = 0.1;


my $datf = './pppdata.txt';
my $repf = './pppreport.txt';

if(-e $datf ){
  print "Removing old pppd data...\n";
  unlink $datf;
}

my @logs = `find /var/log/ -maxdepth 1 -name "syslog*"`;

foreach(@logs){
  chomp($_);
   if($_ =~ /^.*\.gz$/){
     `zcat $_ | grep pppd &gt;&gt; $datf`;
   }
   else{
     `cat $_ | grep pppd &gt;&gt; $datf`;
   }
}

my $data = Parse::Syslog-&gt;new($datf);

my @conns = ( );

while(my $d = $data-&gt;next()){
  my %c = ( );
  # end of connection marker
  if($d-&gt;{text} =~ /^Connect time\s+(\d+\.\d+)\s+minutes.$/){
    $c{da} = $d-&gt;{timestamp};
    $c{ti} = $1;
    # look for data related to marker above
    while($d = $data-&gt;next()){
      if($d-&gt;{text} =~ /^Sent\s+(\d+)\s+bytes, received\s+(\d+)\s+bytes.$/){
        $c{tx} = $1;
        $c{rx} = $2;
        push @conns, \%c;
        last;
      }
    }
  }
}

my ($c,$da,$ti,$rx,$tx,$tti,$trx,$ttx);

open REPORT, '&gt;', $repf;

format REPORT_TOP =
CONN  DATE                          CONN_TIME       BYTES_RX       BYTES_TX
----------------------------------------------------------------------------
.

format REPORT =
@&lt;&lt;&lt;&lt;&lt; @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; @###########.## @############## @##############
$c,    $da,                 $ti,         $rx,            $tx,
.


for($c=0; $c&lt;@conns; $c++){

  $da = DateTime-&gt;from_epoch(epoch =&gt; $conns[$c]-&gt;{da});
  $ti = $conns[$c]-&gt;{ti};
  $rx = $conns[$c]-&gt;{rx};
  $tx = $conns[$c]-&gt;{tx};

  write REPORT;

  $tti += $ti;
  $trx += $rx;
  $ttx += $tx;

}

close REPORT;

my $kbc = 1024;

open REPORT_TOTALS, '&gt;&gt;', $repf;

format REPORT_TOTALS =

----------------------------------------------------------------------------

Total Ti: @###################(mins)  @##########(hours)
                      $tti,           ($tti/60),
Total Rx: @###################(bytes) @##########.###(MB) @#########.###(GB)
                      $trx,           $trx/$kbc/$kbc,  $trx/$kbc/$kbc/$kbc,
Total Tx: @###################(bytes) @##########.###(MB) @#########.###(GB)
                      $ttx,           $ttx/$kbc/$kbc,  $ttx/$kbc/$kbc/$kbc,
.

write REPORT_TOTALS;
close REPORT_TOTALS;

=head1 NAME

connect_times.pl

=head1 DESCRIPTION

Quick and dirty hack to sum-up pppd connection times and data
usage. Meant for the users of GSM modems and alike that get charged by
kilobyte usage. It basically scans syslog files and make a local text
database of pppd cponnections. Then it sums up the info and presents
it to the user.

=head1 PREREQUISITES

warnings, strict, Parse::Syslog, DateTime

=head1 NOTES

B&lt;Must be run as root or via sudo&gt;. Sorry, I don't have the pacience
or time to untaint and fiddle with perlsec right now. If anybody can
fix this please tell me how.

=head1 FILES

=over

=item *

This file: connect_times.pl

=item *

The data file: ./pppdata.txt

=item *

The report file: ./pppreport.txt

=back

=head1 RUNNING

Just execute and it will generate a usage report of pppd called
pppreport.txt in the current directory. Every time you run the script
it will create the data file and the report file, erasing the old
ones. Make sure you save your reports if your syslog rotates.

=head1 AUTHOR

Alejandro Imass &lt;ait@p2ee.org&gt;

=head1 LICENSE

Published on the same terms as perl itself.

=cut

=pod SCRIPT CATEGORIES

UNIX/System_administration

=cut

</pre></body></html>