<?
  function graphNetworkBytes($start, $rrd, $title)
  {
    passthru("/usr/bin/rrdtool graph - \
                               -v 'bits per second' \
			       -l 0 \
			       -a PNG \
			       --start $start \
			       --title '$title' \
			       DEF:Transmit=$rrd:tran:AVERAGE \
			       DEF:Receive=$rrd:recv:AVERAGE \
			       CDEF:Recvbps=Receive,8,* \
			       CDEF:Tranbps=Transmit,8,* \
			       AREA:Recvbps#00ff00:'Receive' \
			       LINE1:Tranbps#0000ff:'Transmit'");
  }

  function graphNetworkErrors($start, $rrd, $title)
  {
    passthru("/usr/bin/rrdtool graph - \
                               -v 'errors' \
			       -l 0 \
			       -a PNG \
			       --start $start \
			       --title '$title' \
			       DEF:Transmit=$rrd:tranerrs:AVERAGE \
			       DEF:Receive=$rrd:recverrs:AVERAGE \
			       DEF:TransmitColl=$rrd:trancoll:AVERAGE \
			       CDEF:trans=Transmit,300,* \
			       CDEF:recv=Receive,300,* \
			       CDEF:coll=TransmitColl,300,* \
			       LINE1:trans#00ff00:'Transmit Errors' \
			       LINE1:recv#ff0000:'Received Errors' \
			       LINE1:coll#0000ff:'Collisions'");
  }

  function graphWvlanSignal($start, $rrd, $title)
  {
    passthru("/usr/bin/rrdtool graph - \
                               -v '-dBm' \
                               -l 0 \
                               -a PNG \
                               --start $start \
                               --title '$title' \
                               DEF:signal=$rrd:signal:AVERAGE \
                               DEF:noise=$rrd:noise:AVERAGE \
                               AREA:noise#00ff00:'Noise' \
                               LINE1:signal#0000ff:'Signal'");
  }

  function graphMemoryFree($start, $rrd)
  {
    passthru("/usr/bin/rrdtool graph - \
                               -v 'bytes' \
			       -l 0 \
			       -a PNG \
			       --start $start \
			       --title 'Memory' \
			       DEF:realfree=$rrd:realfree:AVERAGE \
			       DEF:swapfree=$rrd:swapfree:AVERAGE \
			       DEF:shared=$rrd:shared:AVERAGE \
			       DEF:buffers=$rrd:buffers:AVERAGE \
			       DEF:cached=$rrd:cached:AVERAGE \
			       AREA:swapfree#00ff00:'Swap Free' \
			       STACK:realfree#0000ff:'Real Free' \
			       STACK:shared#ff0000:'Shared' \
			       STACK:buffers#ffff00:'Buffers' \
			       STACK:cached#00ffff:'Cached'");
  }

  $eth0 = "/home/system-rrd/rrds/network-eth0.rrd";
  $eth1 = "/home/system-rrd/rrds/network-eth1.rrd";
$wvlan0 = "/home/system-rrd/rrds/00022D054405.rrd";
$wvlan1 = "/home/system-rrd/rrds/00022D0544DB.rrd";
$memory = "/home/system-rrd/rrds/memory-free.rrd";

  $now = time();
  $last4hours = $now - (4*60*60);
  $lastday = $now - (24*60*60);
  $last2day = $now - (2*24*60*60);
  $lastweek = $now - (7*24*60*60);
  $last4week = $now - (4*7*24*60*60);
  $lastyear = $now - (52*7*24*60*60);

  $start = $last4hours;
  if (isset($p))
  {
    switch($p)
    {
      case '4':
      	$start = $last4hours;
	break;
      case 'd':
      	$start = $lastday;
	break;
      case '2d':
      	$start = $last2day;
	break;
      case 'w':
        $start = $lastweek;
	break;
      case 'm':
        $start = $last4week;
	break;
      case 'y':
        $start = $lastyear;
	break;
      default:
        $start = $lastday;
    }
  }

  header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  header("Cache-Control: no-cache, must-revalidate");
  header("Pragma: no-cache");
  header("Content-type: image/png");

  switch($t)
  {
    case 'e0':
      graphNetworkErrors($start, $eth0, 'Eth0 Error Stats');
      break;
    case 'b0':
      graphNetworkBytes($start, $eth0, 'Eth0 Network Stats');
      break;
    case 'e1':
      graphNetworkErrors($start, $eth1, 'Wavelan Error Stats');
      break;
    case 'b1':
      graphNetworkBytes($start, $eth1, 'Wavelan Network Stats');
      break;
    case 'w0':
      graphWvlanSignal($start, $wvlan0, 'Wavelan 00022D054405');
      break;
    case 'w1':
      graphWvlanSignal($start, $wvlan1, 'Wavelan 00022D0544DB');
      break;
    case 'm':
      graphMemoryFree($start, $memory);
      break;
    default:
      graphNetworkBytes($start, $network);
  }

?>
