2013-06-27 10:14:41 +02:00
|
|
|
<?php
|
|
|
|
|
2014-03-04 13:54:23 +01:00
|
|
|
use Icinga\Module\Monitoring\Plugin\PerfdataSet;
|
2014-03-26 14:56:35 +01:00
|
|
|
use Icinga\Web\Widget\Chart\InlinePie;
|
2013-06-27 10:14:41 +02:00
|
|
|
|
|
|
|
class Zend_View_Helper_Perfdata extends Zend_View_Helper_Abstract
|
|
|
|
{
|
|
|
|
public function perfdata($perfdata, $compact = false, $float = 'right')
|
|
|
|
{
|
2013-07-22 17:10:43 +02:00
|
|
|
|
2013-06-27 10:14:41 +02:00
|
|
|
if (empty($perfdata)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
if ($float) {
|
2014-03-26 14:56:35 +01:00
|
|
|
$float = ' float: ' . $float . ';';
|
2013-06-27 10:14:41 +02:00
|
|
|
} else {
|
|
|
|
$float = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$pset = PerfdataSet::fromString($perfdata);
|
|
|
|
$ps = $pset->getAll();
|
2014-04-29 12:00:05 +02:00
|
|
|
$perfdata = preg_replace_callback('~\'([^\']+)\'~', function($match) { return str_replace(' ', '\'', $match[1]); }, $perfdata);
|
2013-06-27 10:14:41 +02:00
|
|
|
$parts = preg_split('~\s+~', $perfdata, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
|
|
2014-03-08 19:39:28 +01:00
|
|
|
$table = array();
|
2013-06-27 10:14:41 +02:00
|
|
|
$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 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($unit == 'c') continue; // Counter pie graphs are not useful
|
2014-02-21 11:28:25 +01:00
|
|
|
if ($compact && $val < 0.0001) continue;
|
2013-06-27 10:14:41 +02:00
|
|
|
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;
|
2014-03-26 14:56:35 +01:00
|
|
|
$inlinePie = new InlinePie(array($green, $orange, $red, $gray));
|
2013-06-27 10:14:41 +02:00
|
|
|
if ($compact) {
|
2014-03-26 14:56:35 +01:00
|
|
|
$inlinePie->setTitle(htmlspecialchars($name) . ': ' . htmlspecialchars($ps[$name]->getFormattedValue()));
|
|
|
|
$inlinePie->setStyle('float: right;');
|
|
|
|
$result .= $inlinePie->render();
|
2013-06-27 10:14:41 +02:00
|
|
|
} else {
|
2014-03-26 14:56:35 +01:00
|
|
|
$inlinePie->setTitle(htmlspecialchars($name));
|
|
|
|
$inlinePie->setStyle('float: left; margin: 0.2em 0.5em 0.2em 0;');
|
|
|
|
$table[] = '<tr><th>' . $inlinePie->render()
|
|
|
|
. htmlspecialchars($name)
|
|
|
|
. '</th><td>'
|
|
|
|
. htmlspecialchars($ps[$name]->getFormattedValue()) .
|
|
|
|
'</td></tr>';
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-21 13:58:02 +01:00
|
|
|
if ($result == '' && ! $compact) {
|
2013-07-22 17:10:43 +02:00
|
|
|
$result = $perfdata;
|
|
|
|
}
|
2014-03-08 19:39:28 +01:00
|
|
|
if (! empty($table)) {
|
2014-03-08 19:42:03 +01:00
|
|
|
// TODO: What if we have both? And should we trust sprintf-style placeholders in perfdata titles?
|
|
|
|
$result = '<table class="perfdata">' . implode("\n", $table) . '</table>';
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
2013-07-22 17:10:43 +02:00
|
|
|
|
2013-06-27 10:14:41 +02:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|