Skip to content

Instantly share code, notes, and snippets.

@ktnyt
Created July 4, 2013 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktnyt/5928759 to your computer and use it in GitHub Desktop.
Save ktnyt/5928759 to your computer and use it in GitHub Desktop.
An example of access log parsing
#!/usr/bin/env perl
use strict;
use warnings;
my %rank_hour; # Access per hour
my %rank_file; # Access per file
open LOG, "<", "access_log"; # Open log file
open CSV, ">", "access_log.csv"; # Open CSV output file
while( <LOG> ) {
$_ =~ /^(.+) .+ .+ \[.+\/.+\/.+:(.+):(.+):(.+) .+\] ".+ (.+) .+" .+ .+ ".+" ".+"$/; # Parse line
my $ip = $1; # Accessor's IP address
my ( $hour, $min, $sec ) = ( $2, $3, $4 ); # The time
my $file = $5; # The file
$rank_hour{$hour}++; # Count the access per hour
$rank_file{$file}++; # Count the access per file
print CSV "$ip,$hour:$min:$sec,$file\n"; # Print out CSV
}
print "Access ranking per hour:\n";
foreach my $hour ( sort{$rank_hour{$b} <=> $rank_hour{$a}}keys %rank_hour ) { # Sort the access per hour
print "$hour: $rank_hour{$hour}\n";
}
print "Access ranking per file:\n";
foreach my $file ( sort{$rank_file{$b} <=> $rank_file{$a}}keys %rank_file ) { # Sort the access per file
print "$file: $rank_file{$file}\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment