#!/usr/bin/perl
use CGI;
use LWP::Simple;
################################################
# speed.pl keeping track of the bandwidth
#
# Expects time=5.76 as parameter, which is the
# time in seconds it took to download a 1MB file
#
# Wrires an html table based graph in Kbps
#
# kishan@hackorama.com http://www.hackorama.com
################################################
# 1. The filename to write the table
$speed_file="/var/www/html/speedometer.html";
# 2. The number of MB's to be used o the scale
$scale=2;
# 3. The width in pixels of the scale
$max=400;
###### normally no changes beyond this #######
$unit= int( $max / $scale );
$size=1024;
$q = new CGI;
$time = $q->param('time');
chomp $time;
$speed = $size / $time ;
$speed = int($speed * 8);
print "Content-type: text/html\n\n";
$font = " face=\"Verdana, Arial,Helvetica, sans-serif\" size=-4 ";
$table = " cellpadding=0 cellspacing=0 border=0 ";
$table1 = " cellpadding=1 cellspacing=0 border=1 ";
$table2 = " cellpadding=1 cellspacing=0 border=0 ";
open ( FILE, ">$speed_file") || exit;
flock ( FILE , 2) or die "ERROR: $!";
print FILE "<html>\n";
print FILE "<table width=$max $table1 ><tr><td>\n";
print FILE "\n";
print FILE "<table width=$max $table2 >\n";
print FILE "<tr><td>\n";
print FILE "<table width=$max $table bgcolor=#eeeeee>\n";
print FILE "<tr>\n";
for( $i=1; $i <= $scale; $i++ ){
print FILE "<td width=$unit align=right><font $font >$i MB |</font></td>\n";
}
print FILE "</tr>\n";
print FILE "</table>\n";
print FILE "</td></tr>\n";
do_graph( $speed );
print FILE "</table>\n";
print FILE "\n";
print FILE "</td></tr></table>\n";
print FILE "</html>\n";
close( FILE );
print "OK";
sub do_graph
{
$speed = $_[0];
$width= int( ($speed / ($scale * $size)) * $max );
print FILE "<tr>\n";
print FILE "<td>\n";
print FILE "<table width=$max $table >\n";
print FILE "<tr>\n";
print FILE "<td width=$width bgcolor=red align=right><font $font >$speed kbps</font>\n";
print FILE "</td><td><font $font > </font></td>\n";
print FILE "</tr>\n";
print FILE "</table>\n";
print FILE "</td></tr>\n";
}
syntax highlighted by Code2HTML, v. 0.9