#!/usr/bin/perl
#
# This script belongs in the cgi-bin dir of your web-server
# Version 0.1

use CGI ;
use RRDs ;

my($q) = new CGI;

my($webcgi) = $q->url ; # Get our URL
my($handle) = $q->param('handle') or die "parameter : handle " ;
@handle = split ("_",$handle) ;
my($type) = $q->param('type') ;
my($int) = $q->param('int') ;
my($graph) = $q->param('graph') ;
my($parent) = $q->param('parent') ;

# Location of the rrd files, the web-server needs read access
$dir_rrd = "/qos/rrds" ;

# place to store the graphs, the web-server needs write access
$tmp_dir = "/tmp/tc/" ;
@graphs = (1,2,3,4) ; # 4 types of graph
@color = ("#FF0000","#00FF00","#0000FF","#FFFF00") ;
$date = localtime ;

# What data is stored where
%config = (
	bytes => {
		rrd => ".1.rrd" ,
		data => "by",
	},
	packets => {
		rrd => ".1.rrd" ,
		data => "pa",
	},
	dropped => {
		rrd => ".1.rrd" ,
		data => "dr",
	},
	overlimits => {
		rrd => ".1.rrd" ,
		data => "ov",
	},
	lended => {
		rrd => ".2.rrd" ,
		data => "le",
	},
	borrowed => {
		rrd => ".2.rrd" ,
		data => "bo",
	},
	giants => {
		rrd => ".2.rrd" ,
		data => "gi",
	},
	injects => {
		rrd => ".2.rrd" ,
		data => "in",
	},
	tokens => {
		rrd => ".3.rrd" ,
		data => "to",
	},
	ctokens => {
		rrd => ".3.rrd" ,
		data => "ct",
	},
) ;

# If we have to draw a graph, make one :
if($graph) {
	print $q->header(-type=>'image/png',-expires=>'+5m');

	$rrd_base = $dir_rrd . "/" . $int . "." . $handle ;
	$start{1} = "e-48h";
	$start{2} = "e-12d";
	$start{3} = "e-48d";
	$start{4} = "e-576d";

	if (not -d $tmp_dir) {
		mkdir $tmp_dir,0755 ;
	}
	$graph_png = $tmp_dir . "$int.$handle.$type.$graph.png" ;

	@option = ("-s","$start{$graph}","-a","PNG","--lazy") ;
	if ( $type eq "packet_size") {
		$color=0;
		foreach $handle (@handle) {
			$temp = $handle ;
			$temp =~ s/:/_/g ;
			$handle =~ s/:/\\:/g ;
			$rrd = $dir_rrd . "/" . $int . "." . $temp . ".1.rrd" ;
			push (@option,"DEF:b_$temp=$rrd:by:AVERAGE") ;
			push (@option,"DEF:p_$temp=$rrd:pa:AVERAGE") ;
			push (@option,"CDEF:size_$temp=b_$temp,p_$temp,/") ;
			#push (@option,"AREA:b$temp#00FF00:Bytes/s:") ;
			if ( $color == "0" ) {
				push (@option, "AREA:size_$temp$color[$color]:$handle:") ;
			} else {
				push (@option,"STACK:size_$temp$color[$color]:$handle:") ;
			}
			push (@option,"GPRINT:size_$temp:LAST:Current \\: %5.1lf $type\\n") ;
			$color ++ ;
		}
	} elsif ( $type eq "procent") {
		push (@option,"-l","0","-u","100","-r") ;
		@handle_backup = @handle ;
		$first = shift @handle ;
		$first =~ s/:/_/g ;
		$rrd = $dir_rrd . "/" . $int . "." . $first . ".1.rrd" ;
		push (@option,"DEF:tot_0=$rrd:by:AVERAGE") ;
#push (@option,"GPRINT:tot_0:LAST:TOT \\: %5.1lf \\n") ;
		$color=0;
		foreach $handle (@handle) {
			$temp = $handle ;
			$color_old = $color ;
			$color ++ ;
			$temp =~ s/:/_/g ;
			$handle =~ s/:/\\:/g ;
			$rrd = $dir_rrd . "/" . $int . "." . $temp . ".1.rrd" ;
			push (@option,"DEF:b_$temp=$rrd:by:AVERAGE") ;
#push (@option,"GPRINT:b_$temp:LAST:Waarde $temp \\: %5.1lf \\n") ;
			push (@option,"CDEF:tot_$color=b_$temp,tot_$color_old,+") ;
#push (@option,"GPRINT:tot_$color:LAST:TOT $temp  \\: %5.1lf \\n") ;
		}

		$temp = $parent ;
		$parent =~ s/:/\\:/g ;
		$temp =~ s/:/_/g ;
		$rrd = $dir_rrd . "/" . $int . "." . $temp . ".1.rrd" ;
		push (@option,"DEF:total=$rrd:by:AVERAGE") ;
#push (@option,"GPRINT:tot_$color:LAST:total $total \\: %5.1lf \\n") ;
		push (@option,"CDEF:parent=total,tot_$color,-") ;
#push (@option,"GPRINT:parent:LAST:TOT \\: %5.1lf \\n") ;

		@handle = @handle_backup ;
		$first = shift @handle ;
		$first =~ s/:/\\:/g ;
		$color1=0;
		push (@option,"CDEF:tot_0_pct=tot_0,total,/,100,*") ;
		push (@option,"AREA:tot_0_pct$color[$color1]:$first:") ;
		push (@option,"GPRINT:tot_0_pct:LAST:Current \\: %5.1lf $type\\n") ;
		foreach $handle (@handle) {
			$temp = $handle ;
			$temp =~ s/:/_/g ;
			$color1 ++ ;
			$handle =~ s/:/\\:/g ;
			push (@option,"CDEF:tot_pct_$temp=b_$temp,total,/,100,*") ;
			push (@option,"STACK:tot_pct_$temp$color[$color1]:$handle:") ;
			push (@option,"GPRINT:tot_pct_$temp:LAST:Current \\: %5.1lf $type\\n") ;
		}

		$color1 ++ ;
		push (@option,"CDEF:total_pct=parent,total,/,100,*") ;
		push (@option,"STACK:total_pct$color[$color1]:$parent:") ;
		push (@option,"GPRINT:total_pct:LAST:Parent \\: %5.1lf $type\\n") ;
		$test = join ("         ", @option) ;
		`echo $test > /tmp/test`  ;
	} else {
		$color=0;
		foreach $handle (@handle) {
			$temp = $handle ;
			$temp =~ s/:/_/g ;
			$handle =~ s/:/\\:/g ;
			$rrd = $dir_rrd . "/" . $int . "." . $temp . $config{$type}{rrd} ;
			push (@option,"DEF:$config{$type}{data}$temp=$rrd:$config{$type}{data}:AVERAGE") ;
			if ( $color == "0" ) {
				push (@option, "AREA:$config{$type}{data}$temp$color[$color]:$handle:") ;
			} else {
				push (@option,"STACK:$config{$type}{data}$temp$color[$color]:$handle:") ;
			}
			push (@option,"GPRINT:$config{$type}{data}$temp:LAST:Current \\: %5.1lf $type/s\\n") ;
			$color ++ ;
		}
	}

	push (@option,"COMMENT:$date") ;
	RRDs::graph($graph_png, @option ) ;
	my ($err) = RRDs::error;
	die $err if $err;

	open ( GRAPH, "<$graph_png" ) ;
	binmode(GRAPH);
	while ($len = read(GRAPH,$bits,8192)) {
		$pic .= $bits;
	}
	print $pic;

# If we have a type, show the 4 possible graphs :
} elsif ($type) {
	print $q->header ;
	print qq{<html><head>
<META HTTP-EQUIV="REFRESH" CONTENT="300">
</head><body>
} ;
#<META HTTP-EQUIV="EXPIRES" CONTENT="Sat, 01 Jan 2001 00:00:00 GMT">
	$temp = $handle ;
	$temp =~ s/_/:/g ;
	print qq{$date}; 
	print qq{<H1>Handle @handle on interface $int, parent $parent</H1>}; 
	foreach my $graph (@graphs) {
		print qq{<P><IMG SRC=$webcgi?handle=$handle&type=$type&int=$int&graph=$graph&parent=$parent BORDER=0>};

	}
	print qq{</BODY></HTML>} ;

# Show all the possible types of graph we can draw :
} else {
	$date = localtime ;

	print $q->header ;
	print qq{<html><head>
<META HTTP-EQUIV="REFRESH" CONTENT="300">
</head><body>
} ;
#<META HTTP-EQUIV="EXPIRES" CONTENT="Sat, 01 Jan 2001 00:00:00 GMT">
	print qq{$date}; 

	$temp = $handle ;
	$temp =~ s/_/:/g ;
	print qq{<H1>Handle @handle on interface $int, parent $parent</H1>} ;
	foreach $ele (keys(%config )) {
		print "<br><A HREF=$webcgi?handle=$handle&type=$ele&int=$int&parent=$parent>$ele</A>" ;
	}
	if ( $#handle != 0 ) {
		print qq{<P><A HREF=$webcgi?handle=$handle&type=procent&int=$int&parent=$parent><IMG SRC=$webcgi?handle=$handle&type=procent&int=$int&graph=1&parent=$parent BORDER=0></A>} ;
	}
	print qq{
<P><A HREF=$webcgi?handle=$handle&type=bytes&int=$int&parent=$parent><IMG SRC=$webcgi?handle=$handle&type=bytes&int=$int&graph=1&parent=$parent BORDER=0></A>
<P><A HREF=$webcgi?handle=$handle&type=packets&int=$int&parent=$parent><IMG SRC=$webcgi?handle=$handle&type=packets&int=$int&graph=1&parent=$parent BORDER=0></A>
<P><A HREF=$webcgi?handle=$handle&type=packet_size&int=$int&parent=$parent><IMG SRC=$webcgi?handle=$handle&type=packet_size&int=$int&graph=1&parent=$parent BORDER=0></A>
</BODY></HTML>} ;
}

