[mrtg] RRDTool Cgi Problem

Steven Williams Steven.Williams at computershare.com.au
Mon Dec 3 06:45:46 MET 2001


Hi,

Copied below is a cgi I wrote to graph multiple data sources from multiple
rrd's

This works great, except I am having troubles multiplying the data by 8
using CDEF for converting bytes to bits for network traffic.

Could someone have a look at this and help me out please?

Thanks


#!/usr/bin/perl -w -I /usr/rrdtool/lib/perl/
#

use CGI;
use RRDs; 



########################
#HTML Variables
########################
#The page title and heading
$titleHTML = "Equant ICN HSRP Sites";
#Page description, please write the description in HTML
$descriptionHTML = 
"
<h3><p>
ICN HSRP Maximum traffic throughout comparison to indicate routing changes
from primary sites to secondary sites.

Still under development. Dont use data as it may be incorrect.
</h3>
";

#footer, in html
$footer =
"
<A
HREF=\"http://commsweb.mel.computershare.com.au\">www.commsweb.mel.computers
hare.com.au</A>
";


########################
#Global Config Variables.
########################
#name of the prefix for all the graph files
my $prefix = "hsrp";
#Path for graph files
my $graphPath = ".\\equant\\ <\\equant\\> ";
#path containing the rrd files
my $rrdPath = ".\\equant\\ <\\equant\\> ";
#How often do you want to update the graph? In seconds
my $updateInterval = 5 * 60;
#do you want to create the avg graph, an average of the ds0 and ds1
#$createAvg = 0;

#The files that you want to display
#implemented as a hash of hashes
#in the format
#       "Title of the graph" => {
#		 	"File1.rrd"	     
#			=> "Key for DS0 |Key for DS1 |.....",
#			"File2.rrd"      
#			=> "Key for DS0 |.... <--NOTE the last one does not
have a coma
#       }, <--NOTE the last one does not have a coma

#To skip a DS simply place an '&&' like so   => "&&|Key for DS1|&&|Key for
DS3"
 %graphTable = (
 #-----------------------------------------
#        "VPN Testing - Reponse Times" 	=> {
#			"vpntest_equantping.rrd"		
#			=> "&&|Minimum Response Times to Chicago CHILCORE1
via EQuant",
#			"vpntest_internetping.rrd"      
#			=> 	"Maximum Response Times to Chicago Internet
Router via the WWW |Minimum Response Times to Chicago Internet Router via
the WWW"
        },
       "VPN TEST via Internet to Chicago Internet Router"	=> {

        	"vpntest_internetping.rrd"	
        	=> "Maximum Round Trip Time in ms |Minimum Round Trip Time
in ms"
        },
        "Melbourne HSRP Sites Traffic Comparison (Bps)"	=> {
        	"pmel315_wanbps.rrd"				
        	=>"RX Bps|Tx Bps",
        }
#------------------------------------------
);  
 



########################
########################


#create new CGI instance
my $query = new CGI;

#what is the url to which the files are stored
my @pathParts = split (/\\/,$graphPath);
shift @pathParts;
my $urlGraph = "http://commsweb.mel.computershare.com.au/
<http://commsweb.mel.computershare.com.au/> ";
foreach $n  (@pathParts) {
$urlGraph .= "$n/";
}

#the scripts url
my $url = $query->url;

#check if its a derivation
if (defined $query->param('full') ) {
	$deriv = 1;
}
else {
	$deriv =0;
}

