====== Automatize awstats config file creation ====== In my opinion [[http://awstats.sourceforge.net/|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 [[http://www.gnu.org/software/gawk/|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 "

awstats index file

" > indexfile } /^[^#]*virtualhost[[:space:]]/ { virtualhost = $2 gsub("[*<> ]","",virtualhost) } /^[^#]*servername[[:space:]]/ { servername = $2 } /^[^#]*serveralias[[:space:]]/ { serveralias = serveralias " " $2 } /^[^#]*transferlog[[:space:]]/ { transferlog = $2 print "" servername "_" virtualhost "
" >> 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
{{tag>awstats awk bash util apache}} ~~LINKBACK~~