+#!/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$
+# Revision 1.7.2.2 2005/03/12 23:07:17 jimmyo
+# Avoid negative spike in generic/exim_mailstats.
+#
+# Revision 1.7.2.1 2005/03/09 19:24:12 jimmyo
+# Thanks to Stephen Gran, generic/exim_mailstats now graphs rejects (Deb#295799).
+#
+# Revision 1.7 2004/12/10 18:51:43 jimmyo
+# linux/apt* has been forced to LANG=C, to get predictable output.
+#
+# Revision 1.6 2004/12/10 10:47:47 jimmyo
+# Change name from ${scale} to ${graph_period}, to be more consistent.
+#
+# Revision 1.5 2004/12/09 22:12:54 jimmyo
+# Added "graph_period" option, to make "graph_sums" usable.
+#
+# Revision 1.4 2004/11/21 00:16:56 jimmyo
+# Changed a lot of plugins so they use DERIVE instead of COUNTER.
+#
+# Revision 1.3 2004/11/10 15:54:49 jimmyo
+# Applied patch from Torstein T. Svendsen to generic/exim_mailstats, to handle logfiles with timestamps in the name (SF#1055214)
+#
+# Revision 1.2 2004/05/20 13:57:12 jimmyo
+# Set categories to some of the plugins.
+#
+# 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
+
+
+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 = "/var/lib/munin/plugin-state/plugin-exim_mailstats.state";
+$pos = undef;
+$received = 0;
+$completed = 0;
+$rejected = 0;
+$greylisted = 0;
+$rbl = 0;
+$spam = 0;
+$sender = 0;
+$user = 0;
+$protocol = 0;
+$helo = 0;
+$virus = 0;
+$fakemx = 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'};
+$EXIM = "/usr/exim/bin/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 Mail\n";
+ print "received.label received\n";
+ print "received.type DERIVE\n";
+ print "received.min 0\n";
+ print "received.draw LINE\n";
+ print "completed.label completed\n";
+ print "completed.type DERIVE\n";
+ print "completed.min 0\n";
+# print "completed.draw AREA\n";
+ print "rejected.label rejected (other)\n";
+ print "rejected.type DERIVE\n";
+ print "rejected.min 0\n";
+# print "rejected.draw STACK\n";
+ print "rbl.label rejected (RBL)\n";
+ print "rbl.type DERIVE\n";
+ print "rbl.min 0\n";
+# print "rbl.draw STACK\n";
+ print "spam.label rejected (spam)\n";
+ print "spam.type DERIVE\n";
+ print "spam.min 0\n";
+# print "spam.draw STACK\n";
+ print "greylisted.label greylisted\n";
+ print "greylisted.type DERIVE\n";
+ print "greylisted.min 0\n";
+# print "greylisted.draw STACK\n";
+ print "sender.label rejected (sender verify)\n";
+ print "sender.type DERIVE\n";
+ print "sender.min 0\n";
+# print "sender.draw STACK\n";
+ print "user.label rejected (user unknown)\n";
+ print "user.type DERIVE\n";
+ print "user.min 0\n";
+# print "user.draw STACK\n";
+ print "protocol.label rejected (protocol violation)\n";
+ print "protocol.type DERIVE\n";
+ print "protocol.min 0\n";
+# print "protocol.draw STACK\n";
+ print "helo.label rejected (helo/ehlo)\n";
+ print "helo.type DERIVE\n";
+ print "helo.min 0\n";
+# print "helo.draw STACK\n";
+ print "virus.label rejected (virus)\n";
+ print "virus.type DERIVE\n";
+ print "virus.min 0\n";
+# print "virus.draw STACK\n";
+ print "fakemx.label rejected (fakeMX)\n";
+ print "fakemx.type DERIVE\n";
+ print "fakemx.min 0\n";
+# print "fakemx.draw STACK\n";
+ exit 0;
+}
+
+if (! -f $logfile and ! -f $rotlogfile)
+{
+ print "completed.value U\n";
+ print "received.value U\n";
+ print "rejected.value U\n";
+ print "greylisted.value U\n";
+ print "rbl.value U\n";
+ print "spam.value U\n";
+ print "sender.value U\n";
+ print "user.value U\n";
+ print "protocol.value U\n";
+ print "helo.value U\n";
+ print "virus.value U\n";
+ print "fakemx.value U\n";
+ exit 0;
+}
+
+if (-f "$statefile")
+{
+ open (IN, "$statefile") or exit 4;
+ my $in = <IN>;
+ if ($in =~ /^(\d+):(\d+):(\d+):(\d+):(\d+):(\d+):(\d+):(\d+):(\d+):(\d+):(\d+):(\d+):(\d+)/)
+ {
+ ($pos, $received, $completed, $rejected, $greylisted, $rbl, $spam, $sender, $user, $protocol, $helo, $virus, $fakemx) = ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13);
+ }
+ elsif ($in =~ /^(\d+):(\d+):(\d+):(\d+):(\d+):(\d+):(\d+)/)
+ {
+ ($pos, $received, $completed, $rejected, $greylisted, $rbl, $spam) = ($1, $2, $3, $4, $5, $6, $7);
+ }
+ elsif ($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";
+print "rbl.value $rbl\n";
+print "spam.value $spam\n";
+print "greylisted.value $greylisted\n";
+print "sender.value $sender\n";
+print "user.value $user\n";
+print "protocol.value $protocol\n";
+print "helo.value $helo\n";
+print "virus.value $virus\n";
+print "fakemx.value $fakemx\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:$greylisted:$rbl:$spam:$sender:$user:$protocol:$helo:$virus:$fakemx\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=~/greylisted\.$/)
+ {
+ $greylisted++;
+ }
+ elsif ($line=~/in a RBL/)
+ {
+ $rbl++;
+ }
+ elsif ($line=~/considered spam/)
+ {
+ $spam++;
+ }
+ elsif ($line=~/sender verify/)
+ {
+ $sender++;
+ }
+ elsif ($line=~/Unknown user/)
+ {
+ $user++;
+ }
+ elsif ($line=~/synchronization error/)
+ {
+ $protocol++;
+ }
+ elsif ($line=~/HELO\/EHLO/)
+ {
+ $helo++;
+ }
+ elsif ($line=~/contains a virus/)
+ {
+ $virus++;
+ }
+ elsif ($line=~/fakemx for/)
+ {
+ $fakemx++;
+ }
+ elsif ($line=~/rejected/)
+ {
+ $rejected++;
+ }
+ }
+ close(LOGFILE);
+}
+
+# vim:syntax=perl