-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathMonitoringConnectionForm.php
More file actions
367 lines (328 loc) · 12.5 KB
/
MonitoringConnectionForm.php
File metadata and controls
367 lines (328 loc) · 12.5 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
namespace Icinga\Module\Vspheredb\Web\Form;
use gipfl\Web\Form\Decorator\DdDtDecorator;
use gipfl\Web\Widget\Hint;
use Icinga\Application\Config;
use Icinga\Data\Db\DbConnection;
use Icinga\Data\ResourceFactory;
use Icinga\Module\Vspheredb\Db;
use Icinga\Module\Vspheredb\Web\QueryParams;
use Icinga\Web\Notification;
use InvalidArgumentException;
use ipl\I18n\Translation;
use gipfl\Web\Form;
use ipl\Html\FormElement\SubmitElement;
use ipl\Html\Html;
use Ramsey\Uuid\Uuid;
class MonitoringConnectionForm extends Form
{
use Translation;
protected $db;
protected bool $hasBeenDeleted = false;
protected ?int $id = null;
public function __construct(Db $connection)
{
$this->addElementLoader(__NAMESPACE__ . '\\Element');
$this->db = $connection->getDbAdapter();
}
public function hasBeenDeleted(): bool
{
return $this->hasBeenDeleted;
}
protected function assemble()
{
$this->add(Hint::info($this->translate(
'The vSphereDB module can hook into the Icinga monitoring module.'
. ' This allows to show context information related to your Virtualization'
. ' infrastructure next to each monitored Host.'
)));
$this->addElement('select', 'vcenter', [
'label' => $this->translate('vCenter'),
'options' => $this->optionalEnum($this->enumVCenters()),
'ignore' => true,
]);
$this->addElement('select', 'source_type', [
'label' => $this->translate('Source Type'),
'options' => $this->optionalEnum([
'ido' => $this->translate('IDO'),
// 'icinga2-api' => $this->translate('Icinga 2 API'),
'icingadb' => $this->translate('Icinga DB')
]),
'class' => 'autosubmit',
]);
$sourceType = $this->getElement('source_type')->getValue();
if (! $sourceType) {
return;
}
if ($sourceType === 'icingadb') {
$this->addElement('select', 'source_resource_name', [
'label' => $this->translate('Icinga DB Resource'),
'options' => $this->optionalEnum($this->enumIcingadbResourceNames()),
'class' => 'autosubmit'
]);
} else {
$this->addElement('select', 'source_resource_name', [
'label' => $this->translate('IDO Resource'),
'options' => $this->optionalEnum($this->enumIdoResourceNames()),
'class' => 'autosubmit'
]);
}
$resourceName = $this->getElement('source_resource_name')->getValue();
if (! $resourceName) {
$this->addElement('submit', 'submit', [
'label' => $this->translate('Next')
]);
return;
}
if ($sourceType === 'icingadb') {
try {
$resource = ResourceFactory::create($resourceName);
if ($resource instanceof DbConnection) {
$icingadbVars = $this->enumIcingadbCustomVars($resource);
} else {
throw new InvalidArgumentException("Resource '$resourceName' is not a DbConnection");
}
} catch (\Exception $e) {
$this->getElement('source_resource_name')->addMessage($e->getMessage());
return;
}
$varOptions = $this->optionalEnum([
'name' => $this->translate('Hostname'),
'display_name' => $this->translate('Display Name'),
'address' => $this->translate('Address v4'),
'address6' => $this->translate('Address v6'),
] + [$this->translate('Custom Variables') => $icingadbVars]);
} else {
try {
$resource = ResourceFactory::create($resourceName);
if ($resource instanceof DbConnection) {
$idoVars = $this->enumIdoCustomVars($resource);
} else {
throw new InvalidArgumentException("Resource '$resourceName' is not a DbConnection");
}
} catch (\Exception $e) {
$this->getElement('source_resource_name')->addMessage($e->getMessage());
return;
}
$varOptions = $this->optionalEnum([
'host_name' => $this->translate('Hostname'),
'display_name' => $this->translate('Display Name'),
'address' => $this->translate('Address'),
] + [$this->translate('Custom Variables') => $idoVars]);
}
$this->add(Html::tag('h2', $this->translate('Host Systems')));
$this->add(Html::tag('p', $this->translate(
'Map monitored hosts to physical Host Systems belonging to your'
. ' VMware environment'
)));
$this->addElement('select', 'host_property', [
'label' => $this->translate('Host System Property'),
'description' => $this->translate(
'Property of the Host System (known by the vSphereDB module)'
),
'options' => $this->optionalEnum([
'host_name' => $this->translate('Hostname'),
'object_name' => $this->translate('Object Name'),
'sysinfo_uuid' => $this->translate('System (BIOS) UUID'),
'service_tag' => $this->translate('IP Address'),
]),
]);
$this->addElement('select', 'monitoring_host_property', [
'label' => $this->translate('Monitored Host Property'),
'description' => $this->translate(
'Property of the Host System (as known by Icinga)'
),
'options' => $varOptions,
]);
$this->add(Html::tag('h2', $this->translate('Virtual Machines')));
$this->add(Html::tag('p', $this->translate(
'Map monitored hosts to Virtual Machines belonging to your'
. ' VMware environment'
)));
$this->addElement('select', 'vm_property', [
'label' => $this->translate('Virtual Machine Property'),
'description' => $this->translate(
'Property of the Virtual Machine (known by the vSphereDB module)'
),
'options' => $this->optionalEnum([
'guest_host_name' => $this->translate('Guest Hostname'),
'object_name' => $this->translate('Object Name'),
'bios_uuid' => $this->translate('BIOS UUID'),
]),
]);
$this->addElement('select', 'monitoring_vm_host_property', [
'label' => $this->translate('Monitored Host Property'),
'description' => $this->translate(
'Property of the Virtual Machine (as known by Icinga)'
),
'options' => $varOptions,
]);
$submit = new SubmitElement('submit', [
'label' => $this->translate('Store')
]);
$this->addElement($submit);
if ($id = $this->getId()) {
$delete = new SubmitElement('delete', [
'label' => $this->translate('Delete')
]);
$deco = $submit->getWrapper();
assert($deco instanceof DdDtDecorator);
$deco->dd()->add($delete);
$this->registerElement($delete);
if ($delete->hasBeenPressed()) {
$this->db->delete(
'monitoring_connection',
$this->db->quoteInto('id = ?', $id)
);
Notification::success($this->translate('Monitoring Integration has been deleted'));
$this->hasBeenDeleted = true;
}
}
}
public function getId(): ?int
{
if ($this->id === null) {
if ($id = QueryParams::fromRequest($this->getRequest())->get('id')) {
$this->id = (int) $id;
}
}
return $this->id;
}
public function onSuccess()
{
$values = $this->getValues();
$db = $this->db;
$id = $this->getId();
$vCenterUuid = $this->getValue('vcenter');
if ($vCenterUuid === null) {
$values['vcenter_uuid'] = null;
} else {
$values['vcenter_uuid'] = Uuid::fromString($vCenterUuid)->getBytes();
}
if ($id) {
$db->update(
'monitoring_connection',
$values,
$db->quoteInto('id = ?', $id)
);
Notification::success($this->translate('Monitoring Integration has been modified'));
} else {
$priority = (int) $db->fetchOne(
$db->select()->from('monitoring_connection', 'MAX(priority)')
) + 1;
$db->insert('monitoring_connection', $values + [
'priority' => $priority,
]);
$this->id = (int) $db->lastInsertId();
Notification::success($this->translate('Monitoring Integration has been created'));
}
}
protected function enumIcingadbCustomVars(DbConnection $db)
{
$dba = $db->getDbAdapter();
$vars = $dba->fetchPairs(
$dba->select()->from(
['cvs' => 'customvar'],
[
'varname' => 'cvs.name',
'varcount' => 'COUNT(*)'
]
)->join(
['o' => 'host_customvar'],
'o.customvar_id = cvs.id',
[]
)
->group('varname')
->order('varname')
);
$result = [];
foreach ($vars as $name => $count) {
$result["vars.$name"] = "vars.$name ({$count}x)";
}
return $result;
}
protected function enumIdoCustomVars(DbConnection $db)
{
$dba = $db->getDbAdapter();
$vars = $dba->fetchPairs(
$dba->select()->from(
['cvs' => 'icinga_customvariablestatus'],
[
'varname' => 'cvs.varname',
'varcount' => 'COUNT(*)'
]
)->join(
['o' => 'icinga_objects'],
'o.object_id = cvs.object_id AND o.is_active = 1',
[]
)
->group('varname')
->order('varname')
);
$result = [];
foreach ($vars as $name => $count) {
$result["vars.$name"] = "vars.$name ({$count}x)";
}
return $result;
}
/**
* UNUSED
* @return array
*/
protected function enumHostParents(): array
{
$db = $this->db;
$query = $db->select()->from(
['p' => 'object'],
['p.uuid', 'p.object_name']
)->join(
['c' => 'object'],
'c.parent_uuid = p.uuid AND '
. $db->quoteInto('c.object_type = ?', 'HostSystem')
. ' AND '
. $db->quoteInto('p.object_type = ?', 'ClusterComputeResource'),
[]
)->group('p.uuid')->order('p.object_name');
return $this->makeNiceUuidKeys($db->fetchPairs($query));
}
protected function enumIcingadbResourceNames(): array
{
$resource = Config::module('icingadb')->get('icingadb', 'resource');
return [$resource ?? '' => $resource];
}
protected function enumIdoResourceNames(): array
{
$resources = [];
foreach (Config::module('monitoring', 'backends') as $name => $config) {
if ((bool) $config->get('disabled', false) === false) {
$resourceName = $config->get('resource');
if ($name !== $resourceName) {
$name = sprintf('%s (%s)', $name, $resourceName);
}
$resources[$resourceName ?? ''] = $name;
}
}
return $resources;
}
protected function enumVCenters(): array
{
return $this->makeNiceUuidKeys($this->db->fetchPairs(
$this->db->select()->from(['vc' => 'vcenter'], [
'uuid' => 'vc.instance_uuid',
'name' => 'vc.name',
])->order('vc.name')
));
}
protected function makeNiceUuidKeys(array $enum): array
{
$result = [];
foreach ($enum as $k => $v) {
$result[Uuid::fromBytes($k)->toString()] = $v;
}
return $result;
}
protected function optionalEnum($values): array
{
return ['' => $this->translate('- please choose -')] + $values;
}
}