Perfdata: use ThresholdRange

refs #8194
This commit is contained in:
Alexander A. Klimov 2016-04-27 17:46:24 +02:00 committed by Johannes Meyer
parent cdb1db89e2
commit 2270e250d3

View File

@ -61,18 +61,14 @@ class Perfdata
/** /**
* The WARNING threshold * The WARNING threshold
* *
* TODO: Should be parsed Range-Object instead of string * @var ThresholdRange
*
* @var string
*/ */
protected $warningThreshold; protected $warningThreshold;
/** /**
* The CRITICAL threshold * The CRITICAL threshold
* *
* TODO: Should be parsed Range-Object instead of string * @var ThresholdRange
*
* @var string
*/ */
protected $criticalThreshold; protected $criticalThreshold;
@ -96,6 +92,15 @@ class Perfdata
$this->maxValue = 100.0; $this->maxValue = 100.0;
} }
} }
$warn = $this->warningThreshold->getMax();
if ($warn !== null) {
$crit = $this->criticalThreshold->getMax();
if ($crit !== null && $warn > $crit) {
$this->warningThreshold->setInverted();
$this->criticalThreshold->setInverted();
}
}
} }
/** /**
@ -223,23 +228,23 @@ class Perfdata
} }
/** /**
* Return this performance data's warning treshold or null if it is not available * Return this performance data's warning treshold
* *
* @return null|string * @return string
*/ */
public function getWarningThreshold() public function getWarningThreshold()
{ {
return $this->warningThreshold; return (string) $this->warningThreshold;
} }
/** /**
* Return this performance data's critical treshold or null if it is not available * Return this performance data's critical treshold
* *
* @return null|string * @return string
*/ */
public function getCriticalThreshold() public function getCriticalThreshold()
{ {
return $this->criticalThreshold; return (string) $this->criticalThreshold;
} }
/** /**
@ -302,10 +307,23 @@ class Perfdata
} }
/* @noinspection PhpMissingBreakStatementInspection */ /* @noinspection PhpMissingBreakStatementInspection */
case 3: case 3:
$this->criticalThreshold = trim($parts[2]) ? trim($parts[2]) : null; $this->criticalThreshold = self::convert(
ThresholdRange::fromString(trim($parts[2]) ?: '~:'),
$this->unit
);
// Fallthrough // Fallthrough
case 2: case 2:
$this->warningThreshold = trim($parts[1]) ? trim($parts[1]) : null; $this->warningThreshold = self::convert(
ThresholdRange::fromString(trim($parts[1]) ?: '~:'),
$this->unit
);
}
if ($this->warningThreshold === null) {
$this->warningThreshold = new ThresholdRange();
}
if ($this->criticalThreshold === null) {
$this->criticalThreshold = new ThresholdRange();
} }
} }
@ -319,6 +337,22 @@ class Perfdata
*/ */
protected static function convert($value, $fromUnit = null) protected static function convert($value, $fromUnit = null)
{ {
if ($value instanceof ThresholdRange) {
$value = clone $value;
$min = $value->getMin();
if ($min !== null) {
$value->setMin(self::convert($min, $fromUnit));
}
$max = $value->getMax();
if ($max !== null) {
$value->setMax(self::convert($max, $fromUnit));
}
return $value;
}
if (is_numeric($value)) { if (is_numeric($value)) {
switch ($fromUnit) { switch ($fromUnit) {
case 'us': case 'us':
@ -343,52 +377,21 @@ class Perfdata
{ {
$rawValue = $this->getValue(); $rawValue = $this->getValue();
$minValue = $this->getMinimumValue() !== null ? $this->getMinimumValue() : 0; $minValue = $this->getMinimumValue() !== null ? $this->getMinimumValue() : 0;
$maxValue = $this->getMaximumValue();
$usedValue = ($rawValue - $minValue); $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; $green = $orange = $red = 0;
$pieState = self::PERFDATA_OK; if ($this->criticalThreshold->contains($rawValue)) {
if ($warningThreshold > $criticalThreshold) { if ($this->warningThreshold->contains($rawValue)) {
// inverted threshold parsing OK > warning > critical $green = $usedValue;
if (isset($warningThreshold) && $this->value <= $warningThreshold) { } else {
$pieState = self::PERFDATA_WARNING; $orange = $usedValue;
}
if (isset($criticalThreshold) && $this->value <= $criticalThreshold) {
$pieState = self::PERFDATA_CRITICAL;
} }
} else { } else {
// TODO: Use standard perfdata range format to decide the state #8194 $red = $usedValue;
// 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) { return array($green, $orange, $red, ($this->getMaximumValue() - $minValue) - $usedValue);
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);
} }
@ -444,7 +447,7 @@ class Perfdata
public function toArray() public function toArray()
{ {
$parts = array( return array(
'label' => $this->getLabel(), 'label' => $this->getLabel(),
'value' => $this->format($this->getvalue()), 'value' => $this->format($this->getvalue()),
'min' => isset($this->minValue) && !$this->isPercentage() 'min' => isset($this->minValue) && !$this->isPercentage()
@ -453,14 +456,9 @@ class Perfdata
'max' => isset($this->maxValue) && !$this->isPercentage() 'max' => isset($this->maxValue) && !$this->isPercentage()
? $this->format($this->maxValue) ? $this->format($this->maxValue)
: '', : '',
'warn' => isset($this->warningThreshold) 'warn' => (string) $this->warningThreshold,
? $this->format(self::convert($this->warningThreshold, $this->unit)) 'crit' => (string) $this->criticalThreshold
: '',
'crit' => isset($this->criticalThreshold)
? $this->format(self::convert($this->criticalThreshold, $this->unit))
: ''
); );
return $parts;
} }
/** /**
@ -476,13 +474,11 @@ class Perfdata
return Service::STATE_UNKNOWN; return Service::STATE_UNKNOWN;
} }
if (! ($this->criticalThreshold === null if (! $this->criticalThreshold->contains($this->value)) {
|| $this->value < $this->criticalThreshold)) {
return Service::STATE_CRITICAL; return Service::STATE_CRITICAL;
} }
if (! ($this->warningThreshold === null if (! $this->warningThreshold->contains($this->value)) {
|| $this->value < $this->warningThreshold)) {
return Service::STATE_WARNING; return Service::STATE_WARNING;
} }