DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Apache Access Log Parser

10.11.2007
| 1712 views |
  • submit to reddit
        // This is a perl script that will open an Apache  access log file and parse it for daily and hourly hits.

#!/usr/bin/perl -w

$data_file="access_log";
open(DAT, "$data_file") || die("Could not open file!");
@raw_data=<DAT>;
close(DAT);
$counter=0;

$a1="";
$appcheckfilter="IP_ADDRESS_OF_SERVER";
$localhostfilter="127.0.0.1";
$filter="";
$b1="";
$datetimefilter="";
$datelog="";
$prevhourlog="";
$hourlog="";
$minutelog="";
$secondlog="";

foreach $line(@raw_data)
   {
      if (($line !~ $appcheckfilter) && ($line !~ $localhostfilter))  {
        ($a1,$filter) = split(/]/,$line);
        ($filter,$b1) = split(/\[/,$a1);
        ($datetimefilter,$filter) = split(/\ -/,$b1);
        ($datelog,$hourlog,$minutelog,$secondlog) = split(/:/,$datetimefilter);
        if ($hourlog eq $prevhourlog) {
           $counter++;
        } else {
           print "$datelog, $prevhourlog: $counter\n";
           $counter=0;
        }
        $prevhourlog = $hourlog;
      }
   }
print "$datelog: $counter\n";