[BACK]
A Simple Apache Log Parser
Summary This document serves the purpose of prove of concept to parse a Apache log file. This example script uses regular expression to parse the log file. You can modify the pattern variable to match your specific apache log format.

Usage Examples First you need to copy this script and save it to your local machine. Then copy the example one line log file on this page and save it on the same directory of your perl script. To run the perl log parser, just type this "perl yourscriptname inputlog". Click here to download sample log.
 
Example perl script
#!/usr/bin/perl -w
use strict;


#Change this regular expression pattern according to your apache log forma.

my $LOG_PATTERN = q{(.*) \- \[(.*)\] \"(.*) (.*)\?(.*) HTTP\/(.*)\" ([0-9]*) 
                    ([0-9]*) \"(.*)\" \"(.*)\" \"(.*)\" \"(.*)\" \-};


my $len = @ARGV;
if ($len <1){
     print "Usage : perl script_name logfile\n";
     exit();
}

if (!(-e $ARGV[0])){
      print $ARGV[0]," not exists.\n";  
      exit();
}

#Open log file for reading.

open (SEM, "< $ARGV[0]") or die "Cannot open file $ARGV[0]\n";

while (){
       
       #We can also assign to a hash.
       
       if (my($ip,
              $date,
              $method,
              $url,
              $query,
              $protocol,
              $ret_code,
              $byte,
              $referer,
              $user_agent,
              $tc, $imp)=($_ =~ m/$LOG_PATTERN/)){
              print "IP :",$ip, " referer:",$referer,"\n";
       }
}
close(SEM);

Hosted by www.Geocities.ws

Hosted by www.Geocities.ws

1