@ 2015-06-26T04:30:25Z <?php
/*****************************************************************************
*
* std_bar.php - Sample gadget for NagVis
*
* Copyright (c) 2004-2015 NagVis Project (Contact: info@nagvis.org)
*
* License:
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*****************************************************************************
*
* This is a simple gadget for NagVis. A gadget is a script which generates a
* dynamic image for visualizing things as graphs in NagVis.
* This one was inspired by "fs_usage_bar" (Copyright Richard Leitner)
* http://exchange.nagvis.org/exchange/Gadgets/Filesystem-Usage-Bar/
*
* The gadget gets its data from the NagVis frontend by parameters. This
* gadget only needs the "perfdata" parameter
* The values are accessible using the array aPerfdata. The structure is
* shown in gadgets_core.php.
* The behaviour of the gadget can be influenced by two directives in the
* service definition of the map config file:
* gadget_scale=n (n being the size of the graph(s) in percent, default = 100)
* gadget_opts=option=value (multiple options are separated by spaces)
* option is one of the following:
* columns=n
* number of columns of graphs (if applicable), default is 3
* string=s
* s is a string the perfdata label has to contain
* current=<0|1>
* 1 = show the current value along with the label (default)
* label=<0|1>
* 1 = show host name/performance label in the last line of the graph
* (1 is default)
* NagVis also passes the following parameters to the gadget using the array
* $aOpts:
* - name1: Hostname
* - name2: Service description
* - state: Current state
* - stateType: Current state type (soft/hard)
* This value is ignored as the performance data might contain
* several values
* - scale: Scale of the gadget in percent
* This value is ignored in favour of gadget_scale
*
*****************************************************************************/
/**
* Dummy perfdata for WUI
*
* This string needs to be set in every gadget to have some sample data in the
* WUI to be able to place the gadget easily on the map
* It has to be set BEFORE including gadgets_core.php
******************************************************************************/
$sDummyPerfdata = 'config=20%;80;90;0;100';
/**
* Needs to be configured to tell gadgets_core.php how to handle the outputs
* e.g. in case of error messages. This defaults to 'img'.
*/
$_MODE = 'html';
// Include the gadgets core. Also handle OMD default and local paths
if(substr($_SERVER["SCRIPT_FILENAME"], 0, 4) == '/omd') {
$core = dirname($_SERVER["SCRIPT_FILENAME"]) . '/gadgets_core.php';
if(file_exists($core))
require($core);
else
require(str_replace('local/share/', 'share/', $core));
} else {
require('./gadgets_core.php');
}
/*******************************************************************************
* Start gadget main code
******************************************************************************/
header("Content-type: image/png");
//=====================
// Set image parameters
//=====================
$ratio = $aOpts['scale'] / 100;
$fontDir = '/usr/share/fonts/truetype'; // openSuSE, SLES
// $fontDir = '/usr/share/fonts/truetype/ttf-dejavu'; // Debian, Ubuntu
// $fontDir = '/usr/share/fonts/dejavu-lgc'; // CentOS
// $fontDir = '/usr/share/fonts/dejavu'; // Fedora
$fontName = 'DejaVuSans-Bold.ttf';
$imgwidth = 400 * $ratio;
$imgheight = 50 * $ratio;
/**
* Don't change anything below (unless you know what you do)
*/
$font = "$fontDir/$fontName";
//==========================================
// Set Minimum, Default, and Maximum values.
//==========================================
$min = 0;
$max = -1;
$default = 0;
$pdc = count($aPerfdata); // performance data count
$string = ''; // string in perfdata label
$current = 1; // show current value
$label = 1; // show host/service label
$cols = 3; // no. of columns with graphs
$threshold = 'pct'; // threshold values in percent
$sect1 = intval($imgheight / 5);
$sect2 = intval($imgheight / 2);
$sect3 = intval($imgheight / 5)*3;
$chrSize = $ratio * 5;
if ($chrSize < 1) { $chrSize = 1; }
//==================
// scan gadget_opts
//==================
if (isset($aOpts['opts']) && ($aOpts['opts'] != '')){
preg_match_all ('/(\w+)=(\w+)/',$aOpts['opts'],$matches,PREG_SET_ORDER);
for ($i = 0; $i < count($matches); $i++){
if ($matches[$i][1] == 'columns') { $cols = $matches[$i][2]; }
if ($matches[$i][1] == 'string') { $string = $matches[$i][2]; }
if ($matches[$i][1] == 'current') { $current = $matches[$i][2]; }
if ($matches[$i][1] == 'label') { $label = $matches[$i][2]; }
if ($matches[$i][1] == 'threshold') { $threshold = $matches[$i][2]; }
}
}
$rows = ceil($pdc / $cols); // max. no. of rows with graphs
//====================
// Create image
//====================
$img=imagecreatetruecolor($imgwidth*$cols, $imgheight*$rows);
$oBackground = imagecolorallocate($img, 122, 23, 211);
$oBlack = imagecolorallocate($img, 0, 0, 0);
$oGreen = imagecolorallocate($img, 0, 255, 0);
$oYellow = imagecolorallocate($img, 255, 255, 0);
$oYellowAck = imagecolorallocate($img, 200, 255, 0);
$oRed = imagecolorallocate($img, 255, 0, 0);
$oRedAck = imagecolorallocate($img, 200, 0, 0);
$oBlue = imagecolorallocate($img, 0, 0, 255);
imagefill($img, 0, 0, $oBackground);
imagecolortransparent($img, $oBackground);
$offG = 0; // current graph
for ($i=0; $i < $pdc; $i++){
$desc = preg_replace('(.*::)','',$aPerfdata[$i]['label']); // omit
check_multi description
if (preg_match("/$string/",$desc)) {
$colour = '';
$value = $aPerfdata[$i]['value'];
$warn = $aPerfdata[$i]['warning'];
$warn = preg_replace ('(:.*)','',$warn); // ignore range settings
$crit = $aPerfdata[$i]['critical'];
$crit = preg_replace ('(:.*)','',$crit); // ignore range settings
$min = $aPerfdata[$i]['min'];
$max = $aPerfdata[$i]['max'];
$uom = $aPerfdata[$i]['uom'];
$ack = $aPerfdata[$i]['ack'];
$downtime = $aPerfdata[$i]['downtime'];
$offX = ($offG % $cols) * $imgwidth; // calculate left x-axis
position
$offY = floor($offG / $cols) * $imgheight; // calculate upper y-axis
position
$maxX = $imgwidth-15;
$maxY = $imgheight-5;
// determine the upper limit
$limit = $max;
if ($limit < $crit) {
$limit = $crit;
}
if (($warn > $crit) && ($limit < $warn)) {
$limit = $warn;
}
if ($value > $limit) {
$limit = $value;
}
if ($limit < 1) {
$limit = 1;
}
if ($uom == '%') {
$limit = 100;
}
if (isset($warn) && isset($crit)) {
if ($warn < $crit) {
if ($value >= $warn) { $colour = ($ack) ? $oYellowAck : $oYellow; };
if ($value >= $crit) { $colour = ($ack) ? $oRedAck : $oRed; };
} else {
if ($value <= $warn) { $colour = ($ack) ? $oYellowAck : $oYellow; };
if ($value <= $crit) { $colour = ($ack) ? $oRedAck : $oRed; };
}
}
// create box
imagefilledrectangle ($img, $offX, $offY+1, $offX+$maxX, $offY+$maxY,
$oGreen);
imageRectangle($img, $offX, $offY, $offX+$maxX, $offY+$maxY, $oBlack);
$maxX--;
$maxY--;
// "highlight" graph if non-ok value
if ($colour != '') {
imagefilledrectangle ($img, $offX+1, $offY+$sect3+1, $offX+$maxX,
$offY+$maxY, $colour);
}
//================
// Normalize / Fix value and max
//================
if($value == null) {
$value = $default;
} else {
if($max != '' && $value < $min) {
$value = $min;
} elseif($max != '' && $max != -1 && $value > $max) {
$value = $max;
}
}
// If there is no max value given set it using the critical value
if(intval($max) == 0 || $max == '') {
$max = $crit + 1;
}
//================
// Calculate value, warn, critical percentages/values
//================
$p = 100 / $limit * $value;
$warnp = round(100 / $limit * $warn,0);
$critp = round(100 / $limit * $crit,0);
$valuev = ($maxX / 100 * $p);
$warnv = intval($maxX / 100 * $warnp);
$critv = intval($maxX / 100 * $critp);
$warnt = ($threshold == 'pct') ? $warnp : $warn;
$critt = ($threshold == 'pct') ? $critp : $crit;
//===================
// create warning/critical areas, current value
//===================
// Warning
if($warn) {
if ($warn < $crit) {
imageFilledRectangle($img, $offX+$warnv+1, $offY+1, $offX+$maxX,
$offY+$sect1, $oYellow);
} else {
imageFilledRectangle($img, $offX+1, $offY+1, $offX+$warnv-1, $offY+
$sect1, $oYellow);
}
if (file_exists ("$font")) {
ImageTTFText($img, $chrSize*2, 0, $offX+$warnv+1, $offY+$sect1,
$oBlack, $font, intval($warnt));
} else {
imagestring($img, $chrSize, $offX+$warnv+1, $offY-2, intval($warnt),
$oBlack);
}
}
// Critical
if($crit) {
if ($warn < $crit) {
imageFilledRectangle($img, $offX+$critv+1, $offY+1, $offX+$maxX,
$offY+$sect1, $oRed);
} else {
imageFilledRectangle($img, $offX+1, $offY+1, $offX+$critv-1, $offY+
$sect1, $oRed);
}
if (file_exists ("$font")) {
ImageTTFText($img, $chrSize*2, 0, $offX+$critv+1, $offY+$sect1,
$oBlack, $font, intval($critt));
} else {
imagestring($img, $chrSize, $offX+$critv+1, $offY-2, intval($critt),
$oBlack);
}
}
imagefilledRectangle($img, $offX+1, $offY+$sect1+1, $offX+$valuev+1,
$offY+$sect3, $oBlue);
//===================
// Labels
//===================
if ($current == 1) {
$maxv = "";
if (isset($aPerfdata[$i]['max'])) { $maxv = " of ".$aPerfdata[$i]
['max']; }
if ($down) { $maxv = " [down]"; }
if (file_exists ("$font")) {
ImageTTFText($img, $chrSize*3.5, 0, $offX+5, $offY+$sect3-1,
$oBlack, $font, $desc . ':' . $value . $uom . $maxv);
} else {
imagestring($img, $chrSize, $offX+3, $offY+$sect1+2, $desc.': '.
$value . $uom . $maxv, $oBlack);
}
if ($label == 1) {
$hostname = (strlen($aOpts['name1']) > 15) ? substr($aOpts
['name1'],0,14)."..." : $aOpts['name1'];
$svcdesc = (strlen($aOpts['name2']) > 15) ? substr($aOpts
['name2'],0,14)."..." : $aOpts['name2'];
if (strlen($desc) > 15) {
$desc = substr($desc,0,14)."...";
}
if (file_exists ("$font")) {
ImageTTFText($img, $chrSize*2.5, 0, $offX+3, $offY+$maxY-1, $oBlack,
$font, $hostname);
ImageTTFText($img, $chrSize*2.5, 0, $offX+$imgwidth/2, $offY+$maxY-
1, $oBlack, $font, $svcdesc);
// ImageTTFText($img, $chrSize*2.5, 0, $offX+$imgwidth/2, $offY+$maxY-
1, $oBlack, $font, $desc);
} else {
imagestring($img, $chrSize, $offX+3, $offY+$sect3, $hostname,
$oBlack);
imagestring($img, $chrSize, $offX+$imgwidth/2, $offY+$sect3,
$svcdesc, $oBlack);
// imagestring($img, $chrSize, $offX+$imgwidth/2, $offY+$sect3, $desc,
$oBlack); // perf label
}
}
}
$offG++;
}
}
//==============
// Output image.
//==============
if(function_exists('imageantialias')) {
imageantialias($img, true);
}
imagepng($img);
imagedestroy($img);
?>
define service {
host_name=uxze01
service_description=root-volume
x=50
y=50
view_type=gadget
gadget_url=estd_bar.php
gadget_scale=100
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 5.6.10 0.010 0.080 21.00 5.6.9 0.020 0.073 20.98 5.6.8 0.010 0.067 20.46 5.6.7 0.430 0.037 20.37 5.5.26 0.007 0.047 20.89 5.5.25 0.020 0.060 20.63 5.5.24 0.030 0.063 20.25 5.4.42 0.010 0.057 19.54 5.4.41 0.017 0.057 19.31 5.4.40 0.010 0.053 19.16 5.4.39 0.007 0.057 19.12 5.4.38 0.020 0.047 19.09 5.4.37 0.017 0.043 19.25 5.4.36 0.007 0.053 19.04 5.4.35 0.010 0.053 19.12 5.4.34 0.007 0.057 19.16 5.4.32 0.003 0.070 19.11 5.4.31 0.003 0.063 19.08 5.4.30 0.010 0.053 19.13 5.4.29 0.013 0.050 19.13 5.4.28 0.003 0.060 19.08 5.4.27 0.010 0.053 19.25 5.4.26 0.007 0.057 19.15 5.4.25 0.013 0.053 19.01 5.4.24 0.007 0.057 19.25 5.4.23 0.013 0.047 19.05 5.4.22 0.013 0.050 19.19 5.4.21 0.003 0.057 19.06 5.4.20 0.007 0.050 17.05 5.4.19 0.013 0.047 19.02 5.4.18 0.013 0.050 19.24 5.4.17 0.013 0.050 19.00 5.4.16 0.010 0.050 19.00 5.4.15 0.017 0.043 19.06 5.4.14 0.023 0.053 16.68 5.4.13 0.017 0.040 16.47 5.4.12 0.010 0.047 16.49 5.4.11 0.000 0.057 16.27 5.4.10 0.010 0.047 16.30 5.4.9 0.010 0.050 16.33 5.4.8 0.010 0.050 16.57 5.4.7 0.013 0.047 16.46 5.4.6 0.010 0.053 16.53 5.4.5 0.010 0.047 16.45 5.4.4 0.007 0.053 16.45 5.4.3 0.010 0.047 16.55 5.4.2 0.007 0.053 16.39 5.4.1 0.013 0.040 16.29 5.4.0 0.003 0.053 16.11 5.3.29 0.013 0.047 14.64 5.3.28 0.013 0.047 14.46 5.3.27 0.023 0.053 14.64 5.3.26 0.007 0.057 14.62 5.3.25 0.020 0.040 14.57 5.3.24 0.003 0.057 14.49 5.3.23 0.010 0.050 14.64 5.3.22 0.013 0.047 14.43 5.3.21 0.007 0.057 14.58 5.3.20 0.007 0.053 14.49 5.3.19 0.010 0.050 14.50 5.3.18 0.013 0.047 14.45 5.3.17 0.003 0.057 14.60 5.3.16 0.007 0.050 14.49 5.3.15 0.007 0.053 14.63 5.3.14 0.010 0.057 14.45 5.3.13 0.000 0.063 14.61 5.3.12 0.007 0.053 14.48 5.3.11 0.013 0.047 14.41 5.3.10 0.000 0.057 13.96 5.3.9 0.023 0.037 13.84 5.3.8 0.003 0.053 13.93 5.3.7 0.007 0.053 13.96 5.3.6 0.007 0.050 13.88 5.3.5 0.013 0.043 13.93 5.3.4 0.017 0.040 14.01 5.3.3 0.010 0.047 13.95 5.3.2 0.010 0.047 13.61 5.3.1 0.010 0.047 13.70 5.3.0 0.003 0.060 13.45 5.2.17 0.010 0.037 11.10 5.2.16 0.007 0.040 11.08 5.2.15 0.010 0.037 11.13 5.2.14 0.007 0.040 11.19 5.2.13 0.003 0.043 11.14 5.2.12 0.013 0.033 11.00 5.2.11 0.007 0.040 11.04 5.2.10 0.000 0.047 11.07 5.2.9 0.010 0.040 11.14 5.2.8 0.013 0.037 11.23 5.2.7 0.003 0.043 11.14 5.2.6 0.003 0.050 10.98 5.2.5 0.007 0.043 11.00 5.2.4 0.010 0.037 10.88 5.2.3 0.007 0.043 11.11 5.2.2 0.007 0.040 10.79 5.2.1 0.007 0.040 10.84 5.2.0 0.003 0.043 10.87 5.1.6 0.007 0.050 10.02 5.1.5 0.003 0.037 10.02 5.1.4 0.003 0.037 9.93 5.1.3 0.003 0.043 10.34 5.1.2 0.010 0.033 10.45 5.1.1 0.010 0.030 9.99 5.1.0 0.003 0.037 10.04 5.0.5 0.000 0.033 8.52 5.0.4 0.003 0.040 8.55 5.0.3 0.007 0.040 8.27 5.0.2 0.000 0.033 8.24 5.0.1 0.013 0.020 8.26 5.0.0 0.013 0.033 8.14 4.4.9 0.003 0.023 5.86 4.4.8 0.010 0.023 5.94 4.4.7 0.010 0.017 5.81 4.4.6 0.007 0.020 5.91 4.4.5 0.003 0.023 5.83 4.4.4 0.007 0.033 5.91 4.4.3 0.003 0.020 5.84 4.4.2 0.007 0.020 5.90 4.4.1 0.007 0.017 5.85 4.4.0 0.003 0.033 5.86 4.3.11 0.000 0.023 5.73 4.3.10 0.013 0.017 5.75 4.3.9 0.000 0.027 5.71 4.3.8 0.007 0.030 5.70 4.3.7 0.003 0.020 5.85 4.3.6 0.007 0.020 5.72 4.3.5 0.003 0.023 5.70 4.3.4 0.000 0.037 5.80 4.3.3 0.003 0.020 4.50 4.3.2 0.000 0.027 4.63 4.3.1 0.000 0.023 4.54 4.3.0 0.017 0.017 7.05
preferences:dark mode live preview
141.6 ms | 1394 KiB | 7 Q