<?php

$g = 0;  # Graph number
$colors[0] = "#00ff00";
$colors[1] = "#0000ff";
$colors[2] = "#ff0000";
$colors[3] = "#00a0c1";
$colors[4] = "#ff00ff";
$colors[5] = "#ffab00";
$colors[6] = "#000000";
$colors[7] = "#ffff00";
$colors[8] = "#00ffff";
$colors[9] = "#a000c1";

# First, split queues and latency into separate arrays and assign a color
$ic = 0;
$queue= array ();
$lat= array ();
foreach ($NAME as $key=>$val) {
   if (stristr($val, "queue")) {
      $queue[$key] = $val;
      $COLOR[$key] = $colors[$ic++%(count($colors))];
   } else if (stristr($val, "latency")) {
      $lat[$key] = $val;
      $COLOR[$key] = $colors[$ic++%(count($colors))];
   }
}

# queues graph (if any Queues were found)
if (count($queue) > 0) {
   $g++;
   $ds_name[$g] = "Queues";
   $opt[$g] = "--vertical-label 'Messages' --base=1000 --lower-limit=0 --alt-autoscale-max --title \"Queues for $hostname\" ";
   
   $def[$g] = "";
   foreach ($queue as $key=>$val) {
      $def[$g] .= "DEF:$NAME[$key]=$RRDFILE[$key]:$DS[$key]:AVERAGE ";
   }
   foreach ($queue as $key=>$val) {
      $def[$g] .= "LINE:$NAME[$key]" . "$COLOR[$key]:\"$NAME[$key]\" ";
      $def[$g] .= "GPRINT:$NAME[$key]" . ":LAST:\"Cur\: %6.2lf $UNIT[$key] \" " ;
      $def[$g] .= "GPRINT:$NAME[$key]" . ":AVERAGE:\"Avg\: %6.2lf $UNIT[$key] \" " ;
      $def[$g] .= "GPRINT:$NAME[$key]" . ":MAX:\"Max\: %6.2lf $UNIT[$key] \\n\" " ;
   }
}

# Latency graph (if any latencies were found)
if (count($lat) > 0) {
   $g++;
   $ds_name[$g] = "Latency";
   $opt[$g] = "--vertical-label 'Seconds' --base=1000 --lower-limit=0 --alt-autoscale-max --title \"Latencies for $hostname\" ";
   
   $def[$g] = "";
   foreach ($lat as $key=>$val) {
      $def[$g] .= "DEF:$NAME[$key]=$RRDFILE[$key]:$DS[$key]:AVERAGE ";
   }
   foreach ($lat as $key=>$val) {
      $def[$g] .= "LINE:$NAME[$key]" . "$COLOR[$key]:\"$NAME[$key]\" ";
      $def[$g] .= "GPRINT:$NAME[$key]" . ":LAST:\"Cur\: %6.2lf $UNIT[$key] \" " ;
      $def[$g] .= "GPRINT:$NAME[$key]" . ":AVERAGE:\"Avg\: %6.2lf $UNIT[$key] \" " ;
      $def[$g] .= "GPRINT:$NAME[$key]" . ":MAX:\"Max\: %6.2lf $UNIT[$key] \\n\" " ;
   }
}

?>
