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.

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

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/ 100

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