Skip to content

Commit

Permalink
Do not evaluate invalid performance data
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Aug 31, 2023
1 parent 423c9a8 commit cf78825
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 15 deletions.
79 changes: 65 additions & 14 deletions library/Icingadb/Util/PerfData.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ class PerfData
*/
protected $criticalThreshold;

/**
* The raw value
*
* @var string
*/
protected $rawValue;

/**
* The raw minimum value
*
* @var string
*/
protected $rawMinValue;

/**
* The raw maximum value
*
* @var string
*/
protected $rawMaxValue;

/**
* Create a new PerfData object based on the given performance data label and value
*
Expand Down Expand Up @@ -312,7 +333,7 @@ public function isCounter(): bool
*/
public function isVisualizable(): bool
{
return isset($this->minValue) && isset($this->maxValue) && isset($this->value);
return isset($this->minValue, $this->maxValue, $this->value) && $this->isValid();
}

/**
Expand Down Expand Up @@ -434,19 +455,31 @@ protected function parse()
}

if (! is_numeric($this->value)) {
if ($this->value !== 'U') {
$this->rawValue = $parts[0];
}

$this->value = null;
}

switch (count($parts)) {
/* @noinspection PhpMissingBreakStatementInspection */
case 5:
if ($parts[4] !== '') {
$this->maxValue = $parts[4];
if (is_numeric($parts[4])) {
$this->maxValue = $parts[4];
} else {
$this->rawMaxValue = $parts[4];
}
}
/* @noinspection PhpMissingBreakStatementInspection */
case 4:
if ($parts[3] !== '') {
$this->minValue = $parts[3];
if (is_numeric($parts[3])) {
$this->minValue = $parts[3];
} else {
$this->rawMinValue = $parts[3];
}
}
/* @noinspection PhpMissingBreakStatementInspection */
case 3:
Expand Down Expand Up @@ -509,6 +542,10 @@ protected function format($value)
}

if ($value instanceof ThresholdRange) {
if (! $value->isValid()) {
return (string) $value;
}

if ($value->getMin()) {
return (string) $value;
}
Expand Down Expand Up @@ -578,18 +615,18 @@ public function formatLabel(bool $html = false): string

public function toArray(): array
{
return array(
return [
'label' => $this->getLabel(),
'value' => $this->format($this->getvalue()),
'min' => isset($this->minValue) && !$this->isPercentage()
? $this->format($this->minValue)
: '',
'max' => isset($this->maxValue) && !$this->isPercentage()
? $this->format($this->maxValue)
: '',
'warn' => $this->format($this->warningThreshold),
'crit' => $this->format($this->criticalThreshold)
);
'value' => isset($this->value) ? $this->format($this->value) : $this->rawValue,
'min' => (string)!$this->isPercentage()
? (isset($this->minValue) ? $this->format($this->minValue) : $this->rawMinValue)
: null,
'max' => (string)!$this->isPercentage()
? (isset($this->maxValue) ? $this->format($this->maxValue) : $this->rawMaxValue)
: null,
'warn' => $this->format($this->warningThreshold),
'crit' => $this->format($this->criticalThreshold)
];
}

/**
Expand Down Expand Up @@ -643,4 +680,18 @@ public function worseThan(PerfData $rhs): bool

return false;
}

/**
* Returns whether the performance data can be evaluated
*
* @return bool
*/
public function isValid(): bool
{
return ! isset($this->rawValue)
&& ! isset($this->rawMinValue)
&& ! isset($this->rawMaxValue)
&& $this->criticalThreshold->isValid()
&& $this->warningThreshold->isValid();
}
}
33 changes: 33 additions & 0 deletions library/Icingadb/Util/ThresholdRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class ThresholdRange
*/
protected $raw;

/**
* Whether the threshold range is valid
*
* @var bool
*/
protected $isValid = true;

/**
* Create a new instance based on a threshold range conforming to <https://nagios-plugins.org/doc/guidelines.html>
*
Expand All @@ -61,6 +68,12 @@ public static function fromString(string $rawRange): self

if (strpos($rawRange, ':') === false) {
$min = 0.0;
$max = trim($rawRange);
if (! is_numeric($max)) {
$range->isValid = false;
return $range;
}

$max = floatval(trim($rawRange));
} else {
list($min, $max) = explode(':', $rawRange, 2);
Expand All @@ -75,9 +88,19 @@ public static function fromString(string $rawRange): self
$min = null;
break;
default:
if (! is_numeric($min)) {
$range->isValid = false;
return $range;
}

$min = floatval($min);
}

if (! empty($max) && ! is_numeric($max)) {
$range->isValid = false;
return $range;
}

$max = empty($max) ? null : floatval($max);
}

Expand Down Expand Up @@ -168,6 +191,16 @@ public function contains(float $value): bool
));
}

/**
* Return whether the threshold range is valid
*
* @return bool
*/
public function isValid()
{
return $this->isValid;
}

/**
* Return the textual representation of $this, suitable for fromString()
*
Expand Down
19 changes: 18 additions & 1 deletion library/Icingadb/Widget/Detail/PerfDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
use ipl\Html\HtmlString;
use ipl\Html\Table;
use ipl\Html\Text;
use ipl\I18n\Translation;
use ipl\Web\Widget\Icon;

class PerfDataTable extends Table
{
use Translation;

/** @var bool Whether the table contains a sparkline column */
protected $containsSparkline = false;

Expand Down Expand Up @@ -64,7 +68,7 @@ public function assemble()
]
);
foreach ($pieChartData as $perfdata) {
if ($perfdata->isVisualizable()) {
if ($perfdata->isVisualizable() || ! $perfdata->isValid()) {
$columns[''] = '';
$this->containsSparkline = true;
}
Expand Down Expand Up @@ -108,6 +112,19 @@ public function assemble()
HtmlString::create($perfdata->asInlinePie($this->color)->render()),
[ 'class' => 'sparkline-col']
);
} elseif (! $perfdata->isValid()) {
$cols[] = Table::td(
new Icon(
'triangle-exclamation',
[
'title' => $this->translate(
'Evaluation failed. Performance data is invalid.'
),
'class' => ['invalid-perfdata']
]
),
['class' => 'sparkline-col']
);
} else {
$cols[] = Table::td('');
}
Expand Down
5 changes: 5 additions & 0 deletions public/css/widget/performance-data-table.less
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
min-width: 1.75em;
width: 1.75em;
padding: 2/12em 0;
.invalid-perfdata {
font-size: 1.25em;
vertical-align: text-bottom;
color: @color-warning;
}
}

.inline-pie > svg {
Expand Down

0 comments on commit cf78825

Please sign in to comment.