Created
July 4, 2013 15:50
-
-
Save ktnyt/5928759 to your computer and use it in GitHub Desktop.
An example of access log parsing
This file contains hidden or 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 | |
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