#!/usr/bin/perl -wT
#
# check_qmailq.pl - nagios plugin 
# This plugin allows you to check the number of Mails in a qmail-
# queue. PLUGIN NEEDS CONFIGURATION ! (see below) 
#
# Copyright 2000 Benjamin Schmid
#
# 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#
# Emergency E-Mail :) blueshift@gmx.net
#

require 5.004;
use strict;
use lib utils.pm ;
use Plugin;
use Plugin::Parameter qw(:DEFAULT :standard :thresholds);

use vars qw($opt_t $PROGNAME $queuewarn $queuecrit $prewarn $precrit $statcommand);

use utils qw(%ERRORS);

$w_opt->binding(\$queuewarn);
$w_opt->default(5);
$w_opt->description("Warning generated if more than this number of messages are in the queue");
$c_opt->binding(\$queuecrit);
$c_opt->default(10);
$c_opt->description("Critical generated if more than this number of messages are in the queue");
my $W_opt = new Plugin::Parameter(-name => "prewarn", -flags => [ 'W', 'preq-warning' ],
				  -optional => "yes", -valueoptional => "no", -type => "INTEGER",
				  -binding => \$prewarn, -default => 1,
				  -description => "Warning generated if more than this number of messages are awaiting pre-processing");
my $C_opt = new Plugin::Parameter(-name => "precrit", -flags => [ 'C', 'preq-critical' ],
				  -optional => "yes", -valueoptional => "no", -type => "INTEGER",
				  -binding => \$precrit, -default => 5,
				  -description => "Critical generated if more than this number of messages are awaiting pre-processing");
my $comm_opt = new Plugin::Parameter(-name => "statcommand", -flags => [ 'command', 'stat-command' ],
				     -optional => "yes", -valueoptional => "no", -type => "PROGRAM",
				     -binding => \$statcommand, -default => "/var/qmail/bin/qmail-stat",
				     -description => "Command to run to get the queue statistics");
my $plugin = new Plugin(-revision => '$Revision: 1.1.1.1 $',
			-copyright => "2000 Benjamin Schmid, 2004 Howard Wilkinson <howard\@cohtech.com>",
			-shortcomment => "Mail Queue monitor for Qmail",
			-parameterlists => [ [ $w_opt, $c_opt, $W_opt, $C_opt, $comm_opt, $t_opt ], $h_opts, $V_opts ]);

use Carp;

$plugin->init();

my $state = "UNKNOWN";
my $answer = "";

$ENV{PATH} = "";
$ENV{BASH_ENV} = "";

$plugin->start_timeout($opt_t, "Time-out $opt_t s");

if (! open STAT, "$statcommand|") {
  print ("$PROGNAME $state: $statcommand returns no result!");
  exit $ERRORS{$state};
}
my @lines = <STAT>;
close STAT;

$plugin->stop_timeout();

# Mails in Queues
if ($lines[0]=~/^messages in queue: (\d+)/) {
  my $anzq = $1;
  $answer = $answer . "$anzq";
  $state='WARNING' if ($anzq >= $queuewarn);
  $state='CRITICAL' if ($anzq >= $queuecrit);
} else {
  $state='CRITICAL';
  $answer="Keine gueltigte Antwort (Zeile #1) von $statcommand\n";
}

# Unverarbeite Mails
if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
  my $anzp = $1;
  $answer = $answer . " E-Mail(s) nicht ausgeliefert, $anzp unverarbeitet.";
  $state='WARNING' if ($anzp >= $prewarn && $state eq 'UNKNOWN');
  $state='CRITICAL' if ($anzp >= $precrit);
} else {
  $state='CRITICAL';
  $answer=$answer . "Keine gueltigte Antwort (Zeile #2) von $statcommand\n";
}

$state = 'OK' if ($state eq 'UNKNOWN');

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

