Detecting server email abuse for qmail service

A very simple script that you can use to detect email overuse when qmail is being used for email delivery.

#!/bin/bash
num=`/var/qmail/bin/qmail-qstat | grep -P -o "[0-9]+" | head -2 | awk '{ sum += $1 } END { print sum }'`
if [ $num -ge $1 ]
then
mail -s "Email overuse on `hostname`" "$2" << END_MAIL
Email Overuse on `hostname`
Pending Emails: $num
END_MAIL
fi

Command line parameters

  1. $1 is the number of emails you want to permit. e.g. you may want to have 20 emails in the queue of qmail. In this case If you set $1 to 20 it will not raise overuse email if qmail queue has 20 emails at the time of check.
  2. $2 is the email id where you want email to be sent.

You can also set this as an hourly job in cron.

A sample for hourly cron job is

1 * * * * /path_to_script/detectEmailOveruse.sh 100 test@test.com

Note: We have used this script on our centos server, where it is working beautifully.