#!/usr/bin/perl -w
#
# check_qmailq - plugin to check the length of qmail mail queue.
#   Based on check_qmailq.pl, by Benjamin Schmid, Copyright 2000.
#   
# This plugin must either be run as root, or you can add the nagios user 
#   to the 'qmail' group.
#

use strict;
use File::Basename;
BEGIN {
  use lib dirname($0);
  use Nagios::Plugin;
}

my $VERSION = '0.1 [http://www.openfusion.com.au/labs/nagios/]';

my ($warning, $critical);
my $warn_unprocessed = 1;
my $crit_unprocessed = 5;
my $qmail_qstat = '/var/qmail/bin/qmail-qstat';
my $USAGE = qq(usage: %s -w count -c count [-W pre_count] [-C pre_count]
    [-q /path/to/qmail-qstat]\n); 
my @SPEC = (
  "warning|w=i" => [ \$warning, 
  q(-w, --warning=INTEGER
   Exit with WARNING status if qmail queue is longer than INTEGER emails),
    'REQUIRED' ],
  "critical|c=i" => [ \$critical, 
  q(-c, --critical=INTEGER
   Exit with CRITICAL status if qmail queue is longer than INTEGER emails),
    'REQUIRED' ],
  "warning-preprocessed|W=i" => [ \$warn_unprocessed, 
  qq(-W, --warning-preprocessed=INTEGER
   Exit with WARNING status if un-preprocessed qmail queue is longer than 
     INTEGER emails. Default: $warn_unprocessed.) ],
  "critical-preprocessed|C=i" => [ \$crit_unprocessed, 
  qq(-C, --critical-preprocessed=INTEGER
   Exit with CRITICAL status if un-preprocessed qmail queue is longer than 
     INTEGER emails. Default: $crit_unprocessed.) ],
  "qmail-qstat|q=s" => [ \$qmail_qstat,
  q(-q, --qmail-qstat=/path/to/qmail-qstat
   Path to qmail-qstat. Default: /var/qmail/bin/qmail-qstat.) ],
);
my $extra = qq(
Note that this plugin should either run as root (e.g. using sudo), or you 
should add the nagios user to the 'qmail' group.);

parse_args(
  version => $VERSION,
  usage => $USAGE,
  spec => \@SPEC,
  help_extra => $extra,
);

exit("UNKNOWN", "cannot find $qmail_qstat") 
  unless -f $qmail_qstat && -x $qmail_qstat;

# Do the qmail-qstat
alarm($timeout);
exit("UNKNOWN", "No output from $qmail_qstat") 
  unless open QSTAT, "$qmail_qstat|";
my @lines = <QSTAT>;
chomp foreach @lines;
close QSTAT;

my (@crit, @warn);

# Messages in queue
if ($lines[0] =~ /^messages in queue: (\d+)/) {
  push @crit, $lines[0] if $1 >= $critical;
  push @warn, $lines[0] if $1 >= $warning;
} else {
  push @crit, "weird line 1 response from $qmail_qstat: " . $lines[0] || "''";
}

# Messages not yet preprocessed
if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
  push @crit, $lines[1] if $1 >= $crit_unprocessed;
  push @warn, $lines[1] if $1 >= $warn_unprocessed;
} else {
  push @crit, "weird line 2 response from $qmail_qstat: " . $lines[1] || "''";
}

exit_results(CRITICAL => \@crit, WARNING => \@warn, OK => \@lines);


# arch-tag: ccb70943-0ea1-4af4-b344-a83dcce0c9e4
# vim:ft=perl:sw=2
