#!/usr/bin/perl
print "Content-type: text/html \n\n";

#########################################
# Yu. Semenov	23-Jul-2010		#
# Calculation of file <Time_leng.txt>	#
# on the base of content		#
# <access_log.filt>			#
#########################################

use IO::File;
use Fcntl;
use Socket;
use Time::localtime;
use Time::Local;
use strict;

my ($con, $res,$start_TIME,$stop_TIME,$cur_TIME,$date,$day,$month,$s_cur,$year,$TIME,$stam,$K,$l_sum);
my ($ppp,$T0,$count,$delta,$m);
my ($line,$p,$i,$minutes,$hours,$seconds,$date_time,$record,$j,$k,$pp,$sum_len,$tm,$time_start,$time_stop);
my (@MMM,@records,@ar,@NNN,@MM);
my @mon=("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
my @mon_len=(31,28,31,30,31,30,31,31,30,31,30,31);
my (@ary,@nam, @fields);		# array, retured with request
my %rec=();
my $n_req=0;
my $N_rc=0;
my $n_reqt=0;
my $length=0;

$tm=localtime;
#($day,$month,$year)=(localtime)[3..5];
$year=($tm->year);
$month=($tm->mon);
$day=$tm->mday;

print " year=",$year," mon=",$month," day=",$day,"\n";

$p="/home/httpd/htdocs/saturn/attacks/ids/access_log.filt";	# input file  <access_log.filt>
open(h_point,$p) or die "Can't start tst1: $!\n";

$pp="/home/httpd/htdocs/saturn/attacks/ids/time_2.txt";
sysopen(Ft_l1, $pp, O_RDWR|O_TRUNC|O_CREAT) or die$!;	# The output file
$ppp="/home/httpd/htdocs/saturn/attacks/ids/leng_2.txt";
sysopen(Ft_l2, $ppp, O_RDWR|O_TRUNC|O_CREAT) or die$!;	# The output file

#$day=5;						# for debugging	!!!!!!!!!!!!!!!!!!!!!!!
$stop_TIME=timelocal(0,$minutes,$hours,$day,$month,$year);
if($day==0){$month=$month-1;
	    $day=$mon_len[$month];
	    }
else{$day=$day-1;}
$start_TIME=timelocal(0,$minutes,$hours,$day,$month,$year);
print "Start=",$start_TIME," Stop=",$stop_TIME,"\n";

$K=0;					# Basic counter
#####################################
while (!eof(h_point)) 	{		# Cycle for <access_log.filt>-file
	$line=readline(h_point);	#
	@ar=split(//,$line);            #
if($#ar>52)	{			# line length control	#########
	$N_rc++;			# Next record		#	#
@MM=split/\s+/,$line;						#	#
$date_time=substr($MM[3],1,20);	# leading [-symbol removing	#	#
@MMM=split(/\:/,$date_time);	#					#
if($#MMM==3){			# date terms number control #####	#
	$date=$MMM[0];						#	#
	@NNN=split(/\//,$date);					#	#
if(!(defined$NNN[0]))	{$NNN[0]=1;}				#	#
	$day=$NNN[0];						#	#
if(!(defined$NNN[1]))	{$NNN[1]=1;}				#	#
	$month=$NNN[1];						#	#
for($i=0;$i<12;$i++){			#########		#	#
	if($month eq $mon[$i]){$month=$i;}	#		#	#
		    }			#########		#	#
if(!(defined$NNN[2]))	{$NNN[2]=2;}				#	#
	$year=$NNN[2];						#	#
	$year=$year-1900;					#	#
if(!(defined$MMM[1]))	{$MMM[1]=1;}				#	#
	$hours=$MMM[1];						#	#
if(!(defined$MMM[2]))	{$MMM[2]=1;}				#	#
	$minutes=$MMM[2];					#	#
if(!(defined$MMM[3]))	{$MMM[3]=1;}				#  	#
	$seconds=$MMM[3];					#	#
	$cur_TIME=timelocal($seconds,$minutes,$hours,$day,$month,$year);
#print " year=",$year," mon=",$month," day=",$day," hour=",$hours," min=",$minutes," sec=",$seconds,"\n";
			}		#####	#
		}			#########

if($K==0)	{			# The first record analysis 	#####
	$T0=$cur_TIME;						#
	$s_cur=$T0;			# start time seconds	#
	$count=0;						#
#	$star_TIME=$T0;			# start time for array	#
#print "start_time=",$star_TIME,"\n";				#
$K++;								#
#	$DT0=$datetime;			#
			}		# end of start time development	#####
#print "start=",$start_TIME," cur=",$cur_TIME," stop=",$stop_TIME,"\n";
if($cur_TIME>$start_TIME && $cur_TIME<$stop_TIME)	{	#############
#print "start=",$start_TIME," cur=",$cur_TIME," stop=",$stop_TIME,"\n";
	@ar=split(//,$line);		#	#
	$length=$#ar;				# symbols in current line#

$delta=$cur_TIME-$s_cur;		# Is this record in the same second?
if($delta<0){print "delta=",$delta," count=",$count," Sec=",$seconds,"\n";}
if($delta<0){print "r=",$line,"\n";}	# if time flop				#

if($delta==0)	{			# if inside a second	#####		#
$l_sum=$l_sum+$length;			#			#
$count++;				# records in sec	#		#
		}	#####		#
else	{				# it is not in the same secnd	#####	#
		$count=0;		#	#
		$s_cur=$cur_TIME;	# renew current second	#	#
if($l_sum==0)	{$l_sum=$length;}				#	#
print	Ft_l1 $cur_TIME,"\n";	# Write file <time_2.txt>	#	#
print	Ft_l2 $l_sum,"\n";		# Write file <leng_2.txt>	#	#
		$l_sum=0;		#	#
		}			#####	#
	}	#############
}		# end of basic WHILE	########################
exit(0);
