#!/usr/bin/perl
#
# Plugin to monitor the number of mails received and delivered by exim.
#
# Usage: copy or link into /etc/munin/node.d/
#
# Parameters:
#
# 	config   (required)
# 	autoconf (optional - used by munin-config)
#
# Config variables:
#
# 	logdir       - Override what exim says
# 	exim         - Where's exim?
#
# $Log: exim_mailstats.in,v $
# 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.7  2003/11/15 11:10:28  jimmyo
# Various fixes
#
# Revision 1.6  2003/11/07 17:43:16  jimmyo
# Cleanups and log entries
#
#
#
# Magic markers (optional - used by munin-config and some installation
# scripts):
#
#%# family=auto
#%# capabilities=autoconf

$statefile = "/var/lib/munin/plugin-state/plugin-exim_mailstats.state";
$pos   = undef;
$received = 0;
$completed = 0;
($dirname = $0) =~ s/[^\/]+$//;
$EXIM = "/usr/sbin/exim";
$EXIM = "/usr/sbin/exim4" if (-x "/usr/sbin/exim4"); # a Debianism
$EXIM = $ENV{'exim'} if defined  $ENV{'exim'};
$LOGDIR = $ENV{'logdir'} || undef;

if ( $ARGV[0] and $ARGV[0] eq "autoconf" )
{
    my $logfile;
    if (!$LOGDIR)
    {
	$logfile = `$EXIM -bP log_file_path 2>/dev/null`;
	if (! $?)
	{
	    if ($logfile =~ /^\S+\s+=\s+([^%]+)%/)
	    {
		$logfile = $1;
	    }
	    else
	    {
		print "no (could not find logfile)\n";
	    }
	}
	elsif ($? eq "127")
	{
	    print "no (exim not found)\n";
	}
	else
	{
	    print "no\n";
	}
    }
    else
    {
	if (! -d $LOGDIR)
	{
	    print "no (could not find LOGDIR)\n";
	}
	else
	{
	    $logfile = $LOGDIR;
	}
    }


    if ($logfile)
    {
	if (-r "$logfile/mainlog")
	{
	    print "yes\n";
	    exit 0;
	}
	else
	{
	    print "no (logfile not readable)\n";
	}
    }
    exit 1;
}

exit 1 unless ($LOGDIR || (`$EXIM -bP log_file_path` =~ /^\S+\s+=\s+([^%]+)%/));
$logfile    = ($LOGDIR || $1);
$logfile .= "mainlog";

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 Exim mail throughput\n";
    print "graph_args --base 1000 -l 0\n";
    print "graph_vlabel mails/min\n";
    print "graph_scale  no\n";
    print "received.label received\n";
    print "received.type COUNTER\n";
    print "received.draw AREA\n";
    print "received.cdef received,60,*\n";
    print "completed.label completed\n";
    print "completed.type COUNTER\n";
    print "completed.cdef completed,60,*\n";
    exit 0;
}

if (! -f $logfile and ! -f $rotlogfile)
{
    print "completed.value U\n";
    print "received.value U\n";
    exit 0;
}

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

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

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

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

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

print "received.value $received\n";
print "completed.value $completed\n";

if(-l $statefile) {
	die("$statefile is a symbolic link, refusing to touch it.");
}				
open (OUT, ">$statefile") or exit 4;
print OUT "$pos:$received:$completed\n";
close OUT;

sub parseEximfile 
{    
    my ($fname, $start, $stop) = @_;
    open (LOGFILE, $fname) or exit 3;
    seek (LOGFILE, $start, 0) or exit 2;

    while (tell (LOGFILE) < $stop) 
    {
	my $line =<LOGFILE>;
	chomp ($line);

	if (substr ($line, 37,2 ) eq '<=') 
	{
	    $received++;
	} 
	elsif (substr ($line, 37,9) eq 'Completed')
	{
	    $completed++;
	}
    }
    close(LOGFILE);    
}

# vim:syntax=perl
