#!/usr/bin/perl

# $Log$
# Revision 1.3.2.1  2005/03/07 19:52:31  jimmyo
# generic/mailman now handles regular mailman (Deb#297904).
#
# Revision 1.3  2004/12/09 22:12:55  jimmyo
# Added "graph_period" option, to make "graph_sums" usable.
#
# Revision 1.2  2004/11/21 00:16:56  jimmyo
# Changed a lot of plugins so they use DERIVE instead of COUNTER.
#
# Revision 1.1  2004/01/02 18:50:00  jimmyo
# Renamed occurrances of lrrd -> munin
#
# Revision 1.1.1.1  2004/01/02 15:18:07  jimmyo
# Import of LRRD CVS tree after renaming to Munin
#
# Revision 1.3  2003/11/07 17:43:16  jimmyo
# Cleanups and log entries
#


#%# family=contrib

$statefile = "/var/db/munin-pluginstate/munin-mailman-log.state";
$pos = undef;
$posts = 0;
$members = 0;

# System paths (as known to mailman-limacute
$logfile = "/var/log/mailman/post";
$libdir  = "/var/lib/mailman";
$lister  = "/usr/lib/mailman/bin/list_members";

if (-f "$logfile.0")
{
    $rotlogfile = $logfile . ".0";
}
elsif (-f "$logfile.1")
{
    $rotlogfile = $logfile . ".1";
}
elsif (-f "$logfile.01")
{
    $rotlogfile = $logfile . ".01";
}
else
{
    $rotlogfile = $logfile . ".0";
}

if($ARGV[0] and $ARGV[0] eq "config") {
    print "graph_title Mailman usage\n";
    print "graph_args --base 1000\n";
    print "posts.label posts per minute\n";
    print "posts.type DERIVE\n";
    print "posts.min 0\n";
    print "members.label members\n";
    print "members.type GAUGE\n";
    exit 0;
}

if(! -f $logfile &&
   ! -d $libdir &&
   ! -x $lister &&
   ! -f $rotlogfile) {
    print "posts.value U\n";
    print "members.value U\n";
    exit 0;
}

if(-f $statefile) {
    open(IN, "<$statefile") or exit 4;
    if(<IN> =~ /^(\d+):(\d+)/) {
	($pos,$posts) = ($1, $2);
    }
    close(IN);
}

$startsize = (stat $logfile)[7];

if(!defined $pos) {
    # Initial run.
    $pos = $startsize;
    $startsize = 0;
}

if($startsize < $pos) {
    # Log rotated
    parseMailmanLog($rotlogfile, $pos, (stat $rotlogfile)[7]);
    $pos = 0;
}

parseMailmanLog($logfile, $pos, $startsize);
$pos = $startsize;

parseMailmanLists();

print "posts.value $posts\n";
print "members.value $members\n";

open (OUT, ">$statefile") or exit 4;
print OUT "$pos:$posts\n";
close OUT;

sub parseMailmanLog {
    my($fname, $start, $stop) = @_;
    open(LOGFILE, $fname) or exit 3;
    seek(LOGFILE, $start, 0) or exit 2;
    while(tell(LOGFILE) < $stop) {
	my $line = <LOGFILE>;
	if($line =~ / post to/) {
	    $posts++;
	}
    }
    close(LOGFILE);
}

sub parseMailmanLists {
    my @domains = &mailmanDomains;
    for(my $i = 0; $i < @domains; $i++) {
        my @lists = &mailmanLists($domains[$i]);
        for(my $j = 0; $j < @lists; $j++) {
            $members += &mailmanMembers($domains[$i], $lists[$j]);
        }
    }
}

sub mailmanDomains {
    my @domains;
    if (-e "$libdir/data") {
	@domains = (".");
    } else {
	opendir(DOMAINS, $libdir) or exit 3;
	@domains = grep { !/^\@/ && !/^\./ && -d "$libdir/$_" } readdir(DOMAINS);
	closedir(DOMAINS);
    }
    return @domains;
}

sub mailmanLists {
    my ($domain) = @_;
    opendir(LISTS, "$libdir/$domain/lists") or exit 3;
    my @lists = grep { !/^\@/ && !/^\./ && -d "$libdir/$domain/lists/$_" } readdir(LISTS);
    closedir(LISTS);
    return @lists;
}

sub mailmanMembers {
    my ($domain, $list) = @_;
    return 0 unless $domain;
    return 0 unless $list;
    my $cnt = 0;
    open(MEMBERS, "DOMAIN=$domain $lister $list|") or exit 3;
    while(<MEMBERS>) {
        $cnt++;
    }
    close(MEMBERS);
    return $cnt;
}

# vim:syntax=perl
