monitoring/Perfdata: ignore invalid min/max
key=23;0;0;0;0 and key=23;;;; lead to division by zero exceptions This should fix this by ignoring empty strings for min/max (formerly it got "converted" to float(0)) and also ignores min == max. fixes #6828
This commit is contained in:
parent
3e4944c60a
commit
51484ebf88
|
@ -190,6 +190,9 @@ class Perfdata
|
|||
|
||||
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;
|
||||
|
@ -267,9 +270,13 @@ class Perfdata
|
|||
switch (count($parts))
|
||||
{
|
||||
case 5:
|
||||
$this->maxValue = self::convert($parts[4], $this->unit);
|
||||
if ($parts[4] !== '') {
|
||||
$this->maxValue = self::convert($parts[4], $this->unit);
|
||||
}
|
||||
case 4:
|
||||
$this->minValue = self::convert($parts[3], $this->unit);
|
||||
if ($parts[3] !== '') {
|
||||
$this->minValue = self::convert($parts[3], $this->unit);
|
||||
}
|
||||
case 3:
|
||||
// TODO(#6123): Tresholds have the same UOM and need to be converted as well!
|
||||
$this->criticalThreshold = trim($parts[2]) ? trim($parts[2]) : null;
|
||||
|
|
|
@ -347,6 +347,14 @@ class PerfdataTest extends BaseTestCase
|
|||
Perfdata::fromString('test=25;;;50;100')->getPercentage(),
|
||||
'Perfdata objects do return a percentage though their value is lower than it\'s allowed minimum'
|
||||
);
|
||||
$this->assertNull(
|
||||
Perfdata::fromString('test=25;;;0;')->getPercentage(),
|
||||
'Perfdata objects do not ignore empty max values when returning percentages'
|
||||
);
|
||||
$this->assertNull(
|
||||
Perfdata::fromString('test=25;;;0;0')->getPercentage(),
|
||||
'Perfdata objects do not ignore impossible min/max combinations when returning percentages'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue