#!/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/html/speed.shtml";
$speed_list="/var/html/speed.txt";

# 2. The number of MB's to be used o the scale
$scale=2; 

# 3. The width in pixels of the scale
$max=400;

# 4. The captions change to anything 
$caption="SPEED CHECK AT&T CABLE";

###### normally no changes beyond this #######

$final = 0 ;
$day = scalar(localtime(time));  

$extra=200; # for the date
$maxmax = $max + $extra ;

$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 ";
$font2 = " face=\"Verdana, Arial,Helvetica, sans-serif\" size=-4 color=#ffffff ";
$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 "ERROR1: $!";

add_to_list( $speed );

print FILE "<html>\n";
print FILE "<table width=$maxmax $table1 ><tr><td>\n";
print FILE "\n";
print FILE "<table width=$maxmax $table2 >\n";
print FILE "<tr><td>\n";
print FILE "<table width=$maxmax $table>\n";
print FILE "<tr>\n";
for( $i=1; $i <= $scale; $i++ ){
	print FILE "<td width=$unit align=right bgcolor=#eeeeee><font $font >$i MB |</font></td>\n";
}
# extra column for date
print FILE "<td width=$extra align=right><font $font><b>$caption</b></font></td>\n";
print FILE "</tr>\n";
print FILE "</table>\n";
print FILE "</td></tr>\n";

do_list( *FILE);

print FILE "</table>\n";
print FILE "\n";
print FILE "</td></tr></table>\n";
print FILE "</html>\n";

close( FILE );	

print "OK";

sub do_graph
{
	$color = "red" ;
	$speed = int( $_[0] );
        $date = $_[1];
		
	$width= int( ($speed / ($scale *  $size)) * $max  );
	$remain= $max - $width;
	if( $final == 1 ){
		$color = "green" ;
	}

	print FILE "<tr>\n";
	print FILE "<td>\n";
	print FILE "<table width=$maxmax $table >\n";
	print FILE "<tr>\n";
	print FILE "<td width=$width bgcolor=$color align=right><font $font2 >$speed Kbps</font>\n";
	print FILE "</td><td width=$remain><font $font >&nbsp;</font></td>\n";
	# extra column for date
	if( $final == 1 ){
	  print FILE "<td width=$extra align=right><font $font >AVARAGE SPEED</font></td>\n";
	} else {
	  print FILE "<td width=$extra align=right><font $font > $date </font></td>\n";
	}
	print FILE "</tr>\n";
	print FILE "</table>\n";
	print FILE "</td></tr>\n";

}

sub add_to_list
{
	$speed = $_[0];
	chomp($speed);
	open ( LIST  , ">>$speed_list") || return 0;
	flock ( LIST, 2 ) or die "ERROR 2: $!";
	print LIST "$speed # $day\n";
	close( LIST );
}

sub do_list
{
	$sum = 0;
	$count = 0;
	open ( LIST  , "<$speed_list") || return 0;
	flock ( LIST, 2 ) or die "ERROR 3: $!";
	while( <LIST> ){
		$line = $_;
		chomp($line);
		( $seconds, $date ) = split('#', $line);
		do_graph( $seconds, $date  );
		$sum += $line;
		$count++;
	}
	close ( LIST );
	$final = 1 ;
	do_graph( $sum / $count , "0" );
}


syntax highlighted by Code2HTML, v. 0.9