summaryrefslogtreecommitdiffstats
path: root/modules/monitoring/application/controllers/ActionsController.php
blob: bc13e216babb13df9ccdb7c8ff80749f5faeb388 (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
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Monitoring\Controllers;

use Icinga\Data\Filter\Filter;
use Icinga\Module\Monitoring\Controller;
use Icinga\Module\Monitoring\Forms\Command\Object\DeleteDowntimesCommandForm;
use Icinga\Module\Monitoring\Forms\Command\Object\ScheduleHostDowntimeCommandForm;
use Icinga\Module\Monitoring\Forms\Command\Object\ScheduleServiceDowntimeCommandForm;
use Icinga\Module\Monitoring\Object\HostList;
use Icinga\Module\Monitoring\Object\ServiceList;

/**
 * Monitoring API
 */
class ActionsController extends Controller
{
    /**
     * Get the filter from URL parameters or exit immediately if the filter is empty
     *
     * @return Filter
     */
    protected function getFilterOrExitIfEmpty()
    {
        $filter = Filter::fromQueryString((string) $this->params);
        if ($filter->isEmpty()) {
            $this->getResponse()->json()
                ->setFailData(array('filter' => 'Filter is required and must not be empty'))
                ->sendResponse();
        }
        return $filter;
    }

    /**
     * Schedule host downtimes
     */
    public function scheduleHostDowntimeAction()
    {
        $filter = $this->getFilterOrExitIfEmpty();
        $hostList = new HostList($this->backend);
        $hostList
            ->applyFilter($this->getRestriction('monitoring/filter/objects'))
            ->applyFilter($filter);
        if (! $hostList->count()) {
            $this->getResponse()->json()
                ->setFailData(array('filter' => 'No hosts found matching the filter'))
                ->sendResponse();
        }
        $form = new ScheduleHostDowntimeCommandForm();
        $form
            ->setIsApiTarget(true)
            ->setBackend($this->backend)
            ->setObjects($hostList->fetch())
            ->handleRequest($this->getRequest());
    }

    /**
     * Remove host downtimes
     */
    public function removeHostDowntimeAction()
    {
        $filter = $this->getFilterOrExitIfEmpty();
        $downtimes = $this->backend
            ->select()
            ->from('downtime', array('host_name', 'id' => 'downtime_internal_id', 'name' => 'downtime_name'))
            ->where('object_type', 'host')
            ->applyFilter($this->getRestriction('monitoring/filter/objects'))
            ->applyFilter($filter);
        if (! $downtimes->count()) {
            $this->getResponse()->json()
                ->setFailData(array('filter' => 'No downtimes found matching the filter'))
                ->sendResponse();
        }
        $form = new DeleteDowntimesCommandForm();
        $form
            ->setIsApiTarget(true)
            ->setDowntimes($downtimes->fetchAll())
            ->handleRequest($this->getRequest());
        // @TODO(el): Respond w/ the downtimes deleted instead of the notifiaction added by
        // DeleteDowntimesCommandForm::onSuccess().
    }

    /**
     * Schedule service downtimes
     */
    public function scheduleServiceDowntimeAction()
    {
        $filter = $this->getFilterOrExitIfEmpty();
        $serviceList = new ServiceList($this->backend);
        $serviceList
            ->applyFilter($this->getRestriction('monitoring/filter/objects'))
            ->applyFilter($filter);
        if (! $serviceList->count()) {
            $this->getResponse()->json()
                ->setFailData(array('filter' => 'No services found matching the filter'))
                ->sendResponse();
        }
        $form = new ScheduleServiceDowntimeCommandForm();
        $form
            ->setIsApiTarget(true)
            ->setBackend($this->backend)
            ->setObjects($serviceList->fetch())
            ->handleRequest($this->getRequest());
    }

    /**
     * Remove service downtimes
     */
    public function removeServiceDowntimeAction()
    {
        $filter = $this->getFilterOrExitIfEmpty();
        $downtimes = $this->backend
            ->select()
            ->from(
                'downtime',
                array('host_name', 'service_description', 'id' => 'downtime_internal_id', 'name' => 'downtime_name')
            )
            ->where('object_type', 'service')
            ->applyFilter($this->getRestriction('monitoring/filter/objects'))
            ->applyFilter($filter);
        if (! $downtimes->count()) {
            $this->getResponse()->json()
                ->setFailData(array('filter' => 'No downtimes found matching the filter'))
                ->sendResponse();
        }
        $form = new DeleteDowntimesCommandForm();
        $form
            ->setIsApiTarget(true)
            ->setDowntimes($downtimes->fetchAll())
            ->handleRequest($this->getRequest());
        // @TODO(el): Respond w/ the downtimes deleted instead of the notifiaction added by
        // DeleteDowntimesCommandForm::onSuccess().
    }
}