#!/usr/bin/perl
#
#

use RRDs;


print "Content-type: text/html\n\n";

print "<HTML><HEAD><META HTTP-EQUIV=Pragma CONTENT=no-cache></HEAD><BODY>\n";

#nocache!

# set values for this run
my $logdir = "/opt/HotSaNIC/modules/traffic/rrd/";
my $logfile = "eth0.rrd";
my $cf = "AVERAGE";
my $stepsize = 600;
my $stepnum =6; 
my $ltime = localtime;
my $time = time;
#my $time = 1159209000;
my $endtime = int($time/$stepsize) * $stepsize;
my $starttime = $endtime - ($stepsize * ($stepnum - 1)); 

my ($start,$step,$names,$data) = RRDs::fetch ("$logdir$logfile", "$cf", "-r $stepsize", "-s $starttime", "-e $endtime"); 
my $ERR=RRDs::error;
print "ERROR while updating mydemo.rrd: $ERR\n </BODY></HTML>" if $ERR;

print "<pre>";
print "Start:       ", scalar localtime($start), " ($start)\n";
print "Step size:   $step seconds\n";
print "DS names:    ", join (", ", @$names)."\n";
print "Data points: ", $#$data + 1, "\n";
print "Data:\n";
$pocetdat = $#$data + 1;
$celkem_in = 0;
$celkem_out = 0;
foreach my $line (@$data) {
  print "  ", scalar localtime($start), " ($start) ";
  $lasttime = $start;
  $start += $step;
  $pozice = 0;
  foreach my $val (@$line) {
    printf "%12.1f ", $val;
    printf "%12.1f kB ", ($val * $step * 8);
    $pozice += 1;
    if ($pozice == 1) { $celkem_in += $val };
    if ($pozice == 2) { $celkem_out += $val };
  }
  print "\n";
}
  print "  ", scalar localtime($lasttime), " ($lasttime) ";
    printf "%12.1f ", $celkem_in/$pocetdat;
    printf "%12.1f MB ", ($celkem_in * $step / 1024 / 1024  );
    printf "%12.1f ", $celkem_out/$pocetdat;
    printf "%12.1f MB ", ($celkem_out * $step / 1024 / 1024);
  print "\n";

print "</pre>";
print "<img src=\"../hotsanic/traffic/eth0-hour.png\">";

print "</BODY></HTML>";
