#!/usr/local/bin/perl -w
#
# Copyright (C) Zeus Technology Ltd. 1998
#
# This is a simple perl script which is designed to split the log-files 
# generated from a virtual server which hosts multiple subservers into 
# a log file for each subserver.  
#
# The logfile format should being with "%{Host}i" so the subserver associated
# with each request is also logged.  This would make the CLF format:
#
#  "%{Host}i %h %l %u %t \"%r\" %s %b"
#
# The leading subserver name is chopped off, and the remaining line data
# (in the above case a standard CLF line) is appended to the subserver's
# logfile.
#
# Usage:
#    subserver_logsplit.pl < logfile
#
# This software is provided "as is" and any express or implied warranties, 
# including but not limited to, the implied warranties of merchantability and
# fitness for a particular purpose are disclaimed.  In no event shall 
# Zeus Technology Ltd. be liable for any direct, indirect, incidental,
# special, exemplary, or consequential damaged (including, but not
# limited to, procurement of substitute good or services; loss of use,
# data, or profits; or business interruption) however caused and on
# theory of liability.  Whether in contract, strict liability or tort
# (including negligence or otherwise) arising in any way out of the use
# of this software, even if advised of the possibility of such damage
#
# For further information regarding this piece of software, please email
# support@zeustechnology.com

require 5;

$destination = "./"; # Where we write the logfiles
$bunchup = 5000;     # Amount of lines we bunch up before we process

# ---------

sub flush()
{
    foreach $key (keys %subservers) {
	open(FILE, ">>$destination$key" ) || die "Unable to open file : $!";
	print FILE $subservers{$key};
	close(FILE);
	delete $subservers{ $key };
    }
}

# ---------
$got = 0;

while( <> ) {
    $got++;
    if( !/^([^ ]*) (.*)/ ) { die "Invalid log line $_"; }
    $site = lc( $1 );
    $line = "$2\n";
    if( defined $subservers{$site} ) {
	$subservers{$site} = $subservers{$site} . $line;
    } else {
	$subservers{$site} = $line;
    }
    if( $got >= $bunchup ) { &flush(); $got = 0; }
}

&flush();
