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

#########################################
# Yu.Semenov ITEP	August 2010	#
# GNUPLOT for CHART-Graph-3.2 		#
# Files visits.txt, requests.txt	#
# devolopment and forming filesq	#
# visits.gif and bytes.gif		#
#########################################

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

my $y_max=0;
my (@t_mark,@n_val,@len,@L);
my ($i,$j,$line,$tmm,$tm0,$tm,$year,$mon,$day,$hours,$mins,$secs);
my ($y1,$y2,$y3,$y4,$y5,$z1,$z2,$z3,$z4,$z5);
my ($p,$p1,$p2,$dat1,$dat2,$t_mid,$len_max);
my @mons=("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

$i=0;
$y1=0;
$y2=0;
$y3=0;
$y4=0;
$y5=0;
$tmm=0;
$p="/home/httpd/htdocs/saturn/attacks/ids/visits.txt";
open(AFT, $p) or die$!;      #
#$p1='/home/httpd/htdocs/saturn/attacks/ids/requests.txt';
#sysopen(p_req, $p1,O_WRONLY|O_APPEND|O_CREAT) or die "Can't open $p1; $!\n";
open(p_req, "> $p1") or die "Can't open $p1; $!\n";
$p2='/home/httpd/htdocs/saturn/attacks/ids/bytes.txt';
#sysopen(p_byt, $p2,O_RDWR) or die "Can't open $p2; $!\n";
open(p_byt,"< $p2") or die "Can't open $p2: $!\n";

while (!eof(AFT))       {       # Cycle for <visits.txt>
$line=readline(AFT);
chop($line);
@L=split/\s/,$line;
$t_mark[$i]=$L[0];
$n_val[$i]=$L[1];

print "n_val=",$n_val[0],"\n";
if($n_val[$i]>$y_max)   {$y_max=$n_val[$i];}	# $y_max calculation
$i++;
                        }
$i=0;
while (!eof(p_byt))     {       # Cycle for <bytes.txt>
$line=readline(p_byt);
print "LINE=",$line,"\n";
chop($line);
@L=split/\s/,$line;
$t_mark[$i]=$L[0];
$len[$i]=$L[1];
print "Len=",$len[0],"\n";
if($len[$i]>$len_max)  {$len_max=$len[$i];}    # $len_max calculation
$i++;
                        }

$y1=int($y_max/5);
$y2=$y1*2;
$y3=$y1*3;
$y4=$y1*4;
$y5=$y_max;
$z1=int($len_max/5);
$z2=$z1*2;
$z3=$z1*3;
$z4=$z1*4;
$z5=$len_max;

print "y1=",$y1," y2=",$y2," y3=",$y3," y4=",$y4," y5=",$y5,"\n";
print "z1=",$z1," z2=",$z2," z3=",$z3," z4=",$z4," z5=",$z5,"\n";

$tmm=$t_mark[$i-1];			# maximum time-mark
$tm0=$t_mark[0];			# minimum time-mark
$t_mid=int(($tmm+$tm0)/2);		# middle time-mark
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;
$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 "date1=",$dat1," date2=",$dat2,"\n";
print "Y-max=",$y_max," tm0=",$tm0," tmm=",$tmm," mid=",$t_mid,"\n";			

gnuplot({'title' => 'Requests diagram('.$dat1.' - '.$dat2.')',
        'x2-axis label' => 'time',
	'size' => [1.2, 0.6],
	'y-axis label'=> 'Requests distribution, request/day',
	'x-axis label' => 'time',
	'output type' => 'gif',
	'output file' => '/home/httpd/htdocs/saturn/attacks/ids/visits.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/visits.txt'],
);

gnuplot({'title' => 'Requests diagram('.$dat1.' - '.$dat2.')',
        'x2-axis label' => 'time',
	'size' => [1.2, 0.6],
	'y-axis label'=> 'Total input flow, bytes/day',
	'x-axis label' => 'time',
	'output type' => 'gif',
	'output file' => '/home/httpd/htdocs/saturn/attacks/ids/bytes.gif',
	'ytics' => [$z1,$z2,$z3,$z4,$z5],
	'extra_opts' => 'set nokey',
	'uts' => [$tm0, $tmm,1,1]
	    },
									
        [{'title' => 'data2',
        'style' => 'lines',
	'type' => 'file'}, '/home/httpd/htdocs/saturn/attacks/ids/bytes.txt'],
    );

