#!/usr/local/bin/perl

#-----------------------------------------------------------------------------#
# This is an experimental version of the loganalyzer which will consist of a 
# three routine procedure. First the stripping of the access of unwanted
# informattion such as images and movie files. This will considerably reduce
# the size of the log file. The next step is to convert these log files into a
# format that is acceptable to the database.

# This is written by Deepak Ramanathan email: deepak@npac.syr.edu

#-----------------------------------------------------------------------------#

#-----------------------------------------------------------------------------#
#   Log Analyzer - Main
# This is the main portion of the program from which the other sub routines
# are called. 
#-----------------------------------------------------------------------------#

&Main;

sub Main {
 print "The LogAnalyzer is  starting.\n"; 
    &ParseLog; 
    &PruneLog; 
    &LoadData; 
    exit 0; 

#----------------------------------------------------------------------------# 
#    Log Analyzer - The Stripper
#    Purpose - Removes the superflous information in the logs such as gifs, 
#    jpegs. This can be configured to meet users requirements.
#-----------------------------------------------------------------------------#

sub ParseLog {

$out = '/tmp/stripaccess.log';  # Location of the stripped accesslog
$in = '/project/C067/logs/tmp/access_log'; # Location of the Web server logs 

open(R,"<$in");
open(W,">$out");

	while($l = <R>) {
	if($l =~ /gif HTTP/) { 
}elsif($l =~ /jpg HTTP/) { 
}elsif($l =~ /search.syr.edu/) { 
}elsif($l =~ /xbm HTTP/) { 
}elsif($l =~ /GIF HTTP/) { 
}else{ 
print W "$l"; 
    }	
}
}
#-----------------------------------------------------------------------------#
#     Log Analyzer - The Formatter
#     Purpose - Arranges the stripped log file into a format that the database
#     likes and appreciates
#-----------------------------------------------------------------------------#

sub PruneLog {

$in = '/tmp/stripaccess.log'; # Location of stripped log file
$out ='/tmp/pruned.log';	# Location of file to be dumped
$domaincodes = 'domain.desc';	# Location of domain description codes
open (Deep,"<$in");	

	open (Jethro,">$out");
	while ($line =<Deep>) {
    ($remote, $dash1, $dash2, $date, $tz, $method, $page, $protocol, $protnum1, $size)=
split(/\s/,$line);
	$date =~ s/\[//;
	local($date, $hour, $min, $sec) = split(":", $date);
	local($date, $dd, $mmm, $yyyy) = split("/", $date);
	$domainExt = $remote;
	$domainExt =~ s/.*\.//g; 
	$domainExt = "IP" if ($domainExt =~ /[0-9].*/); 
	$domainExt =~ tr/[a-z]/[A-Z]/; 
	$domain = $domainExt;
	open (CODES, "<$domaincodes") || die "Couldn't open $domaincodes\n";  
	while ($desc =<CODES>) { 
    chop($desc); 
	if($desc =~ /(\w+)\s(.*)/) {
    $description = $2;
	$code = $1;  
	}
    
    if ($domain eq $code) {
$free = $description;
	}
    }

$method =~ s/\"//g;
	$protnum1 =~ s/\"//g;
	
	
	print Jethro "$remote\t$mmm-$dd-$date $hour:$min:$sec\t$method\t$page\t$protocol\t$protnum1\t$size\t$free\n" ||
    die "I cant help it Captain";  
	
	}
#-----------------------------------------------------------------------------#
#      Log Analyzer - The Loader
#      Purpose - The portion of the script that loads the adat into the 
#      database
#-----------------------------------------------------------------------------#

sub LoadData {
$cmd = "msql GIS -c \"micopy Access_Logs from '/tmp/pruned.log';\"";
	exec $cmd;
	$done = 0;
	} 
    }
}




