<?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 voltages and temps into separate arrays and assign a color
$ic = 0;
foreach ($NAME as $key=>$val) {
   if (stristr($val, "temp")) {
      $temp[$key] = $val;
      $COLOR[$key] = $colors[$ic++%(count($colors))];
   } else if (stristr($val, "mv")) {
      $volt[$key] = $val;
      $COLOR[$key] = $colors[$ic++%(count($colors))];
   }
}

# Temps graph (if any temp sensors were found)
if (count($temp) > 0) {
   $g++;
   $ds_name[$g] = "Temps";
   #$opt[$g] = "--vertical-label 'Fahrenheit' -u60 --title \"Temps for $hostname\" ";
   $opt[$g] = "--vertical-label 'Fahrenheit' --alt-autoscale-max --title \"Temps for $hostname\" ";
   
   $def[$g] = "";
   foreach ($temp as $key=>$val) {
      $def[$g] .= "DEF:$NAME[$key]=$RRDFILE[$key]:$DS[$key]:AVERAGE ";
   }
   foreach ($temp as $key=>$val) {
      $def[$g] .= "CDEF:$NAME[$key]" . "f=$NAME[$key],1.8,*,32,+ ";
   }
   foreach ($temp as $key=>$val) {
      $def[$g] .= "LINE:$NAME[$key]" . "f$COLOR[$key]:\"$NAME[$key]\" ";
      $def[$g] .= "GPRINT:$NAME[$key]" . "f:LAST:\"Cur\: %6.2lf $UNIT[$key] \" " ;
      $def[$g] .= "GPRINT:$NAME[$key]" . "f:AVERAGE:\"Avg\: %6.2lf $UNIT[$key] \" " ;
      $def[$g] .= "GPRINT:$NAME[$key]" . "f:MAX:\"Max\: %6.2lf $UNIT[$key] \\n\" " ;
   }
}

# Voltage graph (if any voltage sensors were found)
if (count($volt) > 0) {
$g++;
   $ds_name[$g] = "Voltages";
   $opt[$g] = "--vertical-label 'Volts' --alt-autoscale-max --base=1000 --title \"Voltages for $hostname\" ";
   
   $def[$g] = "";
   foreach ($volt as $key=>$val) {
      $def[$g] .= "DEF:$NAME[$key]=$RRDFILE[$key]:$DS[$key]:AVERAGE ";
   }
   foreach ($volt as $key=>$val) {
      $def[$g] .= "CDEF:$NAME[$key]" . "f=$NAME[$key],1000,/ ";
   }
   foreach ($volt as $key=>$val) {
      $def[$g] .= "LINE:$NAME[$key]" . "f$COLOR[$key]:\"$NAME[$key]\" ";
      $def[$g] .= "GPRINT:$NAME[$key]" . "f:LAST:\"Cur\: %6.2lf $UNIT[$key] \" " ;
      $def[$g] .= "GPRINT:$NAME[$key]" . "f:AVERAGE:\"Avg\: %6.2lf $UNIT[$key] \" " ;
      $def[$g] .= "GPRINT:$NAME[$key]" . "f:MAX:\"Max\: %6.2lf $UNIT[$key] \\n\" " ;
   }
}

?>
