#!/usr/bin/perl -w # # Version 1.0 # Reports on mail distribution into folders by parsing procmail.log # Author: Dan Boger (perl@peeron.com) # Date: 03/16/04 # Usage: Just update the location of the procmail log below, and run daily via cron. # Will report on all the entries from *yesterday* (so schedule to run after midnight use strict; use Date::Manip; # where is the procmail log located? my $procmail = "/home/dan/Mail/procmail.log"; # How should the results be sorted? either Msgs or Bytes my $sortmethod = "Msgs"; # what format should the report use? read perlform for details my $entry; format STDOUT_TOP = Folder | Messages | Bytes -----------------------------------------+-----------------+---------------- . format STDOUT = @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | @<<<< (@<<<<<<) | @<<<< (@<<<<<<) $entry->{Title}, $entry->{Msgs}, $entry->{MPercent}, $entry->{Bytes}, $entry->{BPercent} . # no user cofiguration below this line (currently) # figure out what date we're looking for my $date = &ParseDate("Yesterday"); my $year = &UnixDate($date, '%Y'); $date = &UnixDate($date, "%b %e"); open(LOG, $procmail) or die "Can't read $procmail: $!"; # scan for beginning of yesterday's mail while () { # search up to the destination date last if /^From \S+ \w\w\w $date \d\d:\d\d:\d\d $year/o; } # sum up mail traffic my %mbox; my ($folder, $bytes); while () { # check if we've past the target date if (/^From \S+ \w\w\w (... [ \d]\d)/ and $1 ne $date) { if (defined $folder) { # we already counted this email as if it was from # yesterday - remove it $mbox{$folder}->{Bytes} -= $bytes; $mbox{$folder}->{Msgs} --; $mbox{"* TOTAL *"}->{Bytes} -= $bytes; $mbox{"* TOTAL *"}->{Msgs} --; } last; } next unless /^\s*Folder: (\S+)\s+(\d+)/; ($folder, $bytes) = ($1, $2); $mbox{$folder}->{Bytes} += $bytes; $mbox{$folder}->{Msgs} ++; $mbox{"* TOTAL *"}->{Bytes} += $bytes; $mbox{"* TOTAL *"}->{Msgs} ++; } # done scanning close LOG; # print out results print "Mail report for $date\n\n"; # sorted by either Msgs or Bytes foreach (sort {$mbox{$b}->{$sortmethod} <=> $mbox{$a}->{$sortmethod}} keys %mbox) { $entry = $mbox{$_}; $entry->{Title} = $_; if ($mbox{"* TOTAL *"}->{Msgs} > 0) { $entry->{MPercent} = sprintf("%5.2f%%", $entry->{Msgs} / $mbox{"* TOTAL *"}->{Msgs} * 100); } else { $entry->{MPercent} = "N/A"; } if ($mbox{"* TOTAL *"}->{Bytes} > 0) { $entry->{BPercent} = sprintf("%5.2f%%", $entry->{Bytes} / $mbox{"* TOTAL *"}->{Bytes} * 100); } else { $entry->{BPercent} = "N/A"; } write; }