check_mailq: restore accidentially removed options
[monitoring-plugins.git] / plugins-scripts / check_mailq.pl
blobaac1310ec6ba50e46ea60d958420ae357ca462bf
1 #!@PERL@ -w
3 # check_mailq - check to see how many messages are in the smtp queue awating
4 # transmittal.
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
24 # USA
26 ############################################################################
28 use POSIX;
29 use strict;
30 use Getopt::Long;
31 use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c $opt_t $opt_s
32 $opt_M $mailq $status $state $msg $msg_q $msg_p $opt_W $opt_C $mailq @lines
33 %srcdomains %dstdomains);
34 use FindBin;
35 use lib "$FindBin::Bin";
36 use utils qw(%ERRORS &print_revision &support &usage );
38 my ($sudo);
40 sub print_help ();
41 sub print_usage ();
42 sub process_arguments ();
44 $ENV{'PATH'}='@TRUSTED_PATH@';
45 $ENV{'BASH_ENV'}='';
46 $ENV{'ENV'}='';
47 $PROGNAME = "check_mailq";
48 $mailq = 'sendmail'; # default
49 $msg_q = 0 ;
50 $msg_p = 0 ;
51 $state = $ERRORS{'UNKNOWN'};
53 Getopt::Long::Configure('bundling');
54 $status = process_arguments();
55 if ($status){
56 print "ERROR: processing arguments\n";
57 exit $ERRORS{"UNKNOWN"};
60 if ($opt_s) {
61 if (defined $utils::PATH_TO_SUDO && -x $utils::PATH_TO_SUDO) {
62 $sudo = $utils::PATH_TO_SUDO;
63 } else {
64 print "ERROR: Cannot execute sudo\n";
65 exit $ERRORS{'UNKNOWN'};
67 } else {
68 $sudo = "";
71 $SIG{'ALRM'} = sub {
72 print ("ERROR: timed out waiting for $utils::PATH_TO_MAILQ \n");
73 exit $ERRORS{"WARNING"};
75 alarm($opt_t);
77 # switch based on MTA
79 if ($mailq eq "sendmail") {
81 ## open mailq
82 if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
83 if (! open (MAILQ, "$sudo $utils::PATH_TO_MAILQ | " ) ) {
84 print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
85 exit $ERRORS{'UNKNOWN'};
87 }elsif( defined $utils::PATH_TO_MAILQ){
88 unless (-x $utils::PATH_TO_MAILQ) {
89 print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
90 exit $ERRORS{'UNKNOWN'};
92 } else {
93 print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
94 exit $ERRORS{'UNKNOWN'};
96 # single queue empty
97 ##/var/spool/mqueue is empty
98 # single queue: 1
99 ## /var/spool/mqueue (1 request)
100 ##----Q-ID---- --Size-- -----Q-Time----- ------------Sender/Recipient------------
101 ##h32E30p01763 2782 Wed Apr 2 15:03 <silvaATkpnqwest.pt>
102 ## 8BITMIME
103 ## <silvaATeunet.pt>
105 # multi queue empty
106 ##/var/spool/mqueue/q0/df is empty
107 ##/var/spool/mqueue/q1/df is empty
108 ##/var/spool/mqueue/q2/df is empty
109 ##/var/spool/mqueue/q3/df is empty
110 ##/var/spool/mqueue/q4/df is empty
111 ##/var/spool/mqueue/q5/df is empty
112 ##/var/spool/mqueue/q6/df is empty
113 ##/var/spool/mqueue/q7/df is empty
114 ##/var/spool/mqueue/q8/df is empty
115 ##/var/spool/mqueue/q9/df is empty
116 ##/var/spool/mqueue/qA/df is empty
117 ##/var/spool/mqueue/qB/df is empty
118 ##/var/spool/mqueue/qC/df is empty
119 ##/var/spool/mqueue/qD/df is empty
120 ##/var/spool/mqueue/qE/df is empty
121 ##/var/spool/mqueue/qF/df is empty
122 ## Total Requests: 0
123 # multi queue: 1
124 ##/var/spool/mqueue/q0/df is empty
125 ##/var/spool/mqueue/q1/df is empty
126 ##/var/spool/mqueue/q2/df is empty
127 ## /var/spool/mqueue/q3/df (1 request)
128 ##----Q-ID---- --Size-- -----Q-Time----- ------------Sender/Recipient------------
129 ##h32De2f23534* 48 Wed Apr 2 14:40 nocol
130 ## nouserATEUnet.pt
131 ## canau
132 ##/var/spool/mqueue/q4/df is empty
133 ##/var/spool/mqueue/q5/df is empty
134 ##/var/spool/mqueue/q6/df is empty
135 ##/var/spool/mqueue/q7/df is empty
136 ##/var/spool/mqueue/q8/df is empty
137 ##/var/spool/mqueue/q9/df is empty
138 ##/var/spool/mqueue/qA/df is empty
139 ##/var/spool/mqueue/qB/df is empty
140 ##/var/spool/mqueue/qC/df is empty
141 ##/var/spool/mqueue/qD/df is empty
142 ##/var/spool/mqueue/qE/df is empty
143 ##/var/spool/mqueue/qF/df is empty
144 ## Total Requests: 1
147 while (<MAILQ>) {
149 # match email addr on queue listing
150 if ( (/<.*@.*\.(\w+\.\w+)>/) || (/<.*@(\w+\.\w+)>/) ) {
151 my $domain = $1;
152 if (/^\w+/) {
153 print "$utils::PATH_TO_MAILQ = srcdomain = $domain \n" if $verbose ;
154 $srcdomains{$domain} ++;
156 next;
160 # ...
161 # sendmail considers a message with more than one destiny, say N, to the same MX
162 # to have N messages in queue.
163 # we will only consider one in this code
164 if (( /\s\(reply:\sread\serror\sfrom\s.*\.(\w+\.\w+)\.$/ ) || ( /\s\(reply:\sread\serror\sfrom\s(\w+\.\w+)\.$/ ) ||
165 ( /\s\(timeout\swriting\smessage\sto\s.*\.(\w+\.\w+)\.:/ ) || ( /\s\(timeout\swriting\smessage\sto\s(\w+\.\w+)\.:/ ) ||
166 ( /\s\(host\smap:\slookup\s\(.*\.(\w+\.\w+)\):/ ) || ( /\s\(host\smap:\slookup\s\((\w+\.\w+)\):/ ) ||
167 ( /\s\(Deferred:\s.*\s.*\.(\w+\.\w+)\.\)/ ) || ( /\s\(Deferred:\s.*\s(\w+\.\w+)\.\)/ ) ) {
169 print "$utils::PATH_TO_MAILQ = dstdomain = $1 \n" if $verbose ;
170 $dstdomains{$1} ++;
173 if (/\s+\(I\/O\serror\)/) {
174 print "$utils::PATH_TO_MAILQ = dstdomain = UNKNOWN \n" if $verbose ;
175 $dstdomains{'UNKNOWN'} ++;
178 # Finally look at the overall queue length
180 if (/mqueue/) {
181 print "$utils::PATH_TO_MAILQ = $_ "if $verbose ;
182 if (/ \((\d+) request/) {
184 # single queue: first line
185 # multi queue: one for each queue. overwrite on multi queue below
186 $msg_q = $1 ;
188 } elsif (/^\s+Total\sRequests:\s(\d+)$/i) {
189 print "$utils::PATH_TO_MAILQ = $_ \n" if $verbose ;
191 # multi queue: last line
192 $msg_q = $1 ;
198 ## close mailq
200 close (MAILQ);
202 if ( $? ) {
203 print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
204 exit $ERRORS{CRITICAL};
207 ## shut off the alarm
208 alarm(0);
212 ## now check the queue length(s)
214 if ($msg_q == 0) {
215 $msg = "OK: $mailq mailq is empty";
216 $state = $ERRORS{'OK'};
217 } else {
218 print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
220 # overall queue length
221 if ($msg_q < $opt_w) {
222 $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
223 $state = $ERRORS{'OK'};
224 }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
225 $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
226 $state = $ERRORS{'WARNING'};
227 }else {
228 $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
229 $state = $ERRORS{'CRITICAL'};
232 # check for domain specific queue lengths if requested
233 if (defined $opt_W) {
235 # Apply threshold to queue lengths FROM domain
236 my @srckeys = sort { $srcdomains{$b} <=> $srcdomains{$a} } keys %srcdomains;
237 my $srcmaxkey = $srckeys[0];
238 print "src max is $srcmaxkey with $srcdomains{$srcmaxkey} messages\n" if $verbose;
240 if ($srcdomains{$srcmaxkey} >= $opt_W && $srcdomains{$srcmaxkey} < $opt_C) {
241 if ($state == $ERRORS{'OK'}) {
242 $msg = "WARNING: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
243 $state = $ERRORS{'WARNING'};
244 } elsif (($state == $ERRORS{'WARNING'}) || ($state == $ERRORS{'CRITICAL'})){
245 $msg .= " -and- $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
246 } else {
247 $msg = "WARNING: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
248 $state = $ERRORS{'WARNING'};
250 } elsif ($srcdomains{$srcmaxkey} >= $opt_C) {
251 if ($state == $ERRORS{'OK'}) {
252 $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold C = $opt_C)";
253 $state = $ERRORS{'CRITICAL'};
254 } elsif ($state == $ERRORS{'WARNING'}) {
255 $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold C = $opt_C) -and- " . $msg;
256 $msg =~ s/WARNING: //;
257 } elsif ($state == $ERRORS{'CRITICAL'}) {
258 $msg .= " -and- $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
259 } else {
260 $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
261 $state = $ERRORS{'CRITICAL'};
263 } else {
264 if ($srcdomains{$srcmaxkey} > 0) {
265 $msg .= " $srcdomains{$srcmaxkey} msgs. FROM $srcmaxkey is below threshold ($opt_W/$opt_C)";
269 # Apply threshold to queue lengths TO domain
270 my @dstkeys = sort { $dstdomains{$b} <=> $dstdomains{$a} } keys %dstdomains;
271 my $dstmaxkey = $dstkeys[0];
272 print "dst max is $dstmaxkey with $dstdomains{$dstmaxkey} messages\n" if $verbose;
274 if ($dstdomains{$dstmaxkey} >= $opt_W && $dstdomains{$dstmaxkey} < $opt_C) {
275 if ($state == $ERRORS{'OK'}) {
276 $msg = "WARNING: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
277 $state = $ERRORS{'WARNING'};
278 } elsif (($state == $ERRORS{'WARNING'}) || ($state == $ERRORS{'CRITICAL'})){
279 $msg .= " -and- $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
280 } else {
281 $msg = "WARNING: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
282 $state = $ERRORS{'WARNING'};
284 } elsif ($dstdomains{$dstmaxkey} >= $opt_C) {
285 if ($state == $ERRORS{'OK'}) {
286 $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold C = $opt_C)";
287 $state = $ERRORS{'CRITICAL'};
288 } elsif ($state == $ERRORS{'WARNING'}) {
289 $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold C = $opt_C) -and- " . $msg;
290 $msg =~ s/WARNING: //;
291 } elsif ($state == $ERRORS{'CRITICAL'}) {
292 $msg .= " -and- $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
293 } else {
294 $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
295 $state = $ERRORS{'CRITICAL'};
297 } else {
298 if ($dstdomains{$dstmaxkey} > 0) {
299 $msg .= " $dstdomains{$dstmaxkey} msgs. TO $dstmaxkey is below threshold ($opt_W/$opt_C)";
303 } # End of queue length thresholds
307 } # end of ($mailq eq "sendmail")
308 elsif ( $mailq eq "postfix" ) {
310 ## open mailq
311 if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
312 if (! open (MAILQ, "$sudo $utils::PATH_TO_MAILQ | " ) ) {
313 print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
314 exit $ERRORS{'UNKNOWN'};
316 }elsif( defined $utils::PATH_TO_MAILQ){
317 unless (-x $utils::PATH_TO_MAILQ) {
318 print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
319 exit $ERRORS{'UNKNOWN'};
321 } else {
322 print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
323 exit $ERRORS{'UNKNOWN'};
327 @lines = reverse <MAILQ>;
329 # close qmail-qstat
330 close MAILQ;
332 if ( $? ) {
333 print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
334 exit $ERRORS{CRITICAL};
337 ## shut off the alarm
338 alarm(0);
340 # check queue length
341 if ($lines[0]=~/Kbytes in (\d+)/) {
342 $msg_q = $1 ;
343 }elsif ($lines[0]=~/Mail queue is empty/) {
344 $msg_q = 0;
345 }else{
346 print "Couldn't match $utils::PATH_TO_MAILQ output\n";
347 exit $ERRORS{'UNKNOWN'};
350 # check messages not processed
351 #if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
352 # my $msg_p = $1;
353 #}else{
354 # print "Couldn't match $utils::PATH_TO_MAILQ output\n";
355 # exit $ERRORS{'UNKNOWN'};
358 # check queue length(s)
359 if ($msg_q == 0){
360 $msg = "OK: $mailq mailq reports queue is empty";
361 $state = $ERRORS{'OK'};
362 } else {
363 print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
365 # overall queue length
366 if ($msg_q < $opt_w) {
367 $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
368 $state = $ERRORS{'OK'};
369 }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
370 $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
371 $state = $ERRORS{'WARNING'};
372 }else {
373 $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
374 $state = $ERRORS{'CRITICAL'};
377 # check messages not yet preprocessed (only compare is $opt_W and $opt_C
378 # are defined)
380 #if (defined $opt_W) {
381 # $msg .= "[Preprocessed = $msg_p]";
382 # if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
383 # $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
384 # }elsif ($msg_p >= $opt_C ) {
385 # $state = $ERRORS{"CRITICAL"} ;
389 } # end of ($mailq eq "postfix")
390 elsif ( $mailq eq "qmail" ) {
392 # open qmail-qstat
393 if ( defined $utils::PATH_TO_QMAIL_QSTAT && -x $utils::PATH_TO_QMAIL_QSTAT ) {
394 if (! open (MAILQ, "$sudo $utils::PATH_TO_QMAIL_QSTAT | " ) ) {
395 print "ERROR: could not open $utils::PATH_TO_QMAIL_QSTAT \n";
396 exit $ERRORS{'UNKNOWN'};
398 }elsif( defined $utils::PATH_TO_QMAIL_QSTAT){
399 unless (-x $utils::PATH_TO_QMAIL_QSTAT) {
400 print "ERROR: $utils::PATH_TO_QMAIL_QSTAT is not executable by (uid $>:gid($)))\n";
401 exit $ERRORS{'UNKNOWN'};
403 } else {
404 print "ERROR: \$utils::PATH_TO_QMAIL_QSTAT is not defined\n";
405 exit $ERRORS{'UNKNOWN'};
408 @lines = <MAILQ>;
410 # close qmail-qstat
411 close MAILQ;
413 if ( $? ) {
414 print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
415 exit $ERRORS{CRITICAL};
418 ## shut off the alarm
419 alarm(0);
421 # check queue length
422 if ($lines[0]=~/^messages in queue: (\d+)/) {
423 $msg_q = $1 ;
424 }else{
425 print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
426 exit $ERRORS{'UNKNOWN'};
429 # check messages not processed
430 if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
431 my $msg_p = $1;
432 }else{
433 print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
434 exit $ERRORS{'UNKNOWN'};
438 # check queue length(s)
439 if ($msg_q == 0){
440 $msg = "OK: qmail-qstat reports queue is empty";
441 $state = $ERRORS{'OK'};
442 } else {
443 print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
445 # overall queue length
446 if ($msg_q < $opt_w) {
447 $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
448 $state = $ERRORS{'OK'};
449 }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
450 $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
451 $state = $ERRORS{'WARNING'};
452 }else {
453 $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
454 $state = $ERRORS{'CRITICAL'};
457 # check messages not yet preprocessed (only compare is $opt_W and $opt_C
458 # are defined)
460 if (defined $opt_W) {
461 $msg .= "[Preprocessed = $msg_p]";
462 if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
463 $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
464 }elsif ($msg_p >= $opt_C ) {
465 $state = $ERRORS{"CRITICAL"} ;
472 } # end of ($mailq eq "qmail")
473 elsif ( $mailq eq "exim" ) {
474 ## open mailq
475 if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
476 if (! open (MAILQ, "$sudo $utils::PATH_TO_MAILQ | " ) ) {
477 print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
478 exit $ERRORS{'UNKNOWN'};
480 }elsif( defined $utils::PATH_TO_MAILQ){
481 unless (-x $utils::PATH_TO_MAILQ) {
482 print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
483 exit $ERRORS{'UNKNOWN'};
485 } else {
486 print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
487 exit $ERRORS{'UNKNOWN'};
490 while (<MAILQ>) {
491 #22m 1.7K 19aEEr-0007hx-Dy <> *** frozen ***
492 #root@exlixams.glups.fr
494 if (/\s[\w\d]{6}-[\w\d]{6}-[\w\d]{2}\s/) { # message id 19aEEr-0007hx-Dy
495 $msg_q++ ;
498 close(MAILQ) ;
500 if ( $? ) {
501 print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
502 exit $ERRORS{CRITICAL};
504 if ($msg_q < $opt_w) {
505 $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
506 $state = $ERRORS{'OK'};
507 }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
508 $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
509 $state = $ERRORS{'WARNING'};
510 }else {
511 $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
512 $state = $ERRORS{'CRITICAL'};
514 } # end of ($mailq eq "exim")
516 elsif ( $mailq eq "nullmailer" ) {
517 ## open mailq
518 if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
519 if (! open (MAILQ, "$sudo $utils::PATH_TO_MAILQ | " ) ) {
520 print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
521 exit $ERRORS{'UNKNOWN'};
523 }elsif( defined $utils::PATH_TO_MAILQ){
524 unless (-x $utils::PATH_TO_MAILQ) {
525 print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
526 exit $ERRORS{'UNKNOWN'};
528 } else {
529 print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
530 exit $ERRORS{'UNKNOWN'};
533 while (<MAILQ>) {
534 #2006-06-22 16:00:00 282 bytes
536 if (/^[1-9][0-9]*-[01][0-9]-[0-3][0-9]\s[0-2][0-9]\:[0-2][0-9]\:[0-2][0-9]\s{2}[0-9]+\sbytes$/) {
537 $msg_q++ ;
540 close(MAILQ) ;
541 if ($msg_q < $opt_w) {
542 $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
543 $state = $ERRORS{'OK'};
544 }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
545 $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
546 $state = $ERRORS{'WARNING'};
547 }else {
548 $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
549 $state = $ERRORS{'CRITICAL'};
551 } # end of ($mailq eq "nullmailer")
553 # Perfdata support
554 print "$msg|unsent=$msg_q;$opt_w;$opt_c;0\n";
555 exit $state;
558 #####################################
559 #### subs
562 sub process_arguments(){
563 GetOptions
564 ("V" => \$opt_V, "version" => \$opt_V,
565 "v" => \$opt_v, "verbose" => \$opt_v,
566 "h" => \$opt_h, "help" => \$opt_h,
567 "M:s" => \$opt_M, "mailserver:s" => \$opt_M, # mailserver (default sendmail)
568 "w=i" => \$opt_w, "warning=i" => \$opt_w, # warning if above this number
569 "c=i" => \$opt_c, "critical=i" => \$opt_c, # critical if above this number
570 "t=i" => \$opt_t, "timeout=i" => \$opt_t,
571 "s" => \$opt_s, "sudo" => \$opt_s,
572 "W=i" => \$opt_W, # warning if above this number
573 "C=i" => \$opt_C, # critical if above this number
576 if ($opt_V) {
577 print_revision($PROGNAME,'@NP_VERSION@');
578 exit $ERRORS{'UNKNOWN'};
581 if ($opt_h) {
582 print_help();
583 exit $ERRORS{'UNKNOWN'};
586 if (defined $opt_v ){
587 $verbose = $opt_v;
590 unless (defined $opt_t) {
591 $opt_t = $utils::TIMEOUT ; # default timeout
594 unless ( defined $opt_w && defined $opt_c ) {
595 print_usage();
596 exit $ERRORS{'UNKNOWN'};
599 if ( $opt_w >= $opt_c) {
600 print "Warning (-w) cannot be greater than Critical (-c)!\n";
601 exit $ERRORS{'UNKNOWN'};
604 if (defined $opt_W && ! defined !$opt_C) {
605 print "Need -C if using -W\n";
606 exit $ERRORS{'UNKNOWN'};
607 }elsif(defined $opt_W && defined $opt_C) {
608 if ($opt_W >= $opt_C) {
609 print "Warning (-W) cannot be greater than Critical (-C)!\n";
610 exit $ERRORS{'UNKNOWN'};
614 if (defined $opt_M) {
615 if ($opt_M =~ /^(sendmail|qmail|postfix|exim|nullmailer)$/) {
616 $mailq = $opt_M ;
617 }elsif( $opt_M eq ''){
618 $mailq = 'sendmail';
619 }else{
620 print "-M: $opt_M is not supported\n";
621 exit $ERRORS{'UNKNOWN'};
623 }else{
624 if (defined $utils::PATH_TO_QMAIL_QSTAT
625 && -x $utils::PATH_TO_QMAIL_QSTAT)
627 $mailq = 'qmail';
629 elsif (-d '/var/lib/postfix' || -d '/var/local/lib/postfix'
630 || -e '/usr/sbin/postfix' || -e '/usr/local/sbin/postfix')
632 $mailq = 'postfix';
634 elsif (-d '/usr/lib/exim4' || -d '/usr/local/lib/exim4'
635 || -e '/usr/sbin/exim' || -e '/usr/local/sbin/exim')
637 $mailq = 'exim';
639 elsif (-d '/usr/lib/nullmailer' || -d '/usr/local/lib/nullmailer'
640 || -e '/usr/sbin/nullmailer-send'
641 || -e '/usr/local/sbin/nullmailer-send')
643 $mailq = 'nullmailer';
645 else {
646 $mailq = 'sendmail';
650 return $ERRORS{'OK'};
653 sub print_usage () {
654 print "Usage: $PROGNAME -w <warn> -c <crit> [-W <warn>] [-C <crit>] [-M <MTA>] [-t <timeout>] [-s] [-v]\n";
657 sub print_help () {
658 print_revision($PROGNAME,'@NP_VERSION@');
659 print "Copyright (c) 2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid\n";
660 print "\n";
661 print_usage();
662 print "\n";
663 print " Checks the number of messages in the mail queue (supports multiple sendmail queues, qmail)\n";
664 print " Feedback/patches to support non-sendmail mailqueue welcome\n\n";
665 print "-w (--warning) = Min. number of messages in queue to generate warning\n";
666 print "-c (--critical) = Min. number of messages in queue to generate critical alert ( w < c )\n";
667 print "-W = Min. number of messages for same domain in queue to generate warning\n";
668 print "-C = Min. number of messages for same domain in queue to generate critical alert ( W < C )\n";
669 print "-t (--timeout) = Plugin timeout in seconds (default = $utils::TIMEOUT)\n";
670 print "-M (--mailserver) = [ sendmail | qmail | postfix | exim | nullmailer ] (default = autodetect)\n";
671 print "-s (--sudo) = Use sudo to call the mailq command\n";
672 print "-h (--help)\n";
673 print "-V (--version)\n";
674 print "-v (--verbose) = debugging output\n";
675 print "\n\n";
676 print "Note: -w and -c are required arguments. -W and -C are optional.\n";
677 print " -W and -C are applied to domains listed on the queues - both FROM and TO. (sendmail)\n";
678 print " -W and -C are applied message not yet preproccessed. (qmail)\n";
679 print " This plugin tries to autodetect which mailserver you are running,\n";
680 print " you can override the autodetection with -M.\n";
681 print " This plugin uses the system mailq command (sendmail) or qmail-stat (qmail)\n";
682 print " to look at the queues. Mailq can usually only be accessed by root or \n";
683 print " a TrustedUser. You will have to set appropriate permissions for the plugin to work.\n";
684 print "";
685 print "\n\n";
686 support();