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