Index: trunk/web/addons/toga/graph.php
===================================================================
--- trunk/web/addons/toga/graph.php	(revision 144)
+++ trunk/web/addons/toga/graph.php	(revision 145)
@@ -1,6 +1,4 @@
 <?php
-/* $Id: graph.php,v 1.8 2005/02/18 14:20:35 knobi1 Exp $ */
-include_once "./conf.php";
-include_once "./get_context.php";
+include_once "./libtoga.php";
 
 if ( !empty( $_GET ) ) {
@@ -20,284 +18,339 @@
 $sourcetime = escapeshellcmd($HTTP_GET_VARS["st"]);
 
+$cluster = $c;
+$metricname = ($g) ? $g : $m;
+$hostname = $h;
+
 # Assumes we have a $start variable (set in get_context.php).
-if ($size == "small")
-    {
-      $height = 40;
-      $width = 130;
-    }
-else if ($size == "medium")
-    {
-      $height = 75;
-      $width = 300;
-    }
-else
-    {
-      $height = 100;
-      $width = 400;
-    }
-
-
-# This security fix was brought to my attention by Peter Vreugdenhil <petervre@sci.kun.nl>
-# Dont want users specifying their own malicious command via GET variables e.g.
-# http://ganglia.mrcluster.org/graph.php?graph=blob&command=whoami;cat%20/etc/passwd
-#
-if($command)
-    {
-      exit();
-    }
-
-switch ($context)
-{
-    case "meta":
-      $rrd_dir = "$rrds/__SummaryInfo__";
-      break;
-    case "grid":
-      $rrd_dir = "$rrds/$grid/__SummaryInfo__";
-      break;
-    case "cluster":
-      $rrd_dir = "$rrds/$clustername/__SummaryInfo__";
-      break;
-    case "host":
-      $rrd_dir = "$rrds/$clustername/$hostname";
-      break;
-    default:
-      exit;
-}
-
-if ($graph)   /* Canned graph request */
-    {
-      if($graph == "cpu_report")
-         {
-            $style = "CPU";
-
-            $upper_limit = "--upper-limit 100 --rigid";
-            $lower_limit = "--lower-limit 0";
-
-            $vertical_label = "--vertical-label Percent ";
-
-            if($context != "host" )
-               {
-                  /* If we are not in a host context, then we need to calculate the average */
-                  $series =
-                  "DEF:'num_nodes'='${rrd_dir}/cpu_user.rrd':'num':AVERAGE "
-                  ."DEF:'cpu_user'='${rrd_dir}/cpu_user.rrd':'sum':AVERAGE "
-                  ."CDEF:'ccpu_user'=cpu_user,num_nodes,/ "
-                  ."DEF:'cpu_nice'='${rrd_dir}/cpu_nice.rrd':'sum':AVERAGE "
-                  ."CDEF:'ccpu_nice'=cpu_nice,num_nodes,/ "
-                  ."DEF:'cpu_system'='${rrd_dir}/cpu_system.rrd':'sum':AVERAGE "
-                  ."CDEF:'ccpu_system'=cpu_system,num_nodes,/ "
-                  ."DEF:'cpu_idle'='${rrd_dir}/cpu_idle.rrd':'sum':AVERAGE "
-                  ."CDEF:'ccpu_idle'=cpu_idle,num_nodes,/ "
-                  ."AREA:'ccpu_user'#$cpu_user_color:'User CPU' "
-                  ."STACK:'ccpu_nice'#$cpu_nice_color:'Nice CPU' "
-                  ."STACK:'ccpu_system'#$cpu_system_color:'System CPU' ";
-                  if (file_exists("$rrd_dir/cpu_wio.rrd")) {
-                     $series .= "DEF:'cpu_wio'='${rrd_dir}/cpu_wio.rrd':'sum':AVERAGE "
-                     ."CDEF:'ccpu_wio'=cpu_wio,num_nodes,/ "
-                     ."STACK:'ccpu_wio'#$cpu_wio_color:'WAIT CPU' ";
-                  }
-                  $series .= "STACK:'ccpu_idle'#$cpu_idle_color:'Idle CPU' ";
-               }
-            else
-               {
-                  $series ="DEF:'cpu_user'='${rrd_dir}/cpu_user.rrd':'sum':AVERAGE "
-                  ."DEF:'cpu_nice'='${rrd_dir}/cpu_nice.rrd':'sum':AVERAGE "
-                  ."DEF:'cpu_system'='${rrd_dir}/cpu_system.rrd':'sum':AVERAGE "
-                  ."DEF:'cpu_idle'='${rrd_dir}/cpu_idle.rrd':'sum':AVERAGE "
-                  ."AREA:'cpu_user'#$cpu_user_color:'User CPU' "
-                  ."STACK:'cpu_nice'#$cpu_nice_color:'Nice CPU' "
-                  ."STACK:'cpu_system'#$cpu_system_color:'System CPU' ";
-                  if (file_exists("$rrd_dir/cpu_wio.rrd")) {
-                     $series .= "DEF:'cpu_wio'='${rrd_dir}/cpu_wio.rrd':'sum':AVERAGE "
-                     ."STACK:'cpu_wio'#$cpu_wio_color:'WAIT CPU' ";
-                  }
-                  $series .= "STACK:'cpu_idle'#$cpu_idle_color:'Idle CPU' ";
-               }
-         }
-      else if ($graph == "mem_report")
-         {
-            $style = "Memory";
-
-            $lower_limit = "--lower-limit 0 --rigid";
-            $extras = "--base 1024";
-            $vertical_label = "--vertical-label Bytes";
-
-            $series = "DEF:'mem_total'='${rrd_dir}/mem_total.rrd':'sum':AVERAGE "
-               ."CDEF:'bmem_total'=mem_total,1024,* "
-               ."DEF:'mem_shared'='${rrd_dir}/mem_shared.rrd':'sum':AVERAGE "
-               ."CDEF:'bmem_shared'=mem_shared,1024,* "
-               ."DEF:'mem_free'='${rrd_dir}/mem_free.rrd':'sum':AVERAGE "
-               ."CDEF:'bmem_free'=mem_free,1024,* "
-               ."DEF:'mem_cached'='${rrd_dir}/mem_cached.rrd':'sum':AVERAGE "
-               ."CDEF:'bmem_cached'=mem_cached,1024,* "
-               ."DEF:'mem_buffers'='${rrd_dir}/mem_buffers.rrd':'sum':AVERAGE "
-               ."CDEF:'bmem_buffers'=mem_buffers,1024,* "
-               ."CDEF:'bmem_used'='bmem_total','bmem_shared',-,'bmem_free',-,'bmem_cached',-,'bmem_buffers',- "
-               ."AREA:'bmem_used'#$mem_used_color:'Memory Used' "
-               ."STACK:'bmem_shared'#$mem_shared_color:'Memory Shared' "
-               ."STACK:'bmem_cached'#$mem_cached_color:'Memory Cached' "
-               ."STACK:'bmem_buffers'#$mem_buffered_color:'Memory Buffered' ";
-            if (file_exists("$rrd_dir/swap_total.rrd")) {
-               $series .= "DEF:'swap_total'='${rrd_dir}/swap_total.rrd':'sum':AVERAGE "
-               ."DEF:'swap_free'='${rrd_dir}/swap_free.rrd':'sum':AVERAGE "
-               ."CDEF:'bmem_swapped'='swap_total','swap_free',-,1024,* "
-               ."STACK:'bmem_swapped'#$mem_swapped_color:'Memory Swapped' ";
-            }
-            $series .= "LINE2:'bmem_total'#$cpu_num_color:'Total In-Core Memory' ";
-         }
-      else if ($graph == "load_report")
-         {
-            $style = "Load";
-
-            $lower_limit = "--lower-limit 0 --rigid";
-            $vertical_label = "--vertical-label 'Load/Procs'";
-
-            $series = "DEF:'load_one'='${rrd_dir}/load_one.rrd':'sum':AVERAGE "
-               ."DEF:'proc_run'='${rrd_dir}/proc_run.rrd':'sum':AVERAGE "
-               ."DEF:'cpu_num'='${rrd_dir}/cpu_num.rrd':'sum':AVERAGE ";
-            if( $context != "host" )
-               {
-                  $series .="DEF:'num_nodes'='${rrd_dir}/cpu_num.rrd':'num':AVERAGE ";
-               }
-            $series .="AREA:'load_one'#$load_one_color:'1-min Load' ";
-            if( $context != "host" )
-               {
-                  $series .= "LINE2:'num_nodes'#$num_nodes_color:'Nodes' ";
-               }
-            $series .="LINE2:'cpu_num'#$cpu_num_color:'CPUs' ";
-            $series .="LINE2:'proc_run'#$proc_run_color:'Running Processes' ";
-         }
-      else if ($graph == "network_report")
-         {
-            $style = "Network";
-
-            $lower_limit = "--lower-limit 0 --rigid";
-            $extras = "--base 1024";
-            $vertical_label = "--vertical-label 'Bytes/sec'";
-
-            $series = "DEF:'bytes_in'='${rrd_dir}/bytes_in.rrd':'sum':AVERAGE "
-               ."DEF:'bytes_out'='${rrd_dir}/bytes_out.rrd':'sum':AVERAGE "
-               ."LINE2:'bytes_in'#$mem_cached_color:'In' "
-               ."LINE2:'bytes_out'#$mem_used_color:'Out' ";
-         }
-      else if ($graph == "packet_report")
-         {
-            $style = "Packets";
-
-            $lower_limit = "--lower-limit 0 --rigid";
-            $extras = "--base 1024";
-            $vertical_label = "--vertical-label 'Packets/sec'";
-
-            $series = "DEF:'bytes_in'='${rrd_dir}/pkts_in.rrd':'sum':AVERAGE "
-               ."DEF:'bytes_out'='${rrd_dir}/pkts_out.rrd':'sum':AVERAGE "
-               ."LINE2:'bytes_in'#$mem_cached_color:'In' "
-               ."LINE2:'bytes_out'#$mem_used_color:'Out' ";
-         }
-      else
-         {
-      /* Custom graph */
-      $style = "";
-
-      $subtitle = $metricname;
-      if ($context == "host") 
-      {
-          if ($size == "small")
-              $prefix = $metricname;
-          else
-              $prefix = $hostname;
-
-          $value = $value>1000 ? number_format($value) : number_format($value, 2);
-
-          if ($range=="job") {
-               $hrs = intval( -$jobrange / 3600 );
-               $subtitle = "$prefix last ${hrs}h (now $value)";
-          }
-          else
-              $subtitle = "$prefix last $range (now $value)";
-      }
-
-      if (is_numeric($max))
-         $upper_limit = "--upper-limit '$max' ";
-      if (is_numeric($min))
-         $lower_limit ="--lower-limit '$min' ";
-
-      if ($vlabel)
-         $vertical_label = "--vertical-label '$vlabel'";
-      else if ($upper_limit or $lower_limit)
-         {
-            $max = $max>1000 ? number_format($max) : number_format($max, 2);
-            $min = $min>0 ? number_format($min,2) : $min;
-
-            $vertical_label ="--vertical-label '$min - $max' ";
-         }
-
-      $rrd_file = "$rrd_dir/$metricname.rrd";
-      $series = "DEF:'sum'='$rrd_file':'sum':AVERAGE "
-         ."AREA:'sum'#$default_metric_color:'$subtitle' ";
-      if ($jobstart)
-         $series .= "VRULE:$jobstart#$jobstart_color ";
-    }
-
-# Set the graph title.
-if($context == "meta")
-   {
-     $title = "$self $meta_designator $style last $range";
-   }
-else if ($context == "grid")
-  {
-     $title = "$grid $meta_designator $style last $range";
-  }
-else if ($context == "cluster")
-   {
-      $title = "$clustername $style last $range";
-   }
-else
-   {
-    if ($size == "small")
-      {
-        # Value for this graph define a background color.
-        if (!$load_color) $load_color = "ffffff";
-        $background = "--color BACK#'$load_color'";
-
-        $title = $hostname;
-      }
-    else if ($style)
-       $title = "$hostname $style last $range";
-    else
-       $title = $metricname;
-   }
-
-# Fix from Phil Radden, but step is not always 15 anymore.
-if ($range=="month")
-   $end = floor($end / 672) * 672;
+if ($size == "small") {
+	$height = 40;
+	$width = 130;
+} else if ($size == "medium") {
+	$height = 75;
+	$width = 300;
+} else {
+	$height = 100;
+	$width = 400;
+}
+
+if($command) {
+      $command = '';
+}
+
+//printf( "cluster = %s hostname = %s metric = %s\n", $cluster, $hostname, $metricname );
+
+$trd = new TarchRrdGraph( $cluster, $hostname );
+//$rrd_files = $trd->getRrdFiles( $metricname, $start, $stop );
+
+//print_r( $rrd_files );
+
+$graph = $metricname;
+
+if (isset($graph)) {
+	$rrd_dirs = $trd->getRrdDirs( $start, $stop );
+	$series = '';
+
+	if($graph == "cpu_report") {
+		$style = "CPU";
+		//printf("ik doe die shit!\n");
+
+		$upper_limit = "--upper-limit 100 --rigid";
+		$lower_limit = "--lower-limit 0";
+
+		$vertical_label = "--vertical-label Percent ";
+
+		$def_nr = 0;
+
+		foreach( $rrd_dirs as $rrd_dir ) {
+
+			if( $def_nr == 0 ) {
+
+				$user_str = ":'User CPU'";
+				$nice_str = ":'Nice CPU'";
+				$system_str = ":'System CPU'";
+				$wio_str = ":'WAIT CPU'";
+				$idle_str = ":'Idle CPU'";
+			} else {
+
+				$user_str = "";
+				$nice_str = "";
+				$system_str = "";
+				$wio_str = "";
+				$idle_str = "";
+			}
+
+			$series .= "DEF:'cpu_user${def_nr}'='${rrd_dir}/cpu_user.rrd':'sum':AVERAGE "
+				."DEF:'cpu_nice${def_nr}'='${rrd_dir}/cpu_nice.rrd':'sum':AVERAGE "
+				."DEF:'cpu_system${def_nr}'='${rrd_dir}/cpu_system.rrd':'sum':AVERAGE "
+				."DEF:'cpu_idle${def_nr}'='${rrd_dir}/cpu_idle.rrd':'sum':AVERAGE "
+				."AREA:'cpu_user${def_nr}'#${cpu_user_color}${user_str} "
+				."STACK:'cpu_nice${def_nr}'#${cpu_nice_color}${nice_str} "
+				."STACK:'cpu_system${def_nr}'#${cpu_system_color}${system_str} ";
+
+			if (file_exists("$rrd_dir/cpu_wio.rrd")) {
+				$series .= "DEF:'cpu_wio${def_nr}'='${rrd_dir}/cpu_wio.rrd':'sum':AVERAGE "
+					."STACK:'cpu_wio${def_nr}'#${cpu_wio_color}${wio_str} ";
+			}
+
+			$series .= "STACK:'cpu_idle${def_nr}'#${cpu_idle_color}${idle_str} ";
+
+			$def_nr++;
+		}
+
+	} else if ($graph == "mem_report") {
+		$style = "Memory";
+
+		$lower_limit = "--lower-limit 0 --rigid";
+		$extras = "--base 1024";
+		$vertical_label = "--vertical-label Bytes";
+
+		$def_nr = 0;
+
+		foreach( $rrd_dirs as $rrd_dir ) {
+
+			if( $def_nr == 0 ) {
+
+				$memuse_str = ":'Memory Used'";
+				$memshared_str = ":'Memory Shared'";
+				$memcached_str = ":'Memory Cached'";
+				$membuff_str = ":'Memory Buffered'";
+				$memswap_str = ":'Memory Swapped'";
+				$total_str = ":'Total In-Core Memory'";
+			} else {
+
+				$memuse_str = "";
+				$memshared_str = "";
+				$memcached_str = "";
+				$membuff_str = "";
+				$memswap_str = "";
+				$total_str = "";
+			}
+
+			$series .= "DEF:'mem_total${def_nr}'='${rrd_dir}/mem_total.rrd':'sum':AVERAGE "
+				."CDEF:'bmem_total${def_nr}'=mem_total${def_nr},1024,* "
+				."DEF:'mem_shared${def_nr}'='${rrd_dir}/mem_shared.rrd':'sum':AVERAGE "
+				."CDEF:'bmem_shared${def_nr}'=mem_shared${def_nr},1024,* "
+				."DEF:'mem_free${def_nr}'='${rrd_dir}/mem_free.rrd':'sum':AVERAGE "
+				."CDEF:'bmem_free${def_nr}'=mem_free${def_nr},1024,* "
+				."DEF:'mem_cached${def_nr}'='${rrd_dir}/mem_cached.rrd':'sum':AVERAGE "
+				."CDEF:'bmem_cached${def_nr}'=mem_cached${def_nr},1024,* "
+				."DEF:'mem_buffers${def_nr}'='${rrd_dir}/mem_buffers.rrd':'sum':AVERAGE "
+				."CDEF:'bmem_buffers${def_nr}'=mem_buffers${def_nr},1024,* "
+				."CDEF:'bmem_used${def_nr}'='bmem_total${def_nr}','bmem_shared${def_nr}',-,'bmem_free${def_nr}',-,'bmem_cached${def_nr}',-,'bmem_buffers${def_nr}',- "
+				."AREA:'bmem_used${def_nr}'#${mem_used_color}${memuse_str} "
+				."STACK:'bmem_shared${def_nr}'#${mem_shared_color}${memshared_str} "
+				."STACK:'bmem_cached${def_nr}'#${mem_cached_color}${memcached_str} "
+				."STACK:'bmem_buffers${def_nr}'#${mem_buffered_color}${membuff_str} ";
+
+			if (file_exists("$rrd_dir/swap_total.rrd")) {
+				$series .= "DEF:'swap_total${def_nr}'='${rrd_dir}/swap_total.rrd':'sum':AVERAGE "
+					."DEF:'swap_free${def_nr}'='${rrd_dir}/swap_free.rrd':'sum':AVERAGE "
+					."CDEF:'bmem_swapped${def_nr}'='swap_total${def_nr}','swap_free${def_nr}',-,1024,* "
+					."STACK:'bmem_swapped${def_nr}'#${mem_swapped_color}${memswap_str} ";
+			}
+
+			$series .= "LINE2:'bmem_total${def_nr}'#${cpu_num_color}${total_str} ";
+
+			$def_nr++;
+		}
+
+	} else if ($graph == "load_report") {
+		$style = "Load";
+
+		$lower_limit = "--lower-limit 0 --rigid";
+		$vertical_label = "--vertical-label 'Load/Procs'";
+
+		$def_nr = 0;
+
+		foreach( $rrd_dirs as $rrd_dir ) {
+
+			if( $def_nr == 0 ) {
+
+				$load_str = ":'1-min Load'";
+				$cpu_str = ":'CPUs'";
+				$run_str = ":'Running Processes'";
+			} else {
+
+			}
+
+			$series .= "DEF:'load_one${def_nr}'='${rrd_dir}/load_one.rrd':'sum':AVERAGE "
+				."DEF:'proc_run${def_nr}'='${rrd_dir}/proc_run.rrd':'sum':AVERAGE "
+				."DEF:'cpu_num${def_nr}'='${rrd_dir}/cpu_num.rrd':'sum':AVERAGE ";
+			$series .="AREA:'load_one${def_nr}'#${load_one_color}${load_str} ";
+			$series .="LINE2:'cpu_num${def_nr}'#${cpu_num_color}${cpu_str} ";
+			$series .="LINE2:'proc_run${def_nr}'#${proc_run_color}${run_str} ";
+
+			$def_nr++;
+		}
+
+	} else if ($graph == "network_report") {
+		$style = "Network";
+
+		$lower_limit = "--lower-limit 0 --rigid";
+		$extras = "--base 1024";
+		$vertical_label = "--vertical-label 'Bytes/sec'";
+
+		$def_nr = 0;
+
+		foreach( $rrd_dirs as $rrd_dir ) {
+
+			if( $def_nr == 0 ) {
+
+				$in_str = ":'In'";
+				$out_str = ":'Out'";
+			} else {
+
+				$in_str = "";
+				$out_str = "";
+			}
+
+			$series .= "DEF:'bytes_in${def_nr}'='${rrd_dir}/bytes_in.rrd':'sum':AVERAGE "
+				."DEF:'bytes_out${def_nr}'='${rrd_dir}/bytes_out.rrd':'sum':AVERAGE "
+				."LINE2:'bytes_in${def_nr}'#${mem_cached_color}${in_str} "
+				."LINE2:'bytes_out${def_nr}'#${mem_used_color}${out_str} ";
+
+			$def_nr++;
+		}
+
+	} else if ($graph == "packet_report") {
+		$style = "Packets";
+
+		$lower_limit = "--lower-limit 0 --rigid";
+		$extras = "--base 1024";
+		$vertical_label = "--vertical-label 'Packets/sec'";
+
+		$def_nr = 0;
+
+		foreach( $rrd_dirs as $rrd_dir ) {
+
+			if( $def_nr == 0 ) {
+
+				$in_str = ":'In'";
+				$out_str = ":'Out'";
+			} else {
+
+				$in_str = "";
+				$out_str = "";
+			}
+
+			$series .= "DEF:'bytes_in${def_nr}'='${rrd_dir}/pkts_in.rrd':'sum':AVERAGE "
+				."DEF:'bytes_out${def_nr}'='${rrd_dir}/pkts_out.rrd':'sum':AVERAGE "
+				."LINE2:'bytes_in${def_nr}'#${mem_cached_color}${in_str} "
+				."LINE2:'bytes_out${def_nr}'#${mem_used_color}${out_str} ";
+
+			$def_nr++;
+		}
+
+	} else {
+		/* Custom graph */
+		$style = "";
+
+		$subtitle = $metricname;
+		if ($context == "host") {
+			if ($size == "small")
+				$prefix = $metricname;
+			else
+				$prefix = $hostname;
+
+			$value = $value>1000 ? number_format($value) : number_format($value, 2);
+		}
+
+		//if ($range=="job") {
+		//	$hrs = intval( -$jobrange / 3600 );
+		//	$subtitle = "$prefix last ${hrs}h (now $value)";
+		//} else
+		//	$subtitle = "$prefix last $range (now $value)";
+
+		if (is_numeric($max))
+			$upper_limit = "--upper-limit '$max' ";
+		if (is_numeric($min))
+			$lower_limit ="--lower-limit '$min' ";
+
+		if ($vlabel)
+			$vertical_label = "--vertical-label '$vlabel'";
+		else {
+			if ($upper_limit or $lower_limit) {
+				$max = $max>1000 ? number_format($max) : number_format($max, 2);
+				$min = $min>0 ? number_format($min,2) : $min;
+
+				$vertical_label ="--vertical-label '$min - $max' ";
+			}
+		}
+
+		$def_nr = 0;
+
+		foreach( $rrd_dirs as $rrd_dir ) {
+
+			if( $def_nr == 0 ) {
+				$title_str = ":'${subtitle}'";
+			} else {
+				$title_str = "";
+			}
+
+			$rrd_file = "$rrd_dir/$metricname.rrd";
+			$series .= "DEF:'sum${def_nr}'='$rrd_file':'sum':AVERAGE "
+				."AREA:'sum${def_nr}'#${default_metric_color}${title_str} ";
+
+			$def_nr++;
+		}
+
+	}
+	if( $series != '' ) {
+		if ($job_start)
+			$series .= "VRULE:${job_start}#${jobstart_color} ";
+		if ($job_stop)
+			$series .= "VRULE:${job_stop}#${jobstart_color} ";
+	}
+}
+
+//$title = "$hostname $style $metricname";
+$title = "$hostname";
+
+//# Set the graph title.
+//if($context == "meta") {
+//	$title = "$self $meta_designator $style last $range";
+//} else if ($context == "grid") {
+//	$title = "$grid $meta_designator $style last $range";
+//} else if ($context == "cluster") {
+//	$title = "$clustername $style last $range";
+//} else {
+//	if ($size == "small") {
+//		# Value for this graph define a background color.
+//		if (!$load_color) $load_color = "ffffff";
+//			$background = "--color BACK#'$load_color'";
+
+//		$title = $hostname;
+//	} else {
+//		if ($style)
+//			$title = "$hostname $style last $range";
+//		else
+//			$title = $metricname;
+//	}
+//}
 
 #
 # Generate the rrdtool graph command.
 #
-$command = RRDTOOL . " graph - --start $start --end $end ".
-   "--width $width --height $height $upper_limit $lower_limit ".
-   "--title '$title' $vertical_label $extras $background ".
-   $series;
+$command = RRDTOOL . " graph - --start $start --end $stop ".
+	"--width $width --height $height $upper_limit $lower_limit ".
+	"--title '$title' $vertical_label $extras $background ".
+	$series;
 
 $debug=0;
 
 # Did we generate a command?   Run it.
-if($command)
- {
-   /*Make sure the image is not cached*/
-   header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");   // Date in the past
-   header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
-   header ("Cache-Control: no-cache, must-revalidate");   // HTTP/1.1
-   header ("Pragma: no-cache");                     // HTTP/1.0
-   if ($debug) {
-     header ("Content-type: text/html");
-     print "$command\n\n\n\n\n";
-    }
-   else {
-     header ("Content-type: image/gif");
-     passthru($command);
-    }
- }
-
+if($command) {
+	/*Make sure the image is not cached*/
+	header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");   // Date in the past
+	header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
+	header ("Cache-Control: no-cache, must-revalidate");   // HTTP/1.1
+	header ("Pragma: no-cache");                     // HTTP/1.0
+	if ($debug) {
+		header ("Content-type: text/html");
+		print "$command\n\n\n\n\n";
+	} else {
+		header ("Content-type: image/gif");
+		passthru($command);
+	}
+}
 ?>
-
Index: trunk/web/addons/toga/index.php
===================================================================
--- trunk/web/addons/toga/index.php	(revision 144)
+++ trunk/web/addons/toga/index.php	(revision 145)
@@ -39,5 +39,5 @@
 	global $default_refresh, $filterorder, $view;
 	global $TARCHD;
-
+	
 	if( isset($default_metric) and !isset($m) )
 		$metricname = $default_metric;
@@ -146,4 +146,5 @@
 	foreach ($metrics[$firsthost] as $m => $foo)
 		$context_metrics[] = $m;
+
 	foreach ($reports as $r => $foo)
 		$context_metrics[] = $r;
@@ -319,5 +320,4 @@
 
 makeFooter();
-
 $tpl->printToScreen();
 ?>
Index: trunk/web/addons/toga/libtoga.php
===================================================================
--- trunk/web/addons/toga/libtoga.php	(revision 144)
+++ trunk/web/addons/toga/libtoga.php	(revision 145)
@@ -2,7 +2,7 @@
 // If php is compiled without globals
 //
-if ( !empty( $_GET ) ) {
-        extract( $_GET );
-}
+//if ( !empty( $_GET ) ) {
+//        extract( $_GET );
+//}
 
 class HTTPVariables {
@@ -60,10 +60,10 @@
 chdir( $GANGLIA_PATH );
 
-$context = 'cluster';
-
 include_once "./conf.php";
 include_once "./functions.php";
 include_once "./ganglia.php";
-//include_once "./get_context.php";
+include_once "./get_context.php";
+unset( $start );
+$context = 'cluster';
 include_once "./get_ganglia.php";
 
@@ -229,8 +229,11 @@
 	var $rrdbin, $rrdvalues, $clustername, $hostname, $tempdir, $tarchdir, $metrics;
 
-	function TarchRrd( $clustername, $rrdbin = '/usr/bin/rrdtool', $tarchdir = '/data/toga/rrds' ) {
+	function TarchRrdGraph( $clustername, $hostname, $rrdbin = '/usr/bin/rrdtool', $tarchdir = '/data/toga/rrds' ) {
+	
 		$this->rrdbin = $rrdbin;
 		$this->rrdvalues = array();
 		$this->tarchdir = $tarchdir;
+		$this->clustername = $clustername;
+		$this->hostname = $hostname;
 	}
 
@@ -274,6 +277,10 @@
 	function getTimePeriods( $start, $end ) {
 
+		//printf("start = %s end = %s\n", $start, $end );
 		$times = array();
 		$dirlist = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname );
+
+		//print_r( $dirlist );
+
 		$first = 0;
 		$last = 9999999999999;
@@ -287,13 +294,55 @@
 		}
 
+		//printf( "first = %s last = %s\n", $first, $last );
+
 		foreach( $dirlist as $dir ) {
 
-			if( $dir >= $first and $dir <= $last and !array_key_exists( $dir, $times ) )
+			//printf( "dir %s ", $dir );
+
+			if( $dir >= $first and $dir <= $last and !array_key_exists( $dir, $times ) ) {
+			
 				$times[] = $dir;
-		}
+				//printf("newtime %s ", $dir );
+
+			}
+		}
+
+		//print_r( $times );
 
 		sort( $times );
 
+		//print_r( $times );
+
 		return $times;
+	}
+
+	function getRrdDirs( $start, $stop ) {
+
+		//printf( "tarchdir = %s\n", $this->tarchdir );
+		$timess = $this->getTimePeriods( $start, $stop );
+		//print_r( $timess );
+
+		$rrd_files = array();
+
+		foreach( $timess as $time ) {
+
+			$rrd_files[] = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname. '/'.$time;
+		}
+
+		return $rrd_files;
+	}
+
+	function getRrdFiles( $metric, $start, $stop ) {
+
+		$times = $this->getTimePeriods( $start, $stop );
+
+		$rrd_files = array();
+
+		foreach( $times as $time ) {
+
+			$rrd_files[] = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname . '/' .$time. '/' . $metric. '.rrd';
+		}
+
+		return $rrd_files;
 	}
 
@@ -309,4 +358,5 @@
 
 		//return $graph;
+		return 0;
 	}
 }
Index: trunk/web/addons/toga/search.php
===================================================================
--- trunk/web/addons/toga/search.php	(revision 144)
+++ trunk/web/addons/toga/search.php	(revision 145)
@@ -128,4 +128,9 @@
 }
 
+function epochToDatetime( $epoch ) {
+
+	return strftime( "%d-%m-%Y %H:%M:%S", $epoch );
+}
+
 function timeToEpoch( $time ) {
 
@@ -159,6 +164,9 @@
 	global $clustername, $tpl, $id, $user, $name, $start_from_time, $start_to_time, $queue;
 	global $end_from_time, $end_to_time, $filter, $default_showhosts, $m, $hosts_up;
+	global $start, $stop;
 
 	$metricname = $m;
+	//printf("job_start = %s job_stop = %s\n", $job_start, $job_stop );
+	//printf("start = %s stop = %s\n", $start, $stop );
 
 	$tpl->assign( "cluster", $clustername );
@@ -197,5 +205,18 @@
 
 			$nodes_nr = count( $nodes );
-			$domain = $job[domain];
+
+			// need to replace later with domain stored from dbase
+			//
+			//$job_domain = $job[domain];
+
+			$myhost = $_SERVER[HTTP_HOST];
+			$myhf = explode( '.', $myhost );
+			$myhf = array_reverse( $myhf );
+			array_pop( $myhf );
+			$myhf = array_reverse( $myhf );
+			$job_domain = implode( '.', $myhf );
+			
+			//print_r( $job );
+			//printf( "job domain = %s\n", $job_domain);
 			$ppn = (int) $job[ppn] ? $job[ppn] : 1;
 			$cpus = $nodes_nr * $ppn;
@@ -204,7 +225,9 @@
 			$tpl->assign( "cpus", $cpus );
 
-			$runningtime = intval( $job[stop_timestamp] - $job[start_timestamp] );
-			$tpl->assign( "started", makeDate( $job[start_timestamp] ) );
-			$tpl->assign( "finished", makeDate( $job[stop_timestamp] ) );
+			$job_start = $job[start_timestamp];
+			$job_stop = $job[stop_timestamp];
+			$runningtime = intval( $job_stop - $job_start );
+			$tpl->assign( "started", makeDate( $job_start ) );
+			$tpl->assign( "finished", makeDate( $job_stop ) );
 			$tpl->assign( "runningtime", makeTime( $runningtime ) );
 			
@@ -223,5 +246,5 @@
 
 			# Present a width list
-			$cols_menu = "<SELECT NAME=\"hc\" OnChange=\"toga_form.submit();\">\n";
+			$cols_menu = "<SELECT NAME=\"hc\" OnChange=\"archive_search_form.submit();\">\n";
 
 			$hostcols = ($hc) ? $hc : 4;
@@ -243,24 +266,34 @@
 				//bla
 
-				if( !isset($start) ) $start="jobstart";
-				if( !isset($stop) ) $stop="now";
-				//$tpl->assign("start", $start);
-				//$tpl->assign("stop", $stop);
+				//printf("job_start = %s job_stop = %s\n", $job_start, $job_stop );
+				//printf("start = %s stop = %s\n", $start, $stop );
+
+				if( !$start ) // Add an additional 5 minutes before
+					$start = intval( $job_start - 600 );
+				else
+					$start = datetimeToEpoch( $start );
+
+				if( !$stop ) // Add an additional 5 minutes after
+					$stop = intval( $job_stop + 600 );
+				else
+					$stop = datetimeToEpoch( $stop );
+
+				//printf("start = %s stop = %s\n", $start, $stop );
+
+				$tpl->assign("j_start", epochToDatetime( $start ) );
+				$tpl->assign("j_stop", epochToDatetime( $stop ) );
+
+				$hosts_up = array();
+
+				foreach( $nodes as $mynode )
+					$hosts_up[] = $mynode[hostname];
+
+				//print_r( $hosts_up );
 
 				$sorted_hosts = array();
-				$hosts_up = $jobs[$filter[id]][nodes];
-
-				$r = intval($job_runningtime * 1.25);
-
-				$jobrange = ($job_runningtime < 3600) ? -3600 : -$r ;
-				$jobstart = $report_time - $job_runningtime;
-
-				if ($reports[$metricname])
-					$metricval = "g";
-				else
-					$metricval = "m";
+				//$hosts_up = $jobs[$filter[id]][nodes];
 
 				foreach ($hosts_up as $host ) {
-					$host = $host. '.'.$domain;
+					$host = $host. '.'.$job_domain;
 					$cpus = $metrics[$host]["cpu_num"][VAL];
 					if (!$cpus) $cpus=1;
@@ -307,5 +340,5 @@
 					$val = $metrics[$host][$metricname];
 					$class = "metric";
-					$host_link="\"?c=$cluster_url&h=$host_url&r=job&jr=$jobrange&js=$jobstart\"";
+					$host_link="\"?c=$cluster_url&h=$host_url&job_start=$job_start&job_stop=$job_stop&start=$start&stop=$stop\"";
 
 					if ($val[TYPE]=="timestamp" or $always_timestamp[$metricname]) {
@@ -314,12 +347,10 @@
 						$textval = "$val[VAL] $val[UNITS]";
 					} else {
-						$load_color = load_color($host_load[$host]);
-						$graphargs = ($reports[$metricname]) ? "g=$metricname&" : "m=$metricname&";
-						$graphargs .= "z=small&c=$cluster_url&h=$host_url&l=$load_color" ."&v=$val[VAL]&x=$max&n=$min&r=job&jr=$jobrange&js=$jobstart";
+						$graphargs = "z=small&c=$cluster_url&m=$metricname&h=$host_url&v=$val[VAL]&x=$max&n=$min&job_start=$job_start&job_stop=$job_stop&start=$start&stop=$stop";
 					}
 					if ($textval) {
 						$cell="<td class=$class>".  "<b><a href=$host_link>$host</a></b><br>".  "<i>$metricname:</i> <b>$textval</b></td>";
 					} else {
-						$cell="<td><a href=$host_link>".  "<img src=\"../../graph.php?$graphargs\" ".  "alt=\"$host\" height=112 width=225 border=0></a></td>";
+						$cell="<td><a href=$host_link>".  "<img src=\"./graph.php?$graphargs\" ".  "alt=\"$host\" border=0></a></td>";
 					}
 
Index: trunk/web/addons/toga/templates/search.tpl
===================================================================
--- trunk/web/addons/toga/templates/search.tpl	(revision 144)
+++ trunk/web/addons/toga/templates/search.tpl	(revision 145)
@@ -60,5 +60,4 @@
 <FORM NAME="archive_search_form" ACTION="./">
 
-<INPUT TYPE="hidden" NAME="c" VALUE="{cluster}">
 <INPUT TYPE="hidden" NAME="view" VALUE="search">
 
@@ -260,16 +259,16 @@
    </FONT><BR>
    <FONT SIZE="-1">
-    <INPUT TYPE="HIDDEN" NAME="start" VALUE="{start}">
-    <INPUT TYPE="HIDDEN" NAME="stop" VALUE="{stop}">
-    Set graph timeperiod from 
-    <INPUT TYPE="text" NAME="period_start_pick" VALUE="{start}" ALT="Start time" DISABLED="TRUE">
+    <INPUT TYPE="HIDDEN" NAME="start" VALUE="{j_start}">
+    <INPUT TYPE="HIDDEN" NAME="stop" VALUE="{j_stop}">
+    Graph timeperiod from 
+    <INPUT TYPE="text" NAME="period_start_pick" VALUE="{j_start}" ALT="Start time" DISABLED="TRUE">
     <a href="javascript:show_calendar('document.archive_search_form.period_start_pick', document.archive_search_form.period_start_pick.value);" alt="Click to select a date/time" title="Click to select a date/time">
     <img src="cal.gif" width="16" height="16" border="0"></a> 
     <a href="#" onClick="javascript: document.archive_search_form.period_start_pick.value=''" alt="Click here to clear field" title="Click here to clear field">
     <IMG SRC="redcross.jpg" BORDER=0></A>
-    to <INPUT TYPE="text" NAME="period_stop_pick" VALUE="{stop}" ALT="Stop time" DISABLED="TRUE">
+    to <INPUT TYPE="text" NAME="period_stop_pick" VALUE="{j_stop}" ALT="Stop time" DISABLED="TRUE">
     <a href="javascript:show_calendar('document.archive_search_form.period_stop_pick', document.archive_search_form.period_stop_pick.value);" alt="Click to select a date/time" title="Click to select a date/time">
     <img src="cal.gif" width="16" height="16" border="0"></a> 
-    <a href="#" onClick="javascript: document.archive_search_form.period_to_pick.value=''" alt="Click here to clear field" title="Click here to clear field">
+    <a href="#" onClick="javascript: document.archive_search_form.period_stop_pick.value=''" alt="Click here to clear field" title="Click here to clear field">
     <IMG SRC="redcross.jpg" BORDER=0></A>
     <INPUT TYPE="submit" onClick="setPeriodTimestamps();" VALUE="Refresh graphs">
@@ -289,7 +288,4 @@
 </TABLE>
 
-<p>
-(Nodes colored by 1-minute load) | <A HREF="../../node_legend.html" ALT="Node Image egend">Legend</A>
-
 </CENTER>
 
