summaryrefslogtreecommitdiffstats
path: root/modules/monitoring/application/controllers/HostsController.php
blob: 400c3ade84eacf09ac68e9b212b3d6e8e7eae023 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Monitoring\Controllers;

use Exception;
use Icinga\Data\Filter\Filter;
use Icinga\Data\Filter\FilterEqual;
use Icinga\Module\Monitoring\Controller;
use Icinga\Module\Monitoring\Forms\Command\Object\AcknowledgeProblemCommandForm;
use Icinga\Module\Monitoring\Forms\Command\Object\AddCommentCommandForm;
use Icinga\Module\Monitoring\Forms\Command\Object\CheckNowCommandForm;
use Icinga\Module\Monitoring\Forms\Command\Object\ObjectsCommandForm;
use Icinga\Module\Monitoring\Forms\Command\Object\ProcessCheckResultCommandForm;
use Icinga\Module\Monitoring\Forms\Command\Object\RemoveAcknowledgementCommandForm;
use Icinga\Module\Monitoring\Forms\Command\Object\ScheduleHostCheckCommandForm;
use Icinga\Module\Monitoring\Forms\Command\Object\ScheduleHostDowntimeCommandForm;
use Icinga\Module\Monitoring\Forms\Command\Object\SendCustomNotificationCommandForm;
use Icinga\Module\Monitoring\Forms\Command\Object\ToggleObjectFeaturesCommandForm;
use Icinga\Module\Monitoring\Hook\DetailviewExtensionHook;
use Icinga\Module\Monitoring\Object\HostList;
use Icinga\Web\Hook;
use Icinga\Web\Url;
use Icinga\Web\Widget\Tabextension\DashboardAction;
use Icinga\Web\Widget\Tabextension\MenuAction;

class HostsController extends Controller
{
    /**
     * @var HostList
     */
    protected $hostList;

    public function init()
    {
        $hostList = new HostList($this->backend);
        $this->applyRestriction('monitoring/filter/objects', $hostList);
        $hostList->addFilter(Filter::fromQueryString((string) $this->params));
        $this->hostList = $hostList;
        $this->hostList->setColumns(array(
            'host_acknowledged',
            'host_active_checks_enabled',
            'host_display_name',
            'host_event_handler_enabled',
            'host_flap_detection_enabled',
            'host_handled',
            'host_in_downtime',
            'host_is_flapping',
            'host_last_state_change',
            'host_name',
            'host_notifications_enabled',
            'host_obsessing',
            'host_passive_checks_enabled',
            'host_problem',
            'host_state',
            'instance_name'
        ));
        $this->view->baseFilter = $this->hostList->getFilter();
        $this->getTabs()->add(
            'show',
            array(
                'label' => $this->translate('Hosts') . sprintf(' (%d)', count($this->hostList)),
                'title' => sprintf(
                    $this->translate('Show summarized information for %u hosts'),
                    count($this->hostList)
                ),
                'url'   => Url::fromRequest()
            )
        )->extend(new DashboardAction())->extend(new MenuAction())->activate('show');
        $this->view->listAllLink = Url::fromRequest()->setPath('monitoring/list/hosts');
        $this->view->title = $this->translate('Hosts');
    }

    protected function handleCommandForm(ObjectsCommandForm $form)
    {
        $form
            ->setBackend($this->backend)
            ->setObjects($this->hostList)
            ->setRedirectUrl(Url::fromPath('monitoring/hosts/show')->setParams(
                $this->params->without('host_active_checks_enabled')
            ))
            ->handleRequest();

        $this->view->form = $form;
        $this->view->objects = $this->hostList;
        $this->view->stats = $this->hostList->getStateSummary();
        $this->_helper->viewRenderer('partials/command/objects-command-form', null, true);
        return $form;
    }

