"Webmaster's Domain"
by Lincoln D. Stein
Web Techniques, October 1998

Web Techniques grants permission to use these listings (and code) for
private or commercial use provided that credit to Web Techniques and 
the author is maintained within the comments of the source. For 
questions, contact editors@web-techniques.com. 


[LISTING ONE]

#!/usr/local/bin/perl
# script: mysqllog
## The mysqllog Program Relays Log Entries from Apache to mySQL

use DBI;

use constant DSN       => 'dbi:mysql:www';
use constant DB_TABLE  => 'access_log';
use constant DB_USER   => 'nobody';
use constant DB_PASSWD => 'not-the-real-password';

$PATTERN = '"([^"]+)" (\S+) "(\S+) (\S+) [^"]+" (\S+) "([^"]+)" "([^"]+)" (\d+) 
(\S+)';

$db = DBI->connect(DSN,DB_USER,DB_PASSWD) || die DBI->errstr;
$sth = $db->prepare("INSERT INTO ${\DB_TABLE} VALUES(?,?,?,?,?,?,?,?,?)") 
  || die $db->errstr;
while (<>) {
  chomp;
  my($date,$host,$method,$url,$user,$browser,$referrer,$status,$bytes)
        = map { $_ eq '-' ? undef : $_} /$PATTERN/o;
  $sth-
>execute($date,$host,$method,$url,$user,$browser,$referrer,$status,$bytes)
    || die $sth->errstr;
}
$sth->finish;
$db->disconnect;

---