diff --git a/library/Icinga/Util/Format.php b/library/Icinga/Util/Format.php index 8c55cbafa..a7c0ee0e3 100644 --- a/library/Icinga/Util/Format.php +++ b/library/Icinga/Util/Format.php @@ -50,6 +50,9 @@ class Format ); 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() { 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) { if ($duration === null || $duration === false) { @@ -148,7 +165,7 @@ class Format '%s%0.2f %s', $sign, $result, - $units[$pow] + $units[abs($pow)] ); } diff --git a/modules/monitoring/application/clicommands/ListCommand.php b/modules/monitoring/application/clicommands/ListCommand.php index a07601871..4ddf2e781 100644 --- a/modules/monitoring/application/clicommands/ListCommand.php +++ b/modules/monitoring/application/clicommands/ListCommand.php @@ -263,9 +263,9 @@ class ListCommand extends Command try { $pset = PerfdataSet::fromString($row->service_perfdata); $perfs = array(); - foreach ($pset->getAll() as $perfName => $p) { + foreach ($pset as $perfName => $p) { if ($percent = $p->getPercentage()) { - if ($percent < 0 || $percent > 300) { + if ($percent < 0 || $percent > 100) { continue; } $perfs[] = ' ' diff --git a/modules/monitoring/application/views/helpers/Perfdata.php b/modules/monitoring/application/views/helpers/Perfdata.php index 13997de18..49e88cae6 100644 --- a/modules/monitoring/application/views/helpers/Perfdata.php +++ b/modules/monitoring/application/views/helpers/Perfdata.php @@ -1,90 +1,82 @@ 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 = ''; - if ($compact === true) { - $compact = 5; - } - if ($compact && count($parts) > $compact) { - $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 { + $table = array(); + $pset = array_slice(PerfdataSet::fromString($perfdataStr)->asArray(), 0, ($compact ? 5 : null)); + foreach ($pset as $label => $perfdata) { + if (!$perfdata->isPercentage() && $perfdata->getMaximumValue() === null) { continue; } - if ($unit == 'c') continue; // Counter pie graphs are not useful - if ($compact && $val < 0.0001) continue; - 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)); + + $pieChart = new InlinePie($this->calculatePieChartData($perfdata)); if ($compact) { - $inlinePie->setTitle(htmlspecialchars($name) . ': ' . htmlspecialchars($ps[$name]->getFormattedValue())); - $inlinePie->setStyle('float: right;'); - $result .= $inlinePie->render(); + $pieChart->setTitle( + htmlspecialchars($label) . ': ' . htmlspecialchars($this->formatPerfdataValue($perfdata)) + ); + $pieChart->setStyle('float: right;'); + $result .= $pieChart->render(); } else { - $inlinePie->setTitle(htmlspecialchars($name)); - $inlinePie->setStyle('float: left; margin: 0.2em 0.5em 0.2em 0;'); - $table[] = '' . $inlinePie->render() - . htmlspecialchars($name) + $pieChart->setTitle(htmlspecialchars($label)); + $pieChart->setStyle('float: left; margin: 0.2em 0.5em 0.2em 0;'); + $table[] = '' . $pieChart->render() + . htmlspecialchars($label) . '' - . htmlspecialchars($ps[$name]->getFormattedValue()) . + . htmlspecialchars($this->formatPerfdataValue($perfdata)) . ''; } } - if ($result == '' && ! $compact) { - $result = $perfdata; + + // TODO: What if we have both? And should we trust sprintf-style placeholders in perfdata titles? + if (empty($table)) { + return $compact ? $result : $perfdataStr; + } else { + return '' . implode("\n", $table) . '
'; } - if (! empty($table)) { - // TODO: What if we have both? And should we trust sprintf-style placeholders in perfdata titles? - $result = '' . implode("\n", $table) . '
'; + } + + 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 $result; + 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(); } }