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.
2 Answers
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.
![](../../users/profiles/220834.webp)
- 1,699
- 2
- 15
- 29
![](../../users/profiles/304309.webp)
- 76
- 1
- 4
-
very similar to the qmail process/script – Sean Kimball Aug 11 '15 at 19:40
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
![](../../users/profiles/447583.webp)
- 41
- 1