#!/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

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

require $base.'loganalyzer.cfg' || die "Configuration file not found: $!\n";
#-----------------------------------------------------------------------------#
#   Log Analyzer - Main
# This is the main portion of the program from which the other sub routines
# are called. 
#-----------------------------------------------------------------------------#

&Main;

sub Main {
$startTime = time;
	$currdate = &SetDateInfo; # returns Day/Mon/Year 
    
    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 = "$accessLog"; # Location of the Web server logs 

 local($remote, $dash1, $dash2, $date, $tz, 
	$method, $page, $protocol, $protnum1, $size) = split(/\s/,$line);
	open(R,"<$in") || die "Could not open Access Log file\n";
	open(W,">$out");

&FastSearch;	

	while($line = <R>) {
    
    if (/$currdate/i) {
         chomp;
	}
        if($line =~ /$excludeurl/) {
    }else{ 

print W "$line"; 
	}
    }
}

sub SetDateInfo {
#get current date and return as string (day/month/year)... 
@mnths = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
	'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'); 
	@longmonths = ('January', 'February', 'March', 'April', 'May',
	'June', 'July', 'August', 'September', 'October',
                   'November', 'December'); 
		   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); 
		       $year = "19$year"; 
		       $mday = "0$mday" if ($mday < 10); 
	return "$mday/$mnths[$mon]/$year"; 
	}
    
#-----------------------------------------------------------------------------#
#  AccessWatch function - FastSearch
#    Purpose  : Scans quickly from the bottom of the access log to start
#                   of the current day.
#-----------------------------------------------------------------------------#
sub FastSearch {
    # parses through access log from the bottom up for speed, searching
    #  for the start of the current day...
    local($avgday) = 10000 * $hour + 1000;
    local($logsize) = (-s LOG);
    local($logoffset) = 0;
    local($jumps) = 0;

    $logoffset = $logsize - $avgday if ($logsize > $avgday);

    seek (LOG, $logoffset, 0);
    <LOG>;
    $_=<LOG>;
    while (/$currdate/i && $logoffset > 0 && <LOG>) {
	$jumps++;
	print "$_\n";
	$logoffset = $logoffset - 5000;
	$logoffset = 0 if ($logoffset <= 0);
	seek (LOG, $logoffset, 0);
	<LOG>;
	$_=<LOG>;
    }
# print "***Jumps = $jumps***\n";
}

    
#-----------------------------------------------------------------------------#
#     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.dat';	# 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$page\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 data into the 
#      database
#-----------------------------------------------------------------------------#

sub LoadData {
$cmd = "sqlload userid=deepak/deepak control=loader.ctl" || die "It failed\n";
	exec $cmd;
	$done = 0;
	} 
    }
}

