8

I want to try to monitor postfix queue with monit. I have taken a example from people of Stackoverflow. My version of monit is the latest

This is Monit version 5.10

In /etc/monit.d I have postfixlocal with

check program postfixcola with path "/usr/local/bin/postfixcola.sh"
    #if status != 0 then alert
    if status > 1 then alert

and then in /usr/local/bin/ I have postfixcola.sh with

#!/bin/sh
QUEUE=`/usr/sbin/postqueue -p | tail -n1 | awk '{print $5}'`
exit $QUEUE

But Monit complaints every time about the output of the script. I have made a test, setting and echo before exit and in the moment of test it returned a 1 (because the queue of postfix was 1)

But the error remains: [CET Dec 9 11:10:07] error : 'postfixcola' '/usr/local/bin/postfixcola.sh' failed with exit status (2) -- no output

I really don't know what is the problem here, any thoughts?

chicks
  • 2,393
  • 3
  • 24
  • 40
Rubendob
  • 1,614
  • 3
  • 22
  • 34

2 Answers2

1

In your bash script you should echo a message into /dev/stderr:

#!/bin/bash
QUEUE=`/usr/sbin/postqueue -p | tail -n1 | awk '{print $5}'`
if [ $QUEUE -ne 0 ] ; then
   echo "Queue length > $QUEUE" > /dev/stderr
fi
exit $QUEUE
Alain Beauvois
  • 5,896
  • 3
  • 44
  • 26
0

The first answer of this question helped me a lot, custom Monit bash script (program) should echo a message into /dev/stderr.

But according to my experience, Monit will only alert you, if exit code of your script changed, so you have to set threshold in your custom script.

In my case I'm using nf_conntrack_count, but for postqueue it will be very similar.

/etc/monit/conf.d/server

check program postfixcola with path "/bin/bash -c /usr/local/bin/postfixcola.sh"
    if status > 0 then alert

/usr/local/bin/postfixcola.sh

#!/bin/sh
QUEUE=`/usr/sbin/postqueue -p | tail -n1 | awk '{print $5}'`

echo $QUEUE > /dev/stderr
if [ $QUEUE -gt 1000 ] ; then
   exit 1
fi

exit

Monit command monit status postfixcola output should look like:

Program 'postfixcola'
  status                       Status ok
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  last exit value              0
  last output                  675
  data collected               Sat, 29 Sep 2018 17:22:36

In last output you can see value and in last exit value 0. So if your custom script reaches threshold 1000 emails in queue, exit code will change to 1 and Monit will alert you.

janfai
  • 1