icingadb-web/library/Icingadb/Util/ThresholdRange.php
Alexander A. Klimov 3c8ed68cc6 Upgrade license from GPLv2 to GPLv2+
This was easy because only README.md and doc/01-About.md were redacted manually, everything else via:
git ls-files -z |xargs -0 perl -pi -e 's/Icinga GmbH \| GPLv2/Icinga GmbH | GPLv2+/'

This is legal because we have only merged PRs with label:cla/signed or made by Icinga staff:
https://github.com/Icinga/icingadb-web/pulls?page=1&q=is%3Apr+is%3Aclosed+-label%3Acla%2Fsigned+-author%3Anilmerg

This has no risk for us in people distributing their own version under GPLv3 only.
After all, we won't take their patches anyway, unless they sign our CLA.

This is the cleanest solution for having e.g. these in one address space:

* Icinga Web, GPLv2+
* K8s Web, AGPLv3
* Thirdparty, some LGPLv3 and Apache-2.0

Apropos, K8s Web is even v3-licensed on purpose, to have a stronger protection against cloud ops.
2025-11-21 13:31:24 +01:00

213 lines
4.5 KiB
PHP

<?php
/* Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2+ */
namespace Icinga\Module\Icingadb\Util;
/**
* The warning/critical threshold of a measured value
*/
class ThresholdRange
{
/**
* The smallest value inside the range (null stands for -∞)
*
* @var float|null
*/
protected $min;
/**
* The biggest value inside the range (null stands for ∞)
*
* @var float|null
*/
protected $max;
/**
* Whether to invert the result of contains()
*
* @var bool
*/
protected $inverted = false;
/**
* The unmodified range as passed to fromString()
*
* @var string
*/
protected $raw;
/**
* Whether the threshold range is valid
*
* @var bool
*/
protected $isValid = true;
/**
* 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(string $rawRange): self
{
$range = new static();
$range->raw = $rawRange;
if ($rawRange == '') {
return $range;
}
$rawRange = ltrim($rawRange);
if (substr($rawRange, 0, 1) === '@') {
$range->setInverted();
$rawRange = substr($rawRange, 1);
}
if (strpos($rawRange, ':') === false) {
$min = 0.0;
$max = trim($rawRange);
if (! is_numeric($max)) {
$range->isValid = false;
return $range;
}
$max = floatval(trim($rawRange));
} else {
list($min, $max) = explode(':', $rawRange, 2);
$min = trim($min);
$max = trim($max);
switch ($min) {
case '':
$min = 0.0;
break;
case '~':
$min = null;
break;
default:
if (! is_numeric($min)) {
$range->isValid = false;
return $range;
}
$min = floatval($min);
}
if (! empty($max) && ! is_numeric($max)) {
$range->isValid = false;
return $range;
}
$max = empty($max) ? null : floatval($max);
}
return $range->setMin($min)
->setMax($max);
}
/**
* Set the smallest value inside the range (null stands for -∞)
*
* @param float|null $min
*
* @return $this
*/
public function setMin(?float $min): self
{
$this->min = $min;
return $this;
}
/**
* Get the smallest value inside the range (null stands for -∞)
*
* @return float|null
*/
public function getMin()
{
return $this->min;
}
/**
* Set the biggest value inside the range (null stands for ∞)
*
* @param float|null $max
*
* @return $this
*/
public function setMax(?float $max): self
{
$this->max = $max;
return $this;
}
/**
* Get the biggest 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(bool $inverted = true): self
{
$this->inverted = $inverted;
return $this;
}
/**
* Get whether to invert the result of contains()
*
* @return bool
*/
public function isInverted(): bool
{
return $this->inverted;
}
/**
* Return whether $value is inside $this
*
* @param float $value
*
* @return bool
*/
public function contains(float $value): bool
{
return (bool) ($this->inverted ^ (
($this->min === null || $this->min <= $value) && ($this->max === null || $this->max >= $value)
));
}
/**
* Return whether the threshold range is valid
*
* @return bool
*/
public function isValid()
{
return $this->isValid;
}
/**
* Return the textual representation of $this, suitable for fromString()
*
* @return string
*/
public function __toString()
{
return (string) $this->raw;
}
}