There are probably some adjustments to be made. 

Use at your own risk. Enjoy. 

If you improve on it, please make the improvements available 
to the list. 

Mike Andrews
[EMAIL PROTECTED]
Tired old sysadmin 

#!/usr/bin/perl
#
#       program to produce total mail item / spam (%) / discarded (%) / cleaned
#       stats for each of a set of /var/log/maillog*gz files and for the set
#       as a whole.

#       Uses strings inserted into maillog files by MailScanner and SpamAssassin
#       to determine number of messages that are spam (SpamAssassin) the number
#       of dangerous attachments (MailScanner) that were cleaned. Also notes number 
#       of messages discarded (sendmail).

#       Computes percentages and totals for each maillog*gz file read, and over 
#       all maillog*gz files read.

#       total is the number of matches to /from=/
#       spam is the number of matches to /is spam/
#       discarded is the numnber of matches to /ruleset=/
#       cleaned is the number of matches to /Cleaned/

#       Reads directory "/var/log" and only selects files
#       that match /maillog*.gz/

#       22Aug2003 added support for output of ClamAV antivirus/worm scanner
#       use / FOUND\s$/ as pattern to match it. 
#       

#       We'd like to put out *this* report: 
#       
#              Mails  spamassassin   rejected     scanner      total mails
#              Total  says "spam"    by ruleset   says virus   undelivered  .
#       Aug 18  5033  1076 (21.38%)  297 (5.90%)    0 (0.00%)  1373 (27.28%)
#       Aug 19  6777  1459 (21.53%)  399 (5.89%)    0 (0.00%)  1858 (27.42%)
#       Aug 20  7765  1630 (20.99%)  479 (6.17%)  175 (2.25%)  2284 (29.41%)
#       Aug 21  6555  1310 (19.98%)  476 (7.26%)  759(11.58%)  2545 (38.83%)
#       Aug 22  5342  1189 (22.26%)  416 (7.79%)  431 (8.07%)  2036 (38.11%)
#       Aug 23  2515  1226 (48.75%)  233 (9.26%)  134 (5.33%)  1593 (63.34%)
#       Aug 24  2359   995 (42.18%)  166 (7.04%)  211 (8.94%)  1372 (58.16%)
#       Aug 25  5580  1156 (20.72%)  444 (7.96%)  636(11.40%)  2236 (40.07%)

use IO::File;
use POSIX qw(tmpnam);
use FileHandle;

my $path = "/var/log";

#       formats for static headers

format HEADER0 = 
                        Mail Statistics; 
Produced by isdmon2:/home/mikea/bin/mailstats.pl; Run by isdmon2:/etc/crontab
.

format HEADER1 = 
         Mails   spamassassin   rejected      scanner       total mails
.
format HEADER2 = 
         Total   says 'spam'    by ruleset    says virus    undelivered
.

STDOUT->format_name("HEADER0");
write STDOUT;
STDOUT->format_name("HEADER1");
write STDOUT;
STDOUT->format_name("HEADER2");
write STDOUT;

# print "Mail Statistics;\n Produced by isdmon2:/home/mikea/bin/mailstats.pl;\n Run by 
isdmon2:/etc/crontab \n\n\n";

opendir (DIR,$path) or die "opendir failed for $path: $! \n";

@files = readdir(DIR);

@files = grep  /^maillog/ , @files;

@files = grep  /\.gz$/ , @files ;

@files = sort @files;

# total,    spam,    ruleset, cleaned, undelivered
my $tt = 0; $st = 0; $dt = 0; $ct = 0; $ut = 0; #totals

#  grand total percentages: 
#  %spam     %ruleset  %undelivered
my $stp ; my $dtp ; my $utp ;

$numfiles = @files;

for ( $fileno =0; $fileno < $numfiles; $fileno++)
        {

#       straight from the Perl Cookbook, 
#               section 7.5 Creating Temporary Files

#       Try new temporary filenames until we get one that
#       doesn't already exist

                do      { $name = tmpnam() }
                until $FH = IO::File->new($name, O_RDWR|O_CREAT|O_EXCL);

                $filename = "$path/"[EMAIL PROTECTED];

                system "zcat $filename > $name";
                open (FH, "$name");
                @wholefile = <FH>;

#               print "$wholefile[0] \n";

#               grab the date and time (items 1 and 2)
#               from the first line of the log, which is $wholefile[0])

                ($mo, $da, $junk) = split ' ', $wholefile[0], 3;
#
#print "mo = $mo \n";
#print "da = $da \n";
#print "\$wholefile[0] = $wholefile[0]\n";

                my $t = grep /from=/, @wholefile;               # total
                my $s = grep /is spam/, @wholefile;             # spam
                my $d = grep /ruleset/, @wholefile;             # ruleset
                my $c = grep /Cleaned/, @wholefile;             # cleaned
                my $v = grep / FOUND  $/, @wholefile;   # virus found by ClamAV
                my $u = $s + $d + $v;                                   # undelivered

                my $sp = (100*$s)/$t unless $t == 0;
                my $dp = (100*$d)/$t unless $t == 0;
                my $up = (100*$u)/$t unless $t == 0;
                my $vp = (100*$v)/$t unless $t == 0;

                my $spf = sprintf("%-5.2f",$sp);
                my $dpf = sprintf("%-5.2f",$dp);
                my $upf = sprintf("%-5.2f",$up);
                my $upf = sprintf("%-5.2f",$up);
                my $upf = sprintf("%-5.2f",$up);
                my $vpf = sprintf("%-5.2f",$vp);

#       Print static headers:

#               print "       Mails  spamassassin   rejected     scanner      total 
mails\n";
#           print "       Total  says 'spam'    by ruleset   says virus   
undelivered\n";

                $head1= "       Mails  spamassassin   rejected     scanner      total 
mails\n";
                $head2= "       Total  says 'spam'    by ruleset   says virus   
undelivered\n";

#              00000000011111111112222222222333333333344444444445555555555666666666
#              12345678901234567890123456789012345678901234567890123456789012345678
format DATA = 
#        $t    $s     $spf    $d    $dpf    $v    $vpf    $u     $upf       
@>>>@>>>@[EMAIL PROTECTED] (@#.##%) @### (@#.##%) @### (@#.##%) @#### (@#.##%)
$mo $da $t     $s     $spf    $d    $dpf    $v    $vpf    $u     $upf
.

STDOUT->format_name("DATA");
write STDOUT;
                $tt += $t; $st += $s; $dt += $d; $ct += $c; $ut += $u; $vt += $v;

#               print 
"================================================================================\n\n";

#       delete the new tempfile. 

        unlink $name;

        }

$stp = (100 * $st)/$tt unless $tt == 0; #       total percentage spam
$dtp = (100 * $dt)/$tt unless $tt == 0; #       was total percentage discarded, now 
total percentage caught by ruleset
$utp = (100 * $ut)/$tt unless $tt == 0; #       total percentage undelivered
$vtp = (100 * $vt)/$tt unless $tt == 0; #       total percentage virus

$stpf = sprintf("%-5.2f",$stp); #       formatted 5.2 string 
$dtpf = sprintf("%-5.2f",$dtp); #       formatted 5.2 string
$utpf = sprintf("%-5.2f",$utp); #       formatted 5.2 string
$vtpf = sprintf("%-5.2f",$vtp); #       formatted 5.2 string

#       Install atexit-style handler so that when we exit or die,
#       we delete the new tempfile. 

END     { unlink $name; }


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?   SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/
_______________________________________________
Spamassassin-talk mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/spamassassin-talk

Reply via email to