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

use IO::File;
use Socket;
use Time::localtime;

my ($con, $res);
my (@ary,@len);			# array, retured with request
my $count=0;

#########################################
# Yu. Semenov				#
# Script reads files:			#
# <time_length.txt>, build the file	#
# <rate_distrib.txt>			#
# <avr_len.txt> -> <len_distrib.txt>	#
#########################################

#########################################################
# Calculation of the request's length distribution      #
#########################################################

$p="/home/httpd/htdocs/saturn/attacks/ids/time_length.txt";	# input file  <time_length.txt>
open(h_point,$p) or die "Can't start tst1: $!\n";
$i=0;
$max=0;
while (!eof(h_point)) 	{		# Cycle for <time_length.txt>-file
$line=readline(h_point);		# read line of <time_length.txt>
@nam=split(/\s/,$line);
$len[$i]=$nam[1];
if($len[$i]>$max){$max=$len[$i];}	# Max length definition
$i++;
			}		# end of WHILE
$mmm=int($max/30)+1;			# $mmm = (Max length)/30 (number of bins)
for($j=0;$j<$mmm;$j++)	{$ary[$j]=0;}	# clear distribution array
for($j=0;$j<$i;$j++)	{
	$ary[int($len[$j]/30)]++;	# length-distributin calculation
			}

#print "NNN=",$i," max=",$max," num=",$#ary,"\n";

$pp="/home/httpd/htdocs/saturn/attacks/ids/rate_distrib.txt";
sysopen(FH, $pp, O_RDWR|O_TRUNC|O_CREAT) or die "Can't start tst2: $!\n";
for($j=0;$j<$mmm;$j++)	{
print FH $j*30," ",$ary[$j],"\n";	# Writing into file <rate_distrib.txt>
			}		# Length-distribution
close(h_point);
close(FH);

#print "rate_distrib.txt\n";

#################################################################
# Calculation of the average request's length distribution	#
#################################################################

$p="/home/httpd/htdocs/saturn/attacks/ids/avr_len.txt"; # input file  <avr_len.txt>
open(h_point,$p) or die "Can't start tst1: $!\n";
$i=0;
$max=0;
while (!eof(h_point))   {               # Cycle for <avr_len.txt>-file
$line=readline(h_point);                # read line of <avr_len.txt>
@nam=split(/\s/,$line);
$len[$i]=$nam[1];
if($len[$i]>$max){$max=$len[$i];}       # Max average length definition
$i++;
            		}               # end of WHILE
$mmm=int($max/30)+1;
for($j=0;$j<$mmm;$j++)  {$ary[$j]=0;}   # clear distribution array
for($j=0;$j<$i;$j++)    {
$ary[int($len[$j]/30)]++;
			}

#print "NNN=",$i," max=",$max," num=",$#ary,"\n";
									
$pp="/home/httpd/htdocs/saturn/attacks/ids/avr_len_distrib.txt";
sysopen(FH, $pp, O_RDWR|O_TRUNC|O_CREAT) or die "Can't start tst2: $!\n";
for($j=0;$j<$mmm;$j++)  {
print FH $j*30," ",$ary[$j],"\n";
			}
close(h_point);
close(FH);

#print "len_distrib.txt\n";

#############################################################
# Calculation of the RMS request's length distribution      #
#############################################################

$p="/home/httpd/htdocs/saturn/attacks/ids/disper.txt"; # input file  <disper.txt>
open(h_point,$p) or die "Can't start tst1: $!\n";
$i=0;
$max=0;
while (!eof(h_point))   {               # Cycle for <disper.txt>-file
$line=readline(h_point);                # read line of <disper.txt>
@nam=split(/\s/,$line);
$len[$i]=$nam[1];
if($len[$i]>$max){$max=$len[$i];}       # Max average length definition
$i++;
                        }               # end of WHILE
$mmm=int($max/30)+1;
for($j=0;$j<$mmm;$j++)  {$ary[$j]=0;}   # clear distribution array
for($j=0;$j<$i;$j++)    {
$ary[int($len[$j]/30)]++;
                        }

#print "NNN=",$i," max=",$max," num=",$#ary,"\n";

$pp="/home/httpd/htdocs/saturn/attacks/ids/disper_distrib.txt";
sysopen(FH, $pp, O_RDWR|O_TRUNC|O_CREAT) or die "Can't start tst2: $!\n";
for($j=0;$j<$mmm;$j++)  {
print FH $j*30," ",$ary[$j],"\n";
                        }
close(h_point);
close(FH);
#print "disper_distrib.txt\n";
exit(0);
