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-12-15 13:56:19 +01:00
|
|
|
public function perfdata($perfdataStr, $compact = false)
|
2013-06-27 10:14:41 +02:00
|
|
|
{
|
2014-07-14 13:48:30 +02:00
|
|
|
$pset = PerfdataSet::fromString($perfdataStr)->asArray();
|
|
|
|
$onlyPieChartData = array_filter($pset, function ($e) { return $e->getPercentage() > 0; });
|
|
|
|
if ($compact) {
|
|
|
|
$onlyPieChartData = array_slice($onlyPieChartData, 0, 5);
|
|
|
|
} else {
|
|
|
|
$nonPieChartData = array_filter($pset, function ($e) { return $e->getPercentage() == 0; });
|
|
|
|
}
|
|
|
|
|
2013-06-27 10:14:41 +02:00
|
|
|
$result = '';
|
2014-05-05 16:16:45 +02:00
|
|
|
$table = array();
|
2014-07-14 13:48:30 +02:00
|
|
|
foreach ($onlyPieChartData as $perfdata) {
|
2014-07-11 16:31:00 +02:00
|
|
|
$pieChart = $this->createInlinePie($perfdata);
|
2013-06-27 10:14:41 +02:00
|
|
|
if ($compact) {
|
2014-12-15 13:56:19 +01:00
|
|
|
$result .= $pieChart->render();
|
2013-06-27 10:14:41 +02:00
|
|
|
} else {
|
2014-06-18 13:53:38 +02:00
|
|
|
if (! $perfdata->isPercentage()) {
|
2014-07-14 13:48:30 +02:00
|
|
|
// TODO: Should we trust sprintf-style placeholders in perfdata titles?
|
2014-06-18 13:53:38 +02:00
|
|
|
$pieChart->setTooltipFormat('{{label}}: {{formatted}} ({{percent}}%)');
|
|
|
|
}
|
2014-08-28 18:27:30 +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-23 12:29:27 +02:00
|
|
|
. htmlspecialchars($perfdata->getLabel())
|
2014-05-07 17:03:27 +02:00
|
|
|
. '</th><td> '
|
2014-07-23 12:29:27 +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
|
|
|
|
2014-07-14 13:48:30 +02:00
|
|
|
if ($compact) {
|
|
|
|
return $result;
|
2014-05-05 16:16:45 +02:00
|
|
|
} else {
|
2014-07-14 13:48:30 +02:00
|
|
|
$pieCharts = empty($table) ? '' : '<table class="perfdata">' . implode("\n", $table) . '</table>';
|
|
|
|
return $pieCharts . "\n" . implode("<br>\n", $nonPieChartData);
|
2014-05-05 16:16:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-12-18 16:44:55 +01:00
|
|
|
$pieChart = new InlinePie($this->calculatePieChartData($perfdata),
|
|
|
|
$perfdata->getLabel() . ' ' . (int)$perfdata->getPercentage() . '%');
|
|
|
|
$pieChart->setDisableTooltip();
|
|
|
|
if (Zend_Controller_Front::getInstance()->getRequest()->isXmlHttpRequest()) {
|
|
|
|
$pieChart->disableNoScript();
|
|
|
|
}
|
2014-06-18 17:37:01 +02:00
|
|
|
|
2014-06-18 13:53:38 +02:00
|
|
|
if ($perfdata->isBytes()) {
|
|
|
|
$pieChart->setNumberFormat(InlinePie::NUMBER_FORMAT_BYTES);
|
|
|
|
} else if ($perfdata->isSeconds()) {
|
|
|
|
$pieChart->setNumberFormat(InlinePie::NUMBER_FORMAT_TIME);
|
|
|
|
} else {
|
|
|
|
$pieChart->setNumberFormat(InlinePie::NUMBER_FORMAT_RATIO);
|
|
|
|
}
|
|
|
|
return $pieChart;
|
|
|
|
}
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|