Maintain an Archive of AWStats Monthly Website Traffic Reports

I use AWStats to generate reports from my Apache webserver logs. Now, I prefer to use AWStats to generate statically linked reports, so instead of having the “Update now” option on the AWStats report page, I have the following script that runs every hour or so as a cron job to update the reports instead.

perl /path/to/ -config=MYSITE -update -staticlinks -awstatsprog=/path/to/ -dir=/var/www/MYSITE/statistics/ > /dev/null
chown MYUSERNAME:MYGROUPNAME /var/www/MYSITE/statistics/awstats.MYSITE.*

The result of that little script is that AWStats prepares a detailed report of webserver activity for the current month. Now, that’s great but how can you maintain an archive of detailed reports for previous months? Answer; write another bash script…

Now, this script is somewhat more complicated than the two-line effort above but it will prepare a set of detailed reports for the previous month, store them in a date-named subfolder and generate a web page listing all sets of monthly reports it’s created so far. Just run it as a cron job on the first of every month and make sure it doesn’t conflict with the running of the regular update script above, especially if you’ve got AWStats’ file-lock option turned on.

NOTE: This script won’t go back through your whole history of apache logs, generating AWStats reports for each month of activity it can find. That’s a job for another script…

Here’s a directory tree by way of explanation…

# Website root

# AWStats detailed report for current month, generated by the two-line script above

# AWStats detailed report for October, created by script below when it ran on 1st November

# AWStats detailed report for September, created by script below when it ran on 1st October

# Web page created by script below with links to...
#   - Current month's detailed report
#   - Detailed report for October
#   - Detailed report for September

Below is the script itself. It won’t work out of the box – there are a few variables to set first, mostly paths to AWStats and your webserver. You can copy and paste it from this page or download the script here. As with the other scripts I’ve made available to download, change the extension and permissions before running it. There’s not much more to say about the script here as I’ve made extensive notes in the file. I hope you find it useful!

## ---------------------------------------------------------------------------
## ---------------------------------------------------------------------------
# Script to generate a full set of statically linked awstats reports
# for the previous calendar month for an individual website.
# Author: Richard Leszczynski
# Email: contact NOSPAM
# Created: 30/10/2011
# Modified: 26/11/2011
## ---------------------------------------------------------------------------
# 	To be run once a month at the beginning of the month.
# User running the script must have execute permission for the awstats# perl
#scripts and write permissions for the directory in which the# statistics
# report is to be created. (so 'root', most likely)
# 	1. Creates (if necessary) a 'statistics' subfolder for the website to
# store the monthly statistics reports.
# 	2. Creates a subfolder within 'statistics' for the monthly report that
# is to be created. Folder name will be a numeric# representation of the year
# and month (e.g. 2011_10)
#	3. Creates a full set of statically-linked awstats reports# for the
# previous month using
#	4. Changes the owner & group of the folder/reports created; ideally to
# those of a regular user, not root.
# 	5. Updates statistics/index.html with a link to the newly# created
# report
#	awstats already configured for the website that the monthly# report is
# to be generated for. For instructions. see
# 	That an command such as
# perl /PATH/TO/ -config=MYSITE -update -output -staticlinks > /PATH/TO/mysite/statistics/awstats.MYSITE.html
# is already running on a regular basis through a cron job in order# to give
# you something to look at between the generation of each monthly report.
#	If you change the path of the report that the command above outputs
# to the statistics directory, it will be linked to within the generated
#statistics/index.html page
# It is also recommended that an command such as
# perl /PATH/TO/ -config=MYSITE -update
# is added as a prerotate action within /etc/logrotate.d/apache2. See the
# awstats documentation for more information on this.
## ---------------------------------------------------------------------------
# Paths & Variables  Required: Change to suit your installation...
## ---------------------------------------------------------------------------
# Full path of your website's DocumentRoot - NO TRAILING SLASH!
# e.g. /var/www/MYSITE/
# Full path of script
# Full path of
# awstats website configuration name. The text you'd enter in place
# of MYSITE in the command 'perl -config=MYSITE -update'
# User and group to own the created monthly report
# e.g. USER=myusername , GROUP=users , or
# e.g. USER=www-data , GROUP=www-data
## ---------------------------------------------------------------------------

# Check for pre-existing statistics directory
if [ ! -d "$WEBSITE_DIR" ]; then
echo "$WEBSITE_DIR does not exist! Check path for WEBSITE_DIR has been set correctly. Exiting..."
exit -1
if [ ! -d "$STATS_DIR" ]; then
mkdir "$STATS_DIR"

# New Subdirectory to hold the new set of monthly reports
LMONTH=$(date --date="last month" +%m)
LYEAR=$(date --date="last month" +%Y)

if [ ! -d "$REPORT_DIR" ]; then
mkdir "$REPORT_DIR"

# Run to generate a complete set of
# statically linked reports for the last month
perl "$AWSTATS_BSP_PATH" -config=$MYSITE -update -month=$LMONTH -year=$LYEAR -staticlinks -awstatsprog="$AWSTATS_PATH" -dir="$REPORT_DIR" >/dev/null

# Create an index.html file to link to both the current/rolling report
# and a history of all the monthly reports created to date
echo "<!--File generated automatically by>" &gt; "$INDEX_FILE"
echo "
<h1>AWSTATS Website Statistics for $MYSITE</h1>
" &gt;&gt; "$INDEX_FILE"
echo "
<h2>Rolling report for this month</h2>
" &gt;&gt; "$INDEX_FILE"
echo "<a href="awstats.$MYSITE.html">1st $(date --date="today" +%B %Y) to today</a>" &gt;&gt; "$INDEX_FILE"

echo "
<h2>Archive of monthly website activity reports</h2>
" &gt;&gt; "$INDEX_FILE"
# obtain a list of directories within the STATS_DIR that match the date format which the
# monthly repots are stored in
MONTHLY_DIRS=$(ls -rd */ | grep -E ^[12][9012][0-9]{2}_[01][0-9]/)
for dr in $MONTHLY_DIRS
TEMP_MONTH=$(echo $TEMP_MONTH | sed 's/^0*//')
(( MONTH_OFFSET = $(date --date="today" +%-m) - "$TEMP_MONTH" ))
echo "<a>$(date --date="$MONTH_OFFSET months ago" +%B) $TEMP_YEAR</a>

" &gt;&gt; "$INDEX_FILE"
echo "" &gt;&gt; "$INDEX_FILE"

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s