Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

SpamAssassin / Amavis-new / Clam-AV

by cajun (Chaplain)
on Jul 10, 2005 at 05:16 UTC ( [id://473729]=perlquestion: print w/replies, xml ) Need Help??

cajun has asked for the wisdom of the Perl Monks concerning the following question:

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 this script. The script didn't work, didn't use warnings or strict, it was pretty bad. (even on the website the said it "might need tweeking")

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.

Thanks,
Mike

#!/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; } }

Replies are listed 'Best First'.
Re: SpamAssassin / Amavis-new / Clam-AV
by nothingmuch (Priest) on Jul 10, 2005 at 10:31 UTC
    • This seems in order: sub fmt_percent { sprintf("...", $_[0] }
    • I would try to do away with most of the file scoped vars (my $tt) etc, by trying to consolidate the logic that drives them into single expressions... I find that reading an expression in one go has more of a mnemonic effect than many variables which are similar in nature. This only works to some extent, though.
    • I like your use of grep in scalar context
    • I don't like your use of print-like-a-function.... You can omit the parens.
    • I'm not sure, but maybe my @files = glob("/var/log/maillog*") is in order
    Aside from that, this is good code, readable, well structured, and so forth. I'm assuming it was formatted more structuredly before it was posted ;-)

    Ciao!

    -nuffin
    zz zZ Z Z #!perl
Re: SpamAssassin / Amavis-new / Clam-AV
by chanio (Priest) on Jul 11, 2005 at 04:20 UTC
Re: SpamAssassin / Amavis-new / Clam-AV
by Akhasha (Scribe) on Jul 11, 2005 at 05:28 UTC

    For comparison/ideas you might like to check the source of the amavis-stats Debian package.

    P.S. Thanks for the source :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://473729]
Approved by NetWallah
Front-paged by NetWallah
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2024-04-18 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found