#!/usr/bin/perl
# CONFIG: Change the first line to indicate the Perl binary on your system.
#          Try "which perl", "whereis perl", or "find / -name perl -print"

############################################################## -*- Perl -*- ###
#                                                                          
#     AccessWatch v2.0 - Traffic analysis for World Wide Web sites          
#      IIS Logfile converter
#      Copyright 1994-2001 David Maher. All rights reserved.          
#      http://accesswatch.com/                                              
#                                                                           
#     Contact Information:                                                  
#        AccessWatch                                                          
#        PO Box 60, Shady Side, MD 20764 USA
#                                                                             
#     Please direct comments to:                                              
#        http://accesswatch.com/comments/
#                                                                             
#     Please read the included license agreement (README) before          
#      using this program. Your use of this software indicates your           
#      acceptance of the license agreement and warranty.                      
#      A current copy of the license agreement is online at 
#         http://accesswatch.com/license/                                   
#
#     If you wish to modify this code, mail changes@accesswatch.com.
#     We don't have a problem with modifications by registered users,
#     but redistribution is prohibited. Just write us first.
#                                                                             
#  Credits and thanks for great ideas:
#
#   A huge thank you to Paul Blackman <ictinus@lake.canberra.edu.au> for      
#   his work on hourly server statistics, including a table of hourly       
#   accesses/hour.                                                          
#                                                                           
#   Thanks to Jeff Boulter <jeff@boulter.com> for access error        
#   exclusion code, referer log parsing tips, and for his many suggestions  
#   and debugging help.                                                     
#                                                                           
#   Many thanks for suggestions and code tweaks:                              
#
#      Chris Brown          <cwb3@ra.msstate.edu>                          
#      Ron Gery             <rong@halcyon.com>                               
#      Lars Bo Eriksen      <lb@iesd.auc.dk>                                 
#      Tieshy Lin           <ted@iis.sinica.edu.tw>                         
#      Chris Derossi        <derossi@genmagic.com>                           
#      Arthur Hagen         <art@broomstick.com>                             
#      Eric Ruck            <eruck@ahoynet.com>                                
#      Tim Dobbelaere       <storem@cocoon.net> 
#      Jean-Francois Monnet <jieff@odie.mcom.fr> 
#                                                                           
###############################################################################

my(%logmonths) = (
		  '01' => 'Jan',
		  '02' => 'Feb',
		  '03' => 'Mar',
		  '04' => 'Apr',
		  '05' => 'May',
		  '06' => 'Jun',
		  '07' => 'Jul',
		  '08' => 'Aug',
		  '09' => 'Sep',
		  '10' => 'Oct',
		  '11' => 'Nov',
		  '12' => 'Dec'
		  );

MAIN: {

    my($iislog) = $ARGV[0];

    if (! -f $iislog) {
	print "IIS log file '$iislog' not found.\n";
	print "Please specify a full path name - eg. iisconvert ex980920.log\n";
	exit;
    }
    
    my($clflog) = $iislog;

    $clflog =~ s/\.log$//i;
    $clflog .= '.clf';

    open (IISLOG, "<$iislog") || die "Coudl not open $iislog: $!\n";
    open (CLFLOG, ">$clflog") || die "Coudl not open $clflog: $!\n";

    while (<IISLOG>) {
	next if /^\#/;

	chomp;

	my($date, $time, $remote, $username, $sitename,
	   $computername, $computerip, $method,
	   $url, $query, $status, $winstatus, $sourcebytes, 
	   $remotebytes, $timetaken, $port, $version,
	   $agent, $cookie, $referer) = split(/ /);

	if ($referer ne '-' && $referer ne '') {
	    $referer = "\"$referer\"";
	}
	else {
	    $referer = '"-"';
	}

	if ($agent ne '' && $referer ne '') {
	    $agent =~ s/\+/ /g;
	    $agent = "\"$agent\"";
	}
	else {
	    $agent = '"-"';
	}

	my($year, $month, $day) = split(/-/, $date);

	print CLFLOG "$remote $username - [$day/$logmonths{$month}/$year:$time -0000] \"$method $url $version\" $status $remotebytes $referer $agent\n";

    }
    close IISLOG;
    close CLFLOG;

    print "Conversion complete - common log format $clflog was created.\n";
}

