Code

Better comments for what the substitutions are doing
[nagiosplug.git] / plugins-scripts / check_mailq.pl
1 #!/usr/local/bin/perl -w
3 # check_mailq - check to see how many messages are in the smtp queue awating
4 #   transmittal.  
5 #
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.
15 #
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.
20 #
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.
24 #
25 # $Id$
26 #
27 ############################################################################
29 use POSIX;
30 use strict;
31 use Getopt::Long;
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);
35 use lib  utils.pm;
36 use utils qw(%ERRORS &print_revision &support &usage );
39 sub print_help ();
40 sub print_usage ();
41 sub process_arguments ();
43 $ENV{'PATH'}='';
44 $ENV{'BASH_ENV'}=''; 
45 $ENV{'ENV'}='';
46 $PROGNAME = "check_mailq";
47 $mailq = 'sendmail';    # default
48 $msg_q = 0 ;
49 $msg_p = 0 ;
50 $state = $ERRORS{'UNKNOWN'};
52 Getopt::Long::Configure('bundling');
53 $status = process_arguments();
54 if ($status){
55         print "ERROR: processing arguments\n";
56         exit $ERRORS{"UNKNOWN"};
57 }
59 $SIG{'ALRM'} = sub {
60         print ("ERROR: timed out waiting for $utils::PATH_TO_MAILQ \n");
61         exit $ERRORS{"WARNING"};
62 };
63 alarm($opt_t);
65 # switch based on MTA
67 if ($mailq eq "sendmail") {
69         ## open mailq 
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'};
74                 }
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'};
79                 }
80         } else {
81                 print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
82                 exit $ERRORS{'UNKNOWN'};
83         }
84 #  single queue empty
85 ##/var/spool/mqueue is empty
86 #  single queue: 1
87 ##                /var/spool/mqueue (1 request)
88 ##----Q-ID---- --Size-- -----Q-Time----- ------------Sender/Recipient------------
89 ##h32E30p01763     2782 Wed Apr  2 15:03 <silvaATkpnqwest.pt>
90 ##      8BITMIME
91 ##                                       <silvaATeunet.pt>
93 #  multi queue empty
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
110 ##                Total Requests: 0
111 #  multi queue: 1
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
118 ##                                       nouserATEUnet.pt
119 ##                                       canau
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
132 ##                Total Requests: 1
134         
135         while (<MAILQ>) {
136         
137                 # match email addr on queue listing
138                 if ( (/<.*@.*\.(\w+\.\w+)>/) || (/<.*@(\w+\.\w+)>/) ) {
139                         my $domain = $1;
140                         if (/^\w+/) {
141                         print "$utils::PATH_TO_MAILQ = srcdomain = $domain \n" if $verbose ;
142                     $srcdomains{$domain} ++;
143                         }
144                         next;
145                 }
146         
147                 #
148                 # ...
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+)\.\)/ ) ) {
156         
157                         print "$utils::PATH_TO_MAILQ = dstdomain = $1 \n" if $verbose ;
158                         $dstdomains{$1} ++;
159                 }
160         
161                 if (/\s+\(I\/O\serror\)/) {
162                         print "$utils::PATH_TO_MAILQ = dstdomain = UNKNOWN \n" if $verbose ;
163                         $dstdomains{'UNKNOWN'} ++;
164                 }
166                 # Finally look at the overall queue length
167                 #
168                 if (/mqueue/) {
169                         print "$utils::PATH_TO_MAILQ = $_ "if $verbose ;
170                         if (/ \((\d+) request/) {
171                 #
172                     # single queue: first line
173                     # multi queue: one for each queue. overwrite on multi queue below
174                   $msg_q = $1 ;
175                         }
176                 } elsif (/^\s+Total\sRequests:\s(\d+)$/i) {
177                         print "$utils::PATH_TO_MAILQ = $_ \n" if $verbose ;
178                         #
179                         # multi queue: last line
180                         $msg_q = $1 ;
181                 }
182         
183         }
184         
186         ## close mailq
188         close (MAILQ); 
189         # declare an error if we also get a non-zero return code from mailq
190         # unless already set to critical
191         if ( $? ) {
192                 $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"}  ;
193                 print "STDERR $?: $!\n" if $verbose;
194                 $msg = "$state: (stderr)\n";
195         }
197         ## shut off the alarm
198         alarm(0);
202         ## now check the queue length(s)
204         if ($msg_q == 0) {
205                 $msg = "OK: mailq is empty";
206                 $state = $ERRORS{'OK'};
207         } else {
208                 print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
209         
210                 # overall queue length
211                 if ($msg_q < $opt_w) {
212                         $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
213                         $state = $ERRORS{'OK'};
214                 }elsif ($msg_q >= $opt_w  && $msg_q < $opt_c) {
215                         $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
216                         $state = $ERRORS{'WARNING'};
217                 }else {
218                         $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
219                         $state = $ERRORS{'CRITICAL'};
220                 }
222                 # check for domain specific queue lengths if requested
223                 if (defined $opt_W) {
224                 
225                         # Apply threshold to queue lengths FROM domain
226                         my @srckeys = sort { $srcdomains{$b} <=> $srcdomains{$a} } keys %srcdomains;
227           my $srcmaxkey = $srckeys[0];
228         print "src max is $srcmaxkey with $srcdomains{$srcmaxkey} messages\n" if $verbose;
229                 
230                         if ($srcdomains{$srcmaxkey} >= $opt_W && $srcdomains{$srcmaxkey} < $opt_C) {
231                                 if ($state == $ERRORS{'OK'}) {
232                                         $msg = "WARNING: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
233                                         $state = $ERRORS{'WARNING'};
234                                 } elsif (($state == $ERRORS{'WARNING'}) || ($state == $ERRORS{'CRITICAL'})){
235                         $msg .= " -and- $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
236                                 } else {
237                                         $msg = "WARNING: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
238                                         $state = $ERRORS{'WARNING'};
239                                 }
240           } elsif ($srcdomains{$srcmaxkey} >= $opt_C) {
241                                 if ($state == $ERRORS{'OK'}) {
242                                         $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold C = $opt_C)";
243                                         $state = $ERRORS{'CRITICAL'};
244                                 } elsif ($state == $ERRORS{'WARNING'}) {
245                                         $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold C = $opt_C) -and- " . $msg;
246                                         $msg =~ s/WARNING: //;
247                                 } elsif ($state == $ERRORS{'CRITICAL'}) {
248                                         $msg .= " -and- $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
249                                 } else {
250                                         $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
251                                         $state = $ERRORS{'CRITICAL'};
252                                 }
253             } else {
254                                 if ($srcdomains{$srcmaxkey} > 0) {
255                                         $msg .= " $srcdomains{$srcmaxkey} msgs. FROM $srcmaxkey is below threshold ($opt_W/$opt_C)";
256                                 }
257                         }
259                         # Apply threshold to queue lengths TO domain
260                         my @dstkeys = sort { $dstdomains{$b} <=> $dstdomains{$a} } keys %dstdomains;
261             my $dstmaxkey = $dstkeys[0];
262           print "dst max is $dstmaxkey with $dstdomains{$dstmaxkey} messages\n" if $verbose;
263                 
264                         if ($dstdomains{$dstmaxkey} >= $opt_W && $dstdomains{$dstmaxkey} < $opt_C) {
265                                 if ($state == $ERRORS{'OK'}) {
266                                         $msg = "WARNING: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
267                                         $state = $ERRORS{'WARNING'};
268                                 } elsif (($state == $ERRORS{'WARNING'}) || ($state == $ERRORS{'CRITICAL'})){
269                                         $msg .= " -and- $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
270                                 } else {
271                                         $msg = "WARNING: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
272                                         $state = $ERRORS{'WARNING'};
273                                 }
274                         } elsif ($dstdomains{$dstmaxkey} >= $opt_C) {
275                                 if ($state == $ERRORS{'OK'}) {
276                                         $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold C = $opt_C)";
277                                         $state = $ERRORS{'CRITICAL'};
278                                 } elsif ($state == $ERRORS{'WARNING'}) {
279                                         $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold C = $opt_C) -and- " . $msg;
280                                         $msg =~ s/WARNING: //;
281                                 } elsif ($state == $ERRORS{'CRITICAL'}) {
282                                         $msg .= " -and- $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
283                                 } else {
284                                         $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
285                                         $state = $ERRORS{'CRITICAL'};
286                                 }
287                         } else {
288                                 if ($dstdomains{$dstmaxkey} > 0) {
289                                         $msg .= " $dstdomains{$dstmaxkey} msgs. TO $dstmaxkey is below threshold ($opt_W/$opt_C)";
290                                 }
291                         }
293                 } # End of queue length thresholds
295         }
297 } # end of ($mailq eq "sendmail")
298 elsif ( $mailq eq "postfix" ) {
300      ## open mailq
301         if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
302                 if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
303                         print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
304                         exit $ERRORS{'UNKNOWN'};
305                 }
306         }elsif( defined $utils::PATH_TO_MAILQ){
307                 unless (-x $utils::PATH_TO_MAILQ) {
308                         print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
309                         exit $ERRORS{'UNKNOWN'};
310                 }
311         } else {
312                 print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
313                 exit $ERRORS{'UNKNOWN'};
314         }
317         @lines = reverse <MAILQ>;
319         # close qmail-qstat
320         close MAILQ;
321         # declare an error if we also get a non-zero return code from mailq
322         # unless already set to critical
323         if ( $? ) {
324                 $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"}  ;
325                 print "STDERR $?: $!\n" if $verbose;
326                 $msg = "$state: (stderr)\n";
327         }
329         ## shut off the alarm
330         alarm(0);
332         # check queue length
333         if ($lines[0]=~/Kbytes in (\d+)/) {
334                 $msg_q = $1 ;
335         }elsif ($lines[0]=~/Mail queue is empty/) {
336                 $msg_q = 0;
337         }else{
338                 print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
339                 exit   $ERRORS{'UNKNOWN'};
340         }
342         # check messages not processed
343         #if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
344         #        my $msg_p = $1;
345         #}else{
346         #        print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
347         #        exit  $ERRORS{'UNKNOWN'};
348         #}
350         # check queue length(s)
351         if ($msg_q == 0){
352                 $msg = "OK: mailq reports queue is empty";
353                 $state = $ERRORS{'OK'};
354         } else {
355                 print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
357                 # overall queue length
358                 if ($msg_q < $opt_w) {
359                         $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
360                         $state = $ERRORS{'OK'};
361                 }elsif  ($msg_q >= $opt_w  && $msg_q < $opt_c) {
362                         $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
363                         $state = $ERRORS{'WARNING'};
364                 }else {
365                         $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
366                         $state = $ERRORS{'CRITICAL'};
367                 }
369                 # check messages not yet preprocessed (only compare is $opt_W and $opt_C
370                 # are defined)
372                 #if (defined $opt_W) {
373                 #        $msg .= "[Preprocessed = $msg_p]";
374                 #        if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
375                 #                $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"}  ;
376                 #        }elsif ($msg_p >= $opt_C ) {
377                 #                $state = $ERRORS{"CRITICAL"} ;
378                 #        }
379                 #}
380         }
381 } # end of ($mailq eq "postfixl")
382 elsif ( $mailq eq "qmail" ) {
384         # open qmail-qstat 
385         if ( defined $utils::PATH_TO_QMAIL_QSTAT && -x $utils::PATH_TO_QMAIL_QSTAT ) {
386                 if (! open (MAILQ, "$utils::PATH_TO_QMAIL_QSTAT | " ) ) {
387                         print "ERROR: could not open $utils::PATH_TO_QMAIL_QSTAT \n";
388                         exit $ERRORS{'UNKNOWN'};
389                 }
390         }elsif( defined $utils::PATH_TO_QMAIL_QSTAT){
391                 unless (-x $utils::PATH_TO_QMAIL_QSTAT) {
392                         print "ERROR: $utils::PATH_TO_QMAIL_QSTAT is not executable by (uid $>:gid($)))\n";
393                         exit $ERRORS{'UNKNOWN'};
394                 }
395         } else {
396                 print "ERROR: \$utils::PATH_TO_QMAIL_QSTAT is not defined\n";
397                 exit $ERRORS{'UNKNOWN'};
398         }
400         @lines = <MAILQ>;
402         # close qmail-qstat
403         close MAILQ;
404         # declare an error if we also get a non-zero return code from mailq
405         # unless already set to critical
406         if ( $? ) {
407                 $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"}  ;
408                 print "STDERR $?: $!\n" if $verbose;
409                 $msg = "$state: (stderr)\n";
410         }
412         ## shut off the alarm
413         alarm(0);
415         # check queue length
416         if ($lines[0]=~/^messages in queue: (\d+)/) {
417                 $msg_q = $1 ;
418         }else{
419                 print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
420                 exit   $ERRORS{'UNKNOWN'};
421         }
423         # check messages not processed
424         if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
425                 my $msg_p = $1;
426         }else{
427                 print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
428                 exit  $ERRORS{'UNKNOWN'};
429         }
432         # check queue length(s)
433         if ($msg_q == 0){
434                 $msg = "OK: qmail-qstat reports queue is empty";
435                 $state = $ERRORS{'OK'};
436         } else {
437                 print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
438                 
439                 # overall queue length
440                 if ($msg_q < $opt_w) {
441                         $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
442                         $state = $ERRORS{'OK'};
443                 }elsif ($msg_q >= $opt_w  && $msg_q < $opt_c) {
444                         $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
445                         $state = $ERRORS{'WARNING'};
446                 }else {
447                         $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
448                         $state = $ERRORS{'CRITICAL'};
449                 }
451                 # check messages not yet preprocessed (only compare is $opt_W and $opt_C
452                 # are defined)
453                 
454                 if (defined $opt_W) {
455                         $msg .= "[Preprocessed = $msg_p]";
456                         if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
457                                 $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"}  ;
458                         }elsif ($msg_p >= $opt_C ) {
459                                 $state = $ERRORS{"CRITICAL"} ;
460                         }
461                 }
462         }                               
463                 
466 } # end of ($mailq eq "qmail")
467 elsif ( $mailq eq "exim" ) {
468         ## open mailq 
469         if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
470                 if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
471                         print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
472                         exit $ERRORS{'UNKNOWN'};
473                 }
474         }elsif( defined $utils::PATH_TO_MAILQ){
475                 unless (-x $utils::PATH_TO_MAILQ) {
476                         print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
477                         exit $ERRORS{'UNKNOWN'};
478                 }
479         } else {
480                 print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
481                 exit $ERRORS{'UNKNOWN'};
482         }
484         while (<MAILQ>) {
485             #22m  1.7K 19aEEr-0007hx-Dy <> *** frozen ***
486             #root@exlixams.glups.fr
488             if (/\s[\w\d]{6}-[\w\d]{6}-[\w\d]{2}\s/) { # message id 19aEEr-0007hx-Dy
489                 $msg_q++ ;
490             }
491         }
492         close(MAILQ) ;
493         if ($msg_q < $opt_w) {
494                 $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
495                 $state = $ERRORS{'OK'};
496         }elsif ($msg_q >= $opt_w  && $msg_q < $opt_c) {
497                 $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
498                 $state = $ERRORS{'WARNING'};
499         }else {
500                 $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
501                 $state = $ERRORS{'CRITICAL'};
502         }
503 } # end of ($mailq eq "exim")
505 # Perfdata support
506 print "$msg|unsent=$msg_q;$opt_w;$opt_c;0\n";
507 exit $state;
510 #####################################
511 #### subs
514 sub process_arguments(){
515         GetOptions
516                 ("V"   => \$opt_V, "version"    => \$opt_V,
517                  "v"   => \$opt_v, "verbose"    => \$opt_v,
518                  "h"   => \$opt_h, "help"               => \$opt_h,
519                  "M:s" => \$opt_M, "mailserver:s" => \$opt_M, # mailserver (default     sendmail)
520                  "w=i" => \$opt_w, "warning=i"  => \$opt_w,   # warning if above this number
521                  "c=i" => \$opt_c, "critical=i" => \$opt_c,       # critical if above this number
522                  "t=i" => \$opt_t, "timeout=i"  => \$opt_t 
523                  );
525         if ($opt_V) {
526                 print_revision($PROGNAME,'$Revision$ ');
527                 exit $ERRORS{'OK'};
528         }
530         if ($opt_h) {
531                 print_help();
532                 exit $ERRORS{'OK'};
533         }
535         if (defined $opt_v ){
536                 $verbose = $opt_v;
537         }
539         unless (defined $opt_t) {
540                 $opt_t = $utils::TIMEOUT ;      # default timeout
541         }
543         unless (  defined $opt_w &&  defined $opt_c ) {
544                 print_usage();
545                 exit $ERRORS{'UNKNOWN'};
546         }
548         if ( $opt_w >= $opt_c) {
549                 print "Warning (-w) cannot be greater than Critical (-c)!\n";
550                 exit $ERRORS{'UNKNOWN'};
551         }
553         if (defined $opt_W && ! defined !$opt_C) {
554                 print "Need -C if using -W\n";
555                 exit $ERRORS{'UNKNOWN'};
556         }elsif(defined $opt_W && defined $opt_C) {
557                 if ($opt_W >= $opt_C) {
558                         print "Warning (-W) cannot be greater than Critical (-C)!\n";
559                         exit $ERRORS{'UNKNOWN'};
560                 }
561         }
563         if (defined $opt_M) {
564                 if ($opt_M =~ /^(sendmail|qmail|postfix|exim)$/) {
565                         $mailq = $opt_M ;
566                 }elsif( $opt_M eq ''){
567                         $mailq = 'sendmail';
568                 }else{
569                         print "-M: $opt_M is not supported\n";
570                         exit $ERRORS{'UNKNOWN'};
571                 }
572         }else{
573                 $mailq = 'sendmail' ;
574         }
575                 
576         return $ERRORS{'OK'};
579 sub print_usage () {
580         print "Usage: $PROGNAME -w <warn> -c <crit> [-W <warn>] [-C <crit>] [-M <MTA>] [-t <timeout>] [-v verbose]\n";
583 sub print_help () {
584         print_revision($PROGNAME,'$Revision$');
585         print "Copyright (c) 2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid\n";
586         print "\n";
587         print_usage();
588         print "\n";
589         print "   Checks the number of messages in the mail queue (supports multiple sendmail queues, qmail)\n";
590         print "   Feedback/patches to support non-sendmail mailqueue welcome\n\n";
591         print "-w (--warning)   = Min. number of messages in queue to generate warning\n";
592         print "-c (--critical)  = Min. number of messages in queu to generate critical alert ( w < c )\n";
593         print "-W (--Warning)   = Min. number of messages for same domain in queue to generate warning\n";
594         print "-C (--Critical)  = Min. number of messages for same domain in queue to generate critical alert ( W < C )\n";
595         print "-t (--timeout)   = Plugin timeout in seconds (default = $utils::TIMEOUT)\n";
596         print "-M (--mailserver) = [ sendmail | qmail | postfix | exim ] (default = sendmail)\n";
597         print "-h (--help)\n";
598         print "-V (--version)\n";
599         print "-v (--verbose)   = debugging output\n";
600         print "\n\n";
601         print "Note: -w and -c are required arguments.  -W and -C are optional.\n";
602         print " -W and -C are applied to domains listed on the queues - both FROM and TO. (sendmail)\n";
603         print " -W and -C are applied message not yet preproccessed. (qmail)\n";
604         print " This plugin uses the system mailq command (sendmail) or qmail-stat (qmail)\n";
605         print " to look at the queues. Mailq can usually only be accessed by root or \n";
606         print " a TrustedUser. You will have to set appropriate permissions for the plugin to work.\n";
607         print "";
608         print "\n\n";
609         support();