Parse Plesk Maillog, Count Emails Per Domain Print

  • 2

To get a quick count of messages being sent to and from all my domains in Plesk.   Use this script to get the mail domains from the Plesk database.  Then I loop through them and grep a regex against the maillog file.  Since I’m actually using zgrep to look decompress yesterday’s maillog file at the same time I get the counts, it’s a little processor intensive.  It takes about 10 seconds to run through 30 domains in a 5 meg gzipped file.  If I were to gunzip the file first and just grep it, this would go much quicker.  But I’m fine with a 10 second run time and the bonus of not having to trash a decompressed maillog file when I’m done.

Here’s the bash script I’ve saved as mailcount.sh:

#!/bin/sh

MYSQLPASS=`cat /etc/psa/.psa.shadow`

for DOMAIN in `mysql -uadmin -p$MYSQLPASS -e "select distinct domains.name from mail inner join domains on mail.dom_id=domains.id" -B -N psa`

do
echo $DOMAIN `zgrep -c -E "(to|from)=.+@$DOMAIN" /usr/local/psa/var/log/maillog.processed.1.gz`
done

Here’s a version that copies and gunzip’s the maillog before doing any processing.

#!/bin/sh

MYSQLPASS=`cat /etc/psa/.psa.shadow`

# copy the log file before decompress
cp /usr/local/psa/var/log/maillog.processed.1.gz /usr/local/psa/var/log/maillog.processed.1-working.gz &&

# decompress the log working file
gunzip /usr/local/psa/var/log/maillog.processed.1-working.gz

for DOMAIN in `mysql -uadmin -p$MYSQLPASS -e "select distinct domains.name from mail inner join domains on mail.dom_id=domains.id" -B -N psa`
do
echo $DOMAIN `grep -c -E "(to|from)=.+@$DOMAIN" /usr/local/psa/var/log/maillog.processed.1-working`
done

 remove the working log file
rm -f /usr/local/psa/var/log/maillog.processed.1-working &&
echo "file removed"
exit;

Courtesy: Matt MacDougall

Was this answer helpful?

« Back