User Tools

Site Tools


pnp-0.4:tpl_special

Special Templates

Starting with PNP version 0.4.13 there is a new feature called “special” templates which are stored in the folder templates.special under share/pnp. These files contains PHP code, too. The difference to “normal” templates is that special templates enable you to use datasources from different RRD files. The selection is made via the function “tpl_getdata” using regular expressions for host names, service descriptions and check commands, respectively.

$_list = tpl_getdata("<Hosts>","<Services>","<Check-Commands>");

Using this code the array $_list is filled with the values of the RRD files which match the specified criteria.

You can create multiple arrays. The following examples were posted in the german Nagios forum.

In the first example two input files are used to create three graphs. The first graph contains the aggregated data of two interfaces, the other are “normal” graphs of the interfaces using the usual stuff.

Example 1

<?php

$_port_1 = tpl_getdata("localhost","if_port_27","");
$_port_2 = tpl_getdata("localhost","if_port_44","");

# $debug->doCheck('print_r',$_port_1);

$ds_name[1] = "Traffic Trunk Port";
$opt[1] = "--slope-mode --vertical-label \"Traffic\" -b 1000 --title=\" Aggregated Trunk \" ";
$def[1] = "";

$def[1] .= "DEF:in_1=".$_port_1[1]['RRDFILE'].":1:AVERAGE ";
$def[1] .= "DEF:in_2=".$_port_1[1]['RRDFILE'].":1:AVERAGE ";
$def[1] .= "CDEF:in=in_1,in_2,+ ";

$def[1] .= "DEF:out_1=".$_port_2[1]['RRDFILE'].":2:AVERAGE ";
$def[1] .= "DEF:out_2=".$_port_2[1]['RRDFILE'].":2:AVERAGE ";
$def[1] .= "CDEF:out=out_1,out_2,+ ";

$def[1] .= "LINE1:in#003300:\"in  \" " ;
$def[1] .= "GPRINT:in:AVERAGE:\"%7.2lf %Sb/s everage\\n\" " ;

$def[1] .= "LINE1:out#00ff00:\"out \" " ;
$def[1] .= "GPRINT:out:AVERAGE:\"%7.2lf %Sb/s average\\n\" " ;

$ds_name[2] = "Traffic Port 27";
$opt[2] .= " --vertical-label \"Traffic\" -b 1000 --title \"Interface Traffic\" ";
$def[2] .= "DEF:var1=".$_port_1[1]['RRDFILE'].":1:AVERAGE " ;
$def[2] .= "DEF:var2=".$_port_1[1]['RRDFILE'].":2:AVERAGE " ;
$def[2] .= "LINE1:var1#003300:\"in  \" " ;
$def[2] .= "GPRINT:var1:LAST:\"%7.2lf %Sb/s last\" " ;
$def[2] .= "GPRINT:var1:AVERAGE:\"%7.2lf %Sb/s avg\" " ;
$def[2] .= "GPRINT:var1:MAX:\"%7.2lf %Sb/s max\\n\" " ;
$def[2] .= "LINE1:var2#00ff00:\"out \" " ;
$def[2] .= "GPRINT:var2:LAST:\"%7.2lf %Sb/s last\" " ;
$def[2] .= "GPRINT:var2:AVERAGE:\"%7.2lf %Sb/s avg\" " ;
$def[2] .= "GPRINT:var2:MAX:\"%7.2lf %Sb/s max\\n\" ";

$ds_name[3] = "Traffic Port 44";
$opt[3] .= " --vertical-label \"Traffic\" -b 1000 --title \"Interface Traffic\" ";
$def[3] .= "DEF:var1=".$_port_2[1]['RRDFILE'].":1:AVERAGE " ;
$def[3] .= "DEF:var2=".$_port_2[1]['RRDFILE'].":2:AVERAGE " ;
$def[3] .= "LINE1:var1#003300:\"in  \" " ;
$def[3] .= "GPRINT:var1:LAST:\"%7.2lf %Sb/s last\" " ;
$def[3] .= "GPRINT:var1:AVERAGE:\"%7.2lf %Sb/s avg\" " ;
$def[3] .= "GPRINT:var1:MAX:\"%7.2lf %Sb/s max\\n\" " ;
$def[3] .= "LINE1:var2#00ff00:\"out \" " ;
$def[3] .= "GPRINT:var2:LAST:\"%7.2lf %Sb/s last\" " ;
$def[3] .= "GPRINT:var2:AVERAGE:\"%7.2lf %Sb/s avg\" " ;
$def[3] .= "GPRINT:var2:MAX:\"%7.2lf %Sb/s max\\n\" ";

# $debug->doCheck('var_dump',$def);
?>


The second (modified) example shows how to display the cpu usage of several servers in a single graph.

Example 2

<?php
# create array using regular expressions
# define <hostname>,<service description>,<check_command>
$_list = tpl_getdata( "esx" ,"CPU","");

# activate the following line to check the contents of the array
# $debug->doCheck('print_r',$_list);

# initialize $opt and $def
$opt[1] = "--title=\"CPU usage\" ";
$def[1] = "";

# set some colours
$_colours = array("#80ff80","#00ffff","#008080","#0000ff","#ff8000","#ff0000","#800000","#000000");
# define the datasource to be used
$_ds = 1;

# loop
foreach($_list as $key=>$value){
   $def[1] .= "DEF:var$key=" . $_list[$key]['RRDFILE'] . ":" . $_ds . ":AVERAGE ";
   $def[1] .= "LINE1:var" . $key . $_colours[$key%8] . ":\"" . sprintf("%-20s",$_list[$key]['HOSTNAME']) . "\" ";
   $def[1] .= "GPRINT:var" . $key . ":LAST:\"%10.2lf %S last\" "  ;
   $def[1] .= "GPRINT:var" . $key . ":AVERAGE:\"%10.2lf %S avg\" " ;
   $def[1] .= "GPRINT:var" . $key . ":MAX:\"%10.2lf %S max\\n\" " ;
}
?>

If you have more than eight servers the colours are reused but viewing eight lines might be confusing already.

pnp-0.4/tpl_special.txt · Last modified: 2022/01/21 14:37 by 127.0.0.1