Skip to content

Instantly share code, notes, and snippets.

@vrenjith
Created October 5, 2013 07:41
Show Gist options
  • Save vrenjith/6837969 to your computer and use it in GitHub Desktop.
Save vrenjith/6837969 to your computer and use it in GitHub Desktop.
Q2. Parses and prints the HTTP code for the requests from the server log file
#!/usr/bin/perl
use strict;
printUsage() if ( scalar(@ARGV) != 1 );
open FF, $ARGV[0] or fatal("Unable to open file $ARGV[0]");
my %codeHash;
foreach (<FF>) {
if (m/HTTP\/.+?\s+(\d+)/) {
debug($_);
my $codeCount = 0;
$codeCount = $codeHash{$1} if ( exists( $codeHash{$1} ) );
$codeCount++;
$codeHash{$1} = $codeCount;
}
}
foreach ( keys(%codeHash) ) {
info("HTTP code $_ - $codeHash{$_}");
}
#This is specifically added since the question includes printing of 404 errors
if(exists($codeHash{"404"})){
info("Total 404 errors - $codeHash{'404'}");
} else {
info("No 404 errors detected in the log");
}
#Helper methods
sub printUsage {
print "Usage:
$0 logfilename
";
exit(2);
}
sub fatal {
info(shift);
exit(1);
}
sub info {
print shift . "\n";
}
sub debug {
#info(shift);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment