1<?php 2 3/***************************************************************************** 4 * 5 * std_bar.php - Sample gadget for NagVis 6 * 7 * Copyright (c) 2004-2016 NagVis Project (Contact: info@nagvis.org) 8 * 9 * License: 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License version 2 as 13 * published by the Free Software Foundation. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 23 * 24 ***************************************************************************** 25 * 26 * This is a simple gadget for NagVis. A gadget is a script which generates a 27 * dynamic image for visualizing things as graphs in NagVis. 28 * This one was inspired by "fs_usage_bar" (Copyright Richard Leitner) 29 * http://exchange.nagvis.org/exchange/Gadgets/Filesystem-Usage-Bar/ 30 * 31 * The gadget gets its data from the NagVis frontend by parameters. This 32 * gadget only needs the "perfdata" parameter 33 * The values are accessible using the array aPerfdata. The structure is 34 * shown in gadgets_core.php. 35 36 * The behaviour of the gadget can be influenced by two directives in the 37 * service definition of the map config file: 38 * gadget_scale=n (n being the size of the graph(s) in percent, default = 100) 39 * gadget_opts=option=value (multiple options are separated by spaces) 40 * option is one of the following: 41 * columns=n 42 * number of columns of graphs (if applicable), default is 3 43 * string=s 44 * s is a string the perfdata label has to contain 45 * current=<0|1> 46 * 1 = show the current value along with the label (default) 47 * label=<0|1> 48 * 1 = show host name/performance label in the last line of the graph 49 * (1 is default) 50 51 * NagVis also passes the following parameters to the gadget using the array 52 * $aOpts: 53 * - name1: Hostname 54 * - name2: Service description 55 * - state: Current state 56 * - stateType: Current state type (soft/hard) 57 * This value is ignored as the performance data might contain 58 * several values 59 * - scale: Scale of the gadget in percent 60 * This value is ignored in favour of gadget_scale 61 * 62 *****************************************************************************/ 63 64/** 65 * Dummy perfdata for WUI 66 * 67 * This string needs to be set in every gadget to have some sample data in the 68 * WUI to be able to place the gadget easily on the map 69 * It has to be set BEFORE including gadgets_core.php 70 ******************************************************************************/ 71$sDummyPerfdata = 'config=20%;80;90;0;100'; 72 73/** 74 * Needs to be configured to tell gadgets_core.php how to handle the outputs 75 * e.g. in case of error messages. This defaults to 'img'. 76 */ 77$_MODE = 'html'; 78 79// Include the gadgets core. Also handle OMD default and local paths 80if(substr($_SERVER["SCRIPT_FILENAME"], 0, 4) == '/omd') { 81 $core = dirname($_SERVER["SCRIPT_FILENAME"]) . '/gadgets_core.php'; 82 if(file_exists($core)) 83 require($core); 84 else 85 require(str_replace('local/share/', 'share/', $core)); 86} else { 87 require('./gadgets_core.php'); 88} 89 90/******************************************************************************* 91 * Start gadget main code 92 ******************************************************************************/ 93 94header("Content-type: image/png"); 95 96//===================== 97// Set image parameters 98//===================== 99 100$ratio = $aOpts['scale'] / 100; 101$fontDir = '/usr/share/fonts/truetype'; // openSuSE, SLES 102// $fontDir = '/usr/share/fonts/truetype/ttf-dejavu'; // Debian, Ubuntu 103// $fontDir = '/usr/share/fonts/dejavu-lgc'; // CentOS 104// $fontDir = '/usr/share/fonts/dejavu'; // Fedora 105$fontName = 'DejaVuSans-Bold.ttf'; 106$imgwidth = 400 * $ratio; 107$imgheight = 50 * $ratio; 108 109/** 110 * Don't change anything below (unless you know what you do) 111 */ 112 113$font = "$fontDir/$fontName"; 114 115//========================================== 116// Set Minimum, Default, and Maximum values. 117//========================================== 118 119$min = 0; 120$max = -1; 121$default = 0; 122 123$pdc = count($aPerfdata); // performance data count 124$string = ''; // string in perfdata label 125$current = 1; // show current value 126$label = 1; // show host/service label 127$cols = 3; // no. of columns with graphs 128$threshold = 'pct'; // threshold values in percent 129 130$sect1 = intval($imgheight / 5); 131$sect2 = intval($imgheight / 2); 132$sect3 = intval($imgheight / 5)*3; 133$chrSize = $ratio * 5; 134if ($chrSize < 1) { $chrSize = 1; } 135 136//================== 137// scan gadget_opts 138//================== 139 140if (isset($aOpts['opts']) && ($aOpts['opts'] != '')){ 141 preg_match_all ('/(\w+)=(\w+)/',$aOpts['opts'],$matches,PREG_SET_ORDER); 142 for ($i = 0; $i < count($matches); $i++){ 143 if ($matches[$i][1] == 'columns') { $cols = $matches[$i][2]; } 144 if ($matches[$i][1] == 'string') { $string = $matches[$i][2]; } 145 if ($matches[$i][1] == 'current') { $current = $matches[$i][2]; } 146 if ($matches[$i][1] == 'label') { $label = $matches[$i][2]; } 147 if ($matches[$i][1] == 'threshold') { $threshold = $matches[$i][2]; } 148 } 149} 150$rows = ceil($pdc / $cols); // max. no. of rows with graphs 151 152//==================== 153// Create image 154//==================== 155 156$img=imagecreatetruecolor($imgwidth*$cols, $imgheight*$rows); 157 158$oBackground = imagecolorallocate($img, 122, 23, 211); 159$oBlack = imagecolorallocate($img, 0, 0, 0); 160$oGreen = imagecolorallocate($img, 0, 255, 0); 161$oYellow = imagecolorallocate($img, 255, 255, 0); 162$oYellowAck = imagecolorallocate($img, 200, 255, 0); 163$oRed = imagecolorallocate($img, 255, 0, 0); 164$oRedAck = imagecolorallocate($img, 200, 0, 0); 165$oBlue = imagecolorallocate($img, 0, 0, 255); 166 167imagefill($img, 0, 0, $oBackground); 168imagecolortransparent($img, $oBackground); 169 170$offG = 0; // current graph 171for ($i=0; $i < $pdc; $i++){ 172 $desc = preg_replace('(.*::)','',$aPerfdata[$i]['label']); // omit check_multi description 173 if (preg_match("/$string/",$desc)) { 174 $colour = ''; 175 $value = $aPerfdata[$i]['value']; 176 $warn = $aPerfdata[$i]['warning']; 177 $warn = preg_replace ('(:.*)','',$warn); // ignore range settings 178 $crit = $aPerfdata[$i]['critical']; 179 $crit = preg_replace ('(:.*)','',$crit); // ignore range settings 180 $min = $aPerfdata[$i]['min']; 181 $max = $aPerfdata[$i]['max']; 182 $uom = $aPerfdata[$i]['uom']; 183 $ack = $aPerfdata[$i]['ack']; 184 $downtime = $aPerfdata[$i]['downtime']; 185 $offX = ($offG % $cols) * $imgwidth; // calculate left x-axis position 186 $offY = floor($offG / $cols) * $imgheight; // calculate upper y-axis position 187 $maxX = $imgwidth-15; 188 $maxY = $imgheight-5; 189 190 // determine the upper limit 191 $limit = $max; 192 if ($limit < $crit) { 193 $limit = $crit; 194 } 195 if (($warn > $crit) && ($limit < $warn)) { 196 $limit = $warn; 197 } 198 if ($value > $limit) { 199 $limit = $value; 200 } 201 if ($limit < 1) { 202 $limit = 1; 203 } 204 if ($uom == '%') { 205 $limit = 100; 206 } 207 if (isset($warn) && isset($crit)) { 208 if ($warn < $crit) { 209 if ($value >= $warn) { $colour = ($ack) ? $oYellowAck : $oYellow; }; 210 if ($value >= $crit) { $colour = ($ack) ? $oRedAck : $oRed; }; 211 } else { 212 if ($value <= $warn) { $colour = ($ack) ? $oYellowAck : $oYellow; }; 213 if ($value <= $crit) { $colour = ($ack) ? $oRedAck : $oRed; }; 214 } 215 } 216 // create box 217 imagefilledrectangle ($img, $offX, $offY+1, $offX+$maxX, $offY+$maxY, $oGreen); 218 imageRectangle($img, $offX, $offY, $offX+$maxX, $offY+$maxY, $oBlack); 219 $maxX--; 220 $maxY--; 221 222 // "highlight" graph if non-ok value 223 if ($colour != '') { 224 imagefilledrectangle ($img, $offX+1, $offY+$sect3+1, $offX+$maxX, $offY+$maxY, $colour); 225 } 226 227 //================ 228 // Normalize / Fix value and max 229 //================ 230 231 if($value == null) { 232 $value = $default; 233 } else { 234 if($max != '' && $value < $min) { 235 $value = $min; 236 } elseif($max != '' && $max != -1 && $value > $max) { 237 $value = $max; 238 } 239 } 240 241 // If there is no max value given set it using the critical value 242 if(intval($max) == 0 || $max == '') { 243 $max = $crit + 1; 244 } 245 246 //================ 247 // Calculate value, warn, critical percentages/values 248 //================ 249 250 $p = 100 / $limit * $value; 251 $warnp = round(100 / $limit * $warn,0); 252 $critp = round(100 / $limit * $crit,0); 253 $valuev = ($maxX / 100 * $p); 254 $warnv = intval($maxX / 100 * $warnp); 255 $critv = intval($maxX / 100 * $critp); 256 $warnt = ($threshold == 'pct') ? $warnp : $warn; 257 $critt = ($threshold == 'pct') ? $critp : $crit; 258 259 //=================== 260 // create warning/critical areas, current value 261 //=================== 262 263 // Warning 264 if($warn) { 265 if ($warn < $crit) { 266 imageFilledRectangle($img, $offX+$warnv+1, $offY+1, $offX+$maxX, $offY+$sect1, $oYellow); 267 } else { 268 imageFilledRectangle($img, $offX+1, $offY+1, $offX+$warnv-1, $offY+$sect1, $oYellow); 269 } 270 if (file_exists ("$font")) { 271 ImageTTFText($img, $chrSize*2, 0, $offX+$warnv+1, $offY+$sect1, $oBlack, $font, intval($warnt)); 272 } else { 273 imagestring($img, $chrSize, $offX+$warnv+1, $offY-2, intval($warnt), $oBlack); 274 } 275 } 276 // Critical 277 if($crit) { 278 if ($warn < $crit) { 279 imageFilledRectangle($img, $offX+$critv+1, $offY+1, $offX+$maxX, $offY+$sect1, $oRed); 280 } else { 281 imageFilledRectangle($img, $offX+1, $offY+1, $offX+$critv-1, $offY+$sect1, $oRed); 282 } 283 if (file_exists ("$font")) { 284 ImageTTFText($img, $chrSize*2, 0, $offX+$critv+1, $offY+$sect1, $oBlack, $font, intval($critt)); 285 } else { 286 imagestring($img, $chrSize, $offX+$critv+1, $offY-2, intval($critt), $oBlack); 287 } 288 } 289 imagefilledRectangle($img, $offX+1, $offY+$sect1+1, $offX+$valuev+1, $offY+$sect3, $oBlue); 290 291 //=================== 292 // Labels 293 //=================== 294 295 if ($current == 1) { 296 $maxv = ""; 297 if (isset($aPerfdata[$i]['max'])) { $maxv = " of ".$aPerfdata[$i]['max']; } 298 if ($down) { $maxv = " [down]"; } 299 if (file_exists ("$font")) { 300 ImageTTFText($img, $chrSize*3.5, 0, $offX+5, $offY+$sect3-1, $oBlack, $font, $desc . ':' . $value . $uom . $maxv); 301 } else { 302 imagestring($img, $chrSize, $offX+3, $offY+$sect1+2, $desc.': '.$value . $uom . $maxv, $oBlack); 303 } 304 305 if ($label == 1) { 306 $hostname = (strlen($aOpts['name1']) > 15) ? substr($aOpts['name1'],0,14)."..." : $aOpts['name1']; 307 $svcdesc = (strlen($aOpts['name2']) > 15) ? substr($aOpts['name2'],0,14)."..." : $aOpts['name2']; 308 if (strlen($desc) > 15) { 309 $desc = substr($desc,0,14)."..."; 310 } 311 if (file_exists ("$font")) { 312 ImageTTFText($img, $chrSize*2.5, 0, $offX+3, $offY+$maxY-1, $oBlack, $font, $hostname); 313 ImageTTFText($img, $chrSize*2.5, 0, $offX+$imgwidth/2, $offY+$maxY-1, $oBlack, $font, $svcdesc); 314// ImageTTFText($img, $chrSize*2.5, 0, $offX+$imgwidth/2, $offY+$maxY-1, $oBlack, $font, $desc); 315 } else { 316 imagestring($img, $chrSize, $offX+3, $offY+$sect3, $hostname, $oBlack); 317 imagestring($img, $chrSize, $offX+$imgwidth/2, $offY+$sect3, $svcdesc, $oBlack); 318// imagestring($img, $chrSize, $offX+$imgwidth/2, $offY+$sect3, $desc, $oBlack); // perf label 319 } 320 } 321 } 322 $offG++; 323 } 324} 325 326//============== 327// Output image. 328//============== 329 330if(function_exists('imageantialias')) { 331 imageantialias($img, true); 332} 333 334imagepng($img); 335imagedestroy($img); 336?> 337 338define service { 339host_name=uxze01 340service_description=root-volume 341x=50 342y=50 343view_type=gadget 344gadget_url=std_bar.php 345gadget_scale=100 346} 347