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:
Thomas Gelf 2014-07-31 16:52:34 +02:00
parent 3e4944c60a
commit 51484ebf88
2 changed files with 17 additions and 2 deletions

View File

@ -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:
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:
// TODO(#6123): Tresholds have the same UOM and need to be converted as well!
$this->criticalThreshold = trim($parts[2]) ? trim($parts[2]) : null;

View File

@ -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'
);
}
/**