summaryrefslogtreecommitdiffstats
path: root/modules/monitoring/application/views/helpers/Perfdata.php
blob: e7bc72b3885bb4b13b11cdb16457a129a9c61da6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

use Icinga\Module\Monitoring\Plugin\Perfdata;
use Icinga\Module\Monitoring\Plugin\PerfdataSet;
use Icinga\Util\StringHelper;

class Zend_View_Helper_Perfdata extends Zend_View_Helper_Abstract
{
    /**
     * Display the given perfdata string to the user
     *
     * @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
     *
     * @return string
     */
    public function perfdata($perfdataStr, $compact = false, $limit = 0, $color = Perfdata::PERFDATA_OK)
    {
        $pieChartData = PerfdataSet::fromString($perfdataStr)->asArray();
        uasort(
            $pieChartData,
            function ($a, $b) {
                return $a->worseThan($b) ? -1 : ($b->worseThan($a) ? 1 : 0);
            }
        );
        $results = array();
        $keys = array('', 'label', 'value', 'min', 'max', 'warn', 'crit');
        $columns = array();
        $labels = array_combine(
            $keys,
            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')
            )
        );
        foreach ($pieChartData as $perfdata) {
            if ($perfdata->isVisualizable()) {
                $columns[''] = '';
            }
            foreach ($perfdata->toArray() as $column => $value) {
                if (empty($value) ||
                    $column === 'min' && floatval($value) === 0.0 ||
                    $column === 'max' && $perfdata->isPercentage() && floatval($value) === 100) {
                    continue;
                }
                $columns[$column] = $labels[$column];
            }
        }
        // restore original column array sorting
        $headers = array();
        foreach ($keys as $column) {
            if (isset($columns[$column])) {
                $headers[$column] = $labels[$column];
            }
        }
        $table = array('<thead><tr><th>' . implode('</th><th>', $headers) . '</th></tr></thead><tbody>');
        foreach ($pieChartData as $perfdata) {
            if ($compact && $perfdata->isVisualizable()) {
                $results[] = $perfdata->asInlinePie($color)->render();
            } else {
                $data = array();
                if ($perfdata->isVisualizable()) {
                    $data []= $perfdata->asInlinePie($color)->render();
                } elseif (isset($columns[''])) {
                    $data []= '';
                }
                if (! $compact) {
                    foreach ($perfdata->toArray() as $column => $value) {
                        if (! isset($columns[$column])) {
                            continue;
                        }
                        $text = $this->view->escape(empty($value) ? '-' : $value);
                        $data []= sprintf(
                            '<span title="%s">%s</span>',
                            $text,
                            $text
                        );
                    }
                }
                $table []= '<tr><td class="sparkline-col">' . implode('</td><td>', $data) . '</td></tr>';
            }
        }
        $table[] = '</tbody>';
        if ($limit > 0) {
            $count = $compact ? count($results) : count($table);
            if ($count > $limit) {
                if ($compact) {
                    $results = array_slice($results, 0, $limit);
                    $title = sprintf($this->view->translate('%d more ...'), $count - $limit);
                    $results[] = '<span aria-hidden="true" title="' . $title . '">...</span>';
                } else {
                    $table = array_slice($table, 0, $limit);
                }
            }
        }
        if ($compact) {
            return join('', $results);
        } else {
            if (empty($table)) {
                return '';
            }
            return sprintf(
                '<table class="performance-data-table collapsible" data-visible-rows="6">%s</table>',
                implode("\n", $table)
            );
        }
    }
}