Created
July 20, 2011 21:55
-
-
Save jimschubert/1096023 to your computer and use it in GitHub Desktop.
perl script to filter apache logs by http status code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# This script filters an apache log by HTTP code and outputs in a slightly more readable fashion. | |
my $usage = <<"USAGE"; | |
usage: $0 logfile status_code | |
For example: | |
$0 apache.log 302 | |
$0 apache.log | |
Notes: | |
Status 200 is shown by default | |
USAGE | |
@ARGV >= 1 or die $usage; | |
my $request_output = <<"REQ"; | |
%-20s:\t%s | |
%20s:\t%s | |
%20s:\t%s | |
%20s:\t%s | |
%20s:\t%s | |
REQ | |
my($filename, $show_status) = @ARGV; | |
$show_status = $show_status || '200'; | |
open LOG, "$filename" or die "Could not open file ($filename): $!\n"; | |
LINE: while(<LOG>) { | |
next LINE unless (my @groups = /(\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}|::1)[- ]*\[(.*)\](.*)/); | |
my $values = $groups[2]; | |
my ($req, | |
$status, | |
$size, | |
$path, | |
$client) = ( $values =~ /^[^"]*["]([^"]+)["]\s+(\d{0,4})\s+([-\d]+)\s+["]([^"]+)["]\s+["]([^"]+)["].*/ ); | |
printf ($request_output, | |
"Originally requested", $req, | |
"status", $status, | |
"path", $path, | |
"size", $size, | |
"client", $client) if $status eq $show_status; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment