Wednesday, November 16, 2011

check exim mail queue with nagios

Standart nagios plugin for checking mail queue  (/usr/lib/nagios/plugins/check_mailq) doesn't allow to monitor exim queue .. it always return "OK" status  and report that queue is empty

for example:

# exim -bpc 12
and


# /usr/lib/nagios/plugins/check_mailq -w 500 -c 1000 OK: mailq is empty|unsent=0;500;1000;0


so, we need write simple shell script, that allow us to monitor exim4 mail queue with nagios.
name it check_exim_mailqueue.sh and drop it into /usr/local/bin/ directory.



#!/bin/sh
###############################################
#
# Nagios script to check Exim mail queue status
#
# Copyright 2007, 2008 Ian Yates
#
# NOTE: Depending on your config, the nagios user will probably be 
#       needed to be added to the exim group for this script to function correctly
# 
###############################################
. /usr/lib/nagios/plugins/utils.sh
EXIM=/usr/sbin/exim
FLAG_VERBOSE=FALSE
LEVEL_WARN=""
LEVEL_CRIT=""
RESULT=""
EXIT_STATUS=$STATE_OK
###############################################
#
## FUNCTIONS 
#
## Print usage
usage() {
 echo " check_eximailqueue $VERSION - Nagios Exim mail queue check script"
 echo ""
 echo " Usage: check_eximailqueue -w  -c  [ -v ] [ -h ]"
 echo ""
 echo "   -w  Queue size at which a warning is triggered"
 echo "   -c  Queue size at which a critical is triggered"
 echo "   -v  Verbose output (ignored for now)"
 echo "   -h  Show this page"
 echo ""
}
 
## Process command line options
doopts() {
 if ( `test 0 -lt $#` )
 then
  while getopts w:c:vh myarg "$@"
  do
   case $myarg in
    h|\?)
     usage
     exit;;
    w)
     LEVEL_WARN=$OPTARG;;
    c)
     LEVEL_CRIT=$OPTARG;;
    v)
     FLAG_VERBOSE=TRUE;;
    *) # Default
     usage
     exit;;
   esac
  done
 else
  usage
  exit
 fi
}
# Write output and return result
theend() {
 echo $RESULT
 exit $EXIT_STATUS
}
#
## END FUNCTIONS 
#
#############################################
#
## MAIN 
#
# Handle command line options
doopts $@
# Do the do
OUTPUT=`$EXIM -bpc`
if test -z "$OUTPUT" ; then
 RESULT="Mailqueue WARNING - query returned no output!"
 EXIT_STATUS=$STATE_WARNING
else
 if test "$OUTPUT" -lt "$LEVEL_WARN" ; then
  RESULT="Mailqueue OK - $OUTPUT messages on queue"
  EXIT_STATUS=$STATE_OK
 else
  if test "$OUTPUT" -ge "$LEVEL_CRIT" ; then 
   RESULT="Mailqueue CRITICAL - $OUTPUT messages on queue"
   EXIT_STATUS=$STATE_CRITICAL
  else
   if test "$OUTPUT" -ge "$LEVEL_WARN" ; then 
    RESULT="Mailqueue WARNING - $OUTPUT messages on queue"
    EXIT_STATUS=$STATE_WARNING
   fi
  fi
 fi
fi
# Quit and return information and exit status
theend

original script is there http://exchange.nagios.org/directory/Plugins/Email-and-Groupware/Exim/check_eximailqueue/details // 
We modify it to work in debian and ubuntu/// and we threw unnecessary sudo usage.

All you need : add nagios user to Debian-exim group in /etc/group:
Debian-exim:x:118:nagios

than do this script executable:
# chmod u+x /usr/local/bin/check_exim_mailqueue.sh# chown  nagios:root  /usr/local/bin/check_exim_mailqueue.sh

now you can use this script in nagios-nrpe-server: define new command in nrpe configuration file (nrpe.conf ):
command[check_exim_mailq]=/usr/local/bin/check_exim_mailqueue.sh -w 500 -c 1000

# /etc/init.d/nagios-nrpe-server restart
or you an use it directrly with nagios: create new command in nagios config file.
define command{ command_name check_exim_mailq command_line /usr/local/bin/check_exim_mailqueue.sh -w $ARG1$ -c $ARG2$ }
# /etc/init.d/nagios reload

5 comments:

Rhn said...
This comment has been removed by the author.
Anonymous said...

hi,

It's giving error as (No output returned from plugin)

Володя said...

You've added exim-user group nagios?

Володя said...

what output if You run (as root)
/usr/local/bin/check_exim_mailqueue.sh -w 500 -c 1000 ?

Anonymous said...

cPanel Admin