Apache2 and Visitor

From SaruWiki
Revision as of 08:17, 29 December 2009 by Saruman! (talk | contribs) (page created)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Visitor is an Apache2 log analyzer that can show which people visited your website. I found a simple cron-based setup here. The setup for our Debian system goes like this:

First install ip2host and visitor using the well understood

sudo apt-get install ip2host visitor

It will install the two requested packages, and probably also some extra packages like graphviz and ttf-liberation.

Next we create a cache directory for ip2host

sudo mkdir /var/cache/ip2host

Then we create the following script, which we save in /etc/cron.daily/visitors: <source lang="bash">

  1. !/bin/bash

MYIP="192.168" # i want to exclude my home network from the logs SERVERIP="222.222.222.222" # my server's ip REPORTDIR="/var/www/webstats" # folder where to store reports, this folder must exist ALOGDIR="/var/log/apache2" # folder containing the logs VISITORS="/usr/bin/visitors -A --exclude wp-cron.php --exclude robots.txt" # i exclude some files from the reports IP2H="ip2host --cache=/var/cache/ip2host/cache.db" GREPOPTIONS="-hv -e ^$MYIP -e ^$SERVERIP" # exclude my home network and my server's ip from the logs

  1. we create a tmp file that will hold the logs

TMPFILE=$(mktemp) if ! -f "$TMPFILE" ; then

 echo "tmpfile doesn't exist."
 exit 1

fi

  1. if you only have one site, or you want all the logs in a single report

/bin/grep $GREPOPTIONS $ALOGDIR/access*.log{.1,} 2>/dev/null > $TMPFILE # get all the logs into the tmpfile, notice the GREPOPTIONS variable.

  1. resolve all the ips and generate the reports, note that "--trails --prefix http://www.domain.com" is optional it's only needed for generating trails stats

($IP2H < $TMPFILE ) | $VISITORS --trails --prefix http://www.domain.com - > $REPORTDIR/stats.html

  1. -OR-
  2. if you have multiple vhosts/prefixes and want separate reports, you can use this:
  3. replace all the "www prefix1 prefix2 prefix3" by your own prefixes (as in http://PREFIX.domain.com)

for name in www prefix1 prefix2 prefix3; do

 /bin/grep $GREPOPTIONS $ALOGDIR/access-$name.log{.1,} 2>/dev/null > $TMPFILE
 ($IP2H < $TMPFILE ) | $VISITORS --trails --prefix http://$name.domain.com - > $REPORTDIR/stats-$name.html

done

rm -f $TMPFILE </source>