<?xml version="1.0" encoding="windows-1252"?>
<node id="473729" title="SpamAssassin / Amavis-new / Clam-AV" created="2005-07-10 01:16:29" updated="2005-08-15 03:14:42">
<type id="115">
perlquestion</type>
<author id="31634">
cajun</author>
<data>
<field name="doctext">
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 &lt;a href="http://www.exit0.us/index.php?pagename=MikesStatScript"&gt;this script&lt;/a&gt;. The script didn't work, didn't use warnings or strict, it was pretty bad. &lt;i&gt;(even on the website the said it "might need tweeking")&lt;/i&gt;
&lt;p&gt;
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.
&lt;p&gt;
Thanks,&lt;br&gt;
Mike
&lt;p&gt;
&lt;readmore&gt;
&lt;c&gt;
#!/usr/bin/perl -w

use strict;

# original from http://www.exit0.us/index.php?pagename=MikesStatScript

#                                   Mail Statistics
#----------------------------------------------------------------------------------------
#          Total   spamassassin    rejected      scanner      total mails    total mails
#          Mails   says 'spam'    by ruleset    says virus    undelivered     delivered
#----------------------------------------------------------------------------------------
# Jun  25   180     0 ( 0.00%)   65 (36.11%)    2 ( 1.11%)    67 (37.22%)   113 (62.78%)
# Jun  26   396     0 ( 0.00%)  191 (48.23%)    1 ( 0.25%)   192 (48.48%)   204 (51.52%)
# Jun  27   317     0 ( 0.00%)  130 (41.01%)    5 ( 1.58%)   135 (42.59%)   182 (57.41%)
# Jun  28   386     0 ( 0.00%)  235 (60.88%)    6 ( 1.55%)   241 (62.44%)   145 (37.56%)
# Jun  29   478     0 ( 0.00%)  244 (51.05%)    3 ( 0.63%)   247 (51.67%)   231 (48.33%)
# Jun  30   545     0 ( 0.00%)  294 (53.94%)    3 ( 0.55%)   297 (54.50%)   248 (45.50%)
# Jul   1   512     0 ( 0.00%)  265 (51.76%)    0 ( 0.00%)   265 (51.76%)   247 (48.24%)
# Jul   2   344     0 ( 0.00%)  143 (41.57%)    0 ( 0.00%)   143 (41.57%)   201 (58.43%)
# Jul   3   234     0 ( 0.00%)   72 (30.77%)    4 ( 1.71%)    76 (32.48%)   158 (67.52%)
# Jul   4   413     0 ( 0.00%)  175 (42.37%)    1 ( 0.24%)   176 (42.62%)   237 (57.38%)
# Jul   5   346    31 ( 8.96%)  104 (30.06%)    5 ( 1.45%)   140 (40.46%)   206 (59.54%)
# Jul   6   294   181 (61.56%)   50 (17.01%)    3 ( 1.02%)   234 (79.59%)    60 (20.41%)
# Jul   7   261   173 (66.28%)   29 (11.11%)    9 ( 3.45%)   211 (80.84%)    50 (19.16%)
# Jul   8   250   165 (66.00%)    3 ( 1.20%)    0 ( 0.00%)   168 (67.20%)    82 (32.80%)
# Jul   9   213   151 (70.89%)   10 ( 4.69%)    2 ( 0.94%)   163 (76.53%)    50 (23.47%)

#========================================================================================
#Totals    5185   701 (13.52%) 2079 (40.10%)   44 ( 0.85%)  2824 (54.46%)  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;

&amp;printheaders;
&amp;processfiles;
&amp;processdate;
&amp;printtotals;


sub printheaders{
format HEADER0 =


                                   Mail Statistics
----------------------------------------------------------------------------------------
          Total   spamassassin    rejected      scanner      total mails    total mails
          Mails   says 'spam'    by ruleset    says virus    undelivered     delivered
----------------------------------------------------------------------------------------
.

STDOUT-&gt;format_name("HEADER0");
write STDOUT;
}


sub processfiles{
foreach (@files){
   open(IN, $_) || die "Sorry, but I can't open $_ :$!";
   while(&lt;IN&gt;){
      $count++;
      @date = (split)[0,1];
      $date = join(' ',@date);
      if (($lastdate ne $date) &amp;&amp; ($lastdate ne '')){
        &amp;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 &lt; 1){
        @date = (split)[0,1];
        # @date[0] = month
        # @date[1] = day
        $mo = $date[0];
        $da = $date[1];
      }
      $tmp++;
   }

   my $t = grep ((/amavis\[/ &amp;&amp; / ESMTP::10024 /), @tmparray);         # total mail
   my $s = grep ((/SPAM-TAG, / &amp;&amp; /, 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 percent spam
   my $dpf = sprintf("%-5.2f", ((100*$d)/$t));     #  daily total percent caught by ruleset
   my $vpf = sprintf("%-5.2f", ((100*$v)/$t));     #  daily total percent viruses
   my $upf = sprintf("%-5.2f", ((100*$u)/$t));     #  daily total percent undelivered
   my $dlpf = sprintf("%-5.2f", ((100*$dl)/$t));   #  daily total percent delivered

format DATA =
@&gt;&gt;&gt;@&gt;&gt;&gt;@#####@##### (@#.##%) @### (@#.##%) @### (@#.##%) @#### (@#.##%) @#### (@#.##%)
$mo,$da,$t,    $s,    $spf,   $d,   $dpf,   $v,   $vpf,   $u,    $upf,   $dl,   $dlpf
.

STDOUT-&gt;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 percent spam
my $dtpf = sprintf("%-5.2f", ((100*$dt)/$tt));    #  grand total percent caught by ruleset
my $vtpf = sprintf("%-5.2f", ((100*$vt)/$tt));    #  grand total percent viruses
my $utpf = sprintf("%-5.2f", ((100*$ut)/$tt));    #  grand total percent undelivered
my $dlpf = sprintf("%-5.2f", ((100*$dl)/$tt));    #  grand total percent delivered

my $txt = 'Totals';
format FOOTER =

========================================================================================
@&lt;&lt;&lt;&lt;&lt;  @#####@##### (@#.##%) @### (@#.##%) @### (@#.##%) @#### (@#.##%) @#### (@#.##%)
$txt,   $tt,  $st,    $stpf,  $dt,  $dtpf,  $vt,  $vtpf,  $ut,   $utpf,  $dlt,  $dlpf


.

STDOUT-&gt;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;
}
}
&lt;/c&gt;
&lt;/readmore&gt;</field>
</data>
</node>
