#!/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 >> $datf`; } else{ `cat $_ | grep pppd >> $datf`; } } my $data = Parse::Syslog->new($datf); my @conns = ( ); while(my $d = $data->next()){ my %c = ( ); # end of connection marker if($d->{text} =~ /^Connect time\s+(\d+\.\d+)\s+minutes.$/){ $c{da} = $d->{timestamp}; $c{ti} = $1; # look for data related to marker above while($d = $data->next()){ if($d->{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, '>', $repf; format REPORT_TOP = CONN DATE CONN_TIME BYTES_RX BYTES_TX ---------------------------------------------------------------------------- . format REPORT = @<<<<< @<<<<<<<<<<<<<<<<<<<< @###########.## @############## @############## $c, $da, $ti, $rx, $tx, . for($c=0; $c<@conns; $c++){ $da = DateTime->from_epoch(epoch => $conns[$c]->{da}); $ti = $conns[$c]->{ti}; $rx = $conns[$c]->{rx}; $tx = $conns[$c]->{tx}; write REPORT; $tti += $ti; $trx += $rx; $ttx += $tx; } close REPORT; my $kbc = 1024; open REPORT_TOTALS, '>>', $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. 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 =head1 LICENSE Published on the same terms as perl itself. =cut =pod SCRIPT CATEGORIES UNIX/System_administration =cut