-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathMonitoringController.php
More file actions
311 lines (286 loc) · 12.8 KB
/
Copy pathMonitoringController.php
File metadata and controls
311 lines (286 loc) · 12.8 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
// SPDX-FileCopyrightText: 2022 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Icinga\Module\Vspheredb\Controllers;
use gipfl\IcingaWeb2\Link;
use gipfl\Web\Widget\Hint;
use Icinga\Module\Vspheredb\DbObject\ManagedObject;
use Icinga\Module\Vspheredb\DbObject\VCenter;
use Icinga\Module\Vspheredb\Monitoring\Rule\Definition\RuleSetRegistry;
use Icinga\Module\Vspheredb\Monitoring\Rule\Enum\ObjectType;
use Icinga\Module\Vspheredb\Monitoring\Rule\InheritedSettings;
use Icinga\Module\Vspheredb\Monitoring\Rule\MonitoringRuleSet;
use Icinga\Module\Vspheredb\Monitoring\Rule\MonitoringRulesTree;
use Icinga\Module\Vspheredb\Monitoring\Rule\MonitoringRulesTreeRenderer;
use Icinga\Module\Vspheredb\Monitoring\Rule\RuleForm;
use Icinga\Module\Vspheredb\Web\Controller;
use Icinga\Module\Vspheredb\Web\Form\FilterVCenterForm;
use Icinga\Module\Vspheredb\Web\Table\Monitoring\MonitoringRuleProblematicObjectTable;
use Icinga\Module\Vspheredb\Web\Table\Monitoring\MonitoringRuleProblemHistoryTable;
use Icinga\Module\Vspheredb\Web\Table\Monitoring\MonitoringRuleProblemTable;
use Icinga\Module\Vspheredb\Web\Table\TableWithVCenterFilter;
use Icinga\Module\Vspheredb\Web\Widget\Documentation;
use Icinga\Web\Notification;
use ipl\Html\Html;
use Ramsey\Uuid\Uuid;
use RuntimeException;
class MonitoringController extends Controller
{
use AsyncControllerHelper;
protected $vCenterFilterForm;
public function init()
{
parent::init();
$action = $this->getRequest()->getActionName();
if (preg_match('/tree$/', $action) || in_array($action, ['index', 'configuration', 'history'])) {
$tabs = $this->tabs();
$tabs->add('index', [
'label' => $this->translate('Monitoring'),
'url' => 'vspheredb/monitoring'
])->add('history', [
'label' => $this->translate('History'),
'url' => 'vspheredb/monitoring/history'
]);
if ($this->hasPermission('vspheredb/admin')) {
$tabs->add('configuration', [
'label' => $this->translate('Configuration'),
'url' => 'vspheredb/monitoring/configuration'
])->add('hosttree', [
'label' => $this->translate('Hosts'),
'url' => 'vspheredb/monitoring/hosttree'
])->add('vmtree', [
'label' => $this->translate('Virtual Machines'),
'url' => 'vspheredb/monitoring/vmtree'
])->add('datastoretree', [
'label' => $this->translate('Datastores'),
'url' => 'vspheredb/monitoring/datastoretree'
]);
}
$tabs->activate($action);
}
if (preg_match('/tree$/', $action)) {
$this->actions()->add(
Link::create($this->translate('Back to overview'), 'vspheredb/monitoring', null, [
'class' => 'icon-left-small'
])
);
$this->content()->add(Hint::info($this->translate(
'Select any folder (or the root node) to define (and override) Monitoring Rules'
. ' for all related child nodes'
)));
}
}
public function indexAction()
{
$this->addTitle($this->translate('Monitoring Rules'));
$this->setAutorefreshInterval(20);
$table = new MonitoringRuleProblemTable($this->db()->getDbAdapter());
$this->filterByVCenterIfRequired($table);
$table->renderTo($this);
}
public function problemsAction()
{
$this->addSingleTab($this->translate('Current Problems'));
$vCenter = $this->requireVCenter();
$this->getRestrictionHelper()->assertAccessToVCenterUuidIsGranted($vCenter->get('instance_uuid'));
$this->setAutorefreshInterval(20);
$objectType = $this->params->getRequired('objectType');
$ruleSet = $this->params->getRequired('ruleSet');
$rule = $this->params->getRequired('rule');
$this->addTitle(sprintf('%s / %s (%s)', $ruleSet, $rule, $objectType));
$table = new MonitoringRuleProblematicObjectTable($this->db(), $vCenter, $objectType, $ruleSet, $rule);
$table->renderTo($this);
}
public function historyAction()
{
$this->addTitle($this->translate('Monitoring Rules - Problem History'));
$this->setAutorefreshInterval(20);
$table = new MonitoringRuleProblemHistoryTable($this->db()->getDbAdapter());
$this->filterByVCenterIfRequired($table);
$table->renderTo($this);
}
public function configurationAction()
{
$this->assertPermission('vspheredb/admin');
$this->addTitle($this->translate('Monitoring Rules'));
$this->content()->addAttributes([
'class' => 'overview-chapter'
]);
$this->content()->add([
Hint::info(Html::sprintf($this->translate(
'The Icinga vSphere%s Integration ships a lot of data, state and sensor values.'
. ' If you want to define related Icinga Service checks for Alarming reasons,'
. ' Monitoring Rules are %s.'
), '®', Html::tag('strong', $this->translate('the way to go')))),
Hint::info(Html::sprintf($this->translate(
'Instead of checking every single Disk, just define rules (and exemptions'
. ' from those) for %s.'
), Html::tag('strong', $this->translate('all of them')))),
Html::tag('h2', $this->translate('Defining Rules')),
Html::tag('p', Html::sprintf(
$this->translate('Define rules for %s, %s and %s'),
Link::create($this->translate('Host Systems'), 'vspheredb/monitoring/hosttree'),
Link::create($this->translate('Virtual Machines'), 'vspheredb/monitoring/vmtree'),
Link::create($this->translate('Datastores'), 'vspheredb/monitoring/datastoretree')
)),
Html::tag('h2', $this->translate('Defining Check Commands')),
Html::tag('p', Html::sprintf(
$this->translate('Check our %s for instructions of how to set them up'),
Documentation::link(
$this->translate('Documentation'),
'vspheredb',
'32-Monitoring_Rules',
$this->translate('Icinga vSphereDB Check Commands')
)
))
]);
}
public function hostrulesAction()
{
$this->showType(ObjectType::HOST_SYSTEM);
}
public function hosttreeAction()
{
$this->showTree(ObjectType::HOST_SYSTEM);
}
public function vmrulesAction()
{
$this->showType(ObjectType::VIRTUAL_MACHINE);
}
public function vmtreeAction()
{
$this->showTree(ObjectType::VIRTUAL_MACHINE);
}
public function datastorerulesAction()
{
$this->showType(ObjectType::DATASTORE);
}
public function datastoretreeAction()
{
$this->showTree(ObjectType::DATASTORE);
}
public function showTree($chosenType)
{
$this->assertPermission('vspheredb/admin');
$this->addTitle($this->translate('Monitoring'));
$tree = new MonitoringRulesTree($this->db(), $chosenType);
$this->content()->add(new MonitoringRulesTreeRenderer($tree, "vspheredb/monitoring/{$chosenType}rules"));
}
public function showType($chosenType)
{
$this->assertPermission('vspheredb/admin');
$this->addSingleTab($this->translate('Rules'));
$uuid = $this->params->get('uuid');
$db = $this->db();
$objectTypeLabel = $this->getTypeLabelForObjectType($chosenType);
if ($uuid === null) {
$binaryUuid = '';
$title = sprintf($this->translate('Global Monitoring Rules for %s'), $objectTypeLabel);
} else {
$binaryUuid = Uuid::fromString($uuid)->getBytes();
if (VCenter::exists($binaryUuid, $db)) {
$vCenter = VCenter::loadWithUuid($binaryUuid, $db);
$title = sprintf(
$this->translate('vCenter Monitoring Rules for %s: %s'),
$objectTypeLabel,
$vCenter->get('name')
);
} else {
$parent = ManagedObject::load($binaryUuid, $this->db());
$vCenter = VCenter::load($parent->get('vcenter_uuid'), $db);
if ($parent->getNumericLevel() === 2) {
$dataCenter = ManagedObject::load($parent->get('parent_uuid'), $db);
$title = sprintf(
$this->translate('DataCenter Monitoring Rules for %s: %s (%s)'),
$objectTypeLabel,
$dataCenter->get('object_name'),
$vCenter->get('name')
);
} else {
$title = sprintf(
$this->translate('Folder Monitoring Rules for %s: %s (%s)'),
$objectTypeLabel,
$parent->get('object_name'),
$vCenter->get('name')
);
}
}
}
$this->addTitle($title);
$tree = new MonitoringRulesTree($db, $chosenType);
$storedConfig = MonitoringRuleSet::loadOptionalForUuid($binaryUuid, $chosenType, $db);
$inherited = InheritedSettings::loadFor($binaryUuid, $tree, $db);
$inherited->setInternalDefaults(RuleSetRegistry::default());
$form = new RuleForm($chosenType, $binaryUuid, $db, $inherited, $storedConfig);
$form->on(RuleForm::ON_SUCCESS, function (RuleForm $form) use ($title) {
if ($form->hasNotBeenModified()) {
Notification::info($this->translate('No change has been applied'));
$this->redirectNow($this->url());
}
try {
if ($this->syncRpcCall('db.refreshMonitoringRuleProblems')) {
Notification::success($this->translate('Current Problems have been recalculated'));
} else {
Notification::info($this->translate(
'Current problems have NOT been recalculated, they will be applied with a short delay'
));
}
} catch (\Exception $e) {
Notification::info(
$this->translate(
'Error when triggering problem recalculation, changes will be applied with a short delay'
) . ': ' . $e->getMessage()
);
}
if ($form->hasNotBeenModified()) {
Notification::info($this->translate('No change has been applied'));
} elseif ($form->hasBeenModified()) {
Notification::success(sprintf($this->translate('Settings for %s have been modified'), $title));
}
if ($form->hasBeenCreated()) {
Notification::success(sprintf($this->translate('Settings for %s have been stored'), $title));
} elseif ($form->hasBeenDeleted()) {
Notification::success(sprintf($this->translate('Settings for %s have been removed'), $title));
}
$this->redirectNow($this->url());
});
$form->handleRequest($this->getServerRequest());
$this->content()->add($form);
$form->addElement('submit', 'submit', [
'label' => $this->translate('Store')
]);
}
protected function getTypeLabelForObjectType(string $type): string
{
switch ($type) {
case 'host':
return $this->translate('Host Systems');
case 'vm':
return $this->translate('Virtual Machines');
case 'datastore':
return $this->translate('Datastores');
}
throw new RuntimeException("Unexpected object type: '$type'");
}
// Duplicated from ObjectsController
protected function filterByVCenterIfRequired(TableWithVCenterFilter $table)
{
$this->getRestrictionHelper()->restrictTable($table);
$this->controls()->prepend($this->getVCenterFilterForm());
if ($uuid = $this->params->get('vcenter')) {
$table->filterVCenter(VCenter::loadWithUuid($uuid, $this->db()));
}
}
// Duplicated from ObjectsController
protected function getVCenterFilterForm(): FilterVCenterForm
{
if ($this->vCenterFilterForm === null) {
$form = new FilterVCenterForm($this->db(), $this->Auth());
$form->allowAllVCenters();
$form->getAttributes()->add('class', 'vcenter-filter-form');
$form->handleRequest($this->getServerRequest());
$this->vCenterFilterForm = $form;
}
return $this->vCenterFilterForm;
}
}