From 5df6eeaeafc1d26af33077041a94129893f90618 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" <alexander.klimov@netways.de> Date: Tue, 26 Apr 2016 16:26:06 +0200 Subject: [PATCH] Implement ThresholdRange refs #8194 --- .../Monitoring/Plugin/ThresholdRange.php | 167 ++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 modules/monitoring/library/Monitoring/Plugin/ThresholdRange.php diff --git a/modules/monitoring/library/Monitoring/Plugin/ThresholdRange.php b/modules/monitoring/library/Monitoring/Plugin/ThresholdRange.php new file mode 100644 index 000000000..9f5040937 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Plugin/ThresholdRange.php @@ -0,0 +1,167 @@ +<?php +/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Module\Monitoring\Plugin; + +/** + * The warning/critical threshold of a measured value + */ +class ThresholdRange +{ + /** + * The least value inside the range (null stands for -∞) + * + * @var float|null + */ + protected $min; + + /** + * The greatest value inside the range (null stands for ∞) + * + * @var float|null + */ + protected $max; + + /** + * Whether to invert the result of contains() + * + * @var bool + */ + protected $inverted = false; + + /** + * Create a new instance based on a threshold range conforming to <https://nagios-plugins.org/doc/guidelines.html> + * + * @param string $rawRange + * + * @return ThresholdRange + */ + public static function fromString($rawRange) + { + $range = new static(); + + if (strpos($rawRange, '@') === 0) { + $range->setInverted(true); + $rawRange = substr($rawRange, 1); + } + + if (strpos($rawRange, ':') === false) { + $min = 0.0; + $max = floatval($rawRange); + } else { + list($min, $max) = explode(':', $rawRange, 2); + + switch ($min) { + case '': + $min = 0.0; + break; + case '~': + $min = null; + break; + default: + $min = floatval($min); + } + + $max = empty($max) ? null : floatval($max); + } + + return $range->setMin($min) + ->setMax($max); + } + + /** + * Set the least value inside the range (null stands for -∞) + * + * @param float|null $min + * + * @return $this + */ + public function setMin($min) + { + $this->min = $min; + return $this; + } + + /** + * Get the least value inside the range (null stands for -∞) + * + * @return float|null + */ + public function getMin() + { + return $this->min; + } + + /** + * Set the greatest value inside the range (null stands for ∞) + * + * @param float|null $max + * + * @return $this + */ + public function setMax($max) + { + $this->max = $max; + return $this; + } + + /** + * Get the greatest value inside the range (null stands for ∞) + * + * @return float|null + */ + public function getMax() + { + return $this->max; + } + + /** + * Set whether to invert the result of contains() + * + * @param bool $inverted + * + * @return $this + */ + public function setInverted($inverted) + { + $this->inverted = $inverted; + return $this; + } + + /** + * Get whether to invert the result of contains() + * + * @return bool + */ + public function isInverted() + { + return $this->inverted; + } + + /** + * @param float $value + * + * @return bool Whether $value is inside $this + */ + public function contains($value) + { + return (bool) ($this->inverted ^ ( + ($this->min === null || $this->min <= $value) && ($this->max === null || $this->max >= $value) + )); + } + + /** + * @return string The textual representation of $this, suitable for fromString() + */ + public function __toString() + { + if ($this->min === null) { + $res = '~:'; + } else { + $res = $this->min === 0.0 ? '' : $this->min . ':'; + } + $res .= $this->max === null ? '' : $this->max; + + return ($this->inverted ? '@' : '') . (empty($res) ? '0:' : $res); + } +}