Monitor Postfix Mail Queue

Scott Miller srmiller at interbel.net
Mon Sep 28 23:46:23 CEST 2009


I'm trying to get Nagios to monitor a local Postfix Mail Queue, with no
luck.  The service keeps reporting OK: Postfix mailq has 0 messages queued,
when I know for a fact there are messages in the queue.  I'm wanting to use
Nagios on this local machine to monitor the queue, and alarm/notify when it
reaches a certain threshold.  I have modified the check_mailq to the
following: (retrieved from
http://wiki.osuosl.org/display/LNX/Nagios+Postfix+Mailq+Plugin )


##########################################
#
#! /usr/bin/perl -w

# Copyright (c) 2002 Oregon State University - Network Engineering
# All rights reserved.
#
# $Id: check_postfix_mailq,v 1.2 2004/11/18 21:01:58 kveton Exp $
# $Source: /data/cvsroot/eng/nagios/check_postfix_mailq,v $

# This is a Nagios monitoring plugin that will check the status of 
#   the Postfix mailq on the local machine.  It is best used with
#   something like NRPE for remote access.

BEGIN {
	if ($0 =~ m/^(.*?)[\/\\]([^\/\\]+)$/) {
		$PROGNAME = $2;
	}
}

require 5.004;
use POSIX;
use strict;
use Getopt::Long;
use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c $opt_t
$status $state $msg $msg_q);
use lib "/usr/local/nagios/libexec" ;
use utils qw(%ERRORS &print_revision &usage &support);

use Data::Dumper;

# function prototypes
sub print_help ();
sub print_usage ($);
sub help ();
sub version ();
sub get_version ();

# paranoia central
delete @ENV{'PATH', 'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
$PROGNAME = "check_mailq";

Getopt::Long::Configure('bundling', 'no_ignore_case');
GetOptions
	("V|version"    => \&version,
	 "h|help"       => \&help,
	 "w|warning=s"  => \$opt_w,
	 "c|critical=s" => \$opt_c);

# set the defaults for warn
($opt_w) || ($opt_w = shift) || ($opt_w = 2000);
my $warning = $1 if ($opt_w =~ /([0-9]+)/);

# set the defaults for critical
($opt_c) || ($opt_c = shift) || ($opt_c = 5000);
my $critical = $1 if ($opt_c =~ /([0-9]+)/);

# for the final processing of the output
my $state  = 'UNKNOWN';
my $answer = 'Could not initialize tests.';

# Just in case of problems, let's not hang NetSaint
$SIG{'ALRM'} = sub {
	print ("ERROR: No response from postfix mailq (alarm)\n");
	exit $ERRORS{"UNKNOWN"};
};
alarm(30);

# let's get to it
# my $result = `/usr/sbin/postqueue -p`;

# Do it more lightweight
my $result = `ls -1 /var/spool/postfix/deferred/*/* 2>/dev/null | wc -l`;

#if ( $result =~ /-- (\d+) Kbytes in (\d+) Request[s]*\./ ) {

if ( $result =~ /^(\d+)$/ ) {
      if ( $1 > $critical ) {
              $state = 'CRITICAL';
      }
      elsif ( $1 > $warning ) {
		$state = 'WARNING';
	}
	else {
		$state = 'OK';
	}

	#$answer = "Postfix mailq has $2 messages queued for a total of $1
Kbytes.";
	$answer = "Postfix mailq has $1 messages queued.";

}
elsif ( $result =~ /Mail queue is empty/ ) {
	$state = 'OK';
	$answer = 'Mail queue is empty';
}
else {
	$state = 'CRITICAL';
	$answer = "Could not connect to local mailq.  Postfix may be down.";
}

if ( $state eq 'CRITICAL' or $state eq 'WARNING' or $state eq 'UNKNOWN' ) {
        print "$state: $answer\n";
}
else {
        print "OK: $answer\n";
}

exit $ERRORS{$state};

#
############################################################3
#
############################################################3
# Standard Netsaint Support Functions
sub print_usage ($) {
	my ($error) = @_;

	print "Usage: $PROGNAME [--help|h] [-w <warn>] [-c <crit>]\n";

	if ( $error ) {
		print "$error\n";
		exit $ERRORS{'UNKNOWN'};
        }
}

sub get_version () {
	return '$ Revision: 1.0.0 $';
}

sub print_help () {
	my $version = get_version();
	print "\nCopyright (c) 2002 Oregon State University -- $PROGNAME,
$version\n";
	print_usage('');
	my $usage = qq{
		-w	Warning service page threshold.
		-c	Critical service page threshold.
		-h	This message.

};
	print $usage;
}

sub version () {
	print_revision($PROGNAME, get_version() );
	exit $ERRORS{'OK'};
}

sub help () {
	print_help();
	exit $ERRORS{'OK'};
}
#
#
############################################################

I did modify one line above - because it was reporting 0 messages in queue.
I changed:

my $result = `ls -1 /var/spool/postfix/deferred/?/* 2>/dev/null | wc -l`;
to read:
my $result = `ls -1 /var/spool/postfix/deferred/*/* 2>/dev/null | wc -l`;

Still - it reports (OK: Postfix mailq has 0 messages queued), when at this
time there are 9 messages in queue.

Anyone have any luck implementing this with postfix?  If so, can anyone see
what I'm doing wrong?  Or if anyone has a better way of doing this, I'd be
happy to hear about it.

Thanks,
Scott Miller



------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Nagios-users mailing list
Nagios-users at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting any issue. 
::: Messages without supporting info will risk being sent to /dev/null





More information about the Users mailing list