2013-06-27 10:14:41 +02:00
|
|
|
<?php
|
2014-05-05 16:16:45 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-27 10:14:41 +02:00
|
|
|
|
2014-05-05 16:16:45 +02:00
|
|
|
use Icinga\Util\Format;
|
2014-03-26 14:56:35 +01:00
|
|
|
use Icinga\Web\Widget\Chart\InlinePie;
|
2014-05-05 16:16:45 +02:00
|
|
|
use Icinga\Module\Monitoring\Plugin\Perfdata;
|
|
|
|
use Icinga\Module\Monitoring\Plugin\PerfdataSet;
|
2013-06-27 10:14:41 +02:00
|
|
|
|
|
|
|
class Zend_View_Helper_Perfdata extends Zend_View_Helper_Abstract
|
|
|
|
{
|
2014-05-07 17:03:27 +02:00
|
|
|
public function perfdata($perfdataStr, $compact = false, $float = false)
|
2013-06-27 10:14:41 +02:00
|
|
|
{
|
|
|
|
$result = '';
|
2014-05-05 16:16:45 +02:00
|
|
|
$table = array();
|
|
|
|
$pset = array_slice(PerfdataSet::fromString($perfdataStr)->asArray(), 0, ($compact ? 5 : null));
|
2014-07-11 16:31:00 +02:00
|
|
|
foreach ($pset as $perfdata) {
|
2014-07-11 16:32:05 +02:00
|
|
|
if ($perfdata->getPercentage() == 0) {
|
2013-06-27 10:14:41 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-07-11 16:31:00 +02:00
|
|
|
$pieChart = $this->createInlinePie($perfdata);
|
2013-06-27 10:14:41 +02:00
|
|
|
if ($compact) {
|
2014-06-18 17:37:01 +02:00
|
|
|
if (! $float) {
|
2014-05-07 17:03:27 +02:00
|
|
|
$result .= $pieChart->render();
|
|
|
|
} else {
|
|
|
|
$result .= '<div style="float: right;">' . $pieChart->render() . '</div>';
|
|
|
|
}
|
2013-06-27 10:14:41 +02:00
|
|
|
} else {
|
2014-06-18 13:53:38 +02:00
|
|
|
if (! $perfdata->isPercentage()) {
|
|
|
|
$pieChart->setTooltipFormat('{{label}}: {{formatted}} ({{percent}}%)');
|
|
|
|
}
|
2014-07-10 14:54:12 +02:00
|
|
|
$pieChart->setStyle('margin: 0.2em 0.5em 0.2em 0.5em;');
|
2014-05-05 16:16:45 +02:00
|
|
|
$table[] = '<tr><th>' . $pieChart->render()
|
2014-07-11 16:31:00 +02:00
|
|
|
. htmlspecialchars($perfdata->getLabel())
|
2014-05-07 17:03:27 +02:00
|
|
|
. '</th><td> '
|
2014-05-05 16:16:45 +02:00
|
|
|
. htmlspecialchars($this->formatPerfdataValue($perfdata)) .
|
2014-05-07 17:03:27 +02:00
|
|
|
' </td></tr>';
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-05 16:16:45 +02:00
|
|
|
|
|
|
|
// 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 '<table class="perfdata">' . implode("\n", $table) . '</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.
|
2014-06-25 15:02:04 +02:00
|
|
|
if ($perfdata->getCriticalThreshold() && $perfdata->getValue() > $perfdata->getCriticalThreshold()) {
|
2014-05-05 16:16:45 +02:00
|
|
|
$red = $usedValue;
|
2014-06-25 15:02:04 +02:00
|
|
|
} elseif ($perfdata->getWarningThreshold() && $perfdata->getValue() > $perfdata->getWarningThreshold()) {
|
2014-05-05 16:16:45 +02:00
|
|
|
$orange = $usedValue;
|
|
|
|
} else {
|
|
|
|
$green = $usedValue;
|
2013-07-22 17:10:43 +02:00
|
|
|
}
|
2014-05-05 16:16:45 +02:00
|
|
|
|
|
|
|
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() . '%';
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
2013-07-22 17:10:43 +02:00
|
|
|
|
2014-05-05 16:16:45 +02:00
|
|
|
return $perfdata->getValue();
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
2014-06-18 13:53:38 +02:00
|
|
|
|
2014-07-11 16:31:00 +02:00
|
|
|
protected function createInlinePie(Perfdata $perfdata)
|
2014-06-18 13:53:38 +02:00
|
|
|
{
|
2014-07-11 16:31:00 +02:00
|
|
|
$pieChart = new InlinePie($this->calculatePieChartData($perfdata), $perfdata->getLabel());
|
|
|
|
$pieChart->setLabel(htmlspecialchars($perfdata->getLabel()));
|
2014-06-18 17:37:01 +02:00
|
|
|
$pieChart->setHideEmptyLabel();
|
|
|
|
|
2014-06-18 14:33:03 +02:00
|
|
|
//$pieChart->setHeight(32)->setWidth(32);
|
2014-06-18 13:53:38 +02:00
|
|
|
if ($perfdata->isBytes()) {
|
2014-06-18 17:37:01 +02:00
|
|
|
$pieChart->setTooltipFormat('{{label}}: {{formatted}} ({{percent}}%)');
|
2014-06-18 13:53:38 +02:00
|
|
|
$pieChart->setNumberFormat(InlinePie::NUMBER_FORMAT_BYTES);
|
|
|
|
} else if ($perfdata->isSeconds()) {
|
2014-06-18 17:37:01 +02:00
|
|
|
$pieChart->setTooltipFormat('{{label}}: {{formatted}} ({{percent}}%)');
|
2014-06-18 13:53:38 +02:00
|
|
|
$pieChart->setNumberFormat(InlinePie::NUMBER_FORMAT_TIME);
|
|
|
|
} else {
|
|
|
|
$pieChart->setTooltipFormat('{{label}}: {{formatted}}%');
|
|
|
|
$pieChart->setNumberFormat(InlinePie::NUMBER_FORMAT_RATIO);
|
2014-06-18 17:37:01 +02:00
|
|
|
$pieChart->setHideEmptyLabel();
|
2014-06-18 13:53:38 +02:00
|
|
|
}
|
|
|
|
return $pieChart;
|
|
|
|
}
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|