#!@@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 = "@@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 = ; 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 =; 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