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

#########################################################
# Yu.Semenov ITEP	December 2009			#
# <access_log.filt> development				#
# For time interval $t_m-$dt<$t_m<$tm+$dt calculate the	#
# value L(avr)=sum(L(I)*(1/(1+(ti-tm)^2))		#
# $t_m $L(avr) is written into the file <lens.txt)	#
# First we form an array: time_mark request_length	#
# Then calculate an array of L(avr) with index 0 - 1440	#
#########################################################

use IO::File;
use Fcntl;
use Socket;
use Time::Local;
use Time::localtime;
use Chart::Graph::Gnuplot qw(gnuplot);
use strict;

my ($hours,$minutes,$seconds,$TIME,$date,$date_time,$i,$p_in,$p_dis,$p_ac,$f);
my ($j,$k,$m,$count,$pointer,$LLL,$L1m,$L2m,$T0,$low_limit,$x,$nmt,$st,$p_t);
my ($p_out,$line,$Len1,$Len2,$N_min,$f_start,$start1,$start2,$D,$a_c,$nm);
my ($reqs,$pri_start,$time,$ii,$point1,$point2,$t0,$t1,$t3,$j,$jj,$t1);
my (@L,@len_avr1,@len_avr2,@Disp1,@Disp2,@ac,@leng1,@leng2,@t_m,@length,@rq1,@rq2);
my (@n_val,@t_mark);
my ($y1,$y2,$y3,$y4,$y5,$tmm,$tm,$tm0,$secs,$mins,$day,$mon,$year,$t_mid,$dat1,$dat2);
my ($p,$delt,$y_max,$y_min);

my @mons=("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
my $l_len=1;		# init line index
my $n_req=0;
my $N_rc=0;
my $s_cur=0;
my $day_night=86400;	# seconds in a day
my $n_mins=0;		# number of minute
my $star_TIME=time-$day_night;
print "start-time=",$star_TIME,"\n";
my @records=("","","","","","","","","","","");	#records array
my $dt=600;					# seconds
my %len=();					# hash for record-lengths
my @LEN=();

my ($line,$p1,$p_weight,$year,$month,$day,$Tst,$path1,$t_bin_num,$LEN);
my ($D,$s_length,$tot_num,$pi,$K,$j,$datetime,$res,$DT0,$stop_TIME,$st_TIME);
my ($hours,$minutes,$seconds,$TIME,$date,$date_time,$pr,$p_r,$img,$start_TIME,$cur_TIME);
my ($p_dens,$ip_num,$p,$p_del,$T0,$ip,$p_rec,$ip_adr,$p_len,$pin1,$ptd,$pdel,$counts);
my ($i,$nm,$s,$m,$pin,$key,$delta,$k,$A,$jpg,$p_reqv,$fl,$M,$len,$num,$pp,$end_TIME);
my (@ar,@L,@LL,@le,@MM,@MMM,@NNN,@t_bins,@tm);
my @tml=(0,0,0,0,0,0,0,0,0,0,0,0,0,0);		# array of current time-marks
my @lenl=(0,0,0,0,0,0,0,0,0,0,0,0,0,0);		# array of current record lengths

for($i=0;$i<1441;$i++)	{$t_bins[$i]=0;
			 $tm[$i]=0;}		# day-distribution initiating

#$pin="e:/www/attacks/ids/access_log.filt";	# Basic log-file
$pin="/home/httpd/htdocs/saturn/attacks/ids/access_log.filt";

# Opening the access_log.filt-file
sysopen(AFIL, $pin, O_RDWR) or die$!;

#########################################
# Reading of file <access_log.filt>	#
#########################################

while (!eof(AFIL)) {	# Cycle of reading <access_log.filt>-file
## The file <access_log.filt> development START #
$line=readline(AFIL);						#
@ar=split(//,$line);                            		#
if($#ar>52)	{			# line length control	#
#print "Line=",$line,"\n";					#
$N_rc++;				# Next record		#
@MM=split/\s+/,$line;						#
#########################################################################
# First record in the file <access_log.filt> is a last in time		#
# pool-93-186-96-64.lanta-net.ru - - [22/Nov/2009:04:09:51 +0300]	#
# Date-Time format: [22/Nov/2009:04:09:51 +0300]			#
#########################################################################
$date_time=substr($MM[3],1,20);	# leading [-symbol removing	#
@MMM=split(/\:/,$date_time);					#
if($#MMM==3){		# date terms number control		#
$date=$MMM[0];							#	#
#if($N_rc==1)	{print " DA=",$date,"\n";}			#	#
@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 $mons[$i]){$month=$i;}}	#
if(!(defined$NNN[2]))	{$NNN[2]=2;}				#	#
$year=$NNN[2];							#	#
#if($N_rc==1)	{print " Y=",$year,"\n";}			#	#
$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($N_rc==1)	{					#########
$start_TIME=$cur_TIME;						#	#
#print "start-time=",$cur_TIME,"\n";				#	#
		}					#########
$tm[$n_req]=$cur_TIME;	# time-mark array		#
$le[$n_req]=$#ar;	# lengths-array			#
$len{$cur_TIME}=$#ar;	# hash rec					#
$n_req++;								#
    }			# control number of terms in date ###
	}		# if for line length control
		}	# end of basic WHILE	########################
