Complete Virtual Mail Server/awstats

Introduction
Even though postfix provides logs, having them graphically displayed and analyzed can give much more insight. is a popular log analyzer that can parse logs and setup proper results.

Installation
AWStats is a web-application but no longer relies on webapp-config. If it hasn't emerged already, it needs to be emerged.

Once installed, a config file needs to be created, either per domain, or one that handles all domains.

AWStats
AWStats comes with reasonable defaults, but some need to be changed nevertheless.

For one, awstats assumes that vhosts aren't used. When using apache's default combined vhost logs for example, awstats will fail to run.

Assuming apache's combined LogFormat is setup as follows. Apache's LogFormat

The following changes needs to be made.

Next awstats needs to know about the domains and aliases to filter from the log file.

Also, awstats needs to store its database somewhere. Gentoo has created /var/lib/awstats for this use, but can be stored anywhere. Make sure the permissions are set so that the apache user can write to it.

Any other changes to the configuration file are optional, but interesting to look into.

Logging
awstats needs to process the apache logfile to build its database. Once confirmed that it is working manually it can be automated.

Manually
First, awstats should be run from the console, to spot any initial errors.

This should list any issues and missing domain names from the config.

Cron
If everything is working perfectly, it can then be added to cron.hourly.

Remember to make the script executeable if needed.

Logrotate
Awstats will process the log file every hour, but when logrotate rotates apache's log, some entries may be missing. This is easily solved however.

Apache
For awstats to be used from apache, the webhost needs to properly setup. In the alias section, the following needs to be added.

Finally, awstats needs the correct permissions to be accessable.

After a restart of apache, awstats should be available via http://stats.example.com/awstats/awstats.pl?config=stats.example.com. If no config option is passed to awstats, it uses the current hostname, which means in this case could have been omitted.

Awstats for mail log
AWStats is known for being an apache log parser. However it can also be used to parse mail logs.

Configuration
After copying awstats.stats.example.com.conf to awstats.mail.example.com.conf quite a few changes are required to turn awstats into a mail log parser.

With those changes in place, a manual run should work without any issues.

Logging
To scan the mail log every hour, the existing awstats script in cron.hourly can be appended with the following.

Also syslog is getting rotated and thus awstats needs to parse the mail log file before the mail log is being rotated.

If logging of apache files is not desired, or webmail resides on a different server, the webserver log parsing can be removed from cron jobs.