#!/usr/bin/perl  -w
use RRDs;

$DATABASE_DIRECTORY = '/home/mjail/WWW/FINAL/bin/rrd';
$GRAPHS_DIRECTORY = '/home/mjail/WWW/FINAL/bin/images';
$rrdDir = '/usr/local/bin';
# process data for jail
&ProcessInterface();

# if rrdtool database doesn't exist, create it
sub ProcessInterface
{
	# process jail
	@_jpsMem_deneb = `sudo jps deneb -auxw | perl -lane 'print \$F[3]' | grep -v MEM`;
	@_jpsCpu_deneb = `sudo jps deneb -auxw | perl -lane 'print \$F[2]' | grep -v CPU`;

	@_jpsMem_matrix = `sudo jps matrix -auxw | perl -lane 'print \$F[3]' | grep -v MEM`;
	@_jpsCpu_matrix = `sudo jps matrix -auxw | perl -lane 'print \$F[2]' | grep -v CPU`;
	
	
	# Total Proccess in jail
	($totMem_deneb+=$_) for @_jpsMem_deneb;
	($totCpu_deneb+=$_) for @_jpsCpu_deneb;
	my $largo_deneb = $#_jpsMem_deneb;

	($totMem_matrix+=$_) for @_jpsMem_matrix; 
	($totCpu_matrix+=$_) for @_jpsCpu_matrix;
	my $largo_matrix = $#_jpsMem_matrix;
	
	# remove eol chars
	chomp($totMem_deneb);
	chomp($totCpu_deneb);
	chomp($totMem_matrix);
	chomp($totCpu_matrix);
	
	#Data test print
	print "[DATA] $totMem_matrix  $totCpu_matrix \n";
	print "[DATA] $totMem_deneb  $totCpu_deneb \n";

	#Data Baase create
	if (! -e "$DATABASE_DIRECTORY/memory.rrd")
	{
		print "[CREATE INFO] creating rrd database for jail\n";
		RRDs::create "$DATABASE_DIRECTORY/memory.rrd",
			"-s 300",
			"DS:totMem_deneb:GAUGE:600:0:60",
			"DS:totCpu_deneb:GAUGE:600:-256:0",
			"DS:totMem_matrix:GAUGE:600:-256:0",
			"DS:totCpu_matrix:GAUGE:600:0:100",
			"RRA:AVERAGE:0.5:1:576",
			"RRA:AVERAGE:0.5:6:672",
			"RRA:AVERAGE:0.5:24:732",
			"RRA:AVERAGE:0.5:144:1460";
	    if ($ERROR = RRDs::error) { print "failed to create rrd: $ERROR\n"; }
	}
	
	# insert values into rrd
	RRDs::update "$DATABASE_DIRECTORY/memory.rrd",
		"-t", "totMem_deneb:totCpu_deneb:totMem_matrix:totCpu_matrix",
		"N:$totMem_deneb:$totCpu_deneb:$totMem_matrix:$totCpu_matrix";
	if ($ERROR = RRDs::error) { print "$0: failed to insert data into rrd: $ERROR\n"; }

		# create traffic graphs
	&CreateGraphs("day");

}

sub CreateGraphs
{
# creates graph
# inputs: $_[0]: interval (ie, day, week, month, year)
	# generate Deneb graph
	RRDs::graph "$GRAPHS_DIRECTORY/totMem_deneb.png",
		"-s -1$_[0]",
		"-t", "Deneb Memory Graph :: $_[0]",
		"-h", "80", "-w", "600",
		"-a", "PNG",
		"-y", "1:2",
		"-v", "Memory Use [%]",
		"-l", "0",
		"DEF:totMem_deneb=$DATABASE_DIRECTORY/memory.rrd:totMem_deneb:AVERAGE",
		"LINE2:totMem_deneb#0000FF:Mem [%]",
		"GPRINT:totMem_deneb:MIN:     Min\\: %2.lf",
		"GPRINT:totMem_deneb:MAX: Max\\: %2.lf",
		"GPRINT:totMem_deneb:AVERAGE: Avg\\: %4.1lf",
		"GPRINT:totMem_deneb:LAST: Current\\: %2.lf \\n";
	if ($ERROR = RRDs::error) { print "$0: unable to generate DMem graph: $ERROR\n"; }

	# generate Matrix Mem Cpu graph
	RRDs::graph "$GRAPHS_DIRECTORY/Matrix.png",
		"-s -1$_[0]",
		"-t", "MMem MCpu:: $_[0]",
		"-h", "80", "-w", "600",
		"-a", "PNG",
		"-y", "2:2",
		"-v", "CPU & MEM matrix use[%]",
		"DEF:totMem_matrix=$DATABASE_DIRECTORY/memory.rrd:totMem_matrix:AVERAGE",
		"DEF:totCpu_matrix=$DATABASE_DIRECTORY/memory.rrd:totCpu_matrix:AVERAGE",
		
		"LINE2:totMem_matrix#11EE11:Mem [%]",
		"GPRINT:totMem_matrix:MIN:  Min\\: %4.lf",
		"GPRINT:totMem_matrix:MAX: Max\\: %4.lf",
		"GPRINT:totMem_matrix:AVERAGE: Avg\\: %6.1lf",
		"GPRINT:totMem_matrix:LAST: Current\\: %4.lf \\n",
		
		"LINE2:totCpu_matrix#FF0000:Cpu [%]",
		"GPRINT:totCpu_matrix:MIN:   Min\\: %4.lf",
		"GPRINT:totCpu_matrix:MAX: Max\\: %4.lf",
		"GPRINT:totCpu_matrix:AVERAGE: Avg\\: %6.1lf",
		"GPRINT:totCpu_matrix:LAST: Current\\: %4.lf \\n";
	if ($ERROR = RRDs::error) { print "$0: unable to generate MMem & MCpu graph: $ERROR\n"; }

	# generate link rate graph
	RRDs::graph "$GRAPHS_DIRECTORY/totCpu_deneb.png",
		"-s -1$_[0]",
		"-t", "DCpu :: $_[0]",
		"-h", "80", "-w", "600",
		"-a", "PNG",
		"-y", "2:1",
		"-l", "0", "-u", "12", "--rigid",
		"-v", "Cpu use [%]",
		"DEF:totCpu_deneb=$DATABASE_DIRECTORY/memory.rrd:totCpu_deneb:AVERAGE",
		"LINE2:totCpu_deneb#0000FF:DCpu",
		"GPRINT:totCpu_deneb:MIN:  Min\\: %4.1lf",
		"GPRINT:totCpu_deneb:MAX: Max\\: %4.1lf",
		"GPRINT:totCpu_deneb:AVERAGE: Avg\\: %4.1lf",
		"GPRINT:totCpu_deneb:LAST: Current\\: %4.1lf \\n";
	if ($ERROR = RRDs::error) { print "$0: unable to generate DCpu graph: $ERROR\n"; }
}
