<?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 fans and temps into separate arrays and assign a color
# Split out loads too.
$ic = 0;
$fan= array ();
$temp= array ();
$load= array ();
foreach ($NAME as $key=>$val) {
   if (stristr($val, "temp")) {
      $temp[$key] = $val;
      $COLOR[$key] = $colors[$ic++%(count($colors))];
   } else if (stristr($val, "fan")) {
      $fan[$key] = $val;
      $COLOR[$key] = $colors[$ic++%(count($colors))];
   } else if (stristr($val, "load")) {
      $load[$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' --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\" " ;
   }
}

# Fans graph (if any temp sensors were found)
if (count($fan) > 0) {
$g++;
   $ds_name[$g] = "Fans";
   $opt[$g] = "--vertical-label 'RPM' --base=1000 -u60 --alt-autoscale-max --title \"Fan Speeds for $hostname\" ";
   
   $def[$g] = "";
   foreach ($fan as $key=>$val) {
      $def[$g] .= "DEF:$NAME[$key]=$RRDFILE[$key]:$DS[$key]:AVERAGE ";
   }
   foreach ($fan 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\" " ;
   }
}

# Loads graph (if any Load values were found)
if (count($load) > 0) {
$g++;
   $ds_name[$g] = "Loads";
   $opt[$g] = "--vertical-label 'RPM' --base=1000 --lower-limit=0 --alt-autoscale-max --title \"Loads Avgs for $hostname\" ";
   
   $def[$g] = "";
   foreach ($load as $key=>$val) {
      $def[$g] .= "DEF:$NAME[$key]=$RRDFILE[$key]:$DS[$key]:AVERAGE ";
   }
   foreach ($load 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\" " ;
   }
}

?>
