2011-02-16

Perl throw-away: Apache HTTP status codes count by hour.

With the combined LogFormat.

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

I wanted status return code counts for /myuri broken down by hour. I was looking into back end Tomcat problems.

#!/usr/bin/perl -anw

use strict;
use warnings;

our %data;

next if ! m{GET /myuri/};

my $hour = substr($F[3],13,2);

# To get status in this CustomLog format, first, throw away everything up to
# the "GET <URI>" field...
my $status = $_;
$status =~ s/^.*HTTP\/1\.[10]"\s*//;
# ...then grab the first field after that.
$status = (split(/\s+/, $status))[0];

$data{$hour}->{$status}++;

END {
for my $h (sort(keys %data)) {
print "$h\n";
for my $s (sort(keys %{$data{$h}})) {
print "\t$s = $data{$h}->{$s}\n";
}
}
}

Here is some example output.

14
200 = 48812
206 = 2
302 = 31807
304 = 11926
400 = 2462
404 = 2122
416 = 1
503 = 339
15
200 = 40747
206 = 3
302 = 24270
304 = 9157
400 = 2637
404 = 2021
500 = 2
503 = 399

No comments:

Post a Comment