       |
|
Client Side Graph Production with GNUPlot
As part of my production of a network monitoring analysis tool for web100
iperf transfers, it was necessary to create a set of scripts to output tabular
data to graphs. This page will give details of how to set this up.
The script is ripped from a cgi-script (my own!), so there'll be some references
to html code. However, the general jist should be obvious.
The only concern is that without the appropiate libraries, it is necessary
to output the data to file first before re-reading it into the GNUPlot program.
This isn't the most eloquent or effiecient method of doing it, but heck,
it works!
#!/usr/bin/perl
@plot_variables = ( 'PktsIn (Delta)' , 'DataPktsIn (Delta)' , 'AckPktsIn (Delta)' );
@values = (
[ "0" , "3220384141" , "3220384140" ,"3220384141" ],
[ "1" , "3623" , "0" , "3623" ],
[ "2" , "3723" , "0" , "3623" ],
[ "3" , "3523" , "0" , "3623" ],
[ "4" , "3223" , "0" , "3623" ],
[ "5" , "3423" , "0" , "3623" ],
[ "6" , "3647" , "0" , "3623" ],
[ "7" , "3123" , "0" , "3623" ],
[ "8" , "3623" , "0" , "3623" ],
[ "9" , "3325" , "0" , "3623" ],
[ "10" , "3133" , "0" , "3623" ],
); # takes in 2d array of data to plot - @values
my $GNUPlot = '/usr/bin/gnuplot';
my $gnuplot_file = 'gnuplot.gif';
my $file = 'gnuplot.dat';
if ( $#plot_variables != 0 ) { # must be looking at one file
$title = "Web100 Transfer Information - $file";
$keytitle = 'Variables';
}
else {
$title = "Web100 Transfer Information - $plot_variables[0]";
$keytitle = 'Files';
}
my $xaxis = "Time (seconds)";
my $yaxis = "y-axis";
my $x = 640;
my $y = 400;
# save the @values to a temp file
# variable name stored in plot_variable
# values stored in 2d array values - store in temp file
open ( TABLE, ">$file" );
print TABLE "Time\t";
foreach $name (@plot_variables) {
print TABLE "$name\t";
}
print TABLE "\n";
# print "$#values by $#{$values[0]}\n";
# print the contents of array
for my $i ( 0 .. $#values ) {
#unless ($i == 0) { print "$i\t" };
#print "position $i has $#{$values[$i]} elements<br>";
#print TABLE "$i\t";
for my $j ( 0 .. $#{$values[$i]} ) {
unless ($i == 0) { print TABLE "$values[$i][$j]\t"; } # discard
the first instance due to web100 settling down
}
print TABLE "\n";
}
close (TABLE);
# generate plot line for gnuplot
# number of variables is $#plot_variables
$PLOT_LINE = sprintf "$PLOT_LINE" . "plot ";
for $var (1 .. $#plot_variables+1) {
$i=$var+1;
$PLOT_LINE = sprintf "$PLOT_LINE" . "\"$file\"
using 1:$i title \"$plot_variables[$var-1]\" with linespoints";
unless( $var == $#plot_variables+1 ) {
$PLOT_LINE = sprintf "$PLOT_LINE" . ", "; }
}
# print "$PLOT_LINE";
# send data to gnuplot
open ( GNUPLOT, "|$GNUPlot");
print GNUPLOT << "gnuplot_Commands";
set term gif tiny size $x, $y
set output "$gnuplot_file"
set title "$title"
set xlabel "$xaxis"
set ylabel "$yaxis"
set grid
set noxzeroaxis
set noyzeroaxis
set key outside reverse title "$keytitle"
set border
$PLOT_LINE
gnuplot_Commands
close (GNUPLOT);
# print the gif file to screen
$| = 1;
print "<IMG SRC='$gnuplot_file'>\n";
unlink $file;
|
The line, print GNUPLOT
<< "gnuplot_Commands", means that
the following lines should be outputted to the gnuplot program (in this
case the CLI of gnuplot) untill the words "gnuplot_Commands" is
found.
Problems
One problem is gnuplot's method of defining terminal types. Only the newest
versions support direct gif output. And from my experience, even the newest
rpm's of the same gnuplot version (3.7) - which alledgely support gif outputs,
are not consistent. An example of this is the Mandrake and Redhat distributions.
Whilst the Mandrake distribution does support gif output, the Redhat version
didn't. A quick fix is to simply install the Mandrake version - but then
you have to install all the corresponding dependent RPM's as well...
Wed, 23 July, 2003 13:07
|
  |
|
|
|