Adjust check_swap tests
[monitoring-plugins.git] / plugins-scripts / check_mailq.pl
blobf143c6fa91c1297cf149df10378ad47264f6f8e6
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 multiple 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 $opt_d
32 $opt_M $mailq $status $state $msg $msg_q $msg_p $opt_W $opt_C $mailq $mailq_args
33 @lines %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 # If appended, must start with a space
52 $mailq_args = '' ;
53 $state = $ERRORS{'UNKNOWN'};
55 Getopt::Long::Configure('bundling');
56 $status = process_arguments();
57 if ($status){
58 print "ERROR: processing arguments\n";
59 exit $ERRORS{"UNKNOWN"};
62 if ($opt_s) {
63 if (defined $utils::PATH_TO_SUDO && -x $utils::PATH_TO_SUDO) {
64 $sudo = $utils::PATH_TO_SUDO;
65 } else {
66 print "ERROR: Cannot execute sudo\n";
67 exit $ERRORS{'UNKNOWN'};
69 } else {
70 $sudo = "";
73 if ($opt_d) {
74 $mailq_args = $mailq_args . ' -C ' . $opt_d;
77 $SIG{'ALRM'} = sub {
78 print ("ERROR: timed out waiting for $utils::PATH_TO_MAILQ \n");
79 exit $ERRORS{"WARNING"};
81 alarm($opt_t);
83 # switch based on MTA
85 if ($mailq eq "sendmail") {
86 if( ! $utils::PATH_TO_MAILQ) {
87 print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
88 exit $ERRORS{'UNKNOWN'};
91 if ( ! -x $utils::PATH_TO_MAILQ) {
92 print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
93 exit $ERRORS{'UNKNOWN'};
96 ## open mailq
97 if (! open (MAILQ, "$sudo $utils::PATH_TO_MAILQ | " ) ) {
98 print "ERROR: could not open $sudo $utils::PATH_TO_MAILQ \n";
99 exit $ERRORS{'UNKNOWN'};
101 # single queue empty
102 ##/var/spool/mqueue is empty
103 # single queue: 1
104 ## /var/spool/mqueue (1 request)
105 ##----Q-ID---- --Size-- -----Q-Time----- ------------Sender/Recipient------------
106 ##h32E30p01763 2782 Wed Apr 2 15:03 <silvaATkpnqwest.pt>
107 ## 8BITMIME
108 ## <silvaATeunet.pt>
110 # multi queue empty
111 ##/var/spool/mqueue/q0/df is empty
112 ##/var/spool/mqueue/q1/df is empty
113 ##/var/spool/mqueue/q2/df is empty
114 ##/var/spool/mqueue/q3/df is empty
115 ##/var/spool/mqueue/q4/df is empty
116 ##/var/spool/mqueue/q5/df is empty
117 ##/var/spool/mqueue/q6/df is empty
118 ##/var/spool/mqueue/q7/df is empty
119 ##/var/spool/mqueue/q8/df is empty
120 ##/var/spool/mqueue/q9/df is empty
121 ##/var/spool/mqueue/qA/df is empty
122 ##/var/spool/mqueue/qB/df is empty
123 ##/var/spool/mqueue/qC/df is empty
124 ##/var/spool/mqueue/qD/df is empty
125 ##/var/spool/mqueue/qE/df is empty
126 ##/var/spool/mqueue/qF/df is empty
127 ## Total Requests: 0
128 # multi queue: 1
129 ##/var/spool/mqueue/q0/df is empty
130 ##/var/spool/mqueue/q1/df is empty
131 ##/var/spool/mqueue/q2/df is empty
132 ## /var/spool/mqueue/q3/df (1 request)
133 ##----Q-ID---- --Size-- -----Q-Time----- ------------Sender/Recipient------------
134 ##h32De2f23534* 48 Wed Apr 2 14:40 nocol
135 ## nouserATEUnet.pt
136 ## canau
137 ##/var/spool/mqueue/q4/df is empty
138 ##/var/spool/mqueue/q5/df is empty
139 ##/var/spool/mqueue/q6/df is empty
140 ##/var/spool/mqueue/q7/df is empty
141 ##/var/spool/mqueue/q8/df is empty
142 ##/var/spool/mqueue/q9/df is empty
143 ##/var/spool/mqueue/qA/df is empty
144 ##/var/spool/mqueue/qB/df is empty
145 ##/var/spool/mqueue/qC/df is empty
146 ##/var/spool/mqueue/qD/df is empty
147 ##/var/spool/mqueue/qE/df is empty
148 ##/var/spool/mqueue/qF/df is empty
149 ## Total Requests: 1
151 # separate submission/transport queues, empty
152 ## MSP Queue status...
153 ## /var/spool/mqueue-client is empty
154 ## Total requests: 0
155 ## MTA Queue status...
156 ## /var/spool/mqueue is empty
157 ## Total requests: 0
158 # separate submission/transport queues: 1
159 ## MSP Queue status...
160 ## /var/spool/mqueue-client (1 request)
161 ## -----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------
162 ## oAJEfhdW014123 5 Fri Nov 19 14:41 jwm
163 ## (Deferred: Connection refused by [127.0.0.1])
164 ## root
165 ## Total requests: 1
166 ## MTA Queue status...
167 ## /var/spool/mqueue is empty
168 ## Total requests: 0
170 my $this_msg_q = 0;
171 while (<MAILQ>) {
173 # match email addr on queue listing
174 if ( (/<.*@.*\.(\w+\.\w+)>/) || (/<.*@(\w+\.\w+)>/) ) {
175 my $domain = $1;
176 if (/^\w+/) {
177 print "$utils::PATH_TO_MAILQ = srcdomain = $domain \n" if $verbose ;
178 $srcdomains{$domain} ++;
180 next;
184 # ...
185 # sendmail considers a message with more than one destiny, say N, to the same MX
186 # to have N messages in queue.
187 # we will only consider one in this code
188 if (( /\s\(reply:\sread\serror\sfrom\s.*\.(\w+\.\w+)\.$/ ) || ( /\s\(reply:\sread\serror\sfrom\s(\w+\.\w+)\.$/ ) ||
189 ( /\s\(timeout\swriting\smessage\sto\s.*\.(\w+\.\w+)\.:/ ) || ( /\s\(timeout\swriting\smessage\sto\s(\w+\.\w+)\.:/ ) ||
190 ( /\s\(host\smap:\slookup\s\(.*\.(\w+\.\w+)\):/ ) || ( /\s\(host\smap:\slookup\s\((\w+\.\w+)\):/ ) ||
191 ( /\s\(Deferred:\s.*\s.*\.(\w+\.\w+)\.\)/ ) || ( /\s\(Deferred:\s.*\s(\w+\.\w+)\.\)/ ) ) {
193 print "$utils::PATH_TO_MAILQ = dstdomain = $1 \n" if $verbose ;
194 $dstdomains{$1} ++;
197 if (/\s+\(I\/O\serror\)/) {
198 print "$utils::PATH_TO_MAILQ = dstdomain = UNKNOWN \n" if $verbose ;
199 $dstdomains{'UNKNOWN'} ++;
202 # Finally look at the overall queue length
204 if (/mqueue/) {
205 print "$utils::PATH_TO_MAILQ = $_ "if $verbose ;
206 if (/ \((\d+) request/) {
208 # single queue: first line
209 # multi queue: one for each queue. overwrite on multi queue below
210 $this_msg_q = $1 ;
211 $msg_q += $1 ;
213 } elsif (/^\s+Total\sRequests:\s(\d+)$/i) {
214 if ($this_msg_q) {
215 $this_msg_q = 0 ;
216 } else {
217 print "$utils::PATH_TO_MAILQ = $_ \n" if $verbose ;
219 # multi queue: last line
220 $msg_q += $1 ;
226 ## close mailq
227 close (MAILQ);
229 if ( $? ) {
230 print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
231 exit $ERRORS{CRITICAL};
234 ## shut off the alarm
235 alarm(0);
237 ## now check the queue length(s)
239 if ($msg_q == 0) {
240 $msg = "OK: $mailq mailq is empty";
241 $state = $ERRORS{'OK'};
242 } else {
243 print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
245 # overall queue length
246 if ($msg_q < $opt_w) {
247 $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
248 $state = $ERRORS{'OK'};
249 }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
250 $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
251 $state = $ERRORS{'WARNING'};
252 }else {
253 $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
254 $state = $ERRORS{'CRITICAL'};
257 # check for domain specific queue lengths if requested
258 if (defined $opt_W) {
260 # Apply threshold to queue lengths FROM domain
261 my @srckeys = sort { $srcdomains{$b} <=> $srcdomains{$a} } keys %srcdomains;
262 my $srcmaxkey = $srckeys[0];
263 print "src max is $srcmaxkey with $srcdomains{$srcmaxkey} messages\n" if $verbose;
265 if ($srcdomains{$srcmaxkey} >= $opt_W && $srcdomains{$srcmaxkey} < $opt_C) {
266 if ($state == $ERRORS{'OK'}) {
267 $msg = "WARNING: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
268 $state = $ERRORS{'WARNING'};
269 } elsif (($state == $ERRORS{'WARNING'}) || ($state == $ERRORS{'CRITICAL'})){
270 $msg .= " -and- $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
271 } else {
272 $msg = "WARNING: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
273 $state = $ERRORS{'WARNING'};
275 } elsif ($srcdomains{$srcmaxkey} >= $opt_C) {
276 if ($state == $ERRORS{'OK'}) {
277 $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold C = $opt_C)";
278 $state = $ERRORS{'CRITICAL'};
279 } elsif ($state == $ERRORS{'WARNING'}) {
280 $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold C = $opt_C) -and- " . $msg;
281 $msg =~ s/WARNING: //;
282 } elsif ($state == $ERRORS{'CRITICAL'}) {
283 $msg .= " -and- $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
284 } else {
285 $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
286 $state = $ERRORS{'CRITICAL'};
288 } else {
289 if ($srcdomains{$srcmaxkey} > 0) {
290 $msg .= " $srcdomains{$srcmaxkey} msgs. FROM $srcmaxkey is below threshold ($opt_W/$opt_C)";
294 # Apply threshold to queue lengths TO domain
295 my @dstkeys = sort { $dstdomains{$b} <=> $dstdomains{$a} } keys %dstdomains;
296 my $dstmaxkey = $dstkeys[0];
297 print "dst max is $dstmaxkey with $dstdomains{$dstmaxkey} messages\n" if $verbose;
299 if ($dstdomains{$dstmaxkey} >= $opt_W && $dstdomains{$dstmaxkey} < $opt_C) {
300 if ($state == $ERRORS{'OK'}) {
301 $msg = "WARNING: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
302 $state = $ERRORS{'WARNING'};
303 } elsif (($state == $ERRORS{'WARNING'}) || ($state == $ERRORS{'CRITICAL'})){
304 $msg .= " -and- $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
305 } else {
306 $msg = "WARNING: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
307 $state = $ERRORS{'WARNING'};
309 } elsif ($dstdomains{$dstmaxkey} >= $opt_C) {
310 if ($state == $ERRORS{'OK'}) {
311 $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold C = $opt_C)";
312 $state = $ERRORS{'CRITICAL'};
313 } elsif ($state == $ERRORS{'WARNING'}) {
314 $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold C = $opt_C) -and- " . $msg;
315 $msg =~ s/WARNING: //;
316 } elsif ($state == $ERRORS{'CRITICAL'}) {
317 $msg .= " -and- $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
318 } else {
319 $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
320 $state = $ERRORS{'CRITICAL'};
322 } else {
323 if ($dstdomains{$dstmaxkey} > 0) {
324 $msg .= " $dstdomains{$dstmaxkey} msgs. TO $dstmaxkey is below threshold ($opt_W/$opt_C)";
328 } # End of queue length thresholds
332 } # end of ($mailq eq "sendmail")
333 elsif ( $mailq eq "postfix" ) {
334 if( ! $utils::PATH_TO_MAILQ ) {
335 print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
336 exit $ERRORS{'UNKNOWN'};
339 if ( ! -x $utils::PATH_TO_MAILQ) {
340 print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
341 exit $ERRORS{'UNKNOWN'};
344 ## open mailq
345 if (! open (MAILQ, "$sudo $utils::PATH_TO_MAILQ$mailq_args | " ) ) {
346 print "ERROR: could not open $utils::PATH_TO_MAILQ$mailq_args \n";
347 exit $ERRORS{'UNKNOWN'};
350 @lines = reverse <MAILQ>;
352 # close qmail-qstat
353 close MAILQ;
355 if ( $? ) {
356 print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ$mailq_args",$/;
357 exit $ERRORS{CRITICAL};
360 ## shut off the alarm
361 alarm(0);
363 # check queue length
364 if ($lines[0]=~/Kbytes in (\d+)/) {
365 $msg_q = $1 ;
366 }elsif ($lines[0]=~/Mail queue is empty/) {
367 $msg_q = 0;
368 }else{
369 print "Couldn't match $utils::PATH_TO_MAILQ$mailq_args output\n";
370 exit $ERRORS{'UNKNOWN'};
373 # check messages not processed
374 #if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
375 # my $msg_p = $1;
376 #}else{
377 # print "Couldn't match $utils::PATH_TO_MAILQ output\n";
378 # exit $ERRORS{'UNKNOWN'};
381 # check queue length(s)
382 if ($msg_q == 0){
383 $msg = "OK: $mailq mailq reports queue is empty";
384 $state = $ERRORS{'OK'};
385 } else {
386 print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
388 # overall queue length
389 if ($msg_q < $opt_w) {
390 $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
391 $state = $ERRORS{'OK'};
392 }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
393 $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
394 $state = $ERRORS{'WARNING'};
395 }else {
396 $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
397 $state = $ERRORS{'CRITICAL'};
400 # check messages not yet preprocessed (only compare is $opt_W and $opt_C
401 # are defined)
403 #if (defined $opt_W) {
404 # $msg .= "[Preprocessed = $msg_p]";
405 # if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
406 # $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
407 # }elsif ($msg_p >= $opt_C ) {
408 # $state = $ERRORS{"CRITICAL"} ;
412 } # end of ($mailq eq "postfix")
413 elsif ( $mailq eq "qmail" ) {
414 if ( ! $utils::PATH_TO_QMAIL_QSTAT ) {
415 print "ERROR: \$utils::PATH_TO_QMAIL_QSTAT is not defined\n";
416 exit $ERRORS{'UNKNOWN'};
419 if ( ! -x $utils::PATH_TO_QMAIL_QSTAT) {
420 print "ERROR: $utils::PATH_TO_QMAIL_QSTAT is not executable by (uid $>:gid($)))\n";
421 exit $ERRORS{'UNKNOWN'};
424 # open qmail-qstat
425 if (! open (MAILQ, "$sudo $utils::PATH_TO_QMAIL_QSTAT | " ) ) {
426 print "ERROR: could not open $utils::PATH_TO_QMAIL_QSTAT \n";
427 exit $ERRORS{'UNKNOWN'};
430 @lines = <MAILQ>;
432 # close qmail-qstat
433 close MAILQ;
435 if ( $? ) {
436 print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
437 exit $ERRORS{CRITICAL};
440 ## shut off the alarm
441 alarm(0);
443 # check queue length
444 if ($lines[0]=~/^messages in queue: (\d+)/) {
445 $msg_q = $1 ;
446 }else{
447 print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
448 exit $ERRORS{'UNKNOWN'};
451 # check messages not processed
452 if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
453 my $msg_p = $1;
454 }else{
455 print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
456 exit $ERRORS{'UNKNOWN'};
460 # check queue length(s)
461 if ($msg_q == 0){
462 $msg = "OK: qmail-qstat reports queue is empty";
463 $state = $ERRORS{'OK'};
464 } else {
465 print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
467 # overall queue length
468 if ($msg_q < $opt_w) {
469 $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
470 $state = $ERRORS{'OK'};
471 }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
472 $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
473 $state = $ERRORS{'WARNING'};
474 }else {
475 $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
476 $state = $ERRORS{'CRITICAL'};
479 # check messages not yet preprocessed (only compare is $opt_W and $opt_C
480 # are defined)
482 if (defined $opt_W) {
483 $msg .= "[Preprocessed = $msg_p]";
484 if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
485 $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
486 }elsif ($msg_p >= $opt_C ) {
487 $state = $ERRORS{"CRITICAL"} ;
494 } # end of ($mailq eq "qmail")
495 elsif ( $mailq eq "exim" ) {
496 if ( ! $utils::PATH_TO_MAILQ ) {
497 print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
498 exit $ERRORS{'UNKNOWN'};
501 if ( ! -x $utils::PATH_TO_MAILQ) {
502 print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
503 exit $ERRORS{'UNKNOWN'};
506 ## open mailq
507 if (! open (MAILQ, "$sudo $utils::PATH_TO_MAILQ | " ) ) {
508 print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
509 exit $ERRORS{'UNKNOWN'};
512 while (<MAILQ>) {
513 #22m 1.7K 19aEEr-0007hx-Dy <> *** frozen ***
514 #root@exlixams.glups.fr
516 if (/\s[\w\d]{6}-[\w\d]{6}-[\w\d]{2}\s/) { # message id 19aEEr-0007hx-Dy
517 $msg_q++ ;
520 close(MAILQ) ;
522 if ( $? ) {
523 print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
524 exit $ERRORS{CRITICAL};
526 if ($msg_q < $opt_w) {
527 $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
528 $state = $ERRORS{'OK'};
529 }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
530 $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
531 $state = $ERRORS{'WARNING'};
532 }else {
533 $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
534 $state = $ERRORS{'CRITICAL'};
536 } # end of ($mailq eq "exim")
538 elsif ( $mailq eq "nullmailer" ) {
539 if( ! $utils::PATH_TO_MAILQ) {
540 print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
541 exit $ERRORS{'UNKNOWN'};
544 if ( ! -x $utils::PATH_TO_MAILQ) {
545 print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
546 exit $ERRORS{'UNKNOWN'};
549 ## open mailq
550 if ( ! open (MAILQ, "$sudo $utils::PATH_TO_MAILQ | " ) ) {
551 print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
552 exit $ERRORS{'UNKNOWN'};
555 while (<MAILQ>) {
556 #2022-08-25 01:30:40 502 bytes from <user@example.com>
558 if (/^\d{4}-\d{2}-\d{2}\s+\d{2}\:\d{2}\:\d{2}\s+\d+\sbytes/) {
559 $msg_q++ ;
562 close(MAILQ) ;
563 if ($msg_q < $opt_w) {
564 $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
565 $state = $ERRORS{'OK'};
566 }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
567 $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
568 $state = $ERRORS{'WARNING'};
569 }else {
570 $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
571 $state = $ERRORS{'CRITICAL'};
573 } # end of ($mailq eq "nullmailer")
575 # Perfdata support
576 print "$msg|unsent=$msg_q;$opt_w;$opt_c;0\n";
577 exit $state;
580 #####################################
581 #### subs
584 sub process_arguments(){
585 GetOptions
586 ("V" => \$opt_V, "version" => \$opt_V,
587 "v" => \$opt_v, "verbose" => \$opt_v,
588 "h" => \$opt_h, "help" => \$opt_h,
589 "M:s" => \$opt_M, "mailserver:s" => \$opt_M, # mailserver (default sendmail)
590 "w=i" => \$opt_w, "warning=i" => \$opt_w, # warning if above this number
591 "c=i" => \$opt_c, "critical=i" => \$opt_c, # critical if above this number
592 "W=i" => \$opt_W, "warning-domain=i" => \$opt_W, # Warning if above this number
593 "C=i" => \$opt_C, "critical-domain=i" => \$opt_C, # Critical if above this number
594 "t=i" => \$opt_t, "timeout=i" => \$opt_t,
595 "s" => \$opt_s, "sudo" => \$opt_s,
596 "d:s" => \$opt_d, "configdir:s" => \$opt_d,
599 if ($opt_V) {
600 print_revision($PROGNAME,'@NP_VERSION@');
601 exit $ERRORS{'UNKNOWN'};
604 if ($opt_h) {
605 print_help();
606 exit $ERRORS{'UNKNOWN'};
609 if (defined $opt_v ){
610 $verbose = $opt_v;
613 unless (defined $opt_t) {
614 $opt_t = $utils::TIMEOUT ; # default timeout
617 unless ( defined $opt_w && defined $opt_c ) {
618 print_usage();
619 exit $ERRORS{'UNKNOWN'};
622 if ( $opt_w >= $opt_c) {
623 print "Warning (-w) cannot be greater than Critical (-c)!\n";
624 exit $ERRORS{'UNKNOWN'};
627 if (defined $opt_W && ! defined !$opt_C) {
628 print "Need -C if using -W\n";
629 exit $ERRORS{'UNKNOWN'};
630 }elsif(defined $opt_W && defined $opt_C) {
631 if ($opt_W >= $opt_C) {
632 print "Warning (-W) cannot be greater than Critical (-C)!\n";
633 exit $ERRORS{'UNKNOWN'};
637 if (defined $opt_M) {
638 if ($opt_M =~ /^(sendmail|qmail|postfix|exim|nullmailer)$/) {
639 $mailq = $opt_M ;
640 }elsif( $opt_M eq ''){
641 $mailq = 'sendmail';
642 }else{
643 print "-M: $opt_M is not supported\n";
644 exit $ERRORS{'UNKNOWN'};
646 }else{
647 if (defined $utils::PATH_TO_QMAIL_QSTAT
648 && -x $utils::PATH_TO_QMAIL_QSTAT)
650 $mailq = 'qmail';
652 elsif (-d '/var/lib/postfix' || -d '/var/local/lib/postfix'
653 || -e '/usr/sbin/postfix' || -e '/usr/local/sbin/postfix')
655 $mailq = 'postfix';
657 elsif (-d '/usr/lib/exim4' || -d '/usr/local/lib/exim4'
658 || -e '/usr/sbin/exim' || -e '/usr/local/sbin/exim')
660 $mailq = 'exim';
662 elsif (-d '/usr/lib/nullmailer' || -d '/usr/local/lib/nullmailer'
663 || -e '/usr/sbin/nullmailer-send'
664 || -e '/usr/local/sbin/nullmailer-send')
666 $mailq = 'nullmailer';
668 else {
669 $mailq = 'sendmail';
673 return $ERRORS{'OK'};
676 sub print_usage () {
677 print "Usage: $PROGNAME -w <warn> -c <crit> [-W <warn>] [-C <crit>] [-M <MTA>] [-t <timeout>] [-s] [-d <CONFIGDIR>] [-v]\n";
680 sub print_help () {
681 print_revision($PROGNAME,'@NP_VERSION@');
682 print "Copyright (c) 2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid\n";
683 print "\n";
684 print_usage();
685 print "\n";
686 print " Checks the number of messages in the mail queue (supports multiple sendmail queues, qmail)\n";
687 print " Feedback/patches to support non-sendmail mailqueue welcome\n\n";
688 print "-w (--warning) = Min. number of messages in queue to generate warning\n";
689 print "-c (--critical) = Min. number of messages in queue to generate critical alert ( w < c )\n";
690 print "-W (--warning-domain) = Min. number of messages for same domain in queue to generate warning\n";
691 print "-C (--critical-domain) = Min. number of messages for same domain in queue to generate critical alert ( W < C )\n";
692 print "-t (--timeout) = Plugin timeout in seconds (default = $utils::TIMEOUT)\n";
693 print "-M (--mailserver) = [ sendmail | qmail | postfix | exim | nullmailer ] (default = autodetect)\n";
694 print "-s (--sudo) = Use sudo to call the mailq command\n";
695 print "-d (--configdir) = Config file or directory\n";
696 print "-h (--help)\n";
697 print "-V (--version)\n";
698 print "-v (--verbose) = debugging output\n";
699 print "\n\n";
700 print "Note: -w and -c are required arguments. -W and -C are optional.\n";
701 print " -W and -C are applied to domains listed on the queues - both FROM and TO. (sendmail)\n";
702 print " -W and -C are applied message not yet preproccessed. (qmail)\n";
703 print " This plugin tries to autodetect which mailserver you are running,\n";
704 print " you can override the autodetection with -M.\n";
705 print " This plugin uses the system mailq command (sendmail) or qmail-stat (qmail)\n";
706 print " to look at the queues. Mailq can usually only be accessed by root or \n";
707 print " a TrustedUser. You will have to set appropriate permissions for the plugin to work.\n";
708 print "";
709 print "\n\n";
710 support();