<?php
#
# This template generates the PNP graphical representation of data in the script "check_apache-auto.pl" for Nagios.
# It allows a thorough and flexible configuration of texts and units.
# It contains why the debug lines that simply uncomment.
# Since the plugin for Nagios, it was directed by Robert Becht (robert.becht @ habited.net)
# Use a little tedious (even useless) key in the tables promotes greater adaptability of the template.


#
# the known variables

# Debugging
# echo "<pre>";print_r($this->DS);echo "</pre>";die();

$SCRVAL = array();
$valk = '';

# The file name
# $DSFILE = "$this->DS";

# Title frameworks based on its number $ i
$DSNAME = array(1 => "Users (Busy ans Idle Workers)",
		2 => "Uptime",
		3 => "Traffic per sekunds and per request",
		4 => "Request per sekunds",
		5 => "CPU Load",
		6 => "Total traffic",
		7 => "Total accesses" );

# Under axis of ordinates based on the number of frame $ i
$VLABEL = array(	1 => "Connections",
			2 => "Hours",
			3 => "KiB",
			4 => "Requests",
			5 => "Percent(%)",
			6 => "MB",
			7 => "Connections" );

# Number $ k graphs to place in the respective frames i $ (this helps to deal
# with DS non-consecutive or choose which plot!).
# The order is important: areas or lines traced by following the previous crash or hide!

$DSNBR = array(	1 => array(6,9),
		2 => array(1),
		3 => array(2,4),
		4 => array(5),
		5 => array(8),
		6 => array(7),
		7 => array(3) );

# (1)Label graphs based on numbers $ k
#	if you want to rename (or translate) uncomment if necessary in this case and see below)
# $DLABEL = array(0 => "used",
#		1 => "free",
#		2 => "avaible",
#		3 => "ThreadsBusy",
#		4 => "MaxThreads");

# Number format defined by frame, depending on its number $ i
$FMT = array(	1 => "%5.0lf",
		2 => "%5.2lf",
		3 => "%5.3lf",
		4 => "%3.3lf",
		5 => "%2.1lf%%",
		6 => "%5.3lf",
		7 => "%5.0lf" );

# Automatic adjustment of the numbers and units (Simply say no or yes)
$ADJUNIT = array( 1 => 'no',
		  2 => 'no',
		  3 => 'no',
		  4 => 'no',
		  5 => 'no',
		  6 => 'yes',
		  7 => 'no' );


# Units of data collected (if not specified in the DS)
$VALUNIT = array( 1 => "nb",
		  2 => "",
		  3 => "",
		  4 => "",
		  5 => "",
		  6 => "",
		  7 => "nb" );

# Defining Alerts harvested in units per frame $ i (if not specified in the DS): $ i => array (warn, crit).
# Alerts defined here serve only as visual cues (no treatment alerts possible)!
$ALERT = array(	1 => array('W' => 50, 'C' => 70 ),
 		2 => array('W' => 25, 'C' => 30),
		3 => array('W' => 1500, 'C' => 2500),
		4 => array('W' => 15, 'C' => 20),
		5 => array('W' => 20, 'C' => 25),
		6 => array('W' => 1000, 'C' => 2000),
		7 => array('W' => 5000, 'C' => 20000) );
		

# Color assignments for each graph based on the number of DS
$COLORS = array(  0 => '#ffc0ff',
		  1 => '#02ffa2',
		  2 => '#ffbc01',
		  3 => '#02FFA2',
		  4 => '#00FFFF',
		  5 => '#FFD2FF',
		  6 => '#02FFA2',
		  7 => '#FFD2FF',
		  8 => '#FF9797',
		  9 => '#00FFFF');
	    
	  

# Loop creation of frames numbered $ i
for ($i=1;$i<=count($DSNBR);$i++){

# Debugging
# echo "<pre>";print("i=$i"." size=".count($DSNBR));echo "</pre>";die();

$ds_name[$i] = $DSNAME[$i];
$opt[$i] = "--alt-y-grid -l 0 --vertical-label \"$VLABEL[$i]\"  --title \"$ds_name[$i]\" ";

# Choosing a color palette defined (or defined) in /pnp/application/helpers/rrd.php
# (if not set by default ... = () ; )
$def[$i] = rrd::customteint();

    #Loop framework for creating graphs
    foreach ($DSNBR[$i] as $KEY=>$k){

# Debugging
# echo "<pre>";print_r($DSNBR[$i]);print("KEY=$KEY"." k=$k");echo "</pre>";die();

     $DSARRAY = ($this->DS[$k-1]);

# Debugging
# echo "<pre>";print_r($DSARRAY);echo "</pre>";die();


# Warning and critical definitions
#    if ($ADJUNIT[$i] == 'yes'){
	if ($k == $DSNBR[$i][0] && $DSARRAY['WARN'] != "" ) {
	    $warn = pnp::adjust_unit($DSARRAY['WARN'],1000,$FMT[$i] );
	    $warnlevel = $DSARRAY['WARN'];
	 } else {
	    $warnlevel = $ALERT[$i]['W'];
#	    $warn = pnp::adjust_unit($ALERT[$i]['W'],1000,$FMT[$i] );
	    $warn[1] = $ALERT[$i]['W'];
	    }
#	} elseif ($ADJUNIT[$i] == 'no'){
#		$warn[1] = $DSARRAY['WARN'];
#		$warnlevel = $DSARRAY['WARN'];
#		}

# Debugging
# echo "<pre>";print_r($warn);print $warnlevel;echo "</pre>";die();	 

	if ($k == $DSNBR[$i][0] && $DSARRAY['CRIT'] != "") {
	$crit = pnp::adjust_unit($DSARRAY['CRIT'],1000,$FMT[$i] );
	$critlevel = $DSARRAY['CRIT'];
	}
	else {
	$critlevel = $ALERT[$i]['C'];
#	$crit = pnp::adjust_unit($ALERT[$i]['C'],1000,$FMT[$i] );
	$crit[1] = $ALERT[$i]['C'];
	}

# Units definitions
	if ($k == $DSNBR[$i][0] && $DSARRAY['UNIT'] != "") {
	    $unit = $DSARRAY['UNIT'];
	    } else {
	    $unit = $VALUNIT[$i];
	    }
 	if ($unit == "%%") {
 	    $unit = ''; # Unit % is defined in $FMT array
 	    }

# Debugging
# echo "<pre>";print_r($DSARRAY);echo "</pre>";die();
	
 	$def[$i] .= rrd::def( "vara$k", $DSARRAY['RRDFILE'], $DSARRAY['DS'], "MAX" );

# Debugging
# echo "<pre>";echo str_replace(" ", "\r\n--",$def[1]);echo "</pre>";die();
    if ($ADJUNIT[$i] == 'yes'){ 
	$val = pnp::adjust_unit($DSARRAY['ACT'],1024,$FMT[$i]);
	} else {
	$val = array($DSARRAY['ACT'], $DSARRAY['ACT'],"",1 );
	}
	$divis = $val[3];
	$prefix = $val[2];
	$value = $val[1];

# Debugging
# echo "<pre>";print_r($val);echo "</pre>";die();

	$def[$i] .= rrd::cdef("varb$k","vara$k,$divis,/");	
	$def[$i] .= rrd::line2("vara$k", "$COLORS[$k]", $DSARRAY['LABEL'] );
	#(1) Remplacer la ligne précédente par celle-ci si vous souhaiter renommer les étiquettes.
	# $def[$i] .= rrd::line2("vara$k", "$COLORS[$k]", $DLABEL[$k] );
	$def[$i] .= rrd::gprint("varb$k", "LAST", "$FMT[$i] $prefix$unit last");
	$def[$i] .= rrd::gprint("varb$k", "AVERAGE", "$FMT[$i] $prefix$unit avg");
	$def[$i] .= rrd::gprint("varb$k", "MAX", "$FMT[$i] $prefix$unit max \\n");



}

# Debugging
# echo "<pre>";echo str_replace(" ", "\r\n--",$def[1]);echo "</pre>";die();

$def[$i] .= rrd::hrule( $warnlevel, "#FFA858", "Warning=$warn[1] $prefix$unit");
$def[$i] .= rrd::hrule( $critlevel, "#FF0000", "Critical=$crit[1] $prefix$unit\\n");

$def[$i] .= rrd::comment("PNP-Template\:" . $template . ".php \\r");


}


?>
