#!/usr/bin/perl -w use strict; my $basedir = "$ENV{HOME}/mail"; my (%unfiltered_spam, %msent, %mread, %mspam, %monthhash, %monthnumhash); $monthhash{"jan"} = "01"; $monthhash{"feb"} = "02"; $monthhash{"mar"} = "03"; $monthhash{"apr"} = "04"; $monthhash{"may"} = "05"; $monthhash{"jun"} = "06"; $monthhash{"jul"} = "07"; $monthhash{"aug"} = "08"; $monthhash{"sep"} = "09"; $monthhash{"oct"} = "10"; $monthhash{"nov"} = "11"; $monthhash{"dec"} = "12"; $monthnumhash{"01"} = "jan"; $monthnumhash{"02"} = "feb"; $monthnumhash{"03"} = "mar"; $monthnumhash{"04"} = "apr"; $monthnumhash{"05"} = "may"; $monthnumhash{"06"} = "jun"; $monthnumhash{"07"} = "jul"; $monthnumhash{"08"} = "aug"; $monthnumhash{"09"} = "sep"; $monthnumhash{"10"} = "oct"; $monthnumhash{"11"} = "nov"; $monthnumhash{"12"} = "dec"; ################################################################### chdir("$ENV{HOME}/www/mailstats") || die "Could not change directory"; init_hardcoded_data(); scandir($basedir); dumpdata(); gengraph(); printHTML(); print "...Done\nMake sure to update the graph scale to include this month\n"; ################################################################### sub scandir { my ($basedir) = @_; my (@dentries, $entry, $num, $val); my (@dirs, $mon, $year, $type, $dir); @dirs = ("trash", "sent", "spam"); foreach $type (@dirs){ $dir = "$basedir/$type"; # print "Processing $dir...\n"; defined($dir) || die "Huh? Null dir request"; opendir(DHANDLE, $dir) || die "Could not open $dir"; @dentries = readdir(DHANDLE); closedir(DHANDLE); foreach $entry (@dentries) { if ($entry =~ /^mail-[0-9][0-9][0-9][0-9]-[0-9][0-9]$/) { ($year, $mon) = ($entry =~ /^mail-(....)-(..)$/); $mon = $monthnumhash{$mon}; $val = "$year-$mon"; # print $val . "\n"; if($type eq "trash"){ if(!$mread{$val}){ print "$dir/$entry\n"; $num = `grep "^From\ " $dir/$entry | wc -l` - 1; $mread{$val} = $num; } } elsif($type eq "sent"){ if(!$msent{$val}){ print "$dir/$entry\n"; $num = `grep "^From\ " $dir/$entry | wc -l` - 1; # get heather's directory too # HACK - do it here, because of the lost mail # and hard-coded data. #$num += `grep "^From\ " $basedir/heather/sent/$entry | wc -l` - 1; $msent{$val} += $num; } } elsif($type eq "spam"){ if(!$mspam{$val}){ print "$dir/$entry\n"; $num = `grep "^From\ " $dir/$entry | wc -l` - 1; $mspam{$val} = $num; } } else{ print "$dir/$entry\n"; die "Invalid type: $type\n"; } } elsif ($entry =~ /^(\.|\.\.|mail|.imap|[0-9]{4})$/) { # nothing } else { print STDERR "Unrecognized entry $entry\n"; } } } return; } sub msort { my ($amonth, $ayear, $bmonth, $byear); ($ayear, $amonth) = ($a =~ /^(....)-(...)$/); ($byear, $bmonth) = ($b =~ /^(....)-(...)$/); if($ayear == $byear) { $monthhash{$amonth} <=> $monthhash{$bmonth}; } else { $ayear <=> $byear; } } sub dumpdata { dumphash("read", \%mread, undef); dumphash("sent", \%msent, undef); dumphash("spam", \%mspam, undef); dumphash("unfiltered_spam", \%unfiltered_spam, undef); } sub dumphash { my ($mname, $phash, $smonth) = @_; my ($key, $month, $year); open(MAIL, "> $mname-mail.dat"); foreach $key (sort msort keys(%$phash)) { ($year, $month) = ($key =~ /^(....)-(...)$/); if(!defined($smonth) || ($smonth eq $month)) { print(MAIL "$key $$phash{$key}\n"); } } close(MAIL); return; } sub gengraph { my $graph; my @graphs = ("read-mail", "sent-mail", "spam-mail", "unfiltered_spam-mail"); foreach $graph (@graphs){ open(GNUPLOT, "| gnuplot") || die "Could not spawn gnuplot"; print GNUPLOT <\n"; $str .= "\n"; $str .= " \n"; $str .= " The Daley's Email Statistics\n"; $str .= " \n"; $str .= " \n"; $str .= " \n"; $str .= "

Email Stats

\n"; $str .= "

Thanks to SpamAssassin,\n"; $str .= " Razor,\n"; $str .= " Postgrey\n"; $str .= " and SpamCop
\n"; $str .= " (Postgrey installed August 2005, and stops ~2000 spams monthly)
\n"; $str .= " Spam Filter Accuracy "; $str = sprintf("%s%.1f%%", $str, $percent); $str .= "

\n"; $str .= "
\n"; $str .= " \n"; $str .= " \n"; $str .= " \n"; $str .= " \n"; $str .= " \n"; $str .= "
Received (non-spam): $tot_rec total, $avg_rec average
\n"; $str .= "
Sent Mail: $tot_sent total, $avg_sent monthly average
\n"; $str .= "
Filtered Spam: $tot_spam total, $avg_spam average
\n"; $str .= "
Unfiltered Spam: $tot_unfil total, $avg_unfil average
\n"; $str .= " \n"; $str .= "
\n"; $str .= "
\n"; $str .= "

\n"; $str .= " Graph utility heavily modified from a script\n"; $str .= " by Chris Chiappa\n"; $str .= "

\n"; $str .= "\n"; open (OUTHTML, "> index.html"); print OUTHTML $str; close (OUTHTML); } sub gettotal{ my ($phash) = @_; my ($key, $total); $total = 0; foreach $key (keys(%$phash)){ $total += $$phash{$key}; } return $total; } sub getavg{ my ($phash) = @_; my ($key, $average, $total, $count); $count = 0; $total = 0; foreach $key (keys(%$phash)){ $count++; $total += $$phash{$key}; } if($count){ $average = int($total / $count); } else{ $average = 0; } return $average; } # hard code these numbers, in order to save time # when running the script. Also, doing this means # I can erase old listserv emails that I don't care # about without messing up the stats. sub init_hardcoded_data{ $msent{"2003-may"} = 332; $msent{"2003-jun"} = 522; $msent{"2003-jul"} = 625; $msent{"2003-aug"} = 365; $msent{"2003-sep"} = 553; $msent{"2003-oct"} = 439; $msent{"2003-nov"} = 293; $msent{"2003-dec"} = 346; $msent{"2004-jan"} = 386; $msent{"2004-feb"} = 406; $msent{"2004-mar"} = 662; $msent{"2004-apr"} = 709; $msent{"2004-may"} = 627; $msent{"2004-jun"} = 429; $msent{"2004-jul"} = 334; $msent{"2004-aug"} = 467; $msent{"2004-sep"} = 680; $msent{"2004-oct"} = 533; $msent{"2004-nov"} = 493; $msent{"2004-dec"} = 372; $msent{"2005-jan"} = 553; $msent{"2005-feb"} = 653; $msent{"2005-mar"} = 717; $msent{"2005-apr"} = 698; $msent{"2005-may"} = 685; $msent{"2005-jun"} = 545; $msent{"2005-jul"} = 294; $msent{"2005-aug"} = 405; $msent{"2005-sep"} = 538; $msent{"2005-oct"} = 375; $msent{"2005-nov"} = 417; $msent{"2005-dec"} = 386; $msent{"2006-jan"} = 679; $msent{"2006-feb"} = 549; $msent{"2006-mar"} = 544; $msent{"2006-apr"} = 626; $msent{"2006-may"} = 888; $msent{"2006-jun"} = 677; $msent{"2006-jul"} = 700; $msent{"2006-aug"} = 694; $msent{"2006-sep"} = 653; $msent{"2006-oct"} = 470; $msent{"2006-nov"} = 565; $msent{"2006-dec"} = 483; $msent{"2007-jan"} = 760; $msent{"2007-feb"} = 522; $msent{"2007-mar"} = 717; $msent{"2007-apr"} = 541; $msent{"2007-may"} = 666; $msent{"2007-jun"} = 594; $msent{"2007-jul"} = 548; $msent{"2007-aug"} = 628; $msent{"2007-sep"} = 682; $msent{"2007-oct"} = 812; $msent{"2007-nov"} = 517; $msent{"2007-dec"} = 726; $msent{"2008-jan"} = 830; $msent{"2008-feb"} = 490; $msent{"2008-mar"} = 522; $msent{"2008-apr"} = 670; $msent{"2008-may"} = 770; $msent{"2008-jun"} = 577; $msent{"2008-jul"} = 459; $msent{"2008-aug"} = 360; $msent{"2008-sep"} = 373; $msent{"2008-oct"} = 598; $msent{"2008-nov"} = 415; $msent{"2008-dec"} = 634; $msent{"2009-jan"} = 407; $msent{"2009-feb"} = 549; $msent{"2009-mar"} = 529; $msent{"2009-apr"} = 477; $msent{"2009-may"} = 544; $msent{"2009-jun"} = 543; $msent{"2009-jul"} = 493; $msent{"2009-aug"} = 487; $msent{"2009-sep"} = 420; $msent{"2009-oct"} = 547; $msent{"2009-nov"} = 398; $msent{"2009-dec"} = 376; $msent{"2010-jan"} = 490; $msent{"2010-feb"} = 567; $msent{"2010-mar"} = 399; $msent{"2010-apr"} = 383; $msent{"2010-may"} = 476; $msent{"2010-jun"} = 593; $msent{"2010-jul"} = 410; $msent{"2010-aug"} = 308; $msent{"2010-sep"} = 507; $msent{"2010-oct"} = 563; $msent{"2010-nov"} = 407; $msent{"2010-dec"} = 372; $msent{"2011-jan"} = 467; $msent{"2011-feb"} = 453; $msent{"2011-mar"} = 506; $msent{"2011-apr"} = 470; $msent{"2011-may"} = 411; $msent{"2011-jun"} = 337; $msent{"2011-jul"} = 342; $msent{"2011-aug"} = 400; $msent{"2011-sep"} = 496; $msent{"2011-oct"} = 400; $msent{"2011-nov"} = 354; $msent{"2011-dec"} = 480; $msent{"2012-jan"} = 492; $mread{"2003-jun"} = 954; $mread{"2003-jul"} = 1561; $mread{"2003-aug"} = 835; $mread{"2003-sep"} = 1264; $mread{"2003-oct"} = 1143; $mread{"2003-nov"} = 1121; $mread{"2003-dec"} = 1214; $mread{"2004-jan"} = 1218; $mread{"2004-feb"} = 1197; $mread{"2004-mar"} = 1332; $mread{"2004-apr"} = 2198; $mread{"2004-may"} = 1297; $mread{"2004-jun"} = 1171; $mread{"2004-jul"} = 1079; $mread{"2004-aug"} = 1280; $mread{"2004-sep"} = 1981; $mread{"2004-oct"} = 1837; $mread{"2004-nov"} = 1759; $mread{"2004-dec"} = 1401; $mread{"2005-jan"} = 2126; $mread{"2005-feb"} = 2775; $mread{"2005-mar"} = 3753; $mread{"2005-apr"} = 4482; $mread{"2005-may"} = 4210; $mread{"2005-jun"} = 3591; $mread{"2005-jul"} = 2998; $mread{"2005-aug"} = 2826; $mread{"2005-sep"} = 2767; $mread{"2005-oct"} = 2464; $mread{"2005-nov"} = 2340; $mread{"2005-dec"} = 2621; $mread{"2006-jan"} = 3793; $mread{"2006-feb"} = 3165; $mread{"2006-mar"} = 3731; $mread{"2006-apr"} = 4000; $mread{"2006-may"} = 4508; $mread{"2006-jun"} = 4140; $mread{"2006-jul"} = 4703; $mread{"2006-aug"} = 4644; $mread{"2006-sep"} = 4911; $mread{"2006-oct"} = 4816; $mread{"2006-nov"} = 5669; $mread{"2006-dec"} = 3623; $mread{"2007-jan"} = 4856; $mread{"2007-feb"} = 5199; $mread{"2007-mar"} = 5306; $mread{"2007-apr"} = 4651; $mread{"2007-may"} = 5141; $mread{"2007-jun"} = 4962; $mread{"2007-jul"} = 4742; $mread{"2007-aug"} = 5010; $mread{"2007-sep"} = 5155; $mread{"2007-oct"} = 4872; $mread{"2007-nov"} = 3755; $mread{"2007-dec"} = 4304; $mread{"2008-jan"} = 4724; $mread{"2008-feb"} = 3914; $mread{"2008-mar"} = 4004; $mread{"2008-apr"} = 4182; $mread{"2008-may"} = 5401; $mread{"2008-jun"} = 4479; $mread{"2008-jul"} = 4073; $mread{"2008-aug"} = 3576; $mread{"2008-sep"} = 3287; $mread{"2008-oct"} = 3844; $mread{"2008-nov"} = 3534; $mread{"2008-dec"} = 3779; $mread{"2009-jan"} = 2932; $mread{"2009-feb"} = 3731; $mread{"2009-mar"} = 3816; $mread{"2009-apr"} = 3844; $mread{"2009-may"} = 4703; $mread{"2009-jun"} = 5638; $mread{"2009-jul"} = 3425; $mread{"2009-aug"} = 3619; $mread{"2009-sep"} = 3710; $mread{"2009-oct"} = 4556; $mread{"2009-nov"} = 3330; $mread{"2009-dec"} = 4170; $mread{"2010-jan"} = 3889; $mread{"2010-feb"} = 3675; $mread{"2010-mar"} = 3160; $mread{"2010-apr"} = 2832; $mread{"2010-may"} = 2966; $mread{"2010-jun"} = 3305; $mread{"2010-jul"} = 2998; $mread{"2010-aug"} = 2885; $mread{"2010-sep"} = 3384; $mread{"2010-oct"} = 3142; $mread{"2010-nov"} = 2544; $mread{"2010-dec"} = 2229; $mread{"2011-jan"} = 2397; $mread{"2011-feb"} = 2681; $mread{"2011-mar"} = 2580; $mread{"2011-apr"} = 2844; $mread{"2011-may"} = 4345; $mread{"2011-jun"} = 3170; $mread{"2011-jul"} = 2765; $mread{"2011-aug"} = 3066; $mread{"2011-sep"} = 3112; $mread{"2011-oct"} = 3205; $mread{"2011-nov"} = 3224; $mread{"2011-dec"} = 3648; $mread{"2012-jan"} = 4156; $mspam{"2003-aug"} = 41; $mspam{"2003-sep"} = 99; $mspam{"2003-oct"} = 73; $mspam{"2003-nov"} = 61; $mspam{"2003-dec"} = 57; $mspam{"2004-jan"} = 116; $mspam{"2004-feb"} = 261; $mspam{"2004-mar"} = 389; $mspam{"2004-apr"} = 868; $mspam{"2004-may"} = 768; $mspam{"2004-jun"} = 644; $mspam{"2004-jul"} = 511; $mspam{"2004-aug"} = 606; $mspam{"2004-sep"} = 754; $mspam{"2004-oct"} = 867; $mspam{"2004-nov"} = 951; $mspam{"2004-dec"} = 858; $mspam{"2005-jan"} = 773; $mspam{"2005-feb"} = 1372; $mspam{"2005-mar"} = 1212; $mspam{"2005-apr"} = 1296; $mspam{"2005-may"} = 1547; $mspam{"2005-jun"} = 1433; $mspam{"2005-jul"} = 1732; $mspam{"2005-aug"} = 64; $mspam{"2005-sep"} = 58; $mspam{"2005-oct"} = 61; $mspam{"2005-nov"} = 62; $mspam{"2005-dec"} = 67; $mspam{"2006-jan"} = 75; $mspam{"2006-feb"} = 79; $mspam{"2006-mar"} = 101; $mspam{"2006-apr"} = 97; $mspam{"2006-may"} = 190; $mspam{"2006-jun"} = 204; $mspam{"2006-jul"} = 313; $mspam{"2006-aug"} = 303; $mspam{"2006-sep"} = 250; $mspam{"2006-oct"} = 420; $mspam{"2006-nov"} = 701; $mspam{"2006-dec"} = 816; $mspam{"2007-jan"} = 394; $mspam{"2007-feb"} = 307; $mspam{"2007-mar"} = 308; $mspam{"2007-apr"} = 223; $mspam{"2007-may"} = 165; $mspam{"2007-jun"} = 311; $mspam{"2007-jul"} = 378; $mspam{"2007-aug"} = 374; $mspam{"2007-sep"} = 279; $mspam{"2007-oct"} = 665; $mspam{"2007-nov"} = 384; $mspam{"2007-dec"} = 212; $mspam{"2008-jan"} = 180; $mspam{"2008-feb"} = 269; $mspam{"2008-mar"} = 228; $mspam{"2008-apr"} = 185; $mspam{"2008-may"} = 160; $mspam{"2008-jun"} = 276; $mspam{"2008-jul"} = 281; $mspam{"2008-aug"} = 196; $mspam{"2008-sep"} = 242; $mspam{"2008-oct"} = 125; $mspam{"2008-nov"} = 156; $mspam{"2008-dec"} = 193; $mspam{"2009-jan"} = 156; $mspam{"2009-feb"} = 133; $mspam{"2009-mar"} = 214; $mspam{"2009-apr"} = 324; $mspam{"2009-may"} = 282; $mspam{"2009-jun"} = 241; $mspam{"2009-jul"} = 264; $mspam{"2009-aug"} = 226; $mspam{"2009-sep"} = 203; $mspam{"2009-oct"} = 203; $mspam{"2009-nov"} = 174; $mspam{"2009-dec"} = 140; $mspam{"2010-jan"} = 203; $mspam{"2010-feb"} = 185; $mspam{"2010-mar"} = 177; $mspam{"2010-apr"} = 175; $mspam{"2010-may"} = 199; $mspam{"2010-jun"} = 192; $mspam{"2010-jul"} = 276; $mspam{"2010-aug"} = 279; $mspam{"2010-sep"} = 164; $mspam{"2010-oct"} = 167; $mspam{"2010-nov"} = 133; $mspam{"2010-dec"} = 102; $mspam{"2011-jan"} = 91; $mspam{"2011-feb"} = 110; $mspam{"2011-mar"} = 158; $mspam{"2011-apr"} = 141; $mspam{"2011-may"} = 100; # don't really know, deleted the file too quickly $mspam{"2011-jun"} = 81; $mspam{"2011-jul"} = 82; $mspam{"2011-aug"} = 61; $mspam{"2011-sep"} = 78; $mspam{"2011-oct"} = 87; $mspam{"2011-nov"} = 103; $mspam{"2011-dec"} = 114; $mspam{"2012-jan"} = 77; $unfiltered_spam{"2003-oct"} = 0; $unfiltered_spam{"2003-nov"} = 0; $unfiltered_spam{"2003-dec"} = 1; $unfiltered_spam{"2004-jan"} = 0; $unfiltered_spam{"2004-feb"} = 2; $unfiltered_spam{"2004-mar"} = 0; $unfiltered_spam{"2004-apr"} = 2; $unfiltered_spam{"2004-may"} = 1; $unfiltered_spam{"2004-jun"} = 0; $unfiltered_spam{"2004-jul"} = 1; $unfiltered_spam{"2004-aug"} = 3; $unfiltered_spam{"2004-sep"} = 0; $unfiltered_spam{"2004-oct"} = 1; $unfiltered_spam{"2004-nov"} = 0; $unfiltered_spam{"2004-dec"} = 8; $unfiltered_spam{"2005-jan"} = 10; $unfiltered_spam{"2005-feb"} = 12; $unfiltered_spam{"2005-mar"} = 11; $unfiltered_spam{"2005-apr"} = 12; $unfiltered_spam{"2005-may"} = 7; $unfiltered_spam{"2005-jun"} = 11; $unfiltered_spam{"2005-jul"} = 12; $unfiltered_spam{"2005-aug"} = 5; $unfiltered_spam{"2005-sep"} = 1; $unfiltered_spam{"2005-oct"} = 6; $unfiltered_spam{"2005-nov"} = 5; $unfiltered_spam{"2005-dec"} = 0; $unfiltered_spam{"2006-jan"} = 6; $unfiltered_spam{"2006-feb"} = 4; $unfiltered_spam{"2006-mar"} = 8; $unfiltered_spam{"2006-apr"} = 11; $unfiltered_spam{"2006-may"} = 4; $unfiltered_spam{"2006-jun"} = 7; $unfiltered_spam{"2006-jul"} = 10; $unfiltered_spam{"2006-aug"} = 9; $unfiltered_spam{"2006-sep"} = 22; $unfiltered_spam{"2006-oct"} = 71; $unfiltered_spam{"2006-nov"} = 10; $unfiltered_spam{"2006-dec"} = 7; $unfiltered_spam{"2007-jan"} = 15; $unfiltered_spam{"2007-feb"} = 40; $unfiltered_spam{"2007-mar"} = 34; $unfiltered_spam{"2007-apr"} = 26; $unfiltered_spam{"2007-may"} = 27; $unfiltered_spam{"2007-jun"} = 47; $unfiltered_spam{"2007-jul"} = 20; $unfiltered_spam{"2007-aug"} = 7; $unfiltered_spam{"2007-sep"} = 15; $unfiltered_spam{"2007-oct"} = 21; $unfiltered_spam{"2007-nov"} = 113; $unfiltered_spam{"2007-dec"} = 28; $unfiltered_spam{"2008-jan"} = 16; $unfiltered_spam{"2008-feb"} = 16; $unfiltered_spam{"2008-mar"} = 15; $unfiltered_spam{"2008-apr"} = 17; $unfiltered_spam{"2008-may"} = 28; $unfiltered_spam{"2008-jun"} = 14; $unfiltered_spam{"2008-jul"} = 27; $unfiltered_spam{"2008-aug"} = 16; $unfiltered_spam{"2008-sep"} = 15; $unfiltered_spam{"2008-oct"} = 11; $unfiltered_spam{"2008-nov"} = 20; $unfiltered_spam{"2008-dec"} = 31; $unfiltered_spam{"2009-jan"} = 26; $unfiltered_spam{"2009-feb"} = 25; $unfiltered_spam{"2009-mar"} = 46; $unfiltered_spam{"2009-apr"} = 85; $unfiltered_spam{"2009-may"} = 38; $unfiltered_spam{"2009-jun"} = 12; $unfiltered_spam{"2009-jul"} = 27; $unfiltered_spam{"2009-aug"} = 41; $unfiltered_spam{"2009-sep"} = 31; $unfiltered_spam{"2009-oct"} = 23; $unfiltered_spam{"2009-nov"} = 26; $unfiltered_spam{"2009-dec"} = 21; $unfiltered_spam{"2010-jan"} = 19; $unfiltered_spam{"2010-feb"} = 23; $unfiltered_spam{"2010-mar"} = 23; $unfiltered_spam{"2010-apr"} = 27; $unfiltered_spam{"2010-may"} = 16; $unfiltered_spam{"2010-jun"} = 13; $unfiltered_spam{"2010-jul"} = 17; $unfiltered_spam{"2010-aug"} = 13; $unfiltered_spam{"2010-sep"} = 13; $unfiltered_spam{"2010-oct"} = 21; $unfiltered_spam{"2010-nov"} = 16; $unfiltered_spam{"2010-dec"} = 5; $unfiltered_spam{"2011-jan"} = 7; $unfiltered_spam{"2011-feb"} = 15; $unfiltered_spam{"2011-mar"} = 15; $unfiltered_spam{"2011-apr"} = 5; $unfiltered_spam{"2011-apr"} = 20; $unfiltered_spam{"2011-may"} = 25; $unfiltered_spam{"2011-jun"} = 19; $unfiltered_spam{"2011-jul"} = 16; $unfiltered_spam{"2011-aug"} = 13; $unfiltered_spam{"2011-sep"} = 15; $unfiltered_spam{"2011-oct"} = 23; $unfiltered_spam{"2011-nov"} = 19; $unfiltered_spam{"2011-dec"} = 25; $unfiltered_spam{"2012-jan"} = 18; $unfiltered_spam{"2012-feb"} = 7; }