#print "TM=",$tm[0]," ",$tm[1]," ",$tm[2]," ",$tm[3]," ",$tm[4]," ",$tm[5]," ",$tm[6]," ",$tm[7]," ",$tm[8]," ",$tm[9]," ",$tm[10]," ",$tm[11]," ",$tm[12]," ",$tm[13]," ",$tm[14]," ",$tm[15]," ",$tm[16]," ",$tm[17],"\n";
#print "LE=",$le[0]," ",$le[1]," ",$le[2]," ",$le[3]," ",$le[4]," ",$le[5]," ",$le[6]," ",$le[7]," ",$le[8]," ",$le[9]," ",$le[10]," ",$le[11]," ",$le[12]," ",$le[13]," ",$le[14]," ",$le[15]," ",$le[16]," ",$le[17],"\n";

$stop_TIME=timelocal($seconds,$minutes,$hours,$day,$month,$year);
#$start_TIME=$stop_TIME-$day_night;

#print "stop-time=",$stop_TIME," hrs=",$hours," day=",$day," mon=",$month,"\n";
close(AFIL);			# Close file <access_log.filt>

# template for the file names
$p_rec="/home/httpd/htdocs/saturn/attacks/ids/";
$p_len="/home/httpd/htdocs/saturn/attacks/ids/len_req.txt";			#
$p_weight="/home/httpd/htdocs/saturn/attacks/ids/len_weight.txt";

# Opening the len_req.txt-file
sysopen(Flen, $p_len, O_RDWR|O_TRUNC|O_CREAT) or die$!;
# Opening the len_weight.txt-file
sysopen(Fwgt, $p_weight, O_RDWR|O_TRUNC|O_CREAT) or die$!;

# Writing into <len_req.txt>

for($i=0;$i<$n_req;$i++)	{
if($tm[$i]>$star_TIME)	{print Flen $tm[$i]," ",$le[$i],"\n";}
				}

#################################################
# Start of record array development		#
# The start point = start_TIME+10 minute	#
#################################################

#print "start_TIME=",$start_TIME,"\n";
$t_bin_num=0;				# start bin

for($j=0;$j<1441;$j++)	{

$st_TIME=$start_TIME+$j*60+$dt;
#print "st_TIME=",$st_TIME,"\n";
$M=0;					# local array index
for($K=0;$K<$n_req;$K++)
{	# ARRAY CYCLE #######
if($tm[$K]>=($st_TIME-$dt) && $tm[$K]<=($st_TIME+$dt))
	{$tml[$M]=$tm[$K];
#	print "K=",$K," tm=",$tm[$K]," LL=",($st_TIME-$dt)," HL=",($st_TIME+$dt),"\n";
	 $lenl[$M]=$le[$K];
	 $M++;
	}
}					# end of basic FOR cycle
#print "tml=",$tml[0]," ",$tml[1]," ",$tml[2]," ",$tml[3]," ",$tml[4]," ",$tml[5]," ",$tml[6]," ",$tml[7]," ",$tml[8]," ",$tml[9]," ",$tml[10]," ",$tml[11],"\n";
#print "lenl=",$lenl[0]," ",$lenl[1]," ",$lenl[2]," ",$lenl[3]," ",$lenl[4]," ",$lenl[5]," ",$lenl[6]," ",$lenl[7]," ",$lenl[8]," ",$lenl[9]," ",$lenl[10]," ",$lenl[11],"\n";
#################################
#	L(avr) calculation	#
#################################

$LEN[$j]=0;
for($k=0;$k<$M;$k++)	{
	$A=$st_TIME-$tml[$k];
	$LEN[$j]=$LEN[$j]+$lenl[$k]*(1/(1+$A*$A));
			}
#if($LEN[$j]!=0){print "LEN=",$LEN[$j],"\n";}

#################################################
# Writing into the fime <len_weight.txt>	#
#################################################

#print "st_TIME=",$st_TIME," star_TIME=",$star_TIME,"\n";
#if($st_TIME>$star_TIME)
#{
print Fwgt $st_TIME," ",sprintf("%.1f",$LEN[$j]),"\n";
#}
	}			# End of FOR 1440
close(Flen);
close(Fwgt);

# GNUPLOT

$p_weight="/home/httpd/htdocs/saturn/attacks/ids/len_weight.txt";
sysopen(Fwt, $p_weight, O_RDWR) or die$!;      #
$i=0;
while (!eof(Fwt))       {       	# Cycle for <len_weight.txt>
$line=readline(Fwt);
chop($line);
@L=split/\s/,$line;
$t_mark[$i]=int($L[0]);
$n_val[$i]=$L[1];
#print "n_val=",$n_val[1],"\n";
if($n_val[$i]>$y_max)   {$y_max=$n_val[$i];}
if($n_val[$i]<$y_min)   {$y_min=$n_val[$i];}
$i++;
                        }
print "T-m=",$t_mark[0];
	$delt=sprintf("%.4f",($y_max-$y_min)/5);
	$y_min=sprintf("%.4f",$y_min);
print "y_min=",$y_min," delt=",$delt,"\n";
	$y1=$y_min;
	$y2=$y_min+$delt;
	$y3=$y_min+(2*$delt);
	$y4=$y_min+(3*$delt);
	$y5=$y_min+(4*$delt);

print "Weihgt-y1=",$y1," y2=",$y2," y3=",$y3," y4=",$y4," y5=",$y5," ymin=",$y_min,"\n";
	$t_mid=$t_mark[720];
	$tm0=$t_mark[0];
	$tmm=$t_mark[1439];
print "TM0=",$tm0," TMM=",$tmm,"\n";
	$tm=localtime($tm0);
	$year=$tm->year+1900;
	$mon=($tm->mon);
	$mon=$mons[$mon];
	$day=$tm->mday;
	$secs=$tm->sec;
	$hours=$tm->hour;
	$mins=$tm->min;
$dat1=$year."-".$mon."-".$day." ".$hours.":".$mins.":".$secs;
print "DATE1=",$dat1,"\n";
			
	$tm=localtime($tmm);
	$year=$tm->year+1900;
	$mon=($tm->mon);
	$mon=$mons[$mon];
	$day=$tm->mday;
	$secs=$tm->sec;
	$hours=$tm->hour;
	$mins=$tm->min;
$dat2=$year."-".$mon."-".$day." ".$hours.":".$mins.":".$secs;
print "Y-max=",$y_max," tm0=",$tm0," tmm=",$tmm," mid=",$t_mid,"\n";

gnuplot({'title' => 'The last day requests ('.$dat1.' - '.$dat2.')',
        'x2-axis label' => 'Weighted distribution +10min -10min',
	'y-axis label'=> 'weighted request length',
	'x-axis label' => 'time',
	'size'=>[1.0,0.4],
	'output type' => 'gif',
	'output file' => '/home/httpd/htdocs/saturn/attacks/ids/weighting.gif',
	'ytics' => [$y1,$y2,$y3,$y4,$y5],
	'extra_opts' => 'set nokey',
	'uts' => [$tm0, $tmm,1,1]},

	[{'title' => 'data1',
	'style' => 'lines',
	'type' => 'file'}, '/home/httpd/htdocs/saturn/attacks/ids/len_weight.txt'],
	);
close(Fwt);

#print "weighting","\n\n";

exit;
#END { }
