Plesk & Server Architecture Matt | 12 Nov 2008
Parse Plesk Maillog, Count Emails Per Domain
I wanted to get a quick count of messages being sent to and from all my domains in plesk. I wrote 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