CREATE TABLE access_log (
when datetime not null,
host varchar(255) not null,
method char(4) not null,
url varchar(255) not null,
auth varchar(50),
browser varchar(50),
referer varchar(255),
status smallint(3) not null,
bytes int(8) default 0,
key(host(30)),key(url(30)),key(status)
);
LogFormat "\"%{%Y-%m-%d %H:%M:%S}t\" %h \"%r\" %u \"%{User-agent}i\" %{Referer}i %s %b" mysql
CustomLog "| /usr/local/apache/bin/mysqllog" mysql
#!/usr/local/bin/perl
# script: mysqllog
use DBI;
use constant DSN => 'dbi:mysql:www';
use constant DB_TABLE => 'access_log';
use constant DB_USER => 'nobody';
use constant DB_PASSWD => '';
$PATTERN = '"([^"]+)" (\S+) "(\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,$referer,$status,$bytes) = /$PATTERN/o;
$user = undef if $user eq '-';
$referer = undef if $referer eq '-';
$browser = undef if $browser eq '-';
$bytes = undef if $bytes eq '-';
$sth->execute($date,$host,$method,$url,$user,
$browser,$referer,$status,$bytes);
}
$sth->finish;
$db->disconnect;
|
SELECT count(*) from access_log;
SELECT sum(bytes) from access_log;
SELECT to_days(max(when))-to_days(min(when))
from access_log
SELECT date_format(when,'W') as weekday,count(*)
from access_log group by weekday;
SELECT date_format(when,'H') as hour,count(*)
from access_log group by hour;
SELECT date_format(when,'H') as hour,
sum(bytes)/(60*60) as bytes_per_min
from access_log group by hour;
SELECT url,count(*) as count from access_log
group by url order by count desc limit 10;
SELECT url,count(url) from access_log
where status=404 group by url;
SELECT left(referer,60),count(*) as cnt from access_log
where url='/WWW/software/CGI/cgi_docs.html'
group by referer order by cnt desc;
|
| Contents | Next |