    public function showAction()
    {
        $this->setAutorefreshInterval(15);
        $activeChecksEnabled = $this->hostList->getFeatureStatus()['active_checks_enabled'] !== 0;
        if ($this->Auth()->hasPermission('monitoring/command/schedule-check')
            || ($this->Auth()->hasPermission('monitoring/command/schedule-check/active-only')
                && $activeChecksEnabled
            )
        ) {
            $checkNowForm = new CheckNowCommandForm();
            $checkNowForm
                ->setObjects($this->hostList)
                ->handleRequest();
            $this->view->checkNowForm = $checkNowForm;
        }

        $acknowledgedObjects = $this->hostList->getAcknowledgedObjects();
        if ($acknowledgedObjects->count()) {
            $removeAckForm = new RemoveAcknowledgementCommandForm();
            $removeAckForm
                ->setObjects($acknowledgedObjects)
                ->handleRequest();
            $this->view->removeAckForm = $removeAckForm;
        }

        $featureStatus = $this->hostList->getFeatureStatus();
        $toggleFeaturesForm = new ToggleObjectFeaturesCommandForm(array(
            'backend'   => $this->backend,
            'objects'   => $this->hostList
        ));
        $toggleFeaturesForm
            ->load((object) $featureStatus)
            ->handleRequest();
        $this->view->toggleFeaturesForm = $toggleFeaturesForm;

        $hostStates = $this->hostList->getStateSummary();

        if ($activeChecksEnabled) {
            $this->view->rescheduleAllLink = Url::fromRequest()
                ->setPath('monitoring/hosts/reschedule-check')
                ->addParams(['host_active_checks_enabled' => true]);
        }

        $this->view->downtimeAllLink = Url::fromRequest()->setPath('monitoring/hosts/schedule-downtime');
        $this->view->processCheckResultAllLink = Url::fromRequest()->setPath('monitoring/hosts/process-check-result');
        $this->view->addCommentLink = Url::fromRequest()->setPath('monitoring/hosts/add-comment');
        $this->view->stats = $hostStates;
        $this->view->objects = $this->hostList;
        $this->view->unhandledObjects = $this->hostList->getUnhandledObjects();
        $this->view->problemObjects = $this->hostList->getProblemObjects();
        $this->view->acknowledgeUnhandledLink = Url::fromPath('monitoring/hosts/acknowledge-problem')
            ->setQueryString($this->hostList->getUnhandledObjects()->objectsFilter()->toQueryString());
        $this->view->downtimeUnhandledLink = Url::fromPath('monitoring/hosts/schedule-downtime')
            ->setQueryString($this->hostList->getUnhandledObjects()->objectsFilter()->toQueryString());
        $this->view->downtimeLink = Url::fromPath('monitoring/hosts/schedule-downtime')
            ->setQueryString($this->hostList->getProblemObjects()->objectsFilter()->toQueryString());
        $this->view->acknowledgedObjects = $this->hostList->getAcknowledgedObjects();
        $this->view->acknowledgeLink = Url::fromPath('monitoring/hosts/acknowledge-problem')
            ->setQueryString($this->hostList->getUnacknowledgedObjects()->objectsFilter()->toQueryString());
        $this->view->unacknowledgedObjects = $this->hostList->getUnacknowledgedObjects();
        $this->view->objectsInDowntime = $this->hostList->getObjectsInDowntime();
        $this->view->inDowntimeLink = Url::fromPath('monitoring/list/hosts')
            ->setQueryString(
                $this->hostList
                    ->getObjectsInDowntime()
                    ->objectsFilter()
                    ->toQueryString()
            );
        $this->view->showDowntimesLink = Url::fromPath('monitoring/list/downtimes')
            ->setQueryString(
                $this->hostList
                    ->objectsFilter()
                    ->andFilter(FilterEqual::where('object_type', 'host'))
                    ->toQueryString()
            );
        $this->view->commentsLink = Url::fromRequest()->setPath('monitoring/list/comments');
        $this->view->sendCustomNotificationLink = Url::fromRequest()
            ->setPath('monitoring/hosts/send-custom-notification');

        $this->view->extensionsHtml = array();
        foreach (Hook::all('Monitoring\DetailviewExtension') as $hook) {
            /** @var DetailviewExtensionHook $hook */
            try {
                $html = $hook->setView($this->view)->getHtmlForObjects($this->hostList);
            } catch (Exception $e) {
                $html = $this->view->escape($e->getMessage());
            }

            if ($html) {
                $module = $this->view->escape($hook->getModule()->getName());
                $this->view->extensionsHtml[] =
                    '<div class="icinga-module module-' . $module . '" data-icinga-module="' . $module . '">'
                    . $html
                    . '</div>';
            }
        }
    }

    /**
     * Add a host comments
     */
    public function addCommentAction()
    {
        $this->assertPermission('monitoring/command/comment/add');

        $form = new AddCommentCommandForm();
        $form->setTitle($this->translate('Add Host Comments'));
        $this->handleCommandForm($form);
    }

    /**
     * Acknowledge host problems
     */
    public function acknowledgeProblemAction()
    {
        $this->assertPermission('monitoring/command/acknowledge-problem');

        $form = new AcknowledgeProblemCommandForm();
        $form->setTitle($this->translate('Acknowledge Host Problems'));
        $this->handleCommandForm($form);
    }

    /**
     * Reschedule host checks
     */
    public function rescheduleCheckAction()
    {
        $this->assertPermission('monitoring/command/schedule-check');

        $form = new ScheduleHostCheckCommandForm();
        $form->setTitle($this->translate('Reschedule Host Checks'));
        $this->handleCommandForm($form);
    }

    /**
     * Schedule host downtimes
     */
    public function scheduleDowntimeAction()
    {
        $this->assertPermission('monitoring/command/downtime/schedule');

        $form = new ScheduleHostDowntimeCommandForm();
        $form->setTitle($this->translate('Schedule Host Downtimes'));
        $this->handleCommandForm($form);
    }

    /**
     * Submit passive host check results
     */
    public function processCheckResultAction()
    {
        $this->assertPermission('monitoring/command/process-check-result');

        $form = new ProcessCheckResultCommandForm();
        $form->setTitle($this->translate('Submit Passive Host Check Results'));
        $this->handleCommandForm($form);
    }

    /**
     * Send a custom notification for hosts
     */
    public function sendCustomNotificationAction()
    {
        $this->assertPermission('monitoring/command/send-custom-notification');

        $form = new SendCustomNotificationCommandForm();
        $form->setTitle($this->translate('Send Custom Host Notification'));
        $this->handleCommandForm($form);
    }
}