Code

Disable dynamic versionning for releases, and allow generating version out of subversion.
[nagiosplug.git] / plugins-scripts / check_mailq.pl
index 0505207b04e6ffd115e19fd2b13f8e70552e5e1b..95ef2ce10514094a84be9145168123ae17465037 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
@@ -316,12 +315,10 @@ elsif ( $mailq eq "postfix" ) {
 
         # 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
@@ -399,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
@@ -462,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;
 
 
@@ -482,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'};
        }
 
@@ -526,7 +559,7 @@ sub process_arguments(){
        }
 
        if (defined $opt_M) {
-               if ($opt_M =~ /sendmail/ || $opt_M =~ /qmail/ || $opt_M =~ /postfix/ ) {
+               if ($opt_M =~ /^(sendmail|qmail|postfix|exim)$/) {
                        $mailq = $opt_M ;
                }elsif( $opt_M eq ''){
                        $mailq = 'sendmail';
@@ -542,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();
@@ -558,7 +591,7 @@ sub print_help () {
        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 ] (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";