3 # Plugin to monitor the number of mails received and delivered by exim.
5 # Usage: copy or link into /etc/munin/node.d/
10 # autoconf (optional - used by munin-config)
14 # logdir - Override what exim says
15 # exim - Where's exim?
18 # Revision 1.7.2.2 2005/03/12 23:07:17 jimmyo
19 # Avoid negative spike in generic/exim_mailstats.
21 # Revision 1.7.2.1 2005/03/09 19:24:12 jimmyo
22 # Thanks to Stephen Gran, generic/exim_mailstats now graphs rejects (Deb#295799).
24 # Revision 1.7 2004/12/10 18:51:43 jimmyo
25 # linux/apt* has been forced to LANG=C, to get predictable output.
27 # Revision 1.6 2004/12/10 10:47:47 jimmyo
28 # Change name from ${scale} to ${graph_period}, to be more consistent.
30 # Revision 1.5 2004/12/09 22:12:54 jimmyo
31 # Added "graph_period" option, to make "graph_sums" usable.
33 # Revision 1.4 2004/11/21 00:16:56 jimmyo
34 # Changed a lot of plugins so they use DERIVE instead of COUNTER.
36 # Revision 1.3 2004/11/10 15:54:49 jimmyo
37 # Applied patch from Torstein T. Svendsen to generic/exim_mailstats, to handle logfiles with timestamps in the name (SF#1055214)
39 # Revision 1.2 2004/05/20 13:57:12 jimmyo
40 # Set categories to some of the plugins.
42 # Revision 1.1 2004/01/02 18:50:00 jimmyo
43 # Renamed occurrances of lrrd -> munin
45 # Revision 1.1.1.1 2004/01/02 15:18:07 jimmyo
46 # Import of LRRD CVS tree after renaming to Munin
48 # Revision 1.7 2003/11/15 11:10:28 jimmyo
51 # Revision 1.6 2003/11/07 17:43:16 jimmyo
52 # Cleanups and log entries
56 # Magic markers (optional - used by munin-config and some installation
60 #%# capabilities=autoconf
63 sub get_exim_logfile {
64 my ($spec, $type, $time) = @_;
66 $time = time() unless $time;
68 $logfile =~ s/^log_file_path = //;
69 $logfile =~ s/\%s/$type/;
71 if( $logfile =~ /\%D/ ) {
72 my @t=localtime($time);
73 my $ts = sprintf("%04d%02d%02d",$t[5]+1900, $t[4]+1, $t[3]);
74 $logfile =~ s/\%D/$ts/g;
76 my @lfiles = split(/\s?:\s?/, $logfile);
78 return $_ unless /^syslog/;
84 $statefile = "/var/lib/munin/plugin-state/plugin-exim_mailstats.state";
98 ($dirname = $0) =~ s/[^\/]+$//;
99 $EXIM = "/usr/sbin/exim";
100 $EXIM = "/usr/sbin/exim4" if (-x "/usr/sbin/exim4"); # a Debianism
101 $EXIM = $ENV{'exim'} if defined $ENV{'exim'};
102 $EXIM = "/usr/exim/bin/exim";
103 $LOGDIR = $ENV{'logdir'} || undef;
105 if ( $ARGV[0] and $ARGV[0] eq "autoconf" )
109 if(defined($LOGDIR)) {
111 print "no (logdir does not exist)\n";
114 $logfile = $LOGDIR . '/mainlog';
116 my $logfilespec = `$EXIM -bP log_file_path 2>/dev/null`;
119 $logfile = get_exim_logfile( $logfilespec, 'main');
123 print "no (exim not found)\n";
140 print "no (logfile not readable)\n";
149 if(defined($LOGDIR)) {
151 $logfile = $LOGDIR . '/' . 'mainlog';
153 $logfilespec = `$EXIM -bP log_file_path`;
154 $logfile = get_exim_logfile( $logfilespec, 'main');
157 exit 1 unless -r $logfile;
159 if( $logfilespec =~ /\%D/ ) {
160 $rotlogfile = get_exim_logfile( $logfilespec, 'main', (time()-(24*60*60)));
164 $rotlogfile = $logfile . ".0";
166 elsif (-f "$logfile.1")
168 $rotlogfile = $logfile . ".1";
170 elsif (-f "$logfile.01")
172 $rotlogfile = $logfile . ".01";
176 $rotlogfile = $logfile . ".0";
180 if ( $ARGV[0] and $ARGV[0] eq "config" )
182 print "graph_title Exim mail throughput\n";
183 print "graph_args --base 1000 -l 0\n";
184 print "graph_vlabel mails/\${graph_period}\n";
185 print "graph_scale no\n";
186 print "graph_category Mail\n";
187 print "received.label received\n";
188 print "received.type DERIVE\n";
189 print "received.min 0\n";
190 print "received.draw LINE\n";
191 print "completed.label completed\n";
192 print "completed.type DERIVE\n";
193 print "completed.min 0\n";
194 # print "completed.draw AREA\n";
195 print "rejected.label rejected (other)\n";
196 print "rejected.type DERIVE\n";
197 print "rejected.min 0\n";
198 # print "rejected.draw STACK\n";
199 print "rbl.label rejected (RBL)\n";
200 print "rbl.type DERIVE\n";
202 # print "rbl.draw STACK\n";
203 print "spam.label rejected (spam)\n";
204 print "spam.type DERIVE\n";
205 print "spam.min 0\n";
206 # print "spam.draw STACK\n";
207 print "greylisted.label greylisted\n";
208 print "greylisted.type DERIVE\n";
209 print "greylisted.min 0\n";
210 # print "greylisted.draw STACK\n";
211 print "sender.label rejected (sender verify)\n";
212 print "sender.type DERIVE\n";
213 print "sender.min 0\n";
214 # print "sender.draw STACK\n";
215 print "user.label rejected (user unknown)\n";
216 print "user.type DERIVE\n";
217 print "user.min 0\n";
218 # print "user.draw STACK\n";
219 print "protocol.label rejected (protocol violation)\n";
220 print "protocol.type DERIVE\n";
221 print "protocol.min 0\n";
222 # print "protocol.draw STACK\n";
223 print "helo.label rejected (helo/ehlo)\n";
224 print "helo.type DERIVE\n";
225 print "helo.min 0\n";
226 # print "helo.draw STACK\n";
227 print "virus.label rejected (virus)\n";
228 print "virus.type DERIVE\n";
229 print "virus.min 0\n";
230 # print "virus.draw STACK\n";
231 print "fakemx.label rejected (fakeMX)\n";
232 print "fakemx.type DERIVE\n";
233 print "fakemx.min 0\n";
234 # print "fakemx.draw STACK\n";
238 if (! -f $logfile and ! -f $rotlogfile)
240 print "completed.value U\n";
241 print "received.value U\n";
242 print "rejected.value U\n";
243 print "greylisted.value U\n";
244 print "rbl.value U\n";
245 print "spam.value U\n";
246 print "sender.value U\n";
247 print "user.value U\n";
248 print "protocol.value U\n";
249 print "helo.value U\n";
250 print "virus.value U\n";
251 print "fakemx.value U\n";
257 open (IN, "$statefile") or exit 4;
259 if ($in =~ /^(\d+):(\d+):(\d+):(\d+):(\d+):(\d+):(\d+):(\d+):(\d+):(\d+):(\d+):(\d+):(\d+)/)
261 ($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);
263 elsif ($in =~ /^(\d+):(\d+):(\d+):(\d+):(\d+):(\d+):(\d+)/)
265 ($pos, $received, $completed, $rejected, $greylisted, $rbl, $spam) = ($1, $2, $3, $4, $5, $6, $7);
267 elsif ($in =~ /^(\d+):(\d+):(\d+):(\d+)/)
269 ($pos, $received, $completed, $rejected) = ($1, $2, $3, $4);
272 elsif ($in =~ /^(\d+):(\d+):(\d+)/)
274 ($pos, $received, $completed) = ($1, $2, $3);
280 $startsize = (stat $logfile)[7];
288 if ($startsize < $pos)
291 parseEximfile ($rotlogfile, $pos, (stat $rotlogfile)[7]);
295 parseEximfile ($logfile, $pos, $startsize);
298 print "received.value $received\n";
299 print "completed.value $completed\n";
300 print "rejected.value $rejected\n";
301 print "rbl.value $rbl\n";
302 print "spam.value $spam\n";
303 print "greylisted.value $greylisted\n";
304 print "sender.value $sender\n";
305 print "user.value $user\n";
306 print "protocol.value $protocol\n";
307 print "helo.value $helo\n";
308 print "virus.value $virus\n";
309 print "fakemx.value $fakemx\n";
312 die("$statefile is a symbolic link, refusing to touch it.");
314 open (OUT, ">$statefile") or exit 4;
315 print OUT "$pos:$received:$completed:$rejected:$greylisted:$rbl:$spam:$sender:$user:$protocol:$helo:$virus:$fakemx\n";
320 my ($fname, $start, $stop) = @_;
321 open (LOGFILE, $fname) or exit 3;
322 seek (LOGFILE, $start, 0) or exit 2;
324 while (tell (LOGFILE) < $stop)
329 if (substr ($line, 37,2 ) eq '<=')
333 elsif (substr ($line, 37,9) eq 'Completed')
337 elsif ($line=~/greylisted\.$/)
341 elsif ($line=~/in a RBL/)
345 elsif ($line=~/considered spam/)
349 elsif ($line=~/sender verify/)
353 elsif ($line=~/Unknown user/)
357 elsif ($line=~/synchronization error/)
361 elsif ($line=~/HELO\/EHLO/)
365 elsif ($line=~/contains a virus/)
369 elsif ($line=~/fakemx for/)
373 elsif ($line=~/rejected/)