1

Is there a way to monitor the postfix mail queue using monit? the available scripts just check, up/down/memory or CPU. I would like notices when the queue starts filling up. it would be nice to be able to set monitors on the different queues to be able to react appropriately.

Sean Kimball
  • 877
  • 1
  • 8
  • 23

2 Answers2

6

You have to use the "program" feature of Monit.

If your monitrc file includes include /etc/monit.d/*.cfg, then in your /etc/monit.d, create a .cfg file with the content (add the appropriate include statement in your monitrc file if you don't include all *.cfg files in /etc/monit.d)

check program mail-queue path "/usr/local/sbin/check_postfix_queue"
    if status != 0 then alert

Then create the script /usr/local/sbin/check_postfix_queue with this content:

#!/bin/bash

MAXMSG=20
MSG=$( postqueue -p | egrep '\-\- [0-9]+ Kbytes in [0-9]+ Request[s]*\.' | awk '{ print $5 }'  )
[ ${MSG:-0} -le $MAXMSG ] && exit 0 || exit 1

MAXMSG is the message queue limit: if number of messages is above that parameter, monit emits an alert.

austinian
  • 1,699
  • 2
  • 15
  • 29
Miguel Bonera
  • 76
  • 1
  • 4
0

Here is my version of check_postfix_queue script. It does not call mailq command, because it reads and lists all messages from all queues, but we want to know just the number of messages.

It also shows number of messages per queue.

#!/bin/bash
# usage: mailq-check.sh [<max-count>]

TOTAL=0

for QUEUE in maildrop hold incoming active deferred; do
    CNT=$(find /var/spool/postfix/$QUEUE -type f | wc -l)
    TOTAL=$(( TOTAL + CNT ))
    echo $QUEUE: $CNT
done

echo total: $TOTAL

if [ -n "$1" ] && [ "$TOTAL" -gt "$1" ]; then
    exit 1
fi

exit 0

Optional parameter can set max. number of messages in all queues that triggers error.

You can use this script in monit as mentioned in the first answer:

check program mail-queue path "/usr/local/sbin/check_postfix_queue 50"
    if status != 0 then alert