Code

monitor mailq
[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
8 # License Information:
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #
23 ############################################################################
25 use POSIX;
26 use strict;
27 use Getopt::Long;
28 use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c $opt_t $status $state $msg $msg_q );
29 use lib  utils.pm;
30 use utils qw(%ERRORS &print_revision &support &usage );
32 #my $MAILQ = "/usr/bin/mailq";   # need to migrate support to utils.pm and autoconf
35 sub print_help ();
36 sub print_usage ();
37 sub process_arguments ();
39 $ENV{'PATH'}='';
40 $ENV{'BASH_ENV'}=''; 
41 $ENV{'ENV'}='';
42 $PROGNAME = "check_mailq";
44 Getopt::Long::Configure('bundling');
45 $status = process_arguments();
46 if ($status){
47         print "ERROR: processing arguments\n";
48         exit $ERRORS{"UNKNOWN"};
49 }
51 $SIG{'ALRM'} = sub {
52         print ("ERROR: timed out waiting for $utils::PATH_TO_MAILQ \n");
53         exit $ERRORS{"WARNING"};
54 };
55 alarm($opt_t);
57 ## open mailq 
58 if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
59         if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
60                 print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
61                 exit $ERRORS{'UNKNOWN'};
62         }
63 }else{
64         print "ERROR: Could not find mailq executable!\n";
65         exit $ERRORS{'UNKNOWN'};
66 }
68 # only first line is relevant in this iteration.
69 while (<MAILQ>) {
70         if (/mqueue/) {
71                 print "$utils::PATH_TO_MAILQ = $_ "if $verbose ;
72                 if (/empty/ ) {
73                         $msg = "OK: mailq is empty";
74                         $msg_q = 0;
75                         $state = $ERRORS{'OK'};
76                 }elsif ( /(\d+)/ ) {
77                         $msg_q = $1 ;
79                         print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
81                         if ($msg_q < $opt_w) {
82                                 $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
83                                 $state = $ERRORS{'OK'};
84                         }elsif ($msg_q >= $opt_w  && $msg_q < $opt_c) {
85                                 $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
86                                 $state = $ERRORS{'WARNING'};
87                         }else {
88                                 $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
89                                 $state = $ERRORS{'CRITICAL'};
90                         }
91                         
92                 }
94                 last;
95         }
96         
97 }
99 close (MAILQ); 
100 # declare an error if we also get a non-zero return code from mailq
101 # unless already set to critical
102 if ( $? ) {
103         print "stderr = $? : $! \n" if $verbose;
104         $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"UNKNOWN"}  ;
105         print "MAILQ error: $!\n" if $verbose;
107 ## close mailq
109 # Perfdata support
110 print "$msg | mailq = $msg_q\n";
111 exit $state;
114 #####################################
115 #### subs
118 sub process_arguments(){
119         GetOptions
120                 ("V"   => \$opt_V, "version"    => \$opt_V,
121                  "v"   => \$opt_v, "verbose"    => \$opt_v,
122                  "h"   => \$opt_h, "help"               => \$opt_h,
123                  "w=i" => \$opt_w, "warning=i"  => \$opt_w,   # warning if above this number
124                  "c=i" => \$opt_c, "critical=i" => \$opt_c,       # critical if above this number
125                  "t=i" => \$opt_t, "timeout=i"  => \$opt_t 
126                  );
128         if ($opt_V) {
129                 print_revision($PROGNAME,'$Revision$ ');
130                 exit $ERRORS{'OK'};
131         }
133         if ($opt_h) {
134                 print_help();
135                 exit $ERRORS{'OK'};
136         }
138         if (defined $opt_v ){
139                 $verbose = $opt_v;
140         }
142         unless (defined $opt_t) {
143                 $opt_t = $utils::TIMEOUT ;      # default timeout
144         }
146         unless (  defined $opt_w &&  defined $opt_c ) {
147                 print_usage();
148                 exit $ERRORS{'UNKNOWN'};
149         }
151         if ( $opt_w >= $opt_c) {
152                 print "Warning cannot be greater than Critical!\n";
153                 exit $ERRORS{'UNKNOWN'};
154         }
156         return $ERRORS{'OK'};
159 sub print_usage () {
160         print "Usage: $PROGNAME [-w <warn>] [-c <crit>] [-t <timeout>] [-v verbose]\n";
163 sub print_help () {
164         print_revision($PROGNAME,'$Revision$');
165         print "Copyright (c) 2002 Subhendu Ghosh\n";
166         print "\n";
167         print_usage();
168         print "\n";
169         print "   Checks the number of messages in the mail queue\n";
170         print "   Feedback/patches to support non-sendmail mailqueue welcome\n\n";
171         print "-w (--warning)   = Min. number of messages in queue to generate warning\n";
172         print "-c (--critical)  = Min. number of messages in queu to generate critical alert ( w < c )\n";
173         print "-t (--timeout)   = Plugin timeout in seconds (default = $utils::TIMEOUT)\n";
174         print "-h (--help)\n";
175         print "-V (--version)\n";
176         print "-v (--verbose)   = deebugging output\n";
177         print "\n\n";
178         support();