Aquarionics

Wednesday 24th December 2003

Geek at Christmas

So, as is traditional I spend my christmas holidays playing with epistula. Now I have referer tracking turned working again.

The problem with referer tracking is extracting the data from log files. When the server had mod_log_sql it was easy (I have an entire log stats suite built for mod_log_sql), but since log_sql doesn’t support Apache 2 yet (A patch to make it do so was released yesterday. It remains untested) I had to brush off my extremely limited perl skillz to create this, a perl program to send apache logs to mysql:

#!/usr/bin/perl
use DBD::mysql;

#Database options:
$dbUser = "username";
$dbPass = "password";
$dbName = "database";

$database = DBI->connect(
"dbi:mysql:$dbName:localhost:1114",
$dbUser, $dbPass
);

while (<>) {

my ($client, $identuser, $authuser, $date, $method, $url, $protocol, $status, $bytes, $referer,$agent) =
/^(S+) (S+) (S+) [(.*?)] "(S+) (.*?) (S+)" (S+) (S+) "(.*?)" "(.*?)"$/; $q = "insert into apachelogs
(remote_host, remote_user, request_time, request_method,
request_uri, request_protocol, status, bytes_sent, referer, agent) values (".$database->quote($client).", ".$database->quote($authuser).", '"
.$date."', ".$database->quote($method).", ".$database->quote($url)
.", ".$database->quote($protocol).", ".$database->quote($status)
.", ".$database->quote($bytes).", ".$database->quote($referer)
.", ".$database->quote($agent).")"; my $sth = $database->prepare($q); $sth->execute();

}

Those who spoke on this:

gravatar image

paul:

2006-02-26 21:19 2 yrs after the Original Article

I took a look at this and found the regex didn’t quite work: I came up with this one.

(I would have wrapped this in pre tags, but I read the warning: no innocent pair of angle brackets deserves that).

while () {

my ($client, $identuser, $authuser, $date, $method, $url, $protocol, $status,$bytes, $referer, $agent) =
/(S+).*? (S+) (S+) [(.*?)] “(S*) (S+) (S+)” (S+) (S+) ”(S+)” ”(.*)?”/;

Comment Link


Nicholas 'Aquarion' Avenell is a web developer in London, you can find out more about him or how to get in touch.

There are more Articles, Projects, Journal Entries, Photographs and things that defy description here, too.

If you're looking for something specific, there are Calendar & Category -based lists of everything.

And if you want to follow stuff that appears here, try a Syndication Feed, or the generic Feed of everything.


Aquarion's last Twitter was: [updating]
Twitter last updated


More Journal:

[RSS Icon]
[ESF Icon]
[CDF Icon]

That which is relevant:


Explain Ads
© 2000 to 2008 inclusive Nicholas Avenell
All comments are the property of their creators, published with permission
(Unless otherwise indicated, the opinions and sentiments expressed on this site are those of the author and not of any organisation of which he is an affiliate, including his employer. Caveat Lector, E&OE. sigh)
0.865 seconds, 16 queries, 2.65Mb on Wed, 01 Oct 2008 06:05:22 +0000
Generated by Epistula Version 2.0.3