#!/usr/bin/perl -w

# $Id: create_graphs.pl,v 1.4 2006/09/29 11:38:02 mtsouk Exp $
#
# Please note that this code is
# provided without and guarantees.
#
# Programmer: Mihalis Tsoukalos
# Date: Wednesday 27 September 2006
#
# This Perl script creates the images
# using the data from parse_tcpshow.pl
#

# This library is for creating
# graphic images of the data.
use GD;
use GD::Graph::lines;
use GD::Graph::points;
use GD::Graph::bars3d;
use GD::Graph::pie3d;
use strict;

PACKET_TIME("packet_time.png");

TCP_PACKET_TIME("tcp_packet_time.png");

SOURCE_PORT_COUNT("source_port_count.png");

DEST_PORT_COUNT("dest_port_count.png");

PACKET_TYPE_COUNT("packet_type_count.png");

exit 0;


sub PACKET_TIME
{
	my $filename = shift;
	my $graph = new GD::Graph::lines(1600, 1200);
	my $input = "PACKET_TIME.data";
	my $line;
	my $data = GD::Graph::Data->new();

	open ( INPUT, " < $input " )
	  or die "Cannot open $input: $!\n";
	
	while ( defined ($line=<INPUT>) )
	{
		my @row=();
		chomp $line;
		@row = split(/\s/, $line);
		$data->add_point(@row);
	}
	
	close ( INPUT )
	  or die "Cannot close INPUT: $!\n";
	
	$graph->set(
		x_label => "Time",
		y_label => "Number of Packets",
		title   => "Number of packets per minute",
	);
	
	my $gr = $graph->plot($data)
	  or die "Cannot plot: ".$graph->error.": $!\n";

	open ( PICTURE, " > $filename " )
	  or die "Cannot open $filename: $!\n";
	binmode PICTURE;
	print PICTURE $gr->gif;
	close ( PICTURE )
	  or die "Cannot close PICTURE: $!\n";
}

sub TCP_PACKET_TIME
{
	my $filename = shift;
	my $graph = new GD::Graph::points(1600, 1000);
	#
	# * * *
	# * * * I had to sort -n this file first.
	# * * * and remove : from time column.
	# * * *
	#
	my $input = "TCP_PACKET_TIME.data";
	my $line;
	my $data = GD::Graph::Data->new();
	
	open ( INPUT, " < $input " )
	  or die "Cannot open $input: $!\n";
	
	while ( defined ($line=<INPUT>) )
	{
		my @row=();
		chomp $line;
		@row = split(/\s/, $line);
		$data->add_point(@row);
	}
	
	close ( INPUT )
	  or die "Cannot close INPUT: $!\n";
	
	$graph->set(
		y_label			=> "Time",
		x_label 		=> "TCP Packet serial number",
		x_label_skip	=> 500,
		title   		=> "TCP Packet serial number versus Time",
	);
	
	my $gr = $graph->plot($data)
	  or die "Cannot plot: ".$graph->error.": $!\n";

	open ( PICTURE, " > $filename " )
	  or die "Cannot open $filename: $!\n";
	binmode PICTURE;
	print PICTURE $gr->gif;
	close ( PICTURE )
	  or die "Cannot close PICTURE: $!\n";
}

sub SOURCE_PORT_COUNT
{
	my $filename = shift;
	my $graph = new GD::Graph::bars3d(1600, 1000);
	my $input = "SOURCE_PORT_COUNT.data";
	my $line;
	my $data = GD::Graph::Data->new();
	
	open ( INPUT, " < $input " )
	  or die "Cannot open $input: $!\n";
	
	while ( defined ($line=<INPUT>) )
	{
		my @row=();
		chomp $line;
		@row = split(/\s/, $line);
		$data->add_point(@row);
	}
	
	close ( INPUT )
	  or die "Cannot close INPUT: $!\n";
	
	$graph->set(
		x_label			=> "SOURCE PORT",
		y_label 		=> "Times used",
		x_label_skip	=> 1000,
		title   		=> "SOURCE PORT versus Times",
	);
	
	my $gr = $graph->plot($data)
	  or die "Cannot plot: ".$graph->error.": $!\n";

	open ( PICTURE, " > $filename " )
	  or die "Cannot open $filename: $!\n";
	binmode PICTURE;
	print PICTURE $gr->gif;
	close ( PICTURE )
	  or die "Cannot close PICTURE: $!\n";
}

sub DEST_PORT_COUNT
{
	my $filename = shift;
	my $graph = new GD::Graph::bars3d(1600, 1000);
	my $input = "DEST_PORT_COUNT.data";
	my $line;
	my $data = GD::Graph::Data->new();
	
	open ( INPUT, " < $input " )
	  or die "Cannot open $input: $!\n";
	
	while ( defined ($line=<INPUT>) )
	{
		my @row=();
		chomp $line;
		@row = split(/\s/, $line);
		$data->add_point(@row);
	}
	
	close ( INPUT )
	  or die "Cannot close INPUT: $!\n";
	
	$graph->set(
		x_label			=> "DEST PORT",
		y_label 		=> "Times used",
		x_label_skip	=> 200,
		title   		=> "DESTINATION PORT versus Times",
	);
	
	my $gr = $graph->plot($data)
	  or die "Cannot plot: ".$graph->error.": $!\n";

	open ( PICTURE, " > $filename " )
	  or die "Cannot open $filename: $!\n";
	binmode PICTURE;
	print PICTURE $gr->gif;
	close ( PICTURE )
	  or die "Cannot close PICTURE: $!\n";
}

sub PACKET_TYPE_COUNT
{
	my $filename = shift;
	my $graph = new GD::Graph::pie3d(800, 600);
	my $input = "PACKET_TYPE_COUNT.data";
	my $line;
	my $data = GD::Graph::Data->new();
	
	open ( INPUT, " < $input " )
	  or die "Cannot open $input: $!\n";
	
	while ( defined ($line=<INPUT>) )
	{
		my @row=();
		chomp $line;
		@row = split(/\s/, $line);
		$data->add_point(@row);
	}
	
	close ( INPUT )
	  or die "Cannot close INPUT: $!\n";
	
	$graph->set(
		title   		=> "PACKET TYPE Pie Chart",
	);
	
	my $gr = $graph->plot($data)
	  or die "Cannot plot: ".$graph->error.": $!\n";

	open ( PICTURE, " > $filename " )
	  or die "Cannot open $filename: $!\n";
	binmode PICTURE;
	print PICTURE $gr->gif;
	close ( PICTURE )
	  or die "Cannot close PICTURE: $!\n";
}
