2013-06-27 10:14:41 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-06-27 10:14:41 +02:00
|
|
|
|
2014-05-05 16:16:45 +02:00
|
|
|
use Icinga\Module\Monitoring\Plugin\Perfdata;
|
|
|
|
use Icinga\Module\Monitoring\Plugin\PerfdataSet;
|
2015-06-02 17:09:40 +02:00
|
|
|
use Icinga\Util\String;
|
2013-06-27 10:14:41 +02:00
|
|
|
|
|
|
|
class Zend_View_Helper_Perfdata extends Zend_View_Helper_Abstract
|
2015-03-30 12:10:58 +02:00
|
|
|
{
|
2014-12-23 15:45:45 +01:00
|
|
|
/**
|
|
|
|
* Display the given perfdata string to the user
|
|
|
|
*
|
2015-03-30 12:10:58 +02:00
|
|
|
* @param string $perfdataStr The perfdata string
|
|
|
|
* @param bool $compact Whether to display the perfdata in compact mode
|
|
|
|
* @param int $limit Max labels to show; 0 for no limit
|
|
|
|
* @param string $color The color indicating the perfdata state
|
2014-12-23 15:45:45 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-02-03 16:45:01 +01:00
|
|
|
public function perfdata($perfdataStr, $compact = false, $limit = 0, $color = Perfdata::PERFDATA_OK)
|
2013-06-27 10:14:41 +02:00
|
|
|
{
|
2014-12-23 15:45:45 +01:00
|
|
|
$pieChartData = PerfdataSet::fromString($perfdataStr)->asArray();
|
2015-06-16 18:36:12 +02:00
|
|
|
uasort(
|
|
|
|
$pieChartData,
|
2015-07-06 15:47:04 +02:00
|
|
|
function ($a, $b) {
|
2015-06-16 18:36:12 +02:00
|
|
|
return $a->worseThan($b) ? -1 : ($b->worseThan($a) ? 1 : 0);
|
2015-05-29 18:39:16 +02:00
|
|
|
}
|
2015-06-16 18:36:12 +02:00
|
|
|
);
|
2015-02-03 16:45:01 +01:00
|
|
|
$results = array();
|
2015-06-02 16:06:26 +02:00
|
|
|
$keys = array('', 'label', 'value', 'min', 'max', 'warn', 'crit');
|
2015-06-02 15:20:27 +02:00
|
|
|
$columns = array();
|
|
|
|
$labels = array_combine(
|
2015-06-02 16:06:26 +02:00
|
|
|
$keys,
|
2015-06-02 15:20:27 +02:00
|
|
|
array(
|
|
|
|
'',
|
|
|
|
$this->view->translate('Label'),
|
|
|
|
$this->view->translate('Value'),
|
|
|
|
$this->view->translate('Min'),
|
|
|
|
$this->view->translate('Max'),
|
|
|
|
$this->view->translate('Warning'),
|
|
|
|
$this->view->translate('Critical')
|
|
|
|
)
|
2014-12-30 16:31:37 +01:00
|
|
|
);
|
2014-12-23 15:45:45 +01:00
|
|
|
foreach ($pieChartData as $perfdata) {
|
2015-06-02 15:20:27 +02:00
|
|
|
if ($perfdata->isVisualizable()) {
|
|
|
|
$columns[''] = '';
|
|
|
|
}
|
|
|
|
foreach ($perfdata->toArray() as $column => $value) {
|
2015-06-02 16:06:26 +02:00
|
|
|
if (empty($value) ||
|
|
|
|
$column === 'min' && floatval($value) === 0.0 ||
|
|
|
|
$column === 'max' && $perfdata->isPercentage() && floatval($value) === 100) {
|
|
|
|
continue;
|
2015-06-02 15:20:27 +02:00
|
|
|
}
|
2015-06-02 16:06:26 +02:00
|
|
|
$columns[$column] = $labels[$column];
|
|
|
|
}
|
|
|
|
}
|
2015-06-02 17:09:40 +02:00
|
|
|
// restore original column array sorting
|
2015-06-02 16:06:26 +02:00
|
|
|
$headers = array();
|
2015-06-02 17:50:31 +02:00
|
|
|
foreach ($keys as $column) {
|
2015-06-02 16:06:26 +02:00
|
|
|
if (isset($columns[$column])) {
|
|
|
|
$headers[$column] = $labels[$column];
|
2015-06-02 15:20:27 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-02 16:06:26 +02:00
|
|
|
$table = array('<td><b>' . implode('</b></td><td><b>', $headers) . '<b></td>');
|
2015-06-02 15:20:27 +02:00
|
|
|
foreach ($pieChartData as $perfdata) {
|
2014-12-30 16:31:37 +01:00
|
|
|
if ($compact && $perfdata->isVisualizable()) {
|
2015-02-03 16:45:01 +01:00
|
|
|
$results[] = $perfdata->asInlinePie($color)->render();
|
2014-12-23 15:45:45 +01:00
|
|
|
} else {
|
2015-06-02 15:20:27 +02:00
|
|
|
$data = array();
|
2014-12-30 16:31:37 +01:00
|
|
|
if ($perfdata->isVisualizable()) {
|
2015-06-02 15:20:27 +02:00
|
|
|
$data []= $perfdata->asInlinePie($color)->render() . ' ';
|
2015-06-02 17:50:31 +02:00
|
|
|
} elseif (isset($columns[''])) {
|
2015-06-02 15:20:27 +02:00
|
|
|
$data []= '';
|
2014-12-30 16:31:37 +01:00
|
|
|
}
|
2015-03-30 12:12:58 +02:00
|
|
|
if (! $compact) {
|
2015-06-02 15:20:27 +02:00
|
|
|
foreach ($perfdata->toArray() as $column => $value) {
|
|
|
|
if (! isset($columns[$column])) {
|
|
|
|
continue;
|
2014-12-30 16:31:37 +01:00
|
|
|
}
|
2015-06-02 17:09:40 +02:00
|
|
|
$text = $this->view->escape(empty($value) ? '-' : $value);
|
|
|
|
$data []= sprintf(
|
|
|
|
'<span title="%s">%s</span>',
|
|
|
|
$text,
|
|
|
|
String::ellipsisCenter($text, 24)
|
|
|
|
);
|
2014-12-30 16:31:37 +01:00
|
|
|
}
|
|
|
|
}
|
2015-06-02 15:20:27 +02:00
|
|
|
$table []= '<tr><td>' . implode('</td><td>', $data) . '</td></tr>';
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
|
|
|
}
|
2015-02-03 16:45:01 +01:00
|
|
|
if ($limit > 0) {
|
2015-08-11 15:28:37 +02:00
|
|
|
$count = $compact ? count($results) : count($table);
|
2015-02-03 17:33:30 +01:00
|
|
|
if ($count > $limit) {
|
2015-08-11 15:28:37 +02:00
|
|
|
if ($compact) {
|
|
|
|
$results = array_slice($results, 0, $limit);
|
|
|
|
$title = sprintf($this->view->translate('%d more ...'), $count - $limit);
|
|
|
|
$results[] = '<span title="' . $title . '">...</span>';
|
|
|
|
} else {
|
|
|
|
$table = array_slice($table, 0, $limit);
|
|
|
|
}
|
2015-02-03 17:33:30 +01:00
|
|
|
}
|
2015-02-03 16:45:01 +01:00
|
|
|
}
|
2014-07-14 13:48:30 +02:00
|
|
|
if ($compact) {
|
2015-02-03 16:45:01 +01:00
|
|
|
return join('', $results);
|
2014-05-05 16:16:45 +02:00
|
|
|
} else {
|
2015-06-02 17:30:24 +02:00
|
|
|
if (empty($table)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return sprintf(
|
|
|
|
'<table class="perfdata %s">%s</table>',
|
|
|
|
isset($columns['']) ? 'perfdata-piecharts' : '',
|
|
|
|
implode("\n", $table)
|
|
|
|
);
|
2013-07-22 17:10:43 +02:00
|
|
|
}
|
2014-05-05 16:16:45 +02:00
|
|
|
}
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|