#!/usr/bin/perl
print "Content-type: text/html \n\n";

################################################################
# Read file <time_length.txt> and calculate distributions	#
# of average length (L_AVR) for every minute, dispersions 	#
# and avto_correlation coefficients				#
# L_AVR =SUM(length)/N, N - request's number per minute		#
################################################################

use IO::File;
use Socket;
use Time::Local;
use Time::localtime;
use strict;

my ($hours,$minutes,$seconds,$TIME,$date,$date_time,$i,$p_in,$p_dis,$p_ac);
my ($j,$k,$m,$count,$pointer,$LLL,$L1m,$L2m,$T0,$low_limit,$x,$nmt,$st,$p_t);
my ($p_out,$line,$Len1,$Len2,$count1,$count2,$N_min,$f_start,$start1,$start2,$D,$a_c,$nm);
my ($reqs,$pri_start,$time,$ii);
my (@L,@len_avr1,@len_avr2,@Disp1,@Disp2,@leng1,@leng2,@t_m,@length,@rq1,@rq2);
my $delta=60;					# seconds
$f_start=0;
my $r_count=0;				# record counter
my $sum=0;					# products sum
$Len1=0;
$N_min=0;					# minute counter
my @le1;					# length array for the first of adjacent minutes
my @le2;					# second minute array
my $dl0=60;					# time inrevals lag
my $dl1=120;					# interval length for avto-correlation
my $dl3=180;
$count1=0;
$count2=0;
for($i=0;$i<200;$i++){$leng1[$i]=0;}	# Arrays initiating
for($i<0;$i<1440;$i++)	{
	$len_avr1[$i]=0;			# avr-length array
	$Disp1[$i]=0;				# dispersion array
	$rq1[$i]=0;	}
$p_in="/home/httpd/htdocs/saturn/attacks/ids/time_length.txt";	# The input file <time_length.txt>
sysopen(Fin, $p_in, O_RDWR) or die$!;				# Opening the input file

# Output <avr_len.txt>
$p_out="/home/httpd/htdocs/saturn/attacks/ids/avr_len.txt";	# The output file <avr_len.txt>
sysopen(Fout, $p_out, O_RDWR|O_TRUNC|O_CREAT) or die$!;		# Opening the output file

# Output <disper.txt>
$p_dis="/home/httpd/htdocs/saturn/attacks/ids/disper.txt";	# The output file <disper.txt>
sysopen(Fdis, $p_dis, O_RDWR|O_TRUNC|O_CREAT) or die$!;		# Opening the dispersion file

$p_t="/home/httpd/htdocs/saturn/attacks/ids/req_time.txt";	# The output file <req_time.txt>
sysopen(Ftim, $p_t, O_RDWR|O_TRUNC|O_CREAT) or die$!;		# Opening the req_time file

while (!eof(Fin)) 	{			# Cycle for <time_length>-file		#########
$line=readline(Fin);				# read line of <time_length.txt>		#
chop $line;
@L=split(/\s/,$line);			# The line is converted into a list of param-value	#
$t_m[$r_count]=$L[0];			# array of time-marks					#
if($f_start==0) {	$start1=$t_m[0];	# Start time for time distribution		#
			$pri_start=$start1;							#
			$f_start=1;
#print "start=",$t_m[0],"\n";
		}

$length[$r_count]=$L[1];			# Array of request lengths			#
$r_count++;					# Record counter				#
			}			# end of WHILE				#########

#print "FINISH While\n";
#print "Nmin=",$N_min," r_count=",$r_count,"\n";

for($i=0;$i<$r_count; $i++)	{		# looking through all records			########
if($t_m[$i]<($start1+$dl0))										#
            { $Len1=$Len1+$length[$i];	#						########	#
	      $leng1[$count1]=$length[$i];	# form a local array				#	#
	      $rq1[$N_min]++;		# requests number in this minute			#	#
#print "L=",$Len," count=",$count," T=",$t_m[$count],"\n";					#	#
	      $count1++;			# upgrade record number for current minute	#	#
            }											########	#
else{if($count1==0)	{			# if no requests in a minute ####### #########		#
			$len_avr1[$N_min]=0;						#	   #		#
                        $Disp1[$N_min]=0;						#	   #		#
                     }			# end IF		###############	   #		#
if($count1!=0) {$len_avr1[$N_min]=sprintf("%.3f",$Len1/$count1); # average length for current minute
#print "LEN=",$Len," count=",$count1,"\n";						   #		#

for($j=0;$j<$count1;$j++)	{								   #		#
$D=$D+($leng1[$j]-$len_avr1[$N_min])*($leng1[$j]-$len_avr1[$N_min]);			   #		#
				}
if($count1>2)	{
$Disp1[$N_min]=sprintf("%.3f",sqrt($D/($count1-1)));  # Dispersion for the current minute		#
		}
else{$Disp1[$N_min]=0;}
		}
for($k=0;$k<200;$k++){$leng1[$k]=0;}	# Clear leng-array					   #		#
    $start1=$start1+$dl0;			# change start time				   #		#
	$D=0;
	$count1=0;										   #		#
	$Len1=0;
#print "NN=",$rq1[$N_min],"\n";									   #		#
	$N_min++;                       	# Next minute				           #		#
	$i=$i-1;										   #		#
    }                                   	# end of ELSE					####		#
}						# end of FOR						########
#print "N_min=",$N_min,"\n";

#################################
#   Writing output files	#
#################################

for($i=0;$i<1440;$i++)	{		#########
						#
###### File avr_len.txt writing ###########	#
						#	 #
$time=$pri_start+$i*$dl0;			#	 #
if($len_avr1[$i] eq "") {$len_avr1[$i]=0;}#	#
print Fout $time," ",$len_avr1[$i],"\n";	#	 #
###########################################	#
						#
# Files disper.txt & req_time.txt  ########	#
print Fdis $time," ",$Disp1[$i],"\n";	#	#
print Ftim $time," ",$rq1[$i],"\n";	#	#
###########################################	#
			}		#########
close(Fin);
close(Fout);
close(Fdis);
close(Ftim);

do "/home/crontab_files/distr_calc.pl";		##############
print "</head></html>";
END { }
