I want to log the number of times each page gets using a DB_File. Using dbi
to do the logging is not a valid option because this is on a very busy
server and it would wind up opening too many mysql clients. The following
is what I've tried. The problem I have is that when I install this code as
a PerlLogHandler, it kills apache after a few minutes (It looks like the
child servers don't die -- and httpd's consume almost all of the cpu):
package Apache::LogDB;
# file: Apache/LogDB.pm
use strict;
use Apache::Constants qw(:common);
use Fcntl;
use DB_File;
sub LOCK_SH { 1 }
sub LOCK_EX { 2 }
sub LOCK_NB { 4 }
sub LOCK_UN { 8 }
sub handler {
my $orig = shift;
my $r = $orig->last;
my $url=$orig->uri;
my %db_hash;
my $key = $url;
my $db_obj = tie(%db_hash, 'DB_File','/tmp/foo.db',O_CREAT|O_RDWR, 0644);
my $fd = $db_obj->fd;
open(DB_FH, "+<&=$fd");
flock (DB_FH, LOCK_EX);
my $oldval = $db_hash{$key};
my $value=$oldval+1;
$db_hash{$key}=$value;
$db_obj->sync();
flock(DB_FH, LOCK_UN);
undef $db_obj;
untie %db_hash;
return OK;
}
1;
__END__
to do the logging is not a valid option because this is on a very busy
server and it would wind up opening too many mysql clients. The following
is what I've tried. The problem I have is that when I install this code as
a PerlLogHandler, it kills apache after a few minutes (It looks like the
child servers don't die -- and httpd's consume almost all of the cpu):
package Apache::LogDB;
# file: Apache/LogDB.pm
use strict;
use Apache::Constants qw(:common);
use Fcntl;
use DB_File;
sub LOCK_SH { 1 }
sub LOCK_EX { 2 }
sub LOCK_NB { 4 }
sub LOCK_UN { 8 }
sub handler {
my $orig = shift;
my $r = $orig->last;
my $url=$orig->uri;
my %db_hash;
my $key = $url;
my $db_obj = tie(%db_hash, 'DB_File','/tmp/foo.db',O_CREAT|O_RDWR, 0644);
my $fd = $db_obj->fd;
open(DB_FH, "+<&=$fd");
flock (DB_FH, LOCK_EX);
my $oldval = $db_hash{$key};
my $value=$oldval+1;
$db_hash{$key}=$value;
$db_obj->sync();
flock(DB_FH, LOCK_UN);
undef $db_obj;
untie %db_hash;
return OK;
}
1;
__END__