summaryrefslogtreecommitdiffstats
path: root/Monitoring/nagios/plugins/check_mailq
blob: 01a147b993d551ab26718001b86dbbeea0be0fde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#! /usr/bin/perl -w

# check_mailq - check to see how many messages are in the smtp queue awating
#   transmittal.  
#
# Initial version support sendmail's mailq command
#  Support for mutiple sendmail queues (Carlos Canau)
#  Support for qmail (Benjamin Schmid)

# License Information:
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
############################################################################

use POSIX;
use strict;
use Getopt::Long;
use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c $opt_t
					$opt_M $mailq $status $state $msg $msg_q $msg_p $opt_W $opt_C $mailq @lines
					%srcdomains %dstdomains);
use lib  "/usr/lib/nagios/plugins";
use utils qw(%ERRORS &print_revision &support &usage );


sub print_help ();
sub print_usage ();
sub process_arguments ();

$ENV{'PATH'}='';
$ENV{'BASH_ENV'}=''; 
$ENV{'ENV'}='';
$PROGNAME = "check_mailq";
$mailq = 'sendmail';	# default
$msg_q = 0 ;
$msg_p = 0 ;
$state = $ERRORS{'UNKNOWN'};

Getopt::Long::Configure('bundling');
$status = process_arguments();
if ($status){
	print "ERROR: processing arguments\n";
	exit $ERRORS{"UNKNOWN"};
}

$SIG{'ALRM'} = sub {
	print ("ERROR: timed out waiting for $utils::PATH_TO_MAILQ \n");
	exit $ERRORS{"WARNING"};
};
alarm($opt_t);

# switch based on MTA

if ($mailq eq "sendmail") {

	## open mailq 
	if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
		if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
			print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
			exit $ERRORS{'UNKNOWN'};
		}
	}elsif( defined $utils::PATH_TO_MAILQ){
		unless (-x $utils::PATH_TO_MAILQ) {
			print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
			exit $ERRORS{'UNKNOWN'};
		}
	} else {
		print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
		exit $ERRORS{'UNKNOWN'};
	}
#  single queue empty
##/var/spool/mqueue is empty
#  single queue: 1
##                /var/spool/mqueue (1 request)
##----Q-ID---- --Size-- -----Q-Time----- ------------Sender/Recipient------------
##h32E30p01763     2782 Wed Apr  2 15:03 <silvaATkpnqwest.pt>
##      8BITMIME
##                                       <silvaATeunet.pt>

#  multi queue empty
##/var/spool/mqueue/q0/df is empty
##/var/spool/mqueue/q1/df is empty
##/var/spool/mqueue/q2/df is empty
##/var/spool/mqueue/q3/df is empty
##/var/spool/mqueue/q4/df is empty
##/var/spool/mqueue/q5/df is empty
##/var/spool/mqueue/q6/df is empty
##/var/spool/mqueue/q7/df is empty
##/var/spool/mqueue/q8/df is empty
##/var/spool/mqueue/q9/df is empty
##/var/spool/mqueue/qA/df is empty
##/var/spool/mqueue/qB/df is empty
##/var/spool/mqueue/qC/df is empty
##/var/spool/mqueue/qD/df is empty
##/var/spool/mqueue/qE/df is empty
##/var/spool/mqueue/qF/df is empty
##                Total Requests: 0
#  multi queue: 1
##/var/spool/mqueue/q0/df is empty
##/var/spool/mqueue/q1/df is empty
##/var/spool/mqueue/q2/df is empty
##                /var/spool/mqueue/q3/df (1 request)
##----Q-ID---- --Size-- -----Q-Time----- ------------Sender/Recipient------------
##h32De2f23534*      48 Wed Apr  2 14:40 nocol
##                                       nouserATEUnet.pt
##                                       canau
##/var/spool/mqueue/q4/df is empty
##/var/spool/mqueue/q5/df is empty
##/var/spool/mqueue/q6/df is empty
##/var/spool/mqueue/q7/df is empty
##/var/spool/mqueue/q8/df is empty
##/var/spool/mqueue/q9/df is empty
##/var/spool/mqueue/qA/df is empty
##/var/spool/mqueue/qB/df is empty
##/var/spool/mqueue/qC/df is empty
##/var/spool/mqueue/qD/df is empty
##/var/spool/mqueue/qE/df is empty
##/var/spool/mqueue/qF/df is empty
##                Total Requests: 1

	
	while (<MAILQ>) {
	
		# match email addr on queue listing
		if ( (/<.*@.*\.(\w+\.\w+)>/) || (/<.*@(\w+\.\w+)>/) ) {
			my $domain = $1;
			if (/^\w+/) {
	  		print "$utils::PATH_TO_MAILQ = srcdomain = $domain \n" if $verbose ;
		    $srcdomains{$domain} ++;
			}
			next;
		}
	
		#
		# ...
		# sendmail considers a message with more than one destiny, say N, to the same MX 
		# to have N messages in queue.
		# we will only consider one in this code
		if (( /\s\(reply:\sread\serror\sfrom\s.*\.(\w+\.\w+)\.$/ ) || ( /\s\(reply:\sread\serror\sfrom\s(\w+\.\w+)\.$/ ) ||
			( /\s\(timeout\swriting\smessage\sto\s.*\.(\w+\.\w+)\.:/ ) || ( /\s\(timeout\swriting\smessage\sto\s(\w+\.\w+)\.:/ ) ||
			( /\s\(host\smap:\slookup\s\(.*\.(\w+\.\w+)\):/ ) || ( /\s\(host\smap:\slookup\s\((\w+\.\w+)\):/ ) || 
			( /\s\(Deferred:\s.*\s.*\.(\w+\.\w+)\.\)/ ) || ( /\s\(Deferred:\s.*\s(\w+\.\w+)\.\)/ ) ) {
	
			print "$utils::PATH_TO_MAILQ = dstdomain = $1 \n" if $verbose ;
			$dstdomains{$1} ++;
		}
	
		if (/\s+\(I\/O\serror\)/) {
			print "$utils::PATH_TO_MAILQ = dstdomain = UNKNOWN \n" if $verbose ;
			$dstdomains{'UNKNOWN'} ++;
		}

		# Finally look at the overall queue length
		#
		if (/mqueue/) {
			print "$utils::PATH_TO_MAILQ = $_ "if $verbose ;
			if (/ \((\d+) request/) {
	    	#
		    # single queue: first line
		    # multi queue: one for each queue. overwrite on multi queue below
	  	  $msg_q = $1 ;
			}
		} elsif (/^\s+Total\sRequests:\s(\d+)$/i) {
			print "$utils::PATH_TO_MAILQ = $_ \n" if $verbose ;
			#
			# multi queue: last line
			$msg_q = $1 ;
		}
	
	}
	

	## close mailq

	close (MAILQ); 

	if ( $? ) {
		print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
		exit $ERRORS{CRITICAL};
	}

	## shut off the alarm
	alarm(0);



	## now check the queue length(s)

	if ($msg_q == 0) {
		$msg = "OK: mailq is empty";
		$state = $ERRORS{'OK'};
	} else {
		print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
	
		# overall queue length
		if ($msg_q < $opt_w) {
			$msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
			$state = $ERRORS{'OK'};
		}elsif ($msg_q >= $opt_w  && $msg_q < $opt_c) {
			$msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
			$state = $ERRORS{'WARNING'};
		}else {
			$msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
			$state = $ERRORS{'CRITICAL'};
		}

		# check for domain specific queue lengths if requested
		if (defined $opt_W) {
		
			# Apply threshold to queue lengths FROM domain
			my @srckeys = sort { $srcdomains{$b} <=> $srcdomains{$a} } keys %srcdomains;
  	  my $srcmaxkey = $srckeys[0];
    	print "src max is $srcmaxkey with $srcdomains{$srcmaxkey} messages\n" if $verbose;
		
			if ($srcdomains{$srcmaxkey} >= $opt_W && $srcdomains{$srcmaxkey} < $opt_C) {
				if ($state == $ERRORS{'OK'}) {
					$msg = "WARNING: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
					$state = $ERRORS{'WARNING'};
				} elsif (($state == $ERRORS{'WARNING'}) || ($state == $ERRORS{'CRITICAL'})){
		    	$msg .= " -and- $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
				} else {
					$msg = "WARNING: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
					$state = $ERRORS{'WARNING'};
				}
  	  } elsif ($srcdomains{$srcmaxkey} >= $opt_C) {
				if ($state == $ERRORS{'OK'}) {
					$msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold C = $opt_C)";
					$state = $ERRORS{'CRITICAL'};
				} elsif ($state == $ERRORS{'WARNING'}) {
					$msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold C = $opt_C) -and- " . $msg;
					$msg =~ s/WARNING: //;
				} elsif ($state == $ERRORS{'CRITICAL'}) {
					$msg .= " -and- $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
				} else {
					$msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
					$state = $ERRORS{'CRITICAL'};
				}
	    } else {
				if ($srcdomains{$srcmaxkey} > 0) {
					$msg .= " $srcdomains{$srcmaxkey} msgs. FROM $srcmaxkey is below threshold ($opt_W/$opt_C)";
				}
			}

			# Apply threshold to queue lengths TO domain
			my @dstkeys = sort { $dstdomains{$b} <=> $dstdomains{$a} } keys %dstdomains;
	    my $dstmaxkey = $dstkeys[0];
  	  print "dst max is $dstmaxkey with $dstdomains{$dstmaxkey} messages\n" if $verbose;
		
			if ($dstdomains{$dstmaxkey} >= $opt_W && $dstdomains{$dstmaxkey} < $opt_C) {
				if ($state == $ERRORS{'OK'}) {
					$msg = "WARNING: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
					$state = $ERRORS{'WARNING'};
				} elsif (($state == $ERRORS{'WARNING'}) || ($state == $ERRORS{'CRITICAL'})){
					$msg .= " -and- $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
				} else {
					$msg = "WARNING: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
					$state = $ERRORS{'WARNING'};
				}
			} elsif ($dstdomains{$dstmaxkey} >= $opt_C) {
				if ($state == $ERRORS{'OK'}) {
					$msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold C = $opt_C)";
					$state = $ERRORS{'CRITICAL'};
				} elsif ($state == $ERRORS{'WARNING'}) {
					$msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold C = $opt_C) -and- " . $msg;
					$msg =~ s/WARNING: //;
				} elsif ($state == $ERRORS{'CRITICAL'}) {
					$msg .= " -and- $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
				} else {
					$msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
					$state = $ERRORS{'CRITICAL'};
				}
			} else {
				if ($dstdomains{$dstmaxkey} > 0) {
					$msg .= " $dstdomains{$dstmaxkey} msgs. TO $dstmaxkey is below threshold ($opt_W/$opt_C)";
				}
			}

		} # End of queue length thresholds

	}

} # end of ($mailq eq "sendmail")
elsif ( $mailq eq "postfix" ) {

     ## open mailq
        if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
                if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
                        print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
                        exit $ERRORS{'UNKNOWN'};
                }
        }elsif( defined $utils::PATH_TO_MAILQ){
                unless (-x $utils::PATH_TO_MAILQ) {
                        print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
                        exit $ERRORS{'UNKNOWN'};
                }
        } else {
                print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
                exit $ERRORS{'UNKNOWN'};
        }


        @lines = reverse <MAILQ>;

        # close qmail-qstat
        close MAILQ;

        if ( $? ) {
		print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
		exit $ERRORS{CRITICAL};
        }

        ## shut off the alarm
        alarm(0);

        # check queue length
        if ($lines[0]=~/Kbytes in (\d+)/) {
                $msg_q = $1 ;
	}elsif ($lines[0]=~/Mail queue is empty/) {
		$msg_q = 0;
        }else{
                print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
                exit   $ERRORS{'UNKNOWN'};
        }

        # check messages not processed
        #if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
        #        my $msg_p = $1;
        #}else{
        #        print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
        #        exit  $ERRORS{'UNKNOWN'};
        #}

        # check queue length(s)
        if ($msg_q == 0){
                $msg = "OK: mailq reports queue is empty";
                $state = $ERRORS{'OK'};
        } else {
                print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;

                # overall queue length
                if ($msg_q < $opt_w) {
                        $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
                        $state = $ERRORS{'OK'};
                }elsif  ($msg_q >= $opt_w  && $msg_q < $opt_c) {
                        $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
                        $state = $ERRORS{'WARNING'};
                }else {
                        $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
                        $state = $ERRORS{'CRITICAL'};
                }

                # check messages not yet preprocessed (only compare is $opt_W and $opt_C
                # are defined)

                #if (defined $opt_W) {
                #        $msg .= "[Preprocessed = $msg_p]";
                #        if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
                #                $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"}  ;
                #        }elsif ($msg_p >= $opt_C ) {
                #                $state = $ERRORS{"CRITICAL"} ;
                #        }
                #}
        }
} # end of ($mailq eq "postfixl")
elsif ( $mailq eq "qmail" ) {

	# open qmail-qstat 
	if ( defined $utils::PATH_TO_QMAIL_QSTAT && -x $utils::PATH_TO_QMAIL_QSTAT ) {
		if (! open (MAILQ, "$utils::PATH_TO_QMAIL_QSTAT | " ) ) {
			print "ERROR: could not open $utils::PATH_TO_QMAIL_QSTAT \n";
			exit $ERRORS{'UNKNOWN'};
		}
	}elsif( defined $utils::PATH_TO_QMAIL_QSTAT){
		unless (-x $utils::PATH_TO_QMAIL_QSTAT) {
			print "ERROR: $utils::PATH_TO_QMAIL_QSTAT is not executable by (uid $>:gid($)))\n";
			exit $ERRORS{'UNKNOWN'};
		}
	} else {
		print "ERROR: \$utils::PATH_TO_QMAIL_QSTAT is not defined\n";
		exit $ERRORS{'UNKNOWN'};
	}

	@lines = <MAILQ>;

	# close qmail-qstat
	close MAILQ;

	if ( $? ) {
		print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
		exit $ERRORS{CRITICAL};
	}

	## shut off the alarm
	alarm(0);

	# check queue length
	if ($lines[0]=~/^messages in queue: (\d+)/) {
		$msg_q = $1 ;
	}else{
		print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
		exit   $ERRORS{'UNKNOWN'};
	}

	# check messages not processed
	if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
		my $msg_p = $1;
	}else{
		print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
		exit  $ERRORS{'UNKNOWN'};
	}


	# check queue length(s)
	if ($msg_q == 0){
		$msg = "OK: qmail-qstat reports queue is empty";
		$state = $ERRORS{'OK'};
	} else {
		print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
		
		# overall queue length
		if ($msg_q < $opt_w) {
			$msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
			$state = $ERRORS{'OK'};
		}elsif ($msg_q >= $opt_w  && $msg_q < $opt_c) {
			$msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
			$state = $ERRORS{'WARNING'};
		}else {
			$msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
			$state = $ERRORS{'CRITICAL'};
		}

		# check messages not yet preprocessed (only compare is $opt_W and $opt_C
		# are defined)
		
		if (defined $opt_W) {
			$msg .= "[Preprocessed = $msg_p]";
			if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
				$state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"}  ;
			}elsif ($msg_p >= $opt_C ) {
				$state = $ERRORS{"CRITICAL"} ;
			}
		}
	}				
		


} # end of ($mailq eq "qmail")
elsif ( $mailq eq "exim" ) {
	## open mailq 
	if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
		if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
			print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
			exit $ERRORS{'UNKNOWN'};
		}
	}elsif( defined $utils::PATH_TO_MAILQ){
		unless (-x $utils::PATH_TO_MAILQ) {
			print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
			exit $ERRORS{'UNKNOWN'};
		}
	} else {
		print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
		exit $ERRORS{'UNKNOWN'};
	}

	while (<MAILQ>) {
	    #22m  1.7K 19aEEr-0007hx-Dy <> *** frozen ***
            #root@exlixams.glups.fr

	    if (/\s[\w\d]{6}-[\w\d]{6}-[\w\d]{2}\s/) { # message id 19aEEr-0007hx-Dy
		$msg_q++ ;
	    }
	}
	close(MAILQ) ;

	if ( $? ) {
		print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
		exit $ERRORS{CRITICAL};
	}
	if ($msg_q < $opt_w) {
		$msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
		$state = $ERRORS{'OK'};
	}elsif ($msg_q >= $opt_w  && $msg_q < $opt_c) {
		$msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
		$state = $ERRORS{'WARNING'};
	}else {
		$msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
		$state = $ERRORS{'CRITICAL'};
	}
} # end of ($mailq eq "exim")

# Perfdata support
print "$msg|unsent=$msg_q;$opt_w;$opt_c;0\n";
exit $state;


#####################################
#### subs


sub process_arguments(){
	GetOptions
		("V"   => \$opt_V, "version"	=> \$opt_V,
		 "v"   => \$opt_v, "verbose"	=> \$opt_v,
		 "h"   => \$opt_h, "help"		=> \$opt_h,
		 "M:s" => \$opt_M, "mailserver:s" => \$opt_M, # mailserver (default	sendmail)
		 "w=i" => \$opt_w, "warning=i"  => \$opt_w,   # warning if above this number
		 "c=i" => \$opt_c, "critical=i" => \$opt_c,	  # critical if above this number
		 "t=i" => \$opt_t, "timeout=i"  => \$opt_t 
		 );

	if ($opt_V) {
		print_revision($PROGNAME,'1.4.15');
		exit $ERRORS{'OK'};
	}

	if ($opt_h) {
		print_help();
		exit $ERRORS{'OK'};
	}

	if (defined $opt_v ){
		$verbose = $opt_v;
	}

	unless (defined $opt_t) {
		$opt_t = $utils::TIMEOUT ;	# default timeout
	}

	unless (  defined $opt_w &&  defined $opt_c ) {
		print_usage();
		exit $ERRORS{'UNKNOWN'};
	}

	if ( $opt_w >= $opt_c) {
		print "Warning (-w) cannot be greater than Critical (-c)!\n";
		exit $ERRORS{'UNKNOWN'};
	}

	if (defined $opt_W && ! defined !$opt_C) {
		print "Need -C if using -W\n";
		exit $ERRORS{'UNKNOWN'};
	}elsif(defined $opt_W && defined $opt_C) {
		if ($opt_W >= $opt_C) {
			print "Warning (-W) cannot be greater than Critical (-C)!\n";
			exit $ERRORS{'UNKNOWN'};
		}
	}

	if (defined $opt_M) {
		if ($opt_M =~ /^(sendmail|qmail|postfix|exim)$/) {
			$mailq = $opt_M ;
		}elsif( $opt_M eq ''){
			$mailq = 'sendmail';
		}else{
			print "-M: $opt_M is not supported\n";
			exit $ERRORS{'UNKNOWN'};
		}
	}else{
		$mailq = 'sendmail' ;
	}
		
	return $ERRORS{'OK'};
}

sub print_usage () {
	print "Usage: $PROGNAME -w <warn> -c <crit> [-W <warn>] [-C <crit>] [-M <MTA>] [-t <timeout>] [-v verbose]\n";
}

sub print_help () {
	print_revision($PROGNAME,'1.4.15');
	print "Copyright (c) 2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid\n";
	print "\n";
	print_usage();
	print "\n";
	print "   Checks the number of messages in the mail queue (supports multiple sendmail queues, qmail)\n";
	print "   Feedback/patches to support non-sendmail mailqueue welcome\n\n";
	print "-w (--warning)   = Min. number of messages in queue to generate warning\n";
	print "-c (--critical)  = Min. number of messages in queue to generate critical alert ( w < c )\n";
	print "-W (--Warning)   = Min. number of messages for same domain in queue to generate warning\n";
	print "-C (--Critical)  = Min. number of messages for same domain in queue to generate critical alert ( W < C )\n";
	print "-t (--timeout)   = Plugin timeout in seconds (default = $utils::TIMEOUT)\n";
	print "-M (--mailserver) = [ sendmail | qmail | postfix | exim ] (default = sendmail)\n";
	print "-h (--help)\n";
	print "-V (--version)\n";
	print "-v (--verbose)   = debugging output\n";
	print "\n\n";
	print "Note: -w and -c are required arguments.  -W and -C are optional.\n";
	print " -W and -C are applied to domains listed on the queues - both FROM and TO. (sendmail)\n";
	print " -W and -C are applied message not yet preproccessed. (qmail)\n";
	print " This plugin uses the system mailq command (sendmail) or qmail-stat (qmail)\n";
	print " to look at the queues. Mailq can usually only be accessed by root or \n";
	print " a TrustedUser. You will have to set appropriate permissions for the plugin to work.\n";
	print "";
	print "\n\n";
	support();
}