mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-31 01:34:09 +02:00
parent
3d88e720c4
commit
0627d954ac
@ -50,6 +50,9 @@ class Format
|
|||||||
);
|
);
|
||||||
protected static $byteBase = array(1024, 1000);
|
protected static $byteBase = array(1024, 1000);
|
||||||
|
|
||||||
|
protected static $secondPrefix = array('s', 'ms', 'µs', 'ns', 'ps', 'fs', 'as');
|
||||||
|
protected static $secondBase = 1000;
|
||||||
|
|
||||||
public static function getInstance()
|
public static function getInstance()
|
||||||
{
|
{
|
||||||
if (self::$instance === null) {
|
if (self::$instance === null) {
|
||||||
@ -76,6 +79,20 @@ class Format
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function seconds($value)
|
||||||
|
{
|
||||||
|
if ($value < 60) {
|
||||||
|
return self::formatForUnits($value, self::$secondPrefix, self::$secondBase);
|
||||||
|
} elseif ($value < 3600) {
|
||||||
|
return sprintf('0.2f m', $value / 60);
|
||||||
|
} elseif ($value < 86400) {
|
||||||
|
return sprintf('0.2f h', $value / 3600);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Do we need weeks, months and years?
|
||||||
|
return sprintf('0.2f d', $value / 86400);
|
||||||
|
}
|
||||||
|
|
||||||
public static function duration($duration)
|
public static function duration($duration)
|
||||||
{
|
{
|
||||||
if ($duration === null || $duration === false) {
|
if ($duration === null || $duration === false) {
|
||||||
@ -148,7 +165,7 @@ class Format
|
|||||||
'%s%0.2f %s',
|
'%s%0.2f %s',
|
||||||
$sign,
|
$sign,
|
||||||
$result,
|
$result,
|
||||||
$units[$pow]
|
$units[abs($pow)]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -263,9 +263,9 @@ class ListCommand extends Command
|
|||||||
try {
|
try {
|
||||||
$pset = PerfdataSet::fromString($row->service_perfdata);
|
$pset = PerfdataSet::fromString($row->service_perfdata);
|
||||||
$perfs = array();
|
$perfs = array();
|
||||||
foreach ($pset->getAll() as $perfName => $p) {
|
foreach ($pset as $perfName => $p) {
|
||||||
if ($percent = $p->getPercentage()) {
|
if ($percent = $p->getPercentage()) {
|
||||||
if ($percent < 0 || $percent > 300) {
|
if ($percent < 0 || $percent > 100) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$perfs[] = ' '
|
$perfs[] = ' '
|
||||||
|
@ -1,90 +1,82 @@
|
|||||||
<?php
|
<?php
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
|
||||||
use Icinga\Module\Monitoring\Plugin\PerfdataSet;
|
use Icinga\Util\Format;
|
||||||
use Icinga\Web\Widget\Chart\InlinePie;
|
use Icinga\Web\Widget\Chart\InlinePie;
|
||||||
|
use Icinga\Module\Monitoring\Plugin\Perfdata;
|
||||||
|
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
|
||||||
{
|
{
|
||||||
public function perfdata($perfdata, $compact = false, $float = 'right')
|
public function perfdata($perfdataStr, $compact = false)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (empty($perfdata)) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
if ($float) {
|
|
||||||
$float = ' float: ' . $float . ';';
|
|
||||||
} else {
|
|
||||||
$float = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
$pset = PerfdataSet::fromString($perfdata);
|
|
||||||
$ps = $pset->getAll();
|
|
||||||
$perfdata = preg_replace_callback('~\'([^\']+)\'~', function($match) { return str_replace(' ', '\'', $match[1]); }, $perfdata);
|
|
||||||
$parts = preg_split('~\s+~', $perfdata, -1, PREG_SPLIT_NO_EMPTY);
|
|
||||||
|
|
||||||
$table = array();
|
|
||||||
$result = '';
|
$result = '';
|
||||||
if ($compact === true) {
|
$table = array();
|
||||||
$compact = 5;
|
$pset = array_slice(PerfdataSet::fromString($perfdataStr)->asArray(), 0, ($compact ? 5 : null));
|
||||||
}
|
foreach ($pset as $label => $perfdata) {
|
||||||
if ($compact && count($parts) > $compact) {
|
if (!$perfdata->isPercentage() && $perfdata->getMaximumValue() === null) {
|
||||||
$parts = array_slice($parts, 0, $compact);
|
|
||||||
}
|
|
||||||
foreach ($parts as $part) {
|
|
||||||
if (strpos($part, '=') === false) continue;
|
|
||||||
list($name, $vals) = preg_split('~=~', $part, 2);
|
|
||||||
$name = str_replace("'", ' ', $name);
|
|
||||||
$parts = preg_split('~;~', $vals, 5);
|
|
||||||
while (count($parts) < 5) $parts[] = null;
|
|
||||||
list($val, $warn, $crit, $min, $max) = $parts;
|
|
||||||
|
|
||||||
$unit = '';
|
|
||||||
if (preg_match('~^([\d+\.]+)([^\d]+)$~', $val, $m)) {
|
|
||||||
$unit = $m[2];
|
|
||||||
$val = $m[1];
|
|
||||||
} else {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ($unit == 'c') continue; // Counter pie graphs are not useful
|
|
||||||
if ($compact && $val < 0.0001) continue;
|
$pieChart = new InlinePie($this->calculatePieChartData($perfdata));
|
||||||
if ($unit == '%') {
|
|
||||||
if (! $min ) $min = 0;
|
|
||||||
if (! $max) $max = 100;
|
|
||||||
} else {
|
|
||||||
if (! $max && $crit > 0) $max = $crit;
|
|
||||||
//return '';
|
|
||||||
}
|
|
||||||
if (! $max) continue;
|
|
||||||
$green = 0;
|
|
||||||
$orange = 0;
|
|
||||||
$red = 0;
|
|
||||||
$gray = $max - $val;
|
|
||||||
if ($val < $warn) $green = $val;
|
|
||||||
elseif ($val < $crit) $orange = $val;
|
|
||||||
else $red = $val;
|
|
||||||
$inlinePie = new InlinePie(array($green, $orange, $red, $gray));
|
|
||||||
if ($compact) {
|
if ($compact) {
|
||||||
$inlinePie->setTitle(htmlspecialchars($name) . ': ' . htmlspecialchars($ps[$name]->getFormattedValue()));
|
$pieChart->setTitle(
|
||||||
$inlinePie->setStyle('float: right;');
|
htmlspecialchars($label) . ': ' . htmlspecialchars($this->formatPerfdataValue($perfdata))
|
||||||
$result .= $inlinePie->render();
|
);
|
||||||
|
$pieChart->setStyle('float: right;');
|
||||||
|
$result .= $pieChart->render();
|
||||||
} else {
|
} else {
|
||||||
$inlinePie->setTitle(htmlspecialchars($name));
|
$pieChart->setTitle(htmlspecialchars($label));
|
||||||
$inlinePie->setStyle('float: left; margin: 0.2em 0.5em 0.2em 0;');
|
$pieChart->setStyle('float: left; margin: 0.2em 0.5em 0.2em 0;');
|
||||||
$table[] = '<tr><th>' . $inlinePie->render()
|
$table[] = '<tr><th>' . $pieChart->render()
|
||||||
. htmlspecialchars($name)
|
. htmlspecialchars($label)
|
||||||
. '</th><td>'
|
. '</th><td>'
|
||||||
. htmlspecialchars($ps[$name]->getFormattedValue()) .
|
. htmlspecialchars($this->formatPerfdataValue($perfdata)) .
|
||||||
'</td></tr>';
|
'</td></tr>';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($result == '' && ! $compact) {
|
|
||||||
$result = $perfdata;
|
|
||||||
}
|
|
||||||
if (! empty($table)) {
|
|
||||||
// TODO: What if we have both? And should we trust sprintf-style placeholders in perfdata titles?
|
// TODO: What if we have both? And should we trust sprintf-style placeholders in perfdata titles?
|
||||||
$result = '<table class="perfdata">' . implode("\n", $table) . '</table>';
|
if (empty($table)) {
|
||||||
|
return $compact ? $result : $perfdataStr;
|
||||||
|
} else {
|
||||||
|
return '<table class="perfdata">' . implode("\n", $table) . '</table>';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
protected function calculatePieChartData(Perfdata $perfdata)
|
||||||
|
{
|
||||||
|
$rawValue = $perfdata->getValue();
|
||||||
|
$minValue = $perfdata->getMinimumValue() !== null ? $perfdata->getMinimumValue() : 0;
|
||||||
|
$maxValue = $perfdata->getMaximumValue();
|
||||||
|
$usedValue = ($rawValue - $minValue);
|
||||||
|
$unusedValue = ($maxValue - $minValue) - $usedValue;
|
||||||
|
|
||||||
|
$gray = $unusedValue;
|
||||||
|
$green = $orange = $red = 0;
|
||||||
|
// TODO(#6122): Add proper treshold parsing.
|
||||||
|
if ($perfdata->getCriticalTreshold() && $perfdata->getValue() > $perfdata->getCriticalTreshold()) {
|
||||||
|
$red = $usedValue;
|
||||||
|
} elseif ($perfdata->getWarningTreshold() && $perfdata->getValue() > $perfdata->getWarningTreshold()) {
|
||||||
|
$orange = $usedValue;
|
||||||
|
} else {
|
||||||
|
$green = $usedValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array($green, $orange, $red, $gray);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function formatPerfdataValue(Perfdata $perfdata)
|
||||||
|
{
|
||||||
|
if ($perfdata->isBytes()) {
|
||||||
|
return Format::bytes($perfdata->getValue());
|
||||||
|
} elseif ($perfdata->isSeconds()) {
|
||||||
|
return Format::seconds($perfdata->getValue());
|
||||||
|
} elseif ($perfdata->isPercentage()) {
|
||||||
|
return $perfdata->getValue() . '%';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $perfdata->getValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user