Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
This code runs and produces no visible errors, but the SDBM_File never gets bigger than 0 bytes after being created, even in /tmp with plenty of free disk. Permission to write the files exists, but this code runs as root anyway, via Apache. I also noticed it takes quite a bit of CPU from time to time, and eventually Apache stops responding. What am I doing wrong?
In httpd.conf:
The CustomLog target:LogFormat "%a" problem CustomLog "|/path/to/this/script.pl" problem
#!/usr/bin/perl -w $|=1; use strict; use Fcntl qw(:DEFAULT); use Net::Netmask; use Net::Whois::IANA; use Proc::Fork; use SDBM_File; my $iana = Net::Whois::IANA->new; my $bye = join '|', ( 'strings known to occur', 'in custname and orgname', ); tie (my %seen, 'SDBM_File', '/some/sdbm', O_RDWR|O_CREAT, 0666) or die "$!"; open my $fh, "< /dev/stdin" or die "$!"; while (<$fh>) { chomp; next if exists $seen{$_}; $seen{$_} = 1; my $ip = $_; child { my $problem = 0; my $host = `host $ip`; $problem = 1 if $host =~ /some host name regex/; if ($problem or $host =~ /not found/) { $iana->whois_query(-ip=>$ip); my $inetnum = $iana->inetnum || ''; my $custname = $iana->{QUERY}->{custname} || ''; my $orgname = $iana->{QUERY}->{orgname} || ''; next unless $inetnum and ($custname or $orgname); if ($custname =~ /$bye/i or $orgname =~ /$bye/i){ my $mask = Net::Netmask->new($inetnum); `iptables -A INPUT -s $mask -j DROP`; } } } } close $fh or die "$!"; untie %seen;
I have a feeling, even if this worked, there's a better way to do it. Maybe something in mod_perl or an existing Apache module? Meanwhile I can't figure out why this SDMB_File doesn't grow past 0 bytes. Thanks for your consideration.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Apache log piped to Perl
by gellyfish (Monsignor) on Aug 07, 2006 at 21:19 UTC | |
by Anonymous Monk on Aug 08, 2006 at 02:14 UTC | |
by isotope (Deacon) on Aug 08, 2006 at 15:55 UTC | |
|
Re: Apache log piped to Perl
by pileofrogs (Priest) on Aug 08, 2006 at 16:44 UTC | |
by Anonymous Monk on Aug 08, 2006 at 18:38 UTC | |
|
Re: Apache log piped to Perl
by gcalexander (Novice) on Aug 09, 2006 at 02:47 UTC | |
by Anonymous Monk on Aug 09, 2006 at 08:48 UTC |