Wednesday, April 9, 2008

Monitoring mail queue size with monit

The monit project is really useful in many ways. But what I am missing now is a simple method how to test conditions on some output produced by a script or a command. For example, I would like to trigger an alarm when the mail queue size exceeds a defined limit. I'm not sure where but somewhere I found that the feature should be implemented in the future release of monit. I'm hoping.

For now, you can bypass the problem with a simple helper script. It needs to be scheduled via cron daemon in order to check the mail queue size regularly. If the size exceeds the specified limit it will write the size to a helper file whose checksum will be monitored. The size is written only once to avoid repeated alarms. So the script can look like this:




[ -f $CHECK_FILE ] || echo 0 > $CHECK_FILE

CUR_SIZE=`mailq | tail -n 1 | awk '{ print($5); }'`

if [ $CUR_SIZE -gt $MAX_SIZE ]; then
if [ ! -f $LOCK_FILE ]; then
echo > $LOCK_FILE
rm -f $LOCK_FILE

exit 0

Then you can place the script e.g. to the /etc/cron.hourly directory or you can specify your own schedule and define it with own crontab in the /etc/cron.d directory. Further, you can specify the limit as its first command line argument. Now, let's check the monit configuration:

check file mailq with path /var/run/mailq.size
if changed checksum
then alert

The above script is almost an one liner. It is simple and it is working. Unfortunately, I would like to use mailq command directly in the monit configuration.

No comments: