This file is indexed.

/usr/share/icingaweb2/modules/monitoring/library/Monitoring/Web/Navigation/Renderer/MonitoringBadgeNavigationItemRenderer.php is in icingaweb2-module-monitoring 2.1.0-1ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Monitoring\Web\Navigation\Renderer;

use Exception;
use Icinga\Authentication\Auth;
use Icinga\Data\Filter\Filter;
use Icinga\Data\Filterable;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
use Icinga\Web\Navigation\Renderer\SummaryNavigationItemRenderer;

/**
 * Render generic dataView columns as badges in MenuItems
 *
 * Renders numeric data view column values into menu item badges, fully configurable
 * and with a caching mechanism to prevent needless requests to the same data view.
 *
 * It is possible to configure the class of the rendered badge as option 'class', the
 * column to fetch using the option 'column' and the dataView from which the columns
 * will be fetched using the option 'dataView'.
 */
class MonitoringBadgeNavigationItemRenderer extends SummaryNavigationItemRenderer
{
    /**
     * Caches the responses for all executed summaries
     *
     * @var array
     */
    protected static $summaries = array();

    /**
     * Accumulates all needed columns for a view to allow fetching the needed columns in
     * one single query
     *
     * @var array
     */
    protected static $dataViews = array();

    /**
     * The dataview referred to by the navigation item
     *
     * @var string
     */
    protected $dataView;

    /**
     * The columns and titles displayed in the badge
     *
     * @var array
     */
    protected $columns;

    /**
     * Set the dataview referred to by the navigation item
     *
     * @param   string  $dataView
     *
     * @return  $this
     */
    public function setDataView($dataView)
    {
        $this->dataView = $dataView;
        return $this;
    }

    /**
     * Return the dataview referred to by the navigation item
     *
     * @return  string
     */
    public function getDataView()
    {
        return $this->dataView;
    }

    /**
     * Set the columns and titles displayed in the badge
     *
     * @param   array   $columns
     *
     * @return  $this
     */
    public function setColumns(array $columns)
    {
        $this->columns = $columns;
        return $this;
    }

    /**
     * Return the columns and titles displayed in the badge
     *
     * @return  array
     */
    public function getColumns()
    {
        return $this->columns;
    }

    /**
     * {@inheritdoc}
     */
    public function init()
    {
        // clear the outdated summary cache, since new columns are being added. Optimally all menu item are constructed
        // before any rendering is going on to avoid trashing too man old requests
        if (isset(self::$summaries[$this->dataView])) {
            unset(self::$summaries[$this->dataView]);
        }

        // add the new columns to this view
        if (! isset(self::$dataViews[$this->dataView])) {
            self::$dataViews[$this->dataView] = array();
        }

        foreach ($this->columns as $column => $title) {
            if (! array_search($column, self::$dataViews[$this->dataView])) {
                self::$dataViews[$this->dataView][] = $column;
            }
        }
    }

    /**
     * Apply a restriction on the given data view
     *
     * @param   string      $restriction    The name of restriction
     * @param   Filterable  $filterable     The filterable to restrict
     *
     * @return  Filterable  The filterable
     */
    protected static function applyRestriction($restriction, Filterable $filterable)
    {
        $restrictions = Filter::matchAny();
        foreach (Auth::getInstance()->getRestrictions($restriction) as $filter) {
            $restrictions->addFilter(Filter::fromQueryString($filter));
        }
        $filterable->applyFilter($restrictions);
        return $filterable;
    }

    /**
     * Fetch the dataview from the database or access cache
     *
     * @param   string  $view
     *
     * @return  object
     */
    protected static function summary($view)
    {
        if (! isset(self::$summaries[$view])) {
            $summary = MonitoringBackend::instance()->select()->from(
                $view,
                self::$dataViews[$view]
            );
            static::applyRestriction('monitoring/filter/objects', $summary);
            self::$summaries[$view] = $summary->fetchRow();
        }

        return self::$summaries[$view];
    }

    /**
     * {@inheritdoc}
     */
    public function getCount()
    {
        try {
            $summary = self::summary($this->getDataView());
        } catch (Exception $_) {
            return 0;
        }

        $count = 0;
        foreach ($this->getColumns() as $column => $title) {
            if (isset($summary->$column) && $summary->$column > 0) {
                $this->titles[] = sprintf($title, $summary->$column);
                $count += $summary->$column;
            }
        }

        return $count;
    }
}