Never been to DZone Snippets before?

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

About this user

Craig Spurrier craigweb.net

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

A tool to split log files by hostname

This script can be used to split a log file by the hostname in the request. It is designed for use with lighttpd virtual hosting, to prepare the log for Webalizer. It should be run with a cron job and CLI PHP.

   1  
   2  <?php
   3  // Copyright (C) 2006 Craig Spurrier
   4  // Released under the terms of the MIT/expat license.
   5  
   6  $serverip = "127.0.0.1"; //Set to your IP address or default hostname, so requests without hostnames can be sorted 
   7  $log = file_get_contents("/var/log/lighttpd/access.log"); //Log location
   8  $fh = fopen("/var/log/lighttpd/access.log", 'w'); //Clear the log file
   9  fclose($fh);
  10  $lines = explode ("\n", $log);
  11  foreach ($lines as $line){
  12  $parts = explode (" ", $line);
  13  $hostname = strtolower($parts[1]);
  14  if (substr($hostname, 0, 4) == 'www.'){$hostname = substr($hostname, 4);} //Treat www. as the same as the www-less version
  15  if ($hostname == '-'){$hostname = $serverip;} //Set a hostname for request without hostnames
  16  $fh = fopen("/var/log/lighttpd/users/$hostname.access.log", 'a'); //Add the entry to the log for that hostname. Make sure this script can write to this location.
  17  fwrite($fh, "$line\n");
  18  fclose($fh);
  19  }
  20  ?>
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS