The article is summarized from the instructions on the official website: http://docs.pnp4nagios.org/pnp-0.6/tpl
In daily use, it is unavoidable to find that the default drawing does not meet our needs, so you can customize the template to achieve the desired drawing effect according to your needs.
1. Official description of custom templates1. Templates are stored in two locations
share/template.dist stores templates in PNP packages
share/template custom template storage location
If a graph of the HTTP service is displayed on the localhost server, PNP will use the var/perfdata/localhost/http.xml in the pnp4nagios installation directory (this file is automatically generated from the service_description (monitoring description) parameter value name used to define service monitoring), and the <TEMPLATE>tag definition section at the beginning of the file will determine which template to use to render the data.
PNP will automatically use the <TEMPLATE>template name+php suffix as the template file name, for example:
The corresponding data xml for the http service on the localhost server is:
cd /usr/local/pnp4nagios/var/perfdata cat localhost/http.xml <NAGIOS > <DATASOURCE > <TEMPLATE > check_http </ TEMPLATE > <DS > 1 </ DS > ... </ NAGIOS >
Here PNP will look for the check_http.php template to render the data.
The order in which PNP looks for templates is as follows (under share in the pnp4nagios installation directory):
-
- templates/check_http.php
-
- templates.dist/check_http.php
-
- templates/default.php
-
- templates.dist/default.php
The default.php template takes a special place because it is used every time no other applicable template is found.
- templates.dist/default.php
2. Create your own template
PNP templates are PHP files that are included when PNP is executed using the PHP function include().This means that each PHP code in the template will be interpreted so that all values can be manipulated.
Templates must follow the following principles:
- 1. Must be a valid php statement
- 2. Templates cannot produce any output
- 3. Two arrays must be populated: $opt[] and $def[]; they are used to call'rrdtool graph', so every option supported by RRDtool is possible.
Here we use a relatively simple response.php template to describe the most important options:
<?php # $opt[1] = "--title \"Response Time For $hostname / $servicedesc\" "; # $def[1] = "DEF:var1=$RRDFILE[1]:$DS[1]:AVERAGE " ; $def[1] .= "AREA:var1#00FF00:\"Response Times \" " ; $def[1] .= "LINE1:var1#000000 " ; $def[1] .= "GPRINT:var1:LAST:\"%3.4lg %s$UNIT[1] LAST \" "; $def[1] .= "GPRINT:var1:MAX:\"%3.4lg %s$UNIT[1] MAX \" "; $def[1] .= "GPRINT:var1:AVERAGE:\"%3.4lg %s$UNIT[1] AVERAGE \" "; ?>
Note: Because numbers 1 and the lowercase letter'l'look similar in this php file: the format'%3.4lg' contains a lowercase letter L.
Option description:
$opt[1] ='--title..."==>Set the RRDtool option for the first set of data, here is the title you see on the web map.Embedded quotation marks are masked with backslashes ().Variables $hostname and $servicedesc are determined through PNP calls and can also be used for templates.
$def[1] = "DEF:var1=$RRDFILE[1]: $DS[1]: AVERAGE"; ==>Defines which data to read from which RRD file.$RRDFILE[1] contains the path to the RRD file for this service.$DS[1] references the first data series in the RRD file.
$def[1]. = "AREA:var1#00FF00:\"Response Times \";=>The operator'. ='appends more data to the array $def[1].The data from the independent variable will be used to draw the region var1.Colors are defined in hexadecimal notation #00FF00 (red, green, blue).Label is Response Time.
$def[1]. = "LINE1:var1#000000"; ==>When the area just drawn is finished, a line (LINE1) will be drawn in black (#000000).
$def[1] .= "GPRINT:var1:LAST:\"%3.4lg %s$UNIT[1] LAST \" ";
$def[1] .= "GPRINT:var1:MAX:\"%3.4lg %s$UNIT[1] MAX \" ";
$def[1] .= "GPRINT:var1:AVERAGE:\"%3.4lg %s$UNIT[1] AVERAGE \" ";
Three lines of GPRINT lines make up the legend.The current value is formatted using printf syntax.
2. Custom Template UseThe pnp4nagios service has been configured by default before setup and can be mapped normally.
Here, we use the default localhost's memory monitor to demonstrate this.
The drawing results of the pre-set memory monitor are as follows:
Next we'll transform it!!
~
1. Find the Data xml file name for the memory monitoring item parameter
Look at the localhost.cfg configuration file in nagios to find the target service configuration information, which is defined as follows:
less nagios/etc/objects/localhost.cfg define service{ use local-service,srv-pnp ; Name of service template to use host_name localhost service_description Mem Usage check_command check_local_mem!80!90 }
Where service_description is configured as Mem Usage, the Data XML file for the memory service should be: Mem_Usage.xml (spaces are replaced with underscores)
2. Find the drawing template name to use for memory monitoring items
View the check_command.xml file in pnp4nagios
Note: pnp4nagios is installed here under / usr/local
cd /usr/local/pnp4nagios/var/perfdata/localhost/ head -n 10 Mem_Usage.xml <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <NAGIOS> <DATASOURCE> <TEMPLATE>check_local_mem</TEMPLATE> <RRDFILE>/usr/local/pnp4nagios/var/perfdata/localhost/Mem_Usage_Used.rrd</RRDFILE> <RRD_STORAGE_TYPE>MULTIPLE</RRD_STORAGE_TYPE> <RRD_HEARTBEAT>8460</RRD_HEARTBEAT> <IS_MULTI>0</IS_MULTI> <DS>1</DS> <NAME>Used</NAME>
Where the value in the <TEMPLATE>tag is check_local_mem, the drawing PHP settings file used by the memory monitoring item is check_local_mem.php
3. Find and modify templates
cd /usr/local/pnp4nagios/share/templates ls -l total 8 -rw-r--r--. 1 nagios nagios 1091 Jul 8 2018 check_local_mem.php -rw-r--r--. 1 nagios nagios 672 Jul 8 2018 Nouse_check_mem.php
If you find that the check_local_mem.php template already exists, modify it directly; if it does not, create a new one.
vim check_local_mem.php <?php # # Copyright (c) 2006-2010 Joerg Linge (http://www.pnp4nagios.org) # Plugin: check_load # $opt[1] = "--vertical-label Memory -l0 --title \"Memory for $hostname / $servicedesc\" "; # # # $def[1] = rrd::def("var1", $RRDFILE[1], $DS[1], "AVERAGE"); $def[1] .= rrd::def("var2", $RRDFILE[2], $DS[2], "AVERAGE"); $def[1] .= rrd::def("var3", $RRDFILE[3], $DS[3], "AVERAGE"); $def[1] .= rrd::def("var4", $RRDFILE[4], $DS[4], "AVERAGE"); if ($WARN[1] != "") { $def[1] .= "HRULE:$WARN[1]#FFFF00 "; } if ($CRIT[1] != "") { $def[1] .= "HRULE:$CRIT[1]#FF0000 "; } $def[1] .= rrd::line2("var2", "#006400", "Free ") ; $def[1] .= rrd::gprint("var2", array("LAST", "AVERAGE", "MAX"), "%6.2lf"); $def[1] .= rrd::area("var4", "#00FF00", "Buffers") ; $def[1] .= rrd::gprint("var4", array("LAST", "AVERAGE", "MAX"), "%6.2lf"); $def[1] .= rrd::area("var3", "#EA8F00", "Cached : ") ; $def[1] .= rrd::gprint("var3", array("LAST", "AVERAGE", "MAX"), "%6.2lf"); $def[1] .= rrd::line1("var1", "#EACC00", "Used ") ; $def[1] .= rrd::gprint("var1", array("LAST", "AVERAGE", "MAX"), "%6.2lf"); ?>
Save after editing to see the results of the new style without restarting the nagios service.
Note: Make sure the template owner is nagios
The modified drawing has the following effect:
Does it feel refreshing ~~
Complete!