<?php
#
# Define some colors

$red        = '#FF0000';
$magenta    = '#FF00FF';
$navy       = '#000080';
$green      = '#008000';
$yellow     = '#FFFF00';
$orangered  = '#FF4500';
$darkred    = '#8B0000';
$blue       = '#0000FF';
$darkblue   = '#000099';
$darkorange = '#FF8C00';


$line[1]     = $navy;
$line[2]     = $green;
$line[3]     = $darkred;

#Set a counter to have ifferent line colors for each loop
$colour_cnt = 1;


# Replace underscores in services descriptions

$servicedesc = str_replace("_", " ", $servicedesc);

# Main logic


foreach ($DS as $i)
        {
        $opt[$i] = '--vertical-label "' . $UNIT[$i] . '" --lower-limit 0 --upper-limit "' . $MAX[$i] . '" --title "' . $hostname . ' / ' . $servicedesc . '"';

        # Replace underscores
        $ds_name[$i] = str_replace("_", " ", $LABEL[$i]);
        
        $def[$i]  = "DEF:var1=".$RRDFILE[$i].":".$DS[$i].":AVERAGE ";
        $def[$i] .= "LINE1:var1" . $line[$colour_cnt] . ":\"".$NAME[$i]."\" ";
        $def[$i] .= "GPRINT:var1:LAST:\"%3.4lf ".$UNIT[$i]." LAST \" ";
        $def[$i] .= "GPRINT:var1:MAX:\"%3.4lf ".$UNIT[$i]." MAX \" ";
        $def[$i] .= "GPRINT:var1:AVERAGE:\"%3.4lf ".$UNIT[$i]." AVERAGE \\n\" ";
        $colour_cnt++;

        }

# Due to the fact that the plugin calculates warning and critical based on 
# the deliverd percentages we have to seperate the percentage rates from
# the command :-)

# the filtering is a little bit more complex because this plugin runs directly and using nrpe
# Therefore the string to be modified is different.

$threshold = $NAGIOS_SERVICECHECKCOMMAND;
$threshold = preg_replace('/^.*?\!.*?!/','', $threshold);
$threshold = preg_replace('/^.*?\ .*? /','', $threshold);
$threshold = preg_replace('/ /','!', $threshold);

$warning_threshold = $threshold;
$warning_threshold = preg_replace('/!.*$/','', $warning_threshold);

$critical_threshold = $threshold;
$critical_threshold = preg_replace('/^.*\!/','', $critical_threshold);

$def[1] .= "HRULE:$WARN[1]#FFFF00:\"Warning \: $warning_threshold % \\n\" " ;
$def[1] .= "HRULE:$CRIT[1]#FF0000:\"Critical\: $critical_threshold % \" " ;
?>
