| Category: | CGI |
| Author/Contact Info | George_Sherston |
| Description: | I needed to be able to look at the access logs for the small web sites I run, and there's not enough traffic that I need the all-singing, all-dancing model, I just wanted something that extract the lines I was interested in from the access_log file and show them in a helpful way in the browser. I reckoned it wd take me as long to find one as to write one, and writing one wd be marginally more fun and give me the chance to get another code critique from friendly monks, so here it is. And maybe in the future there'll be someone else who wants exactly this level of simplicity, and if you're that person, look at the minimal comments to see how to 'configure' (if that isn't too grand a term) and you're off to the races. Enjoy! |
#!/usr/bin/perl -w
use strict;
use CGI qw(:standard);
my $domain;
&get_domain;
&do_log if defined ($domain = param('Domain'));
sub
get_domain
{
my $title;
if ($domain) {$title = "access log for domain $domain"}
else {$title = 'access log: select domain'}
print
header,
start_html(-title=>'access log: select domain'),
startform(
-method=>'POST',
-action=>'access.pl',
),
hr,
'Enter Domain Name: ',
textfield('Domain'),
br,br,
submit,
endform,
hr,
end_html;
}
sub
do_log
{
my $file = '/home/htdocs/logs/access_log'; # put your log file
+name and path here
my @ignores = ( # suppress log entries that match any of these:
'^62\.253\.128\.5',
'Scooter_trk',
'css HTTP',
'bmp HTTP',
'gif HTTP',
'htm HTTP',
'jpg HTTP',
'robots\.txt',
'http:\/\/blogdex\.media\.mit\.edu\/\/',
);
my $ignores = join ("|",@ignores);
my @log;
open LG, $file or die "can't open $file $!";
while (<LG>) {
next if /$ignores/;
unshift @log, $_ if /$domain/;
}
my $count = 0;
my $lastlog;
while ($count < @log) {
$log[$count] =~ /^(.*?) - - \[(.*?)].*?"GET \/(.*?) HTTP.*?".*
+?"(.*?)".*?".*?" (.*)$/;
if ($lastlog ne $1) {
print
hr,
substr ($2, 0, 17)," ",$1,
br;
}
$lastlog = $1;
print " ",b("$5/$3");
print " (from $4)" unless $4 =~ /$5/;
print br;
$count ++;
}
}
|
|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Access Log Reader
by dws (Chancellor) on Oct 14, 2001 at 08:15 UTC | |
by George_Sherston (Vicar) on Oct 15, 2001 at 00:31 UTC | |
by dws (Chancellor) on Oct 15, 2001 at 03:18 UTC | |
|
Re: Access Log Reader
by George_Sherston (Vicar) on Oct 14, 2001 at 00:43 UTC |