Pixelapes
Web and Graphic Design and Hosting

 

Generate statistics for individual virtual domains using Postfix and Awstats

Full Article

postfix

If you are utilizing Postfix and hosting virtual domains it’s likely you will want an overview of inbound and outbound email activity pre domain.

This sort of statistical information can be of huge benefit when running your own server, especially if you need to get an idea of where you bandwidth consumption is going.

Linking Awstats to Postfix

Awstats Log Analyzer

You can achieve this by using AWStats, but first you must run a few scripts in order to present the log file in a format that AWStats can work with. If you are using Amavis and/or Spamassassin, you will notice additional lines similar to these appear due to re-injection:

Apr 14 19:47:58 mailserver amavis[23740]: (23740-02) FWD via SMTP:  -> , 250 2.6.0 Ok, id=23740-02, from MTA([127.0.0.1]:10025): 250 Ok: queued as DAB321264FE
Apr 14 19:47:58 mailserver amavis[23740]: (23740-02) Passed CLEAN, [192.168.0.10] [192.168.0.10]  -> , Message-ID: <49E4D9FA.4020100@receiverdomain.com>, mail_id: J9-AzDhzWfe0, Hits: -, 160 ms

In order to avoid this, you must use prepflog to process the mail log before it is passed to pflogsumm or AWStats. This script will disregard the lines relating to the re-injection of messages into Postfix.

Once this is completed you will need to use maillogconvert. This is a mail log preprocessor that converts a mail log file (from postfix, sendmail or qmail servers) into a human readable format. The output format is also ready to be used by a log analyzer, like AWStats. The maillogconvert script is provided in the AWStats tools directory.

So let’s look at the configuration. Prepflog documentation says to set the following in the AWStats configuration file:

LogFile="/usr/local/bin/prepflog.pl < /var/log/mail | perl /usr/local/awstats/tools/maillogconvert.pl standard |"

However, we wish to only grab mail from one domain so I altered the setting to this:

LogFile="cat /var/log/mail.log | /etc/awstats/prepflog.pl | perl /etc/awstats/maillogconvert.pl standard | grep interestingdomain.com |"

After the log is striped of unnecessary re-injection entries, the specific domain is grepped, it is then pushed to maillogconvert.pl which outputs a friendly format as follows:

2009-04-14 20:38:03 sender@senderdomain.com receiver@receiverdomain.com 192.168.0.10 - SMTP - xxx ?

You must ensure that the AWStats configuration file is set-up to parse mail logs. I followed the formatting suggested by the AWStats documentation. Your configuration file awstats.mail.interestingdomain.com.conf will now look similar to this:

LogFile="cat /var/log/mail.log | /etc/awstats/prepflog.pl | perl /etc/awstats/maillogconvert.pl standard | grep interestingdomain.com |"
LogType=M
LogFormat="%time2 %email %email_r %host %host_r %method %url %code %bytesd"
LogSeparator=" "
SiteDomain="interestingdomain.com"
HostAliases="interestingdomain.com"
LogType=M
LevelForBrowsersDetection=0
LevelForOSDetection=0
LevelForRefererAnalyze=0
LevelForRobotsDetection=0
LevelForWormsDetection=0
LevelForSearchEnginesDetection=0
LevelForFileTypesDetection=0
ShowMenu=1
ShowSummary=HB
ShowMonthStats=HB
ShowDaysOfMonthStats=HB
ShowDaysOfWeekStats=HB
ShowHoursStats=HB
ShowDomainsStats=0
ShowHostsStats=HBL
ShowAuthenticatedUsers=0
ShowRobotsStats=0
ShowEMailSenders=HBML
ShowEMailReceivers=HBML
ShowSessionsStats=0
ShowPagesStats=0
ShowFileTypesStats=0
ShowFileSizesStats=0
ShowBrowsersStats=0
ShowOSStats=0
ShowOriginStats=0
ShowKeyphrasesStats=0
ShowKeywordsStats=0
ShowMiscStats=0
ShowHTTPErrorsStats=0
ShowSMTPErrorsStats=1

It is important to remember that you will need to set up a crontab entry to process the mail log. Your crontab entry should look similar to this:

/usr/bin/perl /usr/lib/cgi-bin/awstats.pl -update -config=mail.interestingdomain.com

Finally, below is my configuration for Apache 2:

###### AWSTATS ######
Alias /awstatsclasses "/usr/share/awstats/lib/"
Alias /awstats-icon/ "/usr/share/awstats/icon/"
Alias /awstatscss "/usr/share/doc/awstats/examples/css"
ScriptAlias /awstats/ /usr/lib/cgi-bin/
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch

Now you will see a tidy graphical view of all mail traffic for your virtual domains using AWStats. If you have any questions or comments on how you go about generating stats, please fill in the comment form below.

Generate statistics for individual virtual domains using Postfix and Awstats: 3 Comments

Subscribe to comment feed

1. Todd
5:34 pm
April 28th, 2009

Thanks for a good up to date article on awstats and postfix!

2. Gabriel
9:51 pm
August 31st, 2009

Just one thing, in my scenario I have a lot of domains and some of them might have no message in a single day. What I’m seeing is that awstats reports an error if the command LogFile doesnt return no lines. Is there a way to change this ? Or it will always report an error if the command return no lines? For the domains that I have a lot of traffic the report is being generated normally.
Thanks for the great post!!

3. Warren
2:30 am
September 1st, 2009

Thank you for the feedback.
Can you please send me a copy of the error?
I am testing on server here to see if I can recreate your error and fix it.

Leave Your Comment

  1.  (required)
  2.  (will not be published) (required)

 

Web Design and Web Hosting - Pixelapes, Aughadowry, Ballinamuck, Co. Longford, Ireland.

Phone: +35376 6024239. Email: info@pixelapes.com