Mailing List Archive

Please help with perl logging module.
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__
Re: Please help with perl logging module. [ In reply to ]
> 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):

Bad bad locking techniques, both in Perl (your fault, as Randal will
confirm) and moreover in dbm file which gets corrupted (not your fault)!
Please read
http://perl.apache.org/guide/dbm.html#Flawed_Locking_Methods_Which_Mus
to learn the solution to the non-yours fault part. For yours fault part
read DB_File manpage. Search the archives of this list and perl newsgroups
for Randal's comments on using LOCK_UN...

Try to read the http://perl.apache.org/guide/dbm.html chapter and apply
the locking techniques described there. I don't know if that will help you
with your real problem, but you really don't want your DB getting
corrupted...

As for handling hanging processes consuming CPU see:
http://perl.apache.org/guide/performance.html#Limiting_the_Resources_Used_by_h

> 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__
>
>
>



_______________________________________________________________________
Stas Bekman mailto:sbekman@iname.com http://www.stason.org/stas
Guide http://perl.apache.org/guide mod_perl http://perl.apache.org
Perl,CGI,Apache,Linux,Web,Java,PC http://www.stason.org/stas/TULARC
http://modperl.sourcegarden.org http://perlmonth.com http://perl.org
single o-> + single o-+ = singlesheaven http://www.singlesheaven.com
Re: Please help with perl logging module. [ In reply to ]
or use Tie::DB_FileLock .... CPAN