#!@@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?
#
# Magic markers (optional - used by munin-config and some installation
# scripts):
#
#%# family=auto
#%# capabilities=autoconf


sub get_exim_logfile {
    my ($spec, $type, $time) = @_;
    chomp($spec);
    $time = time() unless $time;
    my $logfile = $spec;
    $logfile =~ s/^log_file_path = //;
    $logfile =~ s/\%s/$type/;

    if( $logfile =~ /\%D/ ) {
	my @t=localtime($time);
	my $ts = sprintf("%04d%02d%02d",$t[5]+1900, $t[4]+1, $t[3]);
	$logfile =~ s/\%D/$ts/g;
    }        
    my @lfiles = split(/\s?:\s?/, $logfile);
    foreach (@lfiles) {
	return $_ unless /^syslog/;
    }
}



$statefile = "@@PLUGSTATE@@/plugin-exim_mailstats.state";
$pos   = undef;
$received = 0;
$completed = 0;
$rejected = 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(defined($LOGDIR)) {
	if(! -d $LOGDIR) {
	    print "no (logdir does not exist)\n";
	    exit(1);
	}
	$logfile = $LOGDIR . '/mainlog';
    } else {
	my $logfilespec = `$EXIM -bP log_file_path 2>/dev/null`;
	if (! $?)
	{
	    $logfile = get_exim_logfile( $logfilespec, 'main');
	}
	elsif ($? eq "127")
	{
	    print "no (exim not found)\n";
	}
	else
	{
	    print "no\n";
	}
    }

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


my $logfilespec;

if(defined($LOGDIR)) {
    $logfilespec = '';
    $logfile = $LOGDIR . '/' . 'mainlog';
} else {
    $logfilespec = `$EXIM -bP log_file_path`;
    $logfile = get_exim_logfile( $logfilespec, 'main');
}

exit 1 unless -r $logfile;

if( $logfilespec =~ /\%D/ ) {
    $rotlogfile = get_exim_logfile( $logfilespec, 'main', (time()-(24*60*60)));
} else {
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/\${graph_period}\n";
    print "graph_scale  no\n";
    print "graph_category exim\n";
    print "received.label received\n";
    print "received.type DERIVE\n";
    print "received.min 0\n";
    print "received.draw AREA\n";
    print "completed.label completed\n";
    print "completed.type DERIVE\n";
    print "completed.min 0\n";
    print "rejected.label rejected\n";                                      
    print "rejected.type DERIVE\n";
    print "rejected.min 0\n";
    exit 0;
}

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

if (-f "$statefile")
{
    open (IN, "$statefile") or exit 4;
	my $in = <IN>;
    if ($in =~ /^(\d+):(\d+):(\d+):(\d+)/)
    {
	($pos, $received, $completed, $rejected) = ($1, $2, $3, $4);
    }
    elsif ($in =~ /^(\d+):(\d+):(\d+)/)
    {
	($pos, $received, $completed) = ($1, $2, $3);
	$rejected = 0;
    }
    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";
print "rejected.value $rejected\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:$rejected\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++;
	}
        elsif ($line=~/rejected/)
        {
            $rejected++;
        }
    }
    close(LOGFILE);    
}

# vim:syntax=perl
