In case your server gets listed on blacklists, mails will get refused by destination servers and stick in the deferred mail queue for some time until the sender finally gets a mailer daemon.
As it takes some time until the sender gets the mailer daemon and informs the server admin, it would be better if you could directly get notified by Icinga/Nagios when a mail is in the deferred queue because the destination server refused it.
Therefore I wrote a small shell script which I want to share with you here. I am assuming Debian Wheezy with Icinga and a postfix mailserver.
Create the shell script with the actual plugin in
# detects if mails in mail queue were refused by destination server (because of blacklist?)
# From http://blog.christosoft.de/2014/08/icinga-monitor-refused-mails-postfix-mailqueue/
# Version: 2017-03-07
if mailq | grep -qP "(refused to talk to me(?!(.*out of connection slots)))|(unsolicited mail originating from your IP)|(temporarily deferred due to user complaints)"
mails=`mailq | grep -oP "(refused to talk to me(?!(.*out of connection slots)))|(unsolicited mail originating from your IP)|(temporarily deferred due to user complai$
echo "$mails mail(s) were refused, check mailq!"
if [ "$mails" -le 10 ] && [ "$mails" -gt 1 ]; then
# 2-10 mails -> warning
echo "\nWarning. | refused=$mails;2;11;0"
if [ "$mails" -gt 10 ]; then
# more than 10 mails -> critical
echo "\nCriticial! | refused=$mails;2;11;0"
echo "Ok, there seems to be no refused mail in the mailq | refused=0;2;11;0"
This will check for the texts “refused to talk to me” (not followed by “out of connection slots”) and “unsolicited mail originating from your IP” in the mailq output. These are the most common errors you get when the destination server has your server’s IP blacklisted. In case at least one mail was refused, this causes a warning state in icinga. If more than 10 mails were refused, it causes a critical state.
Now you need to make this script executable:
chmod +x /usr/lib/nagios/plugins/check_mailq_blacklist
Now create the config file for the plugin in
# 'check_mailq_blacklist' command definition
So now we have the command and need to define a service that uses it. Let’s say we use this locally for localhost. In
service_description Mail Queue Refused Mail
This is it, just restart icinga and you are done:
service icinga restart
I hope this is of use to somebody.
Of course it is also useful to monitor in Icinga, if you are on some of the most used blacklists. A script to do this can be found here.