All pastes #397234 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-03-16 10:44 UTC
rendered paste body
#!/usr/bin/perl -w
# converts squid emulate_http_log access logs with enabled
# mime_headers to apache-style access-combined logs
# usage: converSquidLog < <log file>
$| = 1;
my $failures = 0;

# use a hash instead of a list so we can test
# for ignored host without looping over the
# whole list but use exists() instead
my %ignored_hosts = (
);

while (<>) {
    /^\s*([^ ]+) +([^ ]+) +([^ ]+) +(\[[^]]+\]) +("[^"]*") +([^ ]+) +([^ ]+) +([^ ]*) +\[([^]]*)\]/ && do {
        my ($ip,  $user,   $vhost,  $date,       $req,      $resp,   $bytes,             $header) = ($1, $2, $3, $4, $5, $6, $7, $9);

        my ($refer, $uagent) = qw(- -);

        $header =~ /Referr?er:\s*(.*?)\\r\\n/ && do {
            $refer = $1;
        };
        $header =~ /User-Agent:\s*(.*?)\\r\\n/ && do {
            $uagent = $1;
        };
        $uagent =~ s/%5b/[/g;
        $uagent =~ s/%5d/]/g;

        if (!exists($ignored_hosts{$ip})) {
            print "$ip $user $vhost $date $req $resp $bytes \"$refer\" \"$uagent\"\n";
        }
        next;
    };
    $failures++;
}
if ($failures) {
    print STDERR "$failures records ignored\n";
}