1 #!/usr/local/bin/perl -w
3 # check_mailq - check to see how many messages are in the smtp queue awating
6 # Initial version support sendmail's mailq command
7 # Support for mutiple sendmail queues (Carlos Canau)
8 # Support for qmail (Benjamin Schmid)
10 # License Information:
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 ############################################################################
32 use vars
qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c $opt_t
33 $opt_M $mailq $status $state $msg $msg_q $msg_p $opt_W $opt_C $mailq @lines
34 %srcdomains %dstdomains);
36 use utils qw(%ERRORS &print_revision &support &usage );
41 sub process_arguments ();
46 $PROGNAME = "check_mailq";
47 $mailq = 'sendmail'; # default
50 $state = $ERRORS{'UNKNOWN'};
52 Getopt::Long::Configure('bundling');
53 $status = process_arguments();
55 print "ERROR: processing arguments\n";
56 exit $ERRORS{"UNKNOWN"};
60 print ("ERROR: timed out waiting for $utils::PATH_TO_MAILQ \n");
61 exit $ERRORS{"WARNING"};
67 if ($mailq eq "sendmail") {
70 if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
71 if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
72 print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
73 exit $ERRORS{'UNKNOWN'};
75 }elsif( defined $utils::PATH_TO_MAILQ){
76 unless (-x $utils::PATH_TO_MAILQ) {
77 print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
78 exit $ERRORS{'UNKNOWN'};
81 print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
82 exit $ERRORS{'UNKNOWN'};
85 ##/var/spool/mqueue is empty
87 ## /var/spool/mqueue (1 request)
88 ##----Q-ID---- --Size-- -----Q-Time----- ------------Sender/Recipient------------
89 ##h32E30p01763 2782 Wed Apr 2 15:03 <silvaATkpnqwest.pt>
94 ##/var/spool/mqueue/q0/df is empty
95 ##/var/spool/mqueue/q1/df is empty
96 ##/var/spool/mqueue/q2/df is empty
97 ##/var/spool/mqueue/q3/df is empty
98 ##/var/spool/mqueue/q4/df is empty
99 ##/var/spool/mqueue/q5/df is empty
100 ##/var/spool/mqueue/q6/df is empty
101 ##/var/spool/mqueue/q7/df is empty
102 ##/var/spool/mqueue/q8/df is empty
103 ##/var/spool/mqueue/q9/df is empty
104 ##/var/spool/mqueue/qA/df is empty
105 ##/var/spool/mqueue/qB/df is empty
106 ##/var/spool/mqueue/qC/df is empty
107 ##/var/spool/mqueue/qD/df is empty
108 ##/var/spool/mqueue/qE/df is empty
109 ##/var/spool/mqueue/qF/df is empty
112 ##/var/spool/mqueue/q0/df is empty
113 ##/var/spool/mqueue/q1/df is empty
114 ##/var/spool/mqueue/q2/df is empty
115 ## /var/spool/mqueue/q3/df (1 request)
116 ##----Q-ID---- --Size-- -----Q-Time----- ------------Sender/Recipient------------
117 ##h32De2f23534* 48 Wed Apr 2 14:40 nocol
120 ##/var/spool/mqueue/q4/df is empty
121 ##/var/spool/mqueue/q5/df is empty
122 ##/var/spool/mqueue/q6/df is empty
123 ##/var/spool/mqueue/q7/df is empty
124 ##/var/spool/mqueue/q8/df is empty
125 ##/var/spool/mqueue/q9/df is empty
126 ##/var/spool/mqueue/qA/df is empty
127 ##/var/spool/mqueue/qB/df is empty
128 ##/var/spool/mqueue/qC/df is empty
129 ##/var/spool/mqueue/qD/df is empty
130 ##/var/spool/mqueue/qE/df is empty
131 ##/var/spool/mqueue/qF/df is empty
137 # match email addr on queue listing
138 if ( (/<.*@.*\.(\w+\.\w+)>/) || (/<.*@(\w+\.\w+)>/) ) {
141 print "$utils::PATH_TO_MAILQ = srcdomain = $domain \n" if $verbose ;
142 $srcdomains{$domain} ++;
149 # sendmail considers a message with more than one destiny, say N, to the same MX
150 # to have N messages in queue.
151 # we will only consider one in this code
152 if (( /\s\(reply:\sread\serror\sfrom\s.*\.(\w+\.\w+)\.$/ ) || ( /\s\(reply:\sread\serror\sfrom\s(\w+\.\w+)\.$/ ) ||
153 ( /\s\(timeout\swriting\smessage\sto\s.*\.(\w+\.\w+)\.:/ ) || ( /\s\(timeout\swriting\smessage\sto\s(\w+\.\w+)\.:/ ) ||
154 ( /\s\(host\smap:\slookup\s\(.*\.(\w+\.\w+)\):/ ) || ( /\s\(host\smap:\slookup\s\((\w+\.\w+)\):/ ) ||
155 ( /\s\(Deferred:\s.*\s.*\.(\w+\.\w+)\.\)/ ) || ( /\s\(Deferred:\s.*\s(\w+\.\w+)\.\)/ ) ) {
157 print "$utils::PATH_TO_MAILQ = dstdomain = $1 \n" if $verbose ;
161 if (/\s+\(I\/O\serror\)/) {
162 print "$utils::PATH_TO_MAILQ = dstdomain = UNKNOWN \n" if $verbose ;
163 $dstdomains{'UNKNOWN'} ++;
166 # Finally look at the overall queue length
169 print "$utils::PATH_TO_MAILQ = $_ "if $verbose ;
170 if (/ \((\d+) request/) {
172 # single queue: first line
173 # multi queue: one for each queue. overwrite on multi queue below
176 } elsif (/^\s+Total\sRequests:\s(\d+)$/i) {
177 print "$utils::PATH_TO_MAILQ = $_ \n" if $verbose ;
179 # multi queue: last line
191 print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
192 exit $ERRORS{CRITICAL};
195 ## shut off the alarm
200 ## now check the queue length(s)
203 $msg = "OK: mailq is empty";
204 $state = $ERRORS{'OK'};
206 print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
208 # overall queue length
209 if ($msg_q < $opt_w) {
210 $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
211 $state = $ERRORS{'OK'};
212 }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
213 $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
214 $state = $ERRORS{'WARNING'};
216 $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
217 $state = $ERRORS{'CRITICAL'};
220 # check for domain specific queue lengths if requested
221 if (defined $opt_W) {
223 # Apply threshold to queue lengths FROM domain
224 my @srckeys = sort { $srcdomains{$b} <=> $srcdomains{$a} } keys %srcdomains;
225 my $srcmaxkey = $srckeys[0];
226 print "src max is $srcmaxkey with $srcdomains{$srcmaxkey} messages\n" if $verbose;
228 if ($srcdomains{$srcmaxkey} >= $opt_W && $srcdomains{$srcmaxkey} < $opt_C) {
229 if ($state == $ERRORS{'OK'}) {
230 $msg = "WARNING: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
231 $state = $ERRORS{'WARNING'};
232 } elsif (($state == $ERRORS{'WARNING'}) || ($state == $ERRORS{'CRITICAL'})){
233 $msg .= " -and- $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
235 $msg = "WARNING: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
236 $state = $ERRORS{'WARNING'};
238 } elsif ($srcdomains{$srcmaxkey} >= $opt_C) {
239 if ($state == $ERRORS{'OK'}) {
240 $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold C = $opt_C)";
241 $state = $ERRORS{'CRITICAL'};
242 } elsif ($state == $ERRORS{'WARNING'}) {
243 $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold C = $opt_C) -and- " . $msg;
244 $msg =~ s/WARNING: //;
245 } elsif ($state == $ERRORS{'CRITICAL'}) {
246 $msg .= " -and- $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
248 $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
249 $state = $ERRORS{'CRITICAL'};
252 if ($srcdomains{$srcmaxkey} > 0) {
253 $msg .= " $srcdomains{$srcmaxkey} msgs. FROM $srcmaxkey is below threshold ($opt_W/$opt_C)";
257 # Apply threshold to queue lengths TO domain
258 my @dstkeys = sort { $dstdomains{$b} <=> $dstdomains{$a} } keys %dstdomains;
259 my $dstmaxkey = $dstkeys[0];
260 print "dst max is $dstmaxkey with $dstdomains{$dstmaxkey} messages\n" if $verbose;
262 if ($dstdomains{$dstmaxkey} >= $opt_W && $dstdomains{$dstmaxkey} < $opt_C) {
263 if ($state == $ERRORS{'OK'}) {
264 $msg = "WARNING: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
265 $state = $ERRORS{'WARNING'};
266 } elsif (($state == $ERRORS{'WARNING'}) || ($state == $ERRORS{'CRITICAL'})){
267 $msg .= " -and- $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
269 $msg = "WARNING: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
270 $state = $ERRORS{'WARNING'};
272 } elsif ($dstdomains{$dstmaxkey} >= $opt_C) {
273 if ($state == $ERRORS{'OK'}) {
274 $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold C = $opt_C)";
275 $state = $ERRORS{'CRITICAL'};
276 } elsif ($state == $ERRORS{'WARNING'}) {
277 $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold C = $opt_C) -and- " . $msg;
278 $msg =~ s/WARNING: //;
279 } elsif ($state == $ERRORS{'CRITICAL'}) {
280 $msg .= " -and- $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
282 $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
283 $state = $ERRORS{'CRITICAL'};
286 if ($dstdomains{$dstmaxkey} > 0) {
287 $msg .= " $dstdomains{$dstmaxkey} msgs. TO $dstmaxkey is below threshold ($opt_W/$opt_C)";
291 } # End of queue length thresholds
295 } # end of ($mailq eq "sendmail")
296 elsif ( $mailq eq "postfix" ) {
299 if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
300 if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
301 print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
302 exit $ERRORS{'UNKNOWN'};
304 }elsif( defined $utils::PATH_TO_MAILQ){
305 unless (-x $utils::PATH_TO_MAILQ) {
306 print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
307 exit $ERRORS{'UNKNOWN'};
310 print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
311 exit $ERRORS{'UNKNOWN'};
315 @lines = reverse <MAILQ>;
321 print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
322 exit $ERRORS{CRITICAL};
325 ## shut off the alarm
329 if ($lines[0]=~/Kbytes in (\d+)/) {
331 }elsif ($lines[0]=~/Mail queue is empty/) {
334 print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
335 exit $ERRORS{'UNKNOWN'};
338 # check messages not processed
339 #if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
342 # print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
343 # exit $ERRORS{'UNKNOWN'};
346 # check queue length(s)
348 $msg = "OK: mailq reports queue is empty";
349 $state = $ERRORS{'OK'};
351 print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
353 # overall queue length
354 if ($msg_q < $opt_w) {
355 $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
356 $state = $ERRORS{'OK'};
357 }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
358 $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
359 $state = $ERRORS{'WARNING'};
361 $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
362 $state = $ERRORS{'CRITICAL'};
365 # check messages not yet preprocessed (only compare is $opt_W and $opt_C
368 #if (defined $opt_W) {
369 # $msg .= "[Preprocessed = $msg_p]";
370 # if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
371 # $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
372 # }elsif ($msg_p >= $opt_C ) {
373 # $state = $ERRORS{"CRITICAL"} ;
377 } # end of ($mailq eq "postfixl")
378 elsif ( $mailq eq "qmail" ) {
381 if ( defined $utils::PATH_TO_QMAIL_QSTAT && -x $utils::PATH_TO_QMAIL_QSTAT ) {
382 if (! open (MAILQ, "$utils::PATH_TO_QMAIL_QSTAT | " ) ) {
383 print "ERROR: could not open $utils::PATH_TO_QMAIL_QSTAT \n";
384 exit $ERRORS{'UNKNOWN'};
386 }elsif( defined $utils::PATH_TO_QMAIL_QSTAT){
387 unless (-x $utils::PATH_TO_QMAIL_QSTAT) {
388 print "ERROR: $utils::PATH_TO_QMAIL_QSTAT is not executable by (uid $>:gid($)))\n";
389 exit $ERRORS{'UNKNOWN'};
392 print "ERROR: \$utils::PATH_TO_QMAIL_QSTAT is not defined\n";
393 exit $ERRORS{'UNKNOWN'};
402 print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
403 exit $ERRORS{CRITICAL};
406 ## shut off the alarm
410 if ($lines[0]=~/^messages in queue: (\d+)/) {
413 print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
414 exit $ERRORS{'UNKNOWN'};
417 # check messages not processed
418 if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
421 print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
422 exit $ERRORS{'UNKNOWN'};
426 # check queue length(s)
428 $msg = "OK: qmail-qstat reports queue is empty";
429 $state = $ERRORS{'OK'};
431 print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
433 # overall queue length
434 if ($msg_q < $opt_w) {
435 $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
436 $state = $ERRORS{'OK'};
437 }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
438 $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
439 $state = $ERRORS{'WARNING'};
441 $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
442 $state = $ERRORS{'CRITICAL'};
445 # check messages not yet preprocessed (only compare is $opt_W and $opt_C
448 if (defined $opt_W) {
449 $msg .= "[Preprocessed = $msg_p]";
450 if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
451 $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
452 }elsif ($msg_p >= $opt_C ) {
453 $state = $ERRORS{"CRITICAL"} ;
460 } # end of ($mailq eq "qmail")
461 elsif ( $mailq eq "exim" ) {
463 if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
464 if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
465 print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
466 exit $ERRORS{'UNKNOWN'};
468 }elsif( defined $utils::PATH_TO_MAILQ){
469 unless (-x $utils::PATH_TO_MAILQ) {
470 print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
471 exit $ERRORS{'UNKNOWN'};
474 print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
475 exit $ERRORS{'UNKNOWN'};
479 #22m 1.7K 19aEEr-0007hx-Dy <> *** frozen ***
480 #root@exlixams.glups.fr
482 if (/\s[\w\d]{6}-[\w\d]{6}-[\w\d]{2}\s/) { # message id 19aEEr-0007hx-Dy
489 print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
490 exit $ERRORS{CRITICAL};
492 if ($msg_q < $opt_w) {
493 $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
494 $state = $ERRORS{'OK'};
495 }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
496 $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
497 $state = $ERRORS{'WARNING'};
499 $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
500 $state = $ERRORS{'CRITICAL'};
502 } # end of ($mailq eq "exim")
505 print "$msg|unsent=$msg_q;$opt_w;$opt_c;0\n";
509 #####################################
513 sub process_arguments(){
515 ("V" => \$opt_V, "version" => \$opt_V,
516 "v" => \$opt_v, "verbose" => \$opt_v,
517 "h" => \$opt_h, "help" => \$opt_h,
518 "M:s" => \$opt_M, "mailserver:s" => \$opt_M, # mailserver (default sendmail)
519 "w=i" => \$opt_w, "warning=i" => \$opt_w, # warning if above this number
520 "c=i" => \$opt_c, "critical=i" => \$opt_c, # critical if above this number
521 "t=i" => \$opt_t, "timeout=i" => \$opt_t
525 print_revision($PROGNAME,'$Revision$ ');
534 if (defined $opt_v ){
538 unless (defined $opt_t) {
539 $opt_t = $utils::TIMEOUT ; # default timeout
542 unless ( defined $opt_w && defined $opt_c ) {
544 exit $ERRORS{'UNKNOWN'};
547 if ( $opt_w >= $opt_c) {
548 print "Warning (-w) cannot be greater than Critical (-c)!\n";
549 exit $ERRORS{'UNKNOWN'};
552 if (defined $opt_W && ! defined !$opt_C) {
553 print "Need -C if using -W\n";
554 exit $ERRORS{'UNKNOWN'};
555 }elsif(defined $opt_W && defined $opt_C) {
556 if ($opt_W >= $opt_C) {
557 print "Warning (-W) cannot be greater than Critical (-C)!\n";
558 exit $ERRORS{'UNKNOWN'};
562 if (defined $opt_M) {
563 if ($opt_M =~ /^(sendmail|qmail|postfix|exim)$/) {
565 }elsif( $opt_M eq ''){
568 print "-M: $opt_M is not supported\n";
569 exit $ERRORS{'UNKNOWN'};
572 $mailq = 'sendmail' ;
575 return $ERRORS{'OK'};
579 print "Usage: $PROGNAME -w <warn> -c <crit> [-W <warn>] [-C <crit>] [-M <MTA>] [-t <timeout>] [-v verbose]\n";
583 print_revision($PROGNAME,'$Revision$');
584 print "Copyright (c) 2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid\n";
588 print " Checks the number of messages in the mail queue (supports multiple sendmail queues, qmail)\n";
589 print " Feedback/patches to support non-sendmail mailqueue welcome\n\n";
590 print "-w (--warning) = Min. number of messages in queue to generate warning\n";
591 print "-c (--critical) = Min. number of messages in queu to generate critical alert ( w < c )\n";
592 print "-W (--Warning) = Min. number of messages for same domain in queue to generate warning\n";
593 print "-C (--Critical) = Min. number of messages for same domain in queue to generate critical alert ( W < C )\n";
594 print "-t (--timeout) = Plugin timeout in seconds (default = $utils::TIMEOUT)\n";
595 print "-M (--mailserver) = [ sendmail | qmail | postfix | exim ] (default = sendmail)\n";
596 print "-h (--help)\n";
597 print "-V (--version)\n";
598 print "-v (--verbose) = debugging output\n";
600 print "Note: -w and -c are required arguments. -W and -C are optional.\n";
601 print " -W and -C are applied to domains listed on the queues - both FROM and TO. (sendmail)\n";
602 print " -W and -C are applied message not yet preproccessed. (qmail)\n";
603 print " This plugin uses the system mailq command (sendmail) or qmail-stat (qmail)\n";
604 print " to look at the queues. Mailq can usually only be accessed by root or \n";
605 print " a TrustedUser. You will have to set appropriate permissions for the plugin to work.\n";