[Nagiosplug-devel] check_qstat.pl plugin

Bryan Loniewski brylon at jla.rutgers.edu
Fri Apr 16 11:29:03 CEST 2004


Attached is a perl plugin based off of check_mailq, however it checks the
qmail queue using qmail-qstat.

Bryan Loniewski
-------------- next part --------------
#! /usr/local/bin/perl -w	 -- -*- Perl -*-

# check_qstat - check on the number of messages messages in the qmail queue

# based on check-mailq

# Initial version support sendmail's mailq command
# Modified to support qmail-qstat

# 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 $verbose $PROGNAME
	    $status $state $msg $msg_q );
use lib  "/usr/local/nagios/libexec" ;
use utils qw(%ERRORS &print_revision &support &usage );

# suppress strict warns about one time use :-(
$utils::PATH_TO_QSTAT = $utils::PATH_TO_QSTAT;
$utils::QSTAT_QUEUE_WARN = $utils::QSTAT_QUEUE_WARN;
$utils::QSTAT_QUEUE_CRIT = $utils::QSTAT_QUEUE_CRIT;
$utils::QSTAT_PRE_WARN = $utils::QSTAT_PRE_WARN;
$utils::QSTAT_PRE_CRIT = $utils::QSTAT_PRE_CRIT;

my $QSTAT = $utils::PATH_TO_QSTAT || "/var/qmail/bin/qmail-qstat";
my $queuewarn = $utils::QSTAT_QUEUE_WARN || 100;
my $queuecrit = $utils::QSTAT_QUEUE_CRIT || 500;
my $prewarn   = $utils::QSTAT_PRE_WARN || 20;
my $precrit   = $utils::QSTAT_PRE_CRIT || 50;
my $timeout   = $utils::TIMEOUT || 10;

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

$ENV{'PATH'}='/usr/bin:/bin';
$ENV{'BASH_ENV'}=''; 
$ENV{'ENV'}='';
$PROGNAME = "check_qstat";

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

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

## open mailq 
if ( defined $QSTAT && -x $QSTAT ) {
  if (! open(QSTAT, "$QSTAT 2>/dev/null |" ) ) {
    print "ERROR: could not open $QSTAT \n";
    exit $ERRORS{'UNKNOWN'};
  }
} else {
  print "ERROR: Could not find qstat executable $QSTAT!\n";
  exit $ERRORS{'UNKNOWN'};
}

use vars qw ($anzq $anzp);

my $answer = '';

while (<QSTAT>) {
  next if /^\s*$/;		# skip blank lines

  if (/^messages in queue: (\d+)/) { # Mails in Queues
    $anzq = $1;
    if ($anzq < $queuewarn) {
      $answer .= "OK: $anzq messages in queue, limits $queuewarn/$queuecrit ";
    } else {
      if ($anzq >= $queuecrit) {
	$answer = "CRITICAL: $anzq messages in queue, limit $queuecrit ";
      } else {
	$answer = "WARNING: $anzq messages in queue, limit $queuewarn ";
      }
    }
    next;
  }

  # unpreprocessed Mails
  if (/^messages in queue but not yet preprocessed: (\d+)/) {
    $anzp = $1;
    if ($anzp < $prewarn) {
      $answer .= "OK: $anzp messages not preprocessed, limits $prewarn/$precrit ";
    } else {
      if ($anzp >= $precrit) {
	$answer = "CRITICAL: $anzp messages not preprocessed, limit $precrit ";
      } else {
	$answer = "WARNING: $anzp messages not preprocesed, limit $prewarn ";
      }
    }
    next;
  }
  chomp;
  $answer .="unrecognised line output from $QSTAT '$_'\n";
}

close QSTAT;
if ($?) {
  print "ERROR: $QSTAT failed, return code $?\n";
  exit $ERRORS{'UNKNOWN'};
}

if (defined($anzq) and defined($anzp)) {
  if ($anzp >= $precrit or $anzq >= $queuecrit) {
    $state = 'CRITICAL';
  } elsif ($anzp >= $prewarn or $anzq >= $queuewarn) {
    $state = 'WARNING';
  } else {
    $state = 'OK';
  }
} else {
  $state = 'UNKNOWN';
}

print ("$answer\n");
exit $ERRORS{$state};

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


sub process_arguments(){
	GetOptions
		("V"   => \$opt_V, "version"	=> \$opt_V,
		 "v"   => \$verbose, "verbose"	=> \$verbose,
		 "h"   => \$opt_h, "help"		=> \$opt_h,
		 "w=i" => \$queuewarn, "queuewarning=i"  => \$queuewarn,
		 "W=i" => \$prewarn, "prewarning=i"  => \$prewarn,  
		 "c=i" => \$queuecrit, "queuecritical=i" => \$queuecrit,
		 "C=i" => \$precrit, "precritical=i" => \$precrit,
		 "t=i" => \$timeout, "timeout=i"  => \$timeout 
		 );

	if ($opt_V) {
		print_revision($PROGNAME,'$Revision: 1.1 $ ');
		exit $ERRORS{'OK'};
	}

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

	if ( $queuewarn >= $queuecrit) {
		print "ERROR: queue Warning($queuewarn) cannot be greater than Critical($queuecrit)!\n";
		exit $ERRORS{'UNKNOWN'};
	}
	if ( $prewarn >= $precrit) {
		print "ERROR: pre Warning($prewarn) cannot be greater than Critical($precrit)!\n";
		exit $ERRORS{'UNKNOWN'};
	}

	return $ERRORS{'OK'};
}

sub print_usage () {
  print "Usage: $PROGNAME [-w <queuewarn>] [-c <queuecrit>] [-W <prewarn>] [-C <precrit>] [-t <timeout>] [-h] [-v] [-V]\n";
}

sub print_help () {
  print_revision($PROGNAME,'$Revision: 1.1 $');
  print "Copyright (c) 2002 Subhendu Ghosh\n";
  print "\n";
  print_usage();
  print "\n";
  print "   Checks the number of messages in the mail queue\n";
  print "   Feedback/patches to support non-sendmail mailqueue welcome\n\n";
  print "-w (--queuewarning)  = Min. # of messages in queue for a warning\n";
  print "-c (--queuecritical) = Min. # of messages in queue for a critical alert ( w < c )\n";
  print "-W (--prewarning)    = Min. # of not preprocessed messages in queue to generate warning\n";
  print "-C (--precritical)   = Min. # of not preprocessed messages in queue to generate critical alert ( W < C )\n";
  print "-t (--timeout)       = Plugin timeout in seconds (default = $utils::TIMEOUT)\n";
  print "-h (--help)\n";
  print "-V (--version)\n";
  print "-v (--verbose)       = debugging output\n";
  print "\n\n";
  support();
}


More information about the Devel mailing list