#!/usr/local/bin/perl
######################################################################
#
#  listproc_stats.pl
#
#  User contributed utility script.
#
#    10/09/97  submitted by Dick Mead of USC.  listmgr@scat.usc.edu
#
#    12/04/97  slight modifications to use     jrvb@cren.net
#              configurable variables.
#
#  Usage Instructions: 
#
#    Edit the configurable parameters below to match your site.  You can 
#    then either run the script by hand, or run it out of the cron, to 
#    send daily stats.  eg:
#
#    listproc_stats.pl | mail -s "ListProc Daily Stats" user@host.com
#
######################################################################


#######################################################################
# 
#   CONFIGURABLE VARIABLES
#
#######################################################################

$listproc_user = "server";
$LPDIR = $ENV{'LPDIR'};

$incoming_count = "$LPDIR/.incoming.count";
$catmail_log = "/var/log/list-log";




######################################################################
#
#  Actual script...
#
###################################################################### 
 
# use getpwnam to get the home directory because this is a cronjob.
($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $LPDIR, $shell) =
  (getpwnam($listproc_user));
 
#manager name lookup
$mgrline = `grep -v "#" $LPDIR/config | grep manager`;
($tag, $manager) = split(/\s+/,$mgrline);
 
chdir "$LPDIR/lists";
 
opendir(LISTS, ".");
@dirs = sort(grep(-d, readdir(LISTS)));
closedir LISTS;
 
($x1, $x2, $x3, $day,$month,$year) = localtime(time);
$month++;
$year += 1900;
$date = sprintf("%02d/%02d/%02d",$year,$month,$day);

system "touch $incoming_count";
 
print "Date\t\tNew\tTotal\t#Subs\t#Msgs\t#Errors\tListname\n";
$total = 0;
$totnew = 0;
$totmsg = 0;
$totsub = 0;
$toterr = 0;
$errs = 0;
$lists = 0;
$subs = 0;
$subscribers = 0;

foreach $dir (@dirs)
{
        next unless (-f "$dir/.subscribers");
        
        `touch $dir/.msgno`; # make sure it's there
        `touch $dir/.msgno.old`; # make sure it's there
 
        $oldline = `cat $dir/.msgno.old`;
        $newline = `cat $dir/.msgno`;
 
        ($old) = ($oldline =~ /^(\d+)/);
        ($new) = ($newline =~ /^(\d+)/);
        if ($new < $old) { $new = $old; };
        $tmpsub = `wc -l $dir/.subscribers`;
        ($subs) = ($tmpsub =~ /(\d+)/);
        $msgs  = $subs * ($new - $old);
        $errs = 0;
        if (-e "$dir/.errors.te") {
                $tmpsub  = `wc -l $dir/.errors.te`;
                ($errs) = ($tmpsub =~ /(\d+)/); 
                };
 
        print "$date\t",$new - $old, "\t$new\t$subs\t$msgs\t$errs\t$dir\n";
 
        $total += $new; 
        $totnew += $new - $old;
        $totmsg += $msgs;
        $totsub += $subs;
        $toterr += $errs;
 
        unlink "$dir/.msgno.old";
        `cp $dir/.msgno $dir/.msgno.old`;
 
        $lists++;
}


#
# Count the incoming messages
#
$inc = `grep -c LISTPROCESSOR $catmail_log`;
chop $inc;
$incsub = 0;
$oldinc = `cat $incoming_count`;
$totinc = $inc + $oldinc;
$msgs = 0;
$errs = 0;
$dir = '*LISTPROC';
print "$date\t$inc\t$oldinc\t$incsub\t$msgs\t$errs\t$dir\n";
 
open(INC,">$incoming_count");
print INC "$totinc";
close(INC);
 
$total += $totinc;
$totnew += $inc;
 
print "$date\t$totnew\t$total\t$totsub\t$totmsg\t$toterr\t*TOTAL\n";
 
