Code

check_dbi: Added simple regex example to help output.
[nagiosplug.git] / plugins-scripts / check_mailq.pl
index 23bc521fa31732536e4f382681fc0b7aac434c0e..31eb46a4d5aa93da85f39e07e25b93f8930e6c39 100755 (executable)
@@ -22,6 +22,7 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 #
+#
 ############################################################################
 
 use POSIX;
@@ -171,7 +172,7 @@ if ($mailq eq "sendmail") {
                    # multi queue: one for each queue. overwrite on multi queue below
                  $msg_q = $1 ;
                        }
-               } elsif (/^\s+Total\sRequests:\s(\d+)$/) {
+               } elsif (/^\s+Total\sRequests:\s(\d+)$/i) {
                        print "$utils::PATH_TO_MAILQ = $_ \n" if $verbose ;
                        #
                        # multi queue: last line
@@ -184,12 +185,10 @@ if ($mailq eq "sendmail") {
        ## close mailq
 
        close (MAILQ); 
-       # declare an error if we also get a non-zero return code from mailq
-       # unless already set to critical
+
        if ( $? ) {
-               $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"}  ;
-               print "STDERR $?: $!\n" if $verbose;
-               $msg = "$state: (stderr)\n";
+               print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
+               exit $ERRORS{CRITICAL};
        }
 
        ## shut off the alarm
@@ -293,6 +292,88 @@ if ($mailq eq "sendmail") {
        }
 
 } # 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 
@@ -315,12 +396,10 @@ elsif ( $mailq eq "qmail" ) {
 
        # close qmail-qstat
        close MAILQ;
-       # declare an error if we also get a non-zero return code from mailq
-       # unless already set to critical
+
        if ( $? ) {
-               $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"}  ;
-               print "STDERR $?: $!\n" if $verbose;
-               $msg = "$state: (stderr)\n";
+               print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
+               exit $ERRORS{CRITICAL};
        }
 
        ## shut off the alarm
@@ -378,11 +457,51 @@ elsif ( $mailq eq "qmail" ) {
 
 
 } # 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 |mailq=$msg_q\n";
+print "$msg|unsent=$msg_q;$opt_w;$opt_c;0\n";
 exit $state;
 
 
@@ -398,13 +517,11 @@ sub process_arguments(){
                 "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
-                "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,'$Revision$ ');
+               print_revision($PROGNAME,'@NP_VERSION@');
                exit $ERRORS{'OK'};
        }
 
@@ -442,7 +559,7 @@ sub process_arguments(){
        }
 
        if (defined $opt_M) {
-               if ($opt_M =~ /sendmail/ || $opt_M =~ /qmail/ ) {
+               if ($opt_M =~ /^(sendmail|qmail|postfix|exim)$/) {
                        $mailq = $opt_M ;
                }elsif( $opt_M eq ''){
                        $mailq = 'sendmail';
@@ -458,11 +575,11 @@ sub process_arguments(){
 }
 
 sub print_usage () {
-       print "Usage: $PROGNAME [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>] [-M <MTA>] [-t <timeout>] [-v verbose]\n";
+       print "Usage: $PROGNAME -w <warn> -c <crit> [-W <warn>] [-C <crit>] [-M <MTA>] [-t <timeout>] [-v verbose]\n";
 }
 
 sub print_help () {
-       print_revision($PROGNAME,'$Revision$');
+       print_revision($PROGNAME,'@NP_VERSION@');
        print "Copyright (c) 2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid\n";
        print "\n";
        print_usage();
@@ -470,11 +587,11 @@ sub print_help () {
        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 queu to generate critical alert ( w < c )\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 ] (default = sendmail)\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";