I'm trying to pipe a simple Apache log of IP addresses to a Perl program for further processing. In practice it should record each IP once in a hash tied to an SDBM_File. If the IP is new it forks with Proc::Fork for a DNS lookup using `host` and a possible whois lookup with Net::Whois::IANA. If the host or whois info matches my $problem a mask is generated with Net::Netmask so `iptables` can drop packets from that entire subnet.

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:

LogFormat "%a" problem CustomLog "|/path/to/this/script.pl" problem
The CustomLog target:
#!/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.


In reply to Apache log piped to Perl by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.