summaryrefslogtreecommitdiffstats
path: root/application/controllers/GraphController.php
blob: c8dc7dbb88ad598192bf8cea358fd0e7ca6c12c2 (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
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
<?php

namespace Icinga\Module\Graphite\Controllers;

use Icinga\Exception\Http\HttpBadRequestException;
use Icinga\Exception\Http\HttpNotFoundException;
use Icinga\Module\Graphite\Graphing\GraphingTrait;
use Icinga\Module\Graphite\Util\IcingadbUtils;
use Icinga\Module\Graphite\Web\Widget\Graphs;
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Model\Service;
use Icinga\Web\Controller;
use Icinga\Web\UrlParams;
use ipl\Orm\Model;
use ipl\Stdlib\Filter;

class GraphController extends Controller
{
    use GraphingTrait;

    /**
     * The URL parameters for the graph
     *
     * @var string[]
     */
    protected $graphParamsNames = [
        'start', 'end',
        'width', 'height',
        'legend',
        'template', 'default_template',
        'bgcolor', 'fgcolor',
        'majorGridLineColor', 'minorGridLineColor'
    ];

    /**
     * The URL parameters for metrics filtering
     *
     * @var UrlParams
     */
    protected $filterParams;

    /**
     * The URL parameters for the graph
     *
     * @var string[]
     */
    protected $graphParams = [];

    public function init()
    {
        parent::init();

        $this->filterParams = clone $this->getRequest()->getUrl()->getParams();

        foreach ($this->graphParamsNames as $paramName) {
            $this->graphParams[$paramName] = $this->filterParams->shift($paramName);
        }
    }

    public function serviceAction()
    {
        $hostName = $this->filterParams->getRequired('host.name');
        $serviceName = $this->filterParams->getRequired('service.name');
        $icingadbUtils = IcingadbUtils::getInstance();
        $query = Service::on($icingadbUtils->getDb())
            ->with('state')
            ->with('host');

        $query->filter(Filter::all(
            Filter::equal('service.name', $serviceName),
            Filter::equal('service.host.name', $hostName)
        ));

        $icingadbUtils->applyRestrictions($query);

        /** @var Service $service */
        $service = $query->first();

        if ($service === null) {
            throw new HttpNotFoundException($this->translate('No such service'));
        }

        $checkCommandColumn = $service->vars[Graphs::getObscuredCheckCommandCustomVar()] ?? null;

        $this->supplyImage(
            $service,
            $service->checkcommand_name,
            $checkCommandColumn
        );
    }

    public function hostAction()
    {
        $hostName = $this->filterParams->getRequired('host.name');
        $icingadbUtils = IcingadbUtils::getInstance();
        $query = Host::on($icingadbUtils->getDb())->with('state');
        $query->filter(Filter::equal('host.name', $hostName));

        $icingadbUtils->applyRestrictions($query);

        /** @var Host $host */
        $host = $query->first();

        if ($host === null) {
            throw new HttpNotFoundException($this->translate('No such host'));
        }

        $checkCommandColumn = $host->vars[Graphs::getObscuredCheckCommandCustomVar()] ?? null;

        $this->supplyImage(
            $host,
            $host->checkcommand_name,
            $checkCommandColumn
        );
    }

    /**
     * Do all monitored object type independent actions
     *
     * @param Model         $object                 The object to render the graphs for
     * @param string        $checkCommand           The check command of the object we supply an image for
     * @param string|null   $obscuredCheckCommand   The "real" check command (if any) of the  object we
     *                                              display graphs for
     */
    protected function supplyImage($object, $checkCommand, $obscuredCheckCommand)
    {
        if (isset($this->graphParams['default_template'])) {
            $urlParam = 'default_template';
            $templates = $this->getAllTemplates()->getDefaultTemplates();
        } else {
            $urlParam = 'template';
            $templates = $this->getAllTemplates()->getTemplates(
                $obscuredCheckCommand === null ? $checkCommand : $obscuredCheckCommand
            );
        }

        if (! isset($templates[$this->graphParams[$urlParam]])) {
            throw new HttpNotFoundException($this->translate('No such template'));
        }

        $charts = $templates[$this->graphParams[$urlParam]]->getCharts(
            static::getMetricsDataSource(),
            $object,
            array_map('rawurldecode', $this->filterParams->toArray(false))
        );

        switch (count($charts)) {
            case 0:
                throw new HttpNotFoundException($this->translate('No such graph'));

            case 1:
                $charts[0]
                    ->setFrom($this->graphParams['start'])
                    ->setUntil($this->graphParams['end'])
                    ->setWidth($this->graphParams['width'])
                    ->setHeight($this->graphParams['height'])
                    ->setBackgroundColor($this->graphParams['bgcolor'])
                    ->setForegroundColor($this->graphParams['fgcolor'])
                    ->setMajorGridLineColor($this->graphParams['majorGridLineColor'])
                    ->setMinorGridLineColor($this->graphParams['minorGridLineColor'])
                    ->setShowLegend((bool) $this->graphParams['legend'])
                    ->serveImage($this->getResponse());

                // not falling through, serveImage exits
            default:
                throw new HttpBadRequestException('%s', $this->translate(
                    'Graphite Web yields more than one metric for the given filter.'
                    . ' Please specify a more precise filter.'
                ));
        }
    }
}