Parse perfdata thresholds provisionary

Use a heuristic to determine whether thresholds should be inverted, to support plugins with non-standard perfdata output while still being able to parse standard-compliant perfdata input.
This commit is contained in:
Matthias Jentsch 2015-01-13 10:59:33 +01:00
parent e842abf80f
commit 5e2594d250
2 changed files with 47 additions and 17 deletions

View File

@ -8,7 +8,7 @@ use Icinga\Module\Monitoring\Plugin\Perfdata;
use Icinga\Module\Monitoring\Plugin\PerfdataSet; use Icinga\Module\Monitoring\Plugin\PerfdataSet;
class Zend_View_Helper_Perfdata extends Zend_View_Helper_Abstract class Zend_View_Helper_Perfdata extends Zend_View_Helper_Abstract
{ {#
/** /**
* Display the given perfdata string to the user * Display the given perfdata string to the user
@ -19,7 +19,7 @@ class Zend_View_Helper_Perfdata extends Zend_View_Helper_Abstract
* *
* @return string * @return string
*/ */
public function perfdata($perfdataStr, $compact = false, $color = Perfdata::PERFDATA_DEFAULT) public function perfdata($perfdataStr, $compact = false, $color = Perfdata::PERFDATA_OK)
{ {
$pieChartData = PerfdataSet::fromString($perfdataStr)->asArray(); $pieChartData = PerfdataSet::fromString($perfdataStr)->asArray();

View File

@ -12,8 +12,9 @@ use Zend_Controller_Front;
class Perfdata class Perfdata
{ {
const PERFDATA_DEFAULT = 'green'; const PERFDATA_OK = 'ok';
const PERFDATA_RED = 'red'; const PERFDATA_WARNING = 'warning';
const PERFDATA_CRITICAL = 'critical';
/** /**
* The performance data value being parsed * The performance data value being parsed
@ -60,6 +61,8 @@ class Perfdata
/** /**
* The WARNING threshold * The WARNING threshold
* *
* TODO: Should be parsed Range-Object instead of string
*
* @var string * @var string
*/ */
protected $warningThreshold; protected $warningThreshold;
@ -67,6 +70,8 @@ class Perfdata
/** /**
* The CRITICAL threshold * The CRITICAL threshold
* *
* TODO: Should be parsed Range-Object instead of string
*
* @var string * @var string
*/ */
protected $criticalThreshold; protected $criticalThreshold;
@ -240,7 +245,7 @@ class Perfdata
/** /**
* Return the minimum value or null if it is not available * Return the minimum value or null if it is not available
* *
* @return null|float * @return null|string
*/ */
public function getMinimumValue() public function getMinimumValue()
{ {
@ -295,9 +300,9 @@ class Perfdata
$this->minValue = self::convert($parts[3], $this->unit); $this->minValue = self::convert($parts[3], $this->unit);
} }
case 3: case 3:
$this->criticalThreshold = trim($parts[2]) ? self::convert(trim($parts[2]), $this->unit) : null; $this->criticalThreshold = trim($parts[2]) ? trim($parts[2]) : null;
case 2: case 2:
$this->warningThreshold = trim($parts[1]) ? self::convert(trim($parts[1]), $this->unit) : null; $this->warningThreshold = trim($parts[1]) ? trim($parts[1]) : null;
} }
} }
@ -332,7 +337,7 @@ class Perfdata
} }
} }
protected function calculatePieChartData( $color) protected function calculatePieChartData()
{ {
$rawValue = $this->getValue(); $rawValue = $this->getValue();
$minValue = $this->getMinimumValue() !== null ? $this->getMinimumValue() : 0; $minValue = $this->getMinimumValue() !== null ? $this->getMinimumValue() : 0;
@ -340,36 +345,61 @@ class Perfdata
$usedValue = ($rawValue - $minValue); $usedValue = ($rawValue - $minValue);
$unusedValue = ($maxValue - $minValue) - $usedValue; $unusedValue = ($maxValue - $minValue) - $usedValue;
$warningThreshold = $this->convert($this->warningThreshold, $this->unit);
$criticalThreshold = $this->convert($this->criticalThreshold, $this->unit);
$gray = $unusedValue; $gray = $unusedValue;
$green = $orange = $red = 0; $green = $orange = $red = 0;
switch ($color) { $pieState = self::PERFDATA_OK;
case self::PERFDATA_DEFAULT: 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;
}
} 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;
}
}
switch ($pieState) {
case self::PERFDATA_OK:
$green = $usedValue; $green = $usedValue;
break; break;
case self::PERFDATA_RED: case self::PERFDATA_CRITICAL:
$red = $usedValue; $red = $usedValue;
break; break;
case self::PERFDATA_ORANGE: case self::PERFDATA_WARNING:
$orange = $usedValue; $orange = $usedValue;
break; break;
} }
// TODO(#6122): Add proper treshold parsing.
return array($green, $orange, $red, $gray); return array($green, $orange, $red, $gray);
} }
public function asInlinePie($color) public function asInlinePie()
{ {
if (! $this->isVisualizable()) { if (! $this->isVisualizable()) {
throw new ProgrammingError('Cannot calculate piechart data for unvisualizable perfdata entry.'); throw new ProgrammingError('Cannot calculate piechart data for unvisualizable perfdata entry.');
} }
$data = $this->calculatePieChartData($color); $data = $this->calculatePieChartData();
$pieChart = new InlinePie($data, $this); $pieChart = new InlinePie($data, $this);
$pieChart->setColors(array('#44bb77', '#ffaa44', '#ff5566', '#ddccdd'));
$pieChart->setSparklineClass('sparkline-perfdata'); $pieChart->setSparklineClass('sparkline-perfdata');
if (Zend_Controller_Front::getInstance()->getRequest()->isXmlHttpRequest()) { if (Zend_Controller_Front::getInstance()->getRequest()->isXmlHttpRequest()) {
@ -419,8 +449,8 @@ class Perfdata
'value' => $this->format($this->getvalue()), 'value' => $this->format($this->getvalue()),
'min' => isset($this->minValue) && !$this->isPercentage() ? $this->format($this->minValue) : '', 'min' => isset($this->minValue) && !$this->isPercentage() ? $this->format($this->minValue) : '',
'max' => isset($this->maxValue) && !$this->isPercentage() ? $this->format($this->maxValue) : '', 'max' => isset($this->maxValue) && !$this->isPercentage() ? $this->format($this->maxValue) : '',
'warn' => isset($this->warningThreshold) ? $this->format($this->warningThreshold) : '', 'warn' => isset($this->warningThreshold) ? $this->format(self::convert($this->warningThreshold, $this->unit)) : '',
'crit' => isset($this->criticalThreshold) ? $this->format($this->criticalThreshold) : '' 'crit' => isset($this->criticalThreshold) ? $this->format(self::convert($this->criticalThreshold, $this->unit)) : ''
); );
return $parts; return $parts;
} }