/usr/share/icingaweb2/modules/monitoring/library/Monitoring/Plugin/Perfdata.php is in icingaweb2-module-monitoring 2.1.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 | <?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Monitoring\Plugin;
use Icinga\Util\Format;
use InvalidArgumentException;
use Icinga\Exception\ProgrammingError;
use Icinga\Web\Widget\Chart\InlinePie;
use Icinga\Module\Monitoring\Object\Service;
use Zend_Controller_Front;
class Perfdata
{
const PERFDATA_OK = 'ok';
const PERFDATA_WARNING = 'warning';
const PERFDATA_CRITICAL = 'critical';
/**
* The performance data value being parsed
*
* @var string
*/
protected $perfdataValue;
/**
* Unit of measurement (UOM)
*
* @var string
*/
protected $unit;
/**
* The label
*
* @var string
*/
protected $label;
/**
* The value
*
* @var float
*/
protected $value;
/**
* The minimum value
*
* @var float
*/
protected $minValue;
/**
* The maximum value
*
* @var float
*/
protected $maxValue;
/**
* The WARNING threshold
*
* TODO: Should be parsed Range-Object instead of string
*
* @var string
*/
protected $warningThreshold;
/**
* The CRITICAL threshold
*
* TODO: Should be parsed Range-Object instead of string
*
* @var string
*/
protected $criticalThreshold;
/**
* Create a new Perfdata object based on the given performance data label and value
*
* @param string $label The perfdata label
* @param string $value The perfdata value
*/
public function __construct($label, $value)
{
$this->perfdataValue = $value;
$this->label = $label;
$this->parse();
if ($this->unit === '%') {
if ($this->minValue === null) {
$this->minValue = 0.0;
}
if ($this->maxValue === null) {
$this->maxValue = 100.0;
}
}
}
/**
* Return a new Perfdata object based on the given performance data key=value pair
*
* @param string $perfdata The key=value pair to parse
*
* @return Perfdata
*
* @throws InvalidArgumentException In case the given performance data has no content or a invalid format
*/
public static function fromString($perfdata)
{
if (empty($perfdata)) {
throw new InvalidArgumentException('Perfdata::fromString expects a string with content');
} elseif (false === strpos($perfdata, '=')) {
throw new InvalidArgumentException(
'Perfdata::fromString expects a key=value formatted string. Got "' . $perfdata . '" instead'
);
}
list($label, $value) = explode('=', $perfdata, 2);
return new static(trim($label), trim($value));
}
/**
* Return whether this performance data's value is a number
*
* @return bool True in case it's a number, otherwise False
*/
public function isNumber()
{
return $this->unit === null;
}
/**
* Return whether this performance data's value are seconds
*
* @return bool True in case it's seconds, otherwise False
*/
public function isSeconds()
{
return in_array($this->unit, array('s', 'ms', 'us'));
}
/**
* Return whether this performance data's value is in percentage
*
* @return bool True in case it's in percentage, otherwise False
*/
public function isPercentage()
{
return $this->unit === '%';
}
/**
* Return whether this performance data's value is in bytes
*
* @return bool True in case it's in bytes, otherwise False
*/
public function isBytes()
{
return in_array($this->unit, array('b', 'kb', 'mb', 'gb', 'tb'));
}
/**
* Return whether this performance data's value is a counter
*
* @return bool True in case it's a counter, otherwise False
*/
public function isCounter()
{
return $this->unit === 'c';
}
/**
* Returns whether it is possible to display a visual representation
*
* @return bool True when the perfdata is visualizable
*/
public function isVisualizable()
{
return isset($this->minValue) && isset($this->maxValue) && isset($this->value);
}
/**
* Return this perfomance data's label
*/
public function getLabel()
{
return $this->label;
}
/**
* Return the value or null if it is unknown (U)
*
* @return null|float
*/
public function getValue()
{
return $this->value;
}
/**
* Return the value as percentage (0-100)
*
* @return null|float
*/
public function getPercentage()
{
if ($this->isPercentage()) {
return $this->value;
}
if ($this->maxValue !== null) {
$minValue = $this->minValue !== null ? $this->minValue : 0;
if ($this->maxValue - $minValue === 0.0) {
return null;
}
if ($this->value > $minValue) {
return (($this->value - $minValue) / ($this->maxValue - $minValue)) * 100;
}
}
}
/**
* Return this performance data's warning treshold or null if it is not available
*
* @return null|string
*/
public function getWarningThreshold()
{
return $this->warningThreshold;
}
/**
* Return this performance data's critical treshold or null if it is not available
*
* @return null|string
*/
public function getCriticalThreshold()
{
return $this->criticalThreshold;
}
/**
* Return the minimum value or null if it is not available
*
* @return null|string
*/
public function getMinimumValue()
{
return $this->minValue;
}
/**
* Return the maximum value or null if it is not available
*
* @return null|float
*/
public function getMaximumValue()
{
return $this->maxValue;
}
/**
* Return this performance data as string
*
* @return string
*/
public function __toString()
{
return $this->formatLabel();
}
/**
* Parse the current performance data value
*
* @todo Handle optional min/max if UOM == %
*/
protected function parse()
{
$parts = explode(';', $this->perfdataValue);
$matches = array();
if (preg_match('@^(\d+(\.\d+)?)([a-zA-Z%]{1,2})$@', $parts[0], $matches)) {
$this->unit = strtolower($matches[3]);
$this->value = self::convert($matches[1], $this->unit);
} else {
$this->value = self::convert($parts[0]);
}
switch (count($parts))
{
case 5:
if ($parts[4] !== '') {
$this->maxValue = self::convert($parts[4], $this->unit);
}
case 4:
if ($parts[3] !== '') {
$this->minValue = self::convert($parts[3], $this->unit);
}
case 3:
$this->criticalThreshold = trim($parts[2]) ? trim($parts[2]) : null;
case 2:
$this->warningThreshold = trim($parts[1]) ? trim($parts[1]) : null;
}
}
/**
* Return the given value converted to its smallest supported representation
*
* @param string $value The value to convert
* @param string $fromUnit The unit the value currently represents
*
* @return null|float Null in case the value is not a number
*/
protected static function convert($value, $fromUnit = null)
{
if (is_numeric($value)) {
switch ($fromUnit)
{
case 'us':
return $value / pow(10, 6);
case 'ms':
return $value / pow(10, 3);
case 'tb':
return floatval($value) * pow(2, 40);
case 'gb':
return floatval($value) * pow(2, 30);
case 'mb':
return floatval($value) * pow(2, 20);
case 'kb':
return floatval($value) * pow(2, 10);
default:
return (float) $value;
}
}
}
protected function calculatePieChartData()
{
$rawValue = $this->getValue();
$minValue = $this->getMinimumValue() !== null ? $this->getMinimumValue() : 0;
$maxValue = $this->getMaximumValue();
$usedValue = ($rawValue - $minValue);
$unusedValue = ($maxValue - $minValue) - $usedValue;
$warningThreshold = $this->convert($this->warningThreshold, $this->unit);
$criticalThreshold = $this->convert($this->criticalThreshold, $this->unit);
$gray = $unusedValue;
$green = $orange = $red = 0;
$pieState = self::PERFDATA_OK;
if ($warningThreshold > $criticalThreshold) {
// inverted threshold parsing OK > warning > critical
if (isset($warningThreshold) && $this->value <= $warningThreshold) {
$pieState = self::PERFDATA_WARNING;
}
if (isset($criticalThreshold) && $this->value <= $criticalThreshold) {
$pieState = self::PERFDATA_CRITICAL;
}
} else {
// TODO: Use standard perfdata range format to decide the state #8194
// regular threshold parsing OK < warning < critical
if (isset($warningThreshold) && $rawValue > $warningThreshold) {
$pieState = self::PERFDATA_WARNING;
}
if (isset($criticalThreshold) && $rawValue > $criticalThreshold) {
$pieState = self::PERFDATA_CRITICAL;
}
}
switch ($pieState) {
case self::PERFDATA_OK:
$green = $usedValue;
break;
case self::PERFDATA_CRITICAL:
$red = $usedValue;
break;
case self::PERFDATA_WARNING:
$orange = $usedValue;
break;
}
return array($green, $orange, $red, $gray);
}
public function asInlinePie()
{
if (! $this->isVisualizable()) {
throw new ProgrammingError('Cannot calculate piechart data for unvisualizable perfdata entry.');
}
$data = $this->calculatePieChartData();
$pieChart = new InlinePie($data, $this);
$pieChart->setColors(array('#44bb77', '#ffaa44', '#ff5566', '#ddccdd'));
if (Zend_Controller_Front::getInstance()->getRequest()->isXmlHttpRequest()) {
$pieChart->disableNoScript();
}
return $pieChart;
}
/**
* Format the given value depending on the currently used unit
*/
protected function format($value)
{
if ($this->isPercentage()) {
return (string)$value . '%';
}
if ($this->isBytes()) {
return Format::bytes($value);
}
if ($this->isSeconds()) {
return Format::seconds($value);
}
return number_format($value, 2);
}
/**
* Format the title string that represents this perfdata set
*
* @param bool $html
*
* @return string
*/
public function formatLabel($html = false)
{
return sprintf(
$html ? '<b>%s %s</b> (%s%%)' : '%s %s (%s%%)',
htmlspecialchars($this->getLabel()),
$this->format($this->value),
number_format($this->getPercentage(), 2)
);
}
public function toArray()
{
$parts = array(
'label' => $this->getLabel(),
'value' => $this->format($this->getvalue()),
'min' => isset($this->minValue) && !$this->isPercentage() ? $this->format($this->minValue) : '',
'max' => isset($this->maxValue) && !$this->isPercentage() ? $this->format($this->maxValue) : '',
'warn' => isset($this->warningThreshold) ? $this->format(self::convert($this->warningThreshold, $this->unit)) : '',
'crit' => isset($this->criticalThreshold) ? $this->format(self::convert($this->criticalThreshold, $this->unit)) : ''
);
return $parts;
}
/**
* Return the state indicated by this perfdata
*
* @see Service
*
* @return int
*/
public function getState()
{
if ($this->value === null) {
return Service::STATE_UNKNOWN;
}
if (! ($this->criticalThreshold === null
|| $this->value < $this->criticalThreshold)) {
return Service::STATE_CRITICAL;
}
if (! ($this->warningThreshold === null
|| $this->value < $this->warningThreshold)) {
return Service::STATE_WARNING;
}
return Service::STATE_OK;
}
/**
* Return whether the state indicated by this perfdata is worse than
* the state indicated by the other perfdata
* CRITICAL > UNKNOWN > WARNING > OK
*
* @param Perfdata $rhs the other perfdata
*
* @return bool
*/
public function worseThan(Perfdata $rhs)
{
if (($state = $this->getState()) === ($rhsState = $rhs->getState())) {
return $this->getPercentage() > $rhs->getPercentage();
}
if ($state === Service::STATE_CRITICAL) {
return true;
}
if ($state === Service::STATE_UNKNOWN) {
return $rhsState !== Service::STATE_CRITICAL;
}
if ($state === Service::STATE_WARNING) {
return $rhsState === Service::STATE_OK;
}
return false;
}
}
|