#Makes the graphs
my $graphCount=0;
if ($deriv) {
 	my $target = $query->param('full');
	foreach $n ('Yearly','Monthly','Weekly','Daily','Four Hourly') {
		@createOutput = &create_graph($graphCount,"$n:
$target",$n,%{$graphTable{$target}});
		$graphOut[$graphCount][0] = shift @createOutput;
		$graphOut[$graphCount][1] = shift @createOutput;	
		$graphCount++;
	}

}
else {
	@graphOut = ();
	foreach $graph ( keys %graphTable) {
		$id = $graphCount;	 
		@createOutput =
&create_graph($id,$graph,'Daily',%{$graphTable{$graph}});
		$graphOut[$graphCount][0] = shift @createOutput;
		$graphOut[$graphCount][1] = shift @createOutput;
		$graphOut[$graphCount][2] = shift @createOutput;
		$graphCount++;
	}
}

#
#The HTML bit
#
print $query->header;

print $query->start_html(-title=>"$titleHTML",
                         -BGCOLOR=>'white');
                         
#Header info 
                       
if ($deriv) {
	 my $target = $query->param('full');
	print "<h1>$target</h1>";
}
else {
	print "<h1>$titleHTML</h1>";             
	print "$descriptionHTML";
}

#prints the time
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time);  
print "<HR>";
print "The statistics where last updated: ";
print $query->b(scalar(localtime(time)));
print "<HR>\n";

#testing the times
#foreach $tt (keys %graphTable) {
#	foreach $yy (keys %{$graphTable{$tt}}) {
#			my $last = RRDs::last("$rrdPath$yy");
#				print "$tt=> $yy => $last \n";
#				print "<p>";
#	}
#	print "<p>";
#}



#Display the graphs
$graphCount--;
print "<table width=100\%>";
while ($graphCount >= 0) {
	print "<tr><td>";
	
	#If there is an error
	if ($graphOut[$graphCount][1] ne "") {
		print "Error: $graphOut[$graphCount][1]";
		print $query->p;
	}
		
	$deriv or 	print "<a
href=\"$url?iid=$graphCount&full=$graphOut[$graphCount][2]\"><img
src=\"$urlGraph$graphOut[$graphCount][0]\"/></a>";
	!$deriv or	print "<IMG ALIGN=\"LEFT\"
SRC=\"$urlGraph$graphOut[$graphCount][0]\">";

	print $query->p;	
	print "</td>\n";
	$graphCount--;
	
	#Show each graph
	if ($graphCount >= 0 && !(defined $query->param('full')) ) {
		print "<td>";
		
		#If there is an error
		if ($graphOut[$graphCount][1] ne "") {
			print "Error: $graphOut[$graphCount][1]";
			print $query->p;
		}
		
		
		$deriv or 	print "<a
href=\"$url?iid=$graphCount&full=$graphOut[$graphCount][2]\"><img
src=\"$urlGraph$graphOut[$graphCount][0]\"/></a>";
		!$deriv or	print "<IMG ALIGN=\"LEFT\"
SRC=\"$urlGraph$graphOut[$graphCount][0]\">";

		print $query->p;	
		print "</td></tr>\n";
		$graphCount--;
	}
	else {
		print "</tr>\n";
	}
}
print "</table>";


#Footer
print "<hr>\n";
print $footer;


## End
print $query->end_html;


#####################
#Creates the graph given
####################
sub create_graph {
	

	my($id,$title,$period,%instances) = @_;
	my $start;
	
	#Establishes the sampling period
	if ($period eq 'Daily') {
      	$start = 33.33*60*60;
		$start = "-$start";
   	} elsif ($period eq 'Weekly') {
      	$start = 7*24*60*60;
		$start = "-$start";
   	} elsif ($period eq 'Monthly') {
      	$start = 30*7*24*60*60;
		$start = "-$start";
   	} elsif ($period eq 'Yearly') {
      	$start = 52*7*24*60*60;
		$start = "-$start";
	} elsif ($period eq 'Four Hourly') {
      	$start = 4*60*60;
		$start = "-$start";
	}
	 else {
      	$start = 24*60*60;
		$start = "-$start";
	}

	
	#establish the initial colour
	$colourNum = -1;
#	$colourNum = int $colourNum;

	#Check if it is a index page or a detailed page
	if (defined $query->param('iid')) {
		$iid = $query->param('iid');
		#picture name
		$pic = "$prefix$iid.$id.gif";
	
		$width = 800;
		$height = 400;
	}
	else {
		#picture name
		$pic = "$prefix$id.gif";
	
		$width = 300;
		$height = 100;
	}

	#establishes the arguments
	my @args = ();
	push (@args, "$graphPath\\$pic");
	push (@args, "-s $start");
	push (@args, "--title=\"$title\"");

	#Do the defs and LINEs
	foreach $rrdFile ( keys %instances ) {
	
		
		$varName = rand 99999;
		$varName = int $varName;
		
		
		push (@args, "-w $width");
		push (@args, "-h $height");		
	

		#draw each line
		@keys = split (/\|/,($instances{$rrdFile}));
		$DS = 0;
		foreach $keyName (@keys) {

#			if ($DS = 0) {
#				$ds0Name = $keyName;
#			}
#			if ($DS = 1) {
#				$ds1Name = $keyName;
#			}						
		if (!($keyName =~ /.*\&\&.*/)) {
			$colourNum++;
			if ($colourNum > 11) {
				$colourNum = 0;
			}
			$colour = &get_colour ($colourNum);
			
			push (@args,
"DEF:a$varName=$rrdPath$rrdFile:ds$DS:MAX");
			push (@args, "CDEF:$other=$varName,8,*");
			push (@args, "LINE1:a$varName#$colour:$keyName");

			push (@args, "GPRINT:$other:MAX:   max\\:\%3.0lf");
			push (@args, "GPRINT:$other:MIN:min\\:\%3.0lf");
			push (@args,
"GPRINT:$other:AVERAGE:avg\\:\%3.0lf\\l");
			
			$DS++;	
			$varName++;
			
		}
		
		
		}
		
		
		
		#create the average graph
		if ($createAvg) {
			$colourNum++;
			if ($colourNum > 11) {
				$colourNum = 0;
			}
			$colour = &get_colour ($colourNum);
		
			$j = $varName;
			push (@args,
"DEF:a$varName=$rrdPath$rrdFile:ds0:Max");
			$varName++;
			push (@args,
"DEF:a$varName=$rrdPath$rrdFile:ds1:Max");		
			$i = $varName;
			$varName++;
			push (@args, "CDEF:c$varName=0.5,a$j,a$i,+,*");
			push (@args, "LINE1:c$varName#$colour:Average");
		}
		

	}
	
	#Fire up RRD
	my ($results, $rrdx, $rrdy) = RRDs::graph(@args);
	my $e = RRDs::error();
	
	return ($pic,$e,$title);

}


###########
#given a number returns a colour in Hex RGB.
###########
sub get_colour {
	my($number) = @_;
	if ($number == 0) {
		return "0000FF";
	}
	elsif ($number == 1) {
		return "FF0000";
	}
	elsif ($number == 2) {
		return "00FF00";
	}
	elsif ($number == 3) {
		return "555555";
	}
	elsif ($number == 4) {
		return "D753C7";
	}
	elsif ($number == 5) {
		return "5B8FA4";
	}
	elsif ($number == 6) {
		return "D7FF30";
	}
	elsif ($number == 7) {
		return "FF5050";
	}
	elsif ($number == 8) {
		return "30AF00";
	}
	elsif ($number == 9) {
		return "802080";
	}	
	elsif ($number == 10) {
		return "F20080";
	}
	elsif ($number == 11) {
		return "0F20AE";
	}
}
          


Steven Williams
Communications Support Engineer
PH +61 3 9235 5651
FAX +61 3 9473 2441

Computershare Technology Services
18-62 Trennery Cres Abbotsford
Victoria 3067 Australia






---
This email and any files transmitted with it are solely intended for the use of the
addressee(s) and may contain information that is confidential and privileged.  If you
receive this email in error, please advise us by return email immediately.  Please also
disregard the contents of the email, delete it and destroy any copies immediately.
Computershare Limited and its subsidiaries do not accept liability for the views
expressed in the email or for the consequences of any computer viruses that may be
transmitted with this email

This email is also subject to copyright.  No part of it should be reproduced, adapted or 
transmitted without the written consent of the copyright owner.



--
Unsubscribe mailto:mrtg-request at list.ee.ethz.ch?subject=unsubscribe
Archive     http://www.ee.ethz.ch/~slist/mrtg
FAQ         http://faq.mrtg.org    Homepage     http://www.mrtg.org
WebAdmin    http://www.ee.ethz.ch/~slist/lsg2.cgi



More information about the mrtg mailing list