I recently installed Amavis / SpamAssassin / ClamAV and was looking around for a good script to give me some statistics. The only script I found was
. The script didn't work, didn't use warnings or strict, it was pretty bad.
So from those terrible beginnings, I have pretty much rewritten the entire script. It works as advertised now. What I'm looking for is comments on what other things I can do to improve it.
#!/usr/bin/perl -w
use strict;
# original from http://www.exit0.us/index.php?pagename=MikesStatScript
# Mail Statistics
#---------------------------------------------------------------------
+-------------------
# Total spamassassin rejected scanner total ma
+ils total mails
# Mails says 'spam' by ruleset says virus undelive
+red delivered
#---------------------------------------------------------------------
+-------------------
# Jun 25 180 0 ( 0.00%) 65 (36.11%) 2 ( 1.11%) 67 (37.2
+2%) 113 (62.78%)
# Jun 26 396 0 ( 0.00%) 191 (48.23%) 1 ( 0.25%) 192 (48.4
+8%) 204 (51.52%)
# Jun 27 317 0 ( 0.00%) 130 (41.01%) 5 ( 1.58%) 135 (42.5
+9%) 182 (57.41%)
# Jun 28 386 0 ( 0.00%) 235 (60.88%) 6 ( 1.55%) 241 (62.4
+4%) 145 (37.56%)
# Jun 29 478 0 ( 0.00%) 244 (51.05%) 3 ( 0.63%) 247 (51.6
+7%) 231 (48.33%)
# Jun 30 545 0 ( 0.00%) 294 (53.94%) 3 ( 0.55%) 297 (54.5
+0%) 248 (45.50%)
# Jul 1 512 0 ( 0.00%) 265 (51.76%) 0 ( 0.00%) 265 (51.7
+6%) 247 (48.24%)
# Jul 2 344 0 ( 0.00%) 143 (41.57%) 0 ( 0.00%) 143 (41.5
+7%) 201 (58.43%)
# Jul 3 234 0 ( 0.00%) 72 (30.77%) 4 ( 1.71%) 76 (32.4
+8%) 158 (67.52%)
# Jul 4 413 0 ( 0.00%) 175 (42.37%) 1 ( 0.24%) 176 (42.6
+2%) 237 (57.38%)
# Jul 5 346 31 ( 8.96%) 104 (30.06%) 5 ( 1.45%) 140 (40.4
+6%) 206 (59.54%)
# Jul 6 294 181 (61.56%) 50 (17.01%) 3 ( 1.02%) 234 (79.5
+9%) 60 (20.41%)
# Jul 7 261 173 (66.28%) 29 (11.11%) 9 ( 3.45%) 211 (80.8
+4%) 50 (19.16%)
# Jul 8 250 165 (66.00%) 3 ( 1.20%) 0 ( 0.00%) 168 (67.2
+0%) 82 (32.80%)
# Jul 9 213 151 (70.89%) 10 ( 4.69%) 2 ( 0.94%) 163 (76.5
+3%) 50 (23.47%)
#=====================================================================
+===================
#Totals 5185 701 (13.52%) 2079 (40.10%) 44 ( 0.85%) 2824 (54.4
+6%) 2361 ( 0.96%)
use IO::File;
use FileHandle;
use Benchmark;
my $version = '1.09';
my $date;
my @date;
my @files = (
'/var/log/maillog.4',
'/var/log/maillog.3',
'/var/log/maillog.2',
'/var/log/maillog.1',
'/var/log/maillog'
);
my $tt; # total messages
my $st; # total spam
my $dt; # total discards by rules
my $ut; # undeliverable
my $dl; # delivered
my $dlt; # delivered total
my $vt; # viruses total
my $junk;
my $lastdate = '';
my @tmparray;
my $count;
my $start_time = new Benchmark;
&printheaders;
&processfiles;
&processdate;
&printtotals;
sub printheaders{
format HEADER0 =
Mail Statistics
----------------------------------------------------------------------
+------------------
Total spamassassin rejected scanner total mai
+ls total mails
Mails says 'spam' by ruleset says virus undeliver
+ed delivered
----------------------------------------------------------------------
+------------------
.
STDOUT->format_name("HEADER0");
write STDOUT;
}
sub processfiles{
foreach (@files){
open(IN, $_) || die "Sorry, but I can't open $_ :$!";
while(<IN>){
$count++;
@date = (split)[0,1];
$date = join(' ',@date);
if (($lastdate ne $date) && ($lastdate ne '')){
&processdate;
}
push (@tmparray, $_);
$lastdate = $date;
}
close IN;
}
}
sub processdate{
# get the date for the report
my $tmp = 0;
my $mo;
my $da;
foreach(@tmparray){
if ($tmp < 1){
@date = (split)[0,1];
# @date[0] = month
# @date[1] = day
$mo = $date[0];
$da = $date[1];
}
$tmp++;
}
my $t = grep ((/amavis\[/ && / ESMTP::10024 /), @tmparray);
+ # total mail
my $s = grep ((/SPAM-TAG, / && /, Yes, /), @tmparray);
+ # spam
my $d = grep /SPAM-ID:/, @tmparray;
+ # reject by header / body rule
my $v = grep /INFECTED/, @tmparray;
+ # virus found by ClamAV
my $u = $s + $d + $v;
+ # undelivered
$dl = $t - ($s + $d + $v);
+ # delivered
unless ($t == 0){
my $spf = sprintf("%-5.2f", ((100*$s)/$t)); # daily total perc
+ent spam
my $dpf = sprintf("%-5.2f", ((100*$d)/$t)); # daily total perc
+ent caught by ruleset
my $vpf = sprintf("%-5.2f", ((100*$v)/$t)); # daily total perc
+ent viruses
my $upf = sprintf("%-5.2f", ((100*$u)/$t)); # daily total perc
+ent undelivered
my $dlpf = sprintf("%-5.2f", ((100*$dl)/$t)); # daily total perc
+ent delivered
format DATA =
@>>>@>>>@#####@##### (@#.##%) @### (@#.##%) @### (@#.##%) @#### (@#.##
+%) @#### (@#.##%)
$mo,$da,$t, $s, $spf, $d, $dpf, $v, $vpf, $u, $upf,
+ $dl, $dlpf
.
STDOUT->format_name("DATA");
write STDOUT unless $t == 0;
$tt += $t; $st += $s; $dt += $d; $ut += $u; $vt += $v; $dlt += $dl;
}
undef @tmparray;
}
sub printtotals{
unless ($tt == 0){
my $stpf = sprintf("%-5.2f", ((100*$st)/$tt)); # grand total perce
+nt spam
my $dtpf = sprintf("%-5.2f", ((100*$dt)/$tt)); # grand total perce
+nt caught by ruleset
my $vtpf = sprintf("%-5.2f", ((100*$vt)/$tt)); # grand total perce
+nt viruses
my $utpf = sprintf("%-5.2f", ((100*$ut)/$tt)); # grand total perce
+nt undelivered
my $dlpf = sprintf("%-5.2f", ((100*$dl)/$tt)); # grand total perce
+nt delivered
my $txt = 'Totals';
format FOOTER =
======================================================================
+==================
@<<<<< @#####@##### (@#.##%) @### (@#.##%) @### (@#.##%) @#### (@#.##
+%) @#### (@#.##%)
$txt, $tt, $st, $stpf, $dt, $dtpf, $vt, $vtpf, $ut, $utpf
+, $dlt, $dlpf
.
STDOUT->format_name("FOOTER");
write STDOUT;
my $end_time = new Benchmark;
my $difference = timediff($end_time, $start_time);
print ("Processed $count lines\n");
print ("Processing time was ", timestr($difference), "\n");
print "SAStatScript.pl version $version\n";
print "\n"x3;
}
}