procmail
[Top] [All Lists]

Re: Totalling the numbers from mailstat

2000-08-24 14:59:12
In an effort not to re-invent the "wheel," and before 
I start writing a perl script for this, does anyone have
a script that takes the output of mailstat and,

1. If there has been filtered email, sums the emails and displays
the total below the subtotals.

2. Sums the bytes, then does the same kind of display as above.

Something like this:

Total  Number Folder
-----  ------ ------
2677       1 In-L-xyz
4984       1 SlimPickins
--------------------------------
7661       2 Totals

Here's a Perl program which does something like what you are asking, but it
doesn't use mailstat at all. Luke Mewburn wrote it, and Alan Stebbens and I
enhanced it some.

#!/usr/local/bin/perl
#
# Copyright 1995 Luke Mewburn <lm(_at_)werj(_dot_)com(_dot_)au>.  All rights 
reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#       This product includes software developed by Luke Mewburn.
# 4. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Modified by Alan K. Stebbens <aks(_at_)hub(_dot_)ucsb(_dot_)edu>
# Modified by Edward J. Sabol  
<sabol(_at_)lheamail(_dot_)gsfc(_dot_)nasa(_dot_)gov>
# 
# $Id: proclog,v 1.10a 1995/06/09 09:44:52 aks Exp $
#
# Usage: proclog [logfile]
#       pretty parse procmail log
#

($DIR,$PROG) = $0 =~ m=^(.*/)?([^/]+)$=;
$DIR =~ s=/$== || chop($DIR = `pwd`);

require "timelocal.pl";
require "ctime.pl";
#require "dumpvar.pl";

sub usage {
    local($msg) = shift;
    if ($msg) {
        $msg .= "\n" unless $msg =~ /\n$/;
        print STDERR "$PROG: $msg";
    }
    print <<EOF;
Usage: $PROG [OPTIONS] [logfile]

Options:
  -mh           Assume MH-style folders (default if ~/.mh_profile exists)
  -nomh         Don't assume MH-style folders
  -help         Print this message
EOF
    exit 1;
}

# Assume MH-style if the profile exists

$mh = 1 if -f "$HOME/.mh_profile";

while ($_ = shift @ARGV) {
    !index('-mh',$_) && do { $mh++; next; };
    !index('-nomh',$_) && do { $mh = ''; next; };
    !index('-help',$_) && &usage('');
    /^-.*/ && &usage("unknown option: $_");
    $logfile && &usage("too many arguments");
    $logfile = $_;
}

$vars{"HOME"} = $ENV{"HOME"};
$vars{"USER"} = $ENV{"USER"};
$vars{"LOGNAME"} = $ENV{"LOGNAME"};
%months = ( 'Jan', 0, 'Feb', 1, 'Mar', 2, 'Apr', 3, 'May', 4, 'Jun', 5, 'Jul', 
6, 
'Aug', 7, 'Sep', 8, 'Oct', 9, 'Nov', 10, 'Dec', 11 );
$start = 0;
$finish = 0;
%folders = ();
%size = ();
$total = 0;
$totalsize = 0;

unless ($logfile)
{
    $rcfile = $vars{"HOME"} . "/.procmailrc";
    open(PF, $rcfile) || die("can't open $rcfile - $!\n");
    while (<PF>)
    {
        chop;
        s/\s+#.*$//;
        next if /^$/;
        next unless /(\S+)\s*=\s*(\S+)/;
        $vars{$1} = $2;
    }
    close(PF);

    $logfile = $vars{"LOGFILE"};
    1 while $logfile =~ s/\$([A-Za-z_0-9]+)/$vars{$1}/e;
}

open(LF, $logfile) || die("can't open $logfile - $!\n");
while (<LF>)
{
    chop;
    next unless ($mon, $mday, $hh, $mm, $ss, $year) =
        /^From \S+\s+\S+\s+(\S+)\s+(\d+)\s+(\d\d):(\d\d):(\d\d)\s+(\d+)/;
    $year -= 1900;
    $time = &timelocal($ss, $mm, $hh, $mday, $months{$mon}, $year);
#print "$mon (", $months{$mon}, "), $mday, $hh, $mm, $ss, $year, $time\n";
    $start = $time unless ($start);
    chop($_ = <LF>);
    next unless /^ Subject:\s+(.+)/;
    chop($_ = <LF>);
    next unless ($folder, $size) = /^  Folder:\s+(\S+)\s+(\d+)/;
    $folder =~ s=/\d+$=/= if $mh;       # strip MH message #'s
    $folder =~ s=/msg\....$=/=;
    $folders{$folder}++;
    $size{$folder} += $size;
    $total++;
    $totalsize += $size;
}
close(LF);
chop($start = &ctime($start));
chop($finish = &ctime($time));
$totalsize /= 1024;


format STDOUT_TOP =

        procmail log summary
        --------------------

From: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      $start 
To:   @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      $finish 

Messages     %    Size (K)     %     Mailbox
--------   ----   --------   ----    -------
 .

format STDOUT =
@#######  @##.#   @#####.#  @##.#    @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$count, $pcount,  $size,    $psize,  $name
 .

foreach $name (sort keys %folders)
{
    $num++;
    $count = $folders{$name};
    $size = $size{$name} / 1024;
    $name = "Incoming" if ($name =~ m!/(usr|var)/spool/mail/$vars{'USER'}!);
    $pcount = $count * 100 / $total;
    $psize = $size * 100 / $totalsize;
    write;
}

printf(<<SUMMARY, $total, $totalsize);
--------   ----   --------   ----    -------
%8d  100.0   %8.1f  100.0    $num

SUMMARY

_______________________________________________
procmail mailing list
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)DE
http://MailMan.RWTH-Aachen.DE/mailman/listinfo/procmail

<Prev in Thread] Current Thread [Next in Thread>