Automatize awstats config file creation

In my opinion awstats is THE apache log file analyzer solution. Unfortunately it does not support automatic configuration based on the webserver configuration, which is a great feature to miss IMHO. The following shell script has 3 different sections:

  • First it uses a recursive bash function to parse the active and effective apache configuration - taking Include directives into account
  • Second it utilizes a not over-complicated gawk script to collect VirtualHost definitions along with ServerName and ServerAlias directives sequentially until a TransferLog directive is found, then it creates (non-yet-existing) awstats configuration files from the collected info using awstats Include directives pointing to the main awstats config file. It also creates a simple HTML index file using a preconfigured URL prefix for all awstats virtual host configurations during the process.
  • The third part is a simple copy of a contributed awstats script that runs awstats for all virtual host configurations

UPDATE: this new script also includes locking to prevent parallel runs and depth control for recursive apache config files

#!/bin/bash
 
apacheconf="/etc/apache2/apache2.conf"
awstatsconf="/etc/awstats/awstats.conf"
indexfile="/home/ideart/public_html/awstats-index.html"
urlbase="http://www.ideart.hu/awstats?config="
lockfile="/var/run/$0.pid"
 
#############################################################################
 
#locking
if [ -f "$lockfile" ]; then
    ps -p `cat "$lockfile"` >/dev/null || rm -f "$lockfile" # rm stale lockfile
    exit 0
fi
trap "rm -f '$lockfile'" exit
echo $$ > "$lockfile"
 
awstatsdir=`dirname "$awstatsconf"`
 
cat_apacheconf() {
        #echo "catting $1" >&2
 
        depth=$(( $2 + 1 ))
        if [ $depth -ge 1000 ]; then
            return
        fi
 
        if [ -f "$1" ]; then
                # arg is a file
                cat "$1" | egrep -i '^[^#]*include[[:space:]]' | awk '{ print $2 }' | ( while read conf; do
                        cat_apacheconf "$conf" $depth
                done )
                cat "$1"
        elif [ -d "$1" ]; then
                # arg is a directory
                pushd "$1" > /dev/null
                ls -1 . | ( while read conf; do
                        cat_apacheconf "$conf" $depth
                done )    
                popd > /dev/null
        else
                # arg is a glob pattern
                dir=`dirname "$1"`
                base=`basename "$1"`
                pushd "$dir" > /dev/null
                ls -1 $base | ( while read conf; do
                        cat_apacheconf "$conf" $depth
                done )
                popd > /dev/null
        fi
 
cat_apacheconf "$apacheconf" | gawk -v awstatsdir="$awstatsdir" -v awstatsconf="$awstatsconf" -v indexfile="$indexfile" -v urlbase="$urlbase" '
 
BEGIN { 
        IGNORECASE=1 
        servername=default
        virtualhost=default
        serveralias=""
        print "<h1>awstats index file</h1>" > indexfile
}
 
/^[^#]*virtualhost[[:space:]]/ { 
        virtualhost = $2
        gsub("[*<> ]","",virtualhost)
}
 
/^[^#]*servername[[:space:]]/ { 
        servername = $2
}
 
/^[^#]*serveralias[[:space:]]/ { 
        serveralias = serveralias " " $2
}
 
/^[^#]*transferlog[[:space:]]/ { 
        transferlog = $2                
 
        print "<a href=\"" urlbase servername "_" virtualhost "\">" servername "_" virtualhost "</a><br />" >> indexfile
 
        newconf= awstatsdir "/awstats." servername "_" virtualhost ".conf"
        cmd="test -f " newconf
        if ( system(cmd) == 0 ) {
                #print newconf " exists already - skipping"
        } else {
                #print newconf " did not exist - creating"
                print "# autogenerated config file" >> newconf
                print "Include \"" awstatsconf "\"" >> newconf
                print "SiteDomain=\"" servername "\"" >> newconf
                print "HostAliases=\"" serveralias " 127.0.0.1 localhost\"" >> newconf
                print "LogFile=\"" transferlog "\"" >> newconf
                print "LogType=W" >> newconf
                print "LogFormat=1" >> newconf
        }
        serveralias=""
 
}
'
 
[ -f /etc/awstats/awstats.conf ] && /usr/lib/cgi-bin/awstats.pl -config=awstats -update >/dev/null
for cfg in `find /etc/awstats -name 'awstats.*.conf' -printf '%f\n' | sed 's/^awstats\.\(.*\)\.conf/\1/'`; do
    /usr/lib/cgi-bin/awstats.pl -config=$cfg -update >/dev/null
done
 
exit 0

Linkbacks

Use the following URL for manually sending trackbacks: http://rigo.info/lib/plugins/linkback/exe/trackback.php/en:blog:automatize_awstats_config_file_creation
en/blog/automatize_awstats_config_file_creation.txt · Utolsó módosítás: 2009-10-25 00:00 (külső szerkesztés)
CC Attribution-Noncommercial-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0