#!/bin/bash
# should contain subdirectories and log files for each of the Apache domains or subdomains
# For instance: If you want to add a subdomain: my.domain.com you should create
# the directory /var/log/apache2/my-domaincom/ ,the directory should contain both access.log and error.log files
# The script would automatically substitute all "-" dashes with "." to be able to properly work
# you should always follow a syntax when creating new subdirectories for new domains in /var/log/apache2/
# Let's say you'd like to add a newdomain name called newdomain.com, then you should create
# the directory /var/log/apache2/newdomaincom/ and it should contain both access.log and error.log files
# Hope you got the idea
# Preliminary make sure you have configured your VirtualHosts to log to a proper locations the script assumes
# that Apache would have directives for each of our domains Virtualhosts as follows:
#   ErrorLog  /var/log/apache2/newdomaincom/error.log
#   CustomLog /var/log/apache2/newdomaincom/access.log combined
# Also you should configure the script variable:
# (apache_log_file) to point to your original apache log file  - This variable's value should be identical to the value
# of LogFile="/var/log/apache2/access.log" in /etc/awstats/awstats.conf otherwise the script won't work
# (domain_name_orig) variable should be configured to be identical to the
# /etc/awstats/awstats.conf's variable SiteDomain="domainname.com"
# (index_page) should be set to a location where you want to have html with links to awstats statistics
# to all your domains generated from the logs
# To make the script execute routinely use crond
# On Debian Linux edit your /etc/cron.d/awstats
# comment the line
# 0,10,20,30,40,50 * * * * www-data [ -x /usr/lib/cgi-bin/awstats.pl -a -f /etc/awstats/awstats.conf -a -r /var/log/apache2/access.log ] && /usr/lib/cgi-bin/awstats.pl -config=awstats -update >/dev/nul
# Then add something similar to:
# 0,10,20,30,40,50 * * * * www-data cd /etc/awstats/awstats; /etc/awstats/awstats_generate_for_multiple_sites.sh 2>&1 >/dev/null
# in the above cron definition I assume you're storing the awstats_generate_for_multiple_sites.sh script in /etc/awstats/ directory.
#
# This script is licensed under GPL ver. 3.0
# http://www.gnu.org/licenses/gpl-3.0.txt

loop_dir=/var/log/apache2;
domain_name_orig='domain_name.com';
apache_log_file='/var/log/apache2/access.log';
index_page=/var/www/awstats/index.html;
awstats_conf=/etc/awstats/awstats.conf;
Webstatistics_for="Your Company Name";
apache_log_file_name='access.log';
awstats_url="http://www.domain.com/cgi-bin/awstats.pl?config";
awstats_location='/usr/lib/cgi-bin/awstats.pl';

cat /dev/null > $index_page;
echo "<html>" >> $index_page;
echo "<head>" >> $index_page;
echo "<title> Awstats Statistics </title>" >> $index_page;
echo "</head>" >> $index_page;
echo "<body>" >> $index_page;
echo "<center><h2> Awstats Statistics for $Webstatistics_for</h2></center>" >> $index_page;
for i in $(echo $loop_dir/*); do
if [ -d "$i" ]; then
host_logs="$i/$apache_log_file_name";
get_last=$(echo $i |sed -e "s#/# #g" |awk '{ print $4 }');
#echo $get_last;
domain_name=$(echo $get_last |sed -e "s#com#.com#g" |sed -e "s#-#.#g");
echo "Generating Awstats file for: $domain_name";
sed -e "s#$apache_log_file#$host_logs#g" -e "s#$domain_name_orig#$domain_name#g" $awstats_conf > /etc/awstats/awstats."$get_last".conf;
[ -x $awstats_location -a -f $awstats_conf -a -r $loop_dir/$apache_log_file_name ] && $awstats_location -config=awstats -update >/dev/null
# Uncomment for debug
#echo "[ -x $awstats_location -a -f /etc/awstats/awstats."$get_last".conf -a -r $host_logs ] && $awstats_location -config=/etc/awstats/"$get_last" -update >/dev/null";
[ -x $awstats_location -a -f /etc/awstats/awstats."$get_last".conf -a -r $host_logs ] && $awstats_location -config="$get_last" -update
echo "<h3><center><a href="$awstats_url"="$get_last"> Awstats web statistics for $domain_name </a></center></h3>" >> $index_page
fi
done
echo "</body>" >> $index_page;
echo "</html>" >> $index_page;
