Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Awstats with vlogger

Developers
2008-10-26
2012-10-11
  • guy marcenac
    guy marcenac
    2008-10-26

    Hello,

    Vlogger is a utility that can split apaches logs into directories for each VirtualHost.

    I wrote a small program that will automatically create a conf file for each VirtualHost log file found and then run the awstats update statistics.

    The program is intended to include a default configuration file in the dynamically created conf file for the virtual host, and then add only the site related options in the file in order to override the corresponding default options.

    I dont know awstats very well, so any comments amimed at improving this small shell program wil be welcome

    [code]

    !/bin/sh

    this program will scan the logs created by vlogger,

    create an awstat configuration file for each of them

    and then execute an update of the awstats statistics

    it assumes that vlogger is installed and its rotation feature disabled (-n option)

    a good way is to rotate the logs with logrotate and run

    this program in the prerotate directive

    this shold be done if you dont want to lose any record in the rotation

    you can run this program as often as you wish as a cron job

    Apache log facility setting:

    LogFormat "%v %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

    CustomLog "| /usr/sbin/vlogger -n -s access.log -t access.log /var/log/apache2" combined

    You must have a common awsetut file with all the defaults options you wish $awCommonSetupFile

    This program is based on the idea from dave

    http://davelozier.com/2008/06/20/awstats-vlogger/

    logdir=/var/log/apache2
    confdir=/etc/awstats
    datadir=/var/lib/awstats
    cgidir=/cgi-bin
    logfile=access.log
    awCommonSetupFile=/etc/awstats/awstats.conf.local
    apacheUser=www-data
    apacheGroup=www-data

    check if we are the only running instance of the program

    LOCK="/tmp/${0##*/}.lock"
    [ -e "${LOCK}" ] && exit 0
    touch "${LOCK}"

    if a log directory exists in /var/log/apache and this site has no awstat conf file

    create it awstat.sitename.conf

    cd $logdir
    for directory in *
    do
    # does the site (ServerName) directory exist and is there a matching awstats config file?
    awstatsconf="$confdir/awstats.$directory.conf"
    if [ -d ${directory} ]
    then
    if [ ! -f ${awstatsconf} ]
    then
    # create configuration file
    printf "Include \"%s\"\nLogFile=%s\nSiteDomain=%s\nDirData=%s\nDirCgi=%s\n" \ $awCommonSetupFile \ "$logdir/$directory/$logfile" \ $directory \ $datadir/$directory \ $cgidir/$directory > $awstatsconf
    #create logging directory
    mkdir $datadir/$directory
    fi
    fi
    done

    execute all updates found in $confdir

    this allow to execute manually added stats configuration files for sites not logged in the $logdir directory (smtp, ftp for instance)

    cd $confdir
    for conffile in awstats.*.conf
    do
    # extract sitename from $conffile
    sitename=echo $conffile | awk 'BEGIN {FS="."} {i=2; while (i < NF) {if (i<(NF-1)) printf("%s.",$i); else printf("%s",$i); i++; }}'
    # update stats
    /usr/lib/cgi-bin/awstats.pl -config=$sitename -update
    done

    change ownership of stats data in order to allow the direct update from web interface (parameter AllowToUpdateStatsFromBrowser)

    chown -R $apacheUser.$apacheGroup $datadir

    remove lockfile

    rm "${LOCK}"

    exit 0
    [/code]

     

    Related

    Code: code