summaryrefslogtreecommitdiffstats
path: root/application/forms/IcingaServiceForm.php
blob: 5744d8d363960e6e57613c12c77f7678f8a4a72d (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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
<?php

namespace Icinga\Module\Director\Forms;

use gipfl\Web\Widget\Hint;
use Icinga\Data\Filter\Filter;
use Icinga\Exception\IcingaException;
use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\Auth\Permission;
use Icinga\Module\Director\Data\PropertiesFilter\ArrayCustomVariablesFilter;
use Icinga\Module\Director\Exception\NestingError;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Objects\IcingaServiceSet;
use Icinga\Module\Director\Web\Table\ObjectsTableHost;
use ipl\Html\Html;
use gipfl\IcingaWeb2\Link;
use RuntimeException;

class IcingaServiceForm extends DirectorObjectForm
{
    /** @var IcingaHost */
    private $host;

    /** @var IcingaServiceSet */
    private $set;

    private $apply;

    /** @var IcingaService */
    protected $object;

    /** @var IcingaService */
    private $applyGenerated;

    private $inheritedFrom;

    /** @var bool|null */
    private $blacklisted;

    public function setApplyGenerated(IcingaService $applyGenerated)
    {
        $this->applyGenerated = $applyGenerated;

        return $this;
    }

    public function setInheritedFrom($hostname)
    {
        $this->inheritedFrom = $hostname;

        return $this;
    }

    /**
     * @throws IcingaException
     * @throws ProgrammingError
     * @throws \Zend_Form_Exception
     */
    public function setup()
    {
        if (!$this->isNew() || $this->providesOverrides()) {
            $this->tryToFetchHost();
        }

        if ($this->providesOverrides()) {
            return;
        }

        if ($this->host && $this->set) {
            // Probably never reached, as providesOverrides includes this
            $this->setupOnHostForSet();

            return;
        }

        if ($this->set !== null) {
            $this->setupSetRelatedElements();
        } elseif ($this->host === null) {
            $this->setupServiceElements();
        } else {
            $this->setupHostRelatedElements();
        }
    }

    protected function tryToFetchHost()
    {
        try {
            if ($this->host === null) {
                $this->host = $this->object->getResolvedRelated('host');
            }
        } catch (NestingError $nestingError) {
            // ignore for the form to load
        }
    }

    public function providesOverrides()
    {
        return $this->applyGenerated
            || $this->inheritedFrom
            || ($this->host && $this->set)
            || ($this->object && $this->object->usesVarOverrides());
    }

    /**
     * @throws IcingaException
     * @throws ProgrammingError
     * @throws \Zend_Form_Exception
     */
    protected function addFields()
    {
        if ($this->providesOverrides() && $this->hasBeenBlacklisted()) {
            $this->onAddedFields();

            return;
        } else {
            parent::addFields();
        }
    }

    /**
     * @throws IcingaException
     * @throws ProgrammingError
     * @throws \Zend_Form_Exception
     */
    protected function onAddedFields()
    {
        if (! $this->providesOverrides()) {
            return;
        }
        $hasDeleteButton = false;
        $isBranch = $this->branch && $this->branch->isBranch();

        if ($this->hasBeenBlacklisted()) {
            $this->addHtml(
                Hint::warning($this->translate('This Service has been deactivated on this host')),
                ['name' => 'HINT_blacklisted']
            );
            $group = null;
            if (! $isBranch) {
                $this->addDeleteButton($this->translate('Reactivate'));
                $hasDeleteButton = true;
            }
            $this->setSubmitLabel(false);
        } else {
            $this->addOverrideHint();
            $group = $this->getDisplayGroup('custom_fields');
            if (! $group) {
                foreach ($this->getDisplayGroups() as $groupName => $eventualGroup) {
                    if (preg_match('/^custom_fields:/', $groupName)) {
                        $group = $eventualGroup;
                        break;
                    }
                }
            }
            if ($group) {
                $elements = $group->getElements();
                $group->setElements([$this->getElement('inheritance_hint')]);
                $group->addElements($elements);
                $this->setSubmitLabel($this->translate('Override vars'));
            } else {
                $this->addElementsToGroup(
                    ['inheritance_hint'],
                    'custom_fields',
                    20,
                    $this->translate('Hints regarding this service')
                );

                $this->setSubmitLabel(false);
            }

            if (! $isBranch) {
                $this->addDeleteButton($this->translate('Deactivate'));
                $hasDeleteButton = true;
            }
        }

        if (! $this->hasSubmitButton() && $hasDeleteButton) {
            $this->addDisplayGroup([$this->deleteButtonName], 'buttons', [
                'decorators' => [
                    'FormElements',
                    ['HtmlTag', ['tag' => 'dl']],
                    'DtDdWrapper',
                ],
                'order' => self::GROUP_ORDER_BUTTONS,
            ]);
        }
    }

    /**
     * @return IcingaHost|null
     */
    public function getHost()
    {
        return $this->host;
    }

    /**
     * Hint: could be moved elsewhere
     *
     * @param IcingaService $object
     * @return IcingaObject|IcingaService|IcingaServiceSet
     * @throws \Icinga\Exception\NotFoundError
     */
    protected static function getFirstParent(IcingaObject $object)
    {
        /** @var IcingaObject[] $objects */
        $objects = $object->imports()->getObjects();
        if (empty($objects)) {
            throw new RuntimeException('Something went wrong, got no parent');
        }
        reset($objects);

        return current($objects);
    }

    /**
     * @return bool
     * @throws \Icinga\Exception\NotFoundError
     */
    protected function hasBeenBlacklisted()
    {
        if (! $this->providesOverrides() || $this->object === null) {
            return false;
        }

        if ($this->blacklisted === null) {
            $host = $this->host;
            // Safety check, branches
            $hostId = $host->get('id');
            $service = $this->getServiceToBeBlacklisted();
            $serviceId = $service->get('id');
            if (! $hostId || ! $serviceId) {
                return false;
            }
            $db = $this->db->getDbAdapter();
            if ($this->providesOverrides()) {
                $this->blacklisted = 1 === (int)$db->fetchOne(
                    $db->select()->from('icinga_host_service_blacklist', 'COUNT(*)')
                        ->where('host_id = ?', $hostId)
                        ->where('service_id = ?', $serviceId)
                );
            } else {
                $this->blacklisted = false;
            }
        }

        return $this->blacklisted;
    }

    /**
     * @param $object
     * @throws IcingaException
     * @throws ProgrammingError
     * @throws \Zend_Db_Adapter_Exception
     */
    protected function deleteObject($object)
    {
        /** @var IcingaService $object */
        if ($this->providesOverrides()) {
            if ($this->hasBeenBlacklisted()) {
                $this->removeFromBlacklist();
            } else {
                $this->blacklist();
            }
        } else {
            parent::deleteObject($object);
        }
    }

    /**
     * @throws IcingaException
     * @throws \Zend_Db_Adapter_Exception
     */
    protected function blacklist()
    {
        $host = $this->host;
        $service = $this->getServiceToBeBlacklisted();

        $db = $this->db->getDbAdapter();
        $host->unsetOverriddenServiceVars($this->object->getObjectName())->store();

        if ($db->insert('icinga_host_service_blacklist', [
            'host_id'    => $host->get('id'),
            'service_id' => $service->get('id')
        ])) {
            $msg = sprintf(
                $this->translate('%s has been deactivated on %s'),
                $service->getObjectName(),
                $host->getObjectName()
            );
            $this->redirectOnSuccess($msg);
        }
    }

    /**
     * @return IcingaService
     * @throws \Icinga\Exception\NotFoundError
     */
    protected function getServiceToBeBlacklisted()
    {
        if ($this->set) {
            return $this->object;
        } else {
            return self::getFirstParent($this->object);
        }
    }

    /**
     * @throws \Icinga\Exception\NotFoundError
     */
    protected function removeFromBlacklist()
    {
        $host = $this->host;
        $service = $this->getServiceToBeBlacklisted();

        $db = $this->db->getDbAdapter();
        $where = implode(' AND ', [
            $db->quoteInto('host_id = ?', $host->get('id')),
            $db->quoteInto('service_id = ?', $service->get('id')),
        ]);
        if ($db->delete('icinga_host_service_blacklist', $where)) {
            $msg = sprintf(
                $this->translate('%s is no longer deactivated on %s'),
                $service->getObjectName(),
                $host->getObjectName()
            );
            $this->redirectOnSuccess($msg);
        }
    }

    /**
     * @param IcingaService $service
     * @return $this
     */
    public function createApplyRuleFor(IcingaService $service)
    {
        $this->apply = $service;
        $object = $this->object();
        $object->set('imports', $service->getObjectName());
        $object->set('object_type', 'apply');
        $object->set('object_name', $service->getObjectName());

        return $this;
    }

    /**
     * @throws \Zend_Form_Exception
     */
    protected function setupServiceElements()
    {
        if ($this->object) {
            $objectType = $this->object->get('object_type');
        } elseif ($this->preferredObjectType) {
            $objectType = $this->preferredObjectType;
        } else {
            $objectType = 'template';
        }
        $this->addHidden('object_type', $objectType);
        $forceCommandElements = $this->hasPermission(Permission::ADMIN);

        $this->addNameElement()
             ->addHostObjectElement()
             ->addImportsElement()
             ->addChoices('service')
             ->addGroupsElement()
             ->addDisabledElement()
             ->addApplyForElement()
             ->groupMainProperties()
             ->addAssignmentElements()
             ->addCheckCommandElements($forceCommandElements)
             ->addCheckExecutionElements()
             ->addExtraInfoElements()
             ->addAgentAndZoneElements()
             ->setButtons();
    }

    /**
     * @throws IcingaException
     * @throws ProgrammingError
     */
    protected function addOverrideHint()
    {
        if ($this->object && $this->object->usesVarOverrides()) {
            $hint = $this->translate(
                'This service has been generated in an automated way, but still'
                . ' allows you to override the following properties in a safe way.'
            );
        } elseif ($apply = $this->applyGenerated) {
            $hint = Html::sprintf(
                $this->translate(
                    'This service has been generated using the %s apply rule, assigned where %s'
                ),
                Link::create(
                    $apply->getObjectName(),
                    'director/service',
                    ['id' => $apply->get('id')],
                    ['data-base-target' => '_next']
                ),
                (string) Filter::fromQueryString($apply->assign_filter)
            );
        } elseif ($this->host && $this->set) {
            $hint = Html::sprintf(
                $this->translate(
                    'This service belongs to the %s Service Set. Still, you might want'
                    . ' to override the following properties for this host only.'
                ),
                Link::create(
                    $this->set->getObjectName(),
                    'director/serviceset',
                    ['id' => $this->set->get('id')],
                    ['data-base-target' => '_next']
                )
            );
        } elseif ($this->inheritedFrom) {
            $msg = $this->translate(
                'This service has been inherited from %s. Still, you might want'
                . ' to change the following properties for this host only.'
            );

            $name = $this->inheritedFrom;
            $link = Link::create(
                $name,
                'director/service',
                [
                    'host' => $name,
                    'name' => $this->object->getObjectName(),
                ],
                ['data-base-target' => '_next']
            );

            $hint = Html::sprintf($msg, $link);
        } else {
            throw new ProgrammingError('Got no override hint for your situation');
        }

        $this->setSubmitLabel($this->translate('Override vars'));

        $this->addHtmlHint($hint, ['name' => 'inheritance_hint']);
    }

    protected function setupOnHostForSet()
    {
        $msg = $this->translate(
            'This service belongs to the service set "%s". Still, you might want'
            . ' to change the following properties for this host only.'
        );

        $name = $this->set->getObjectName();
        $link = Link::create(
            $name,
            'director/serviceset',
            ['name' => $name],
            ['data-base-target' => '_next']
        );

        $this->addHtmlHint(
            Html::sprintf($msg, $link),
            ['name' => 'inheritance_hint']
        );

        $this->addElementsToGroup(
            ['inheritance_hint'],
            'custom_fields',
            50,
            $this->translate('Custom properties')
        );

        $this->setSubmitLabel($this->translate('Override vars'));
    }

    protected function addAssignmentElements()
    {
        $this->addAssignFilter([
            'suggestionContext' => 'HostFilterColumns',
            'required' => true,
            'description' => $this->translate(
                'This allows you to configure an assignment filter. Please feel'
                . ' free to combine as many nested operators as you want. The'
                . ' "contains" operator is valid for arrays only. Please use'
                . ' wildcards and the = (equals) operator when searching for'
                . ' partial string matches, like in *.example.com'
            )
        ]);

        return $this;
    }

    /**
     * @throws \Zend_Form_Exception
     */
    protected function setupHostRelatedElements()
    {
        $this->addHidden('host', $this->host->getObjectName());
        $this->addHidden('object_type', 'object');
        $this->addImportsElement();
        $imports = $this->getSentOrObjectValue('imports');

        if ($this->hasBeenSent()) {
            $imports = $this->getElement('imports')->setValue($imports)->getValue();
        }

        if ($this->isNew() && empty($imports)) {
            $this->groupMainProperties();
            return;
        }

        $this->addNameElement()
             ->addChoices('service')
             ->addDisabledElement()
             ->addGroupsElement()
             ->groupMainProperties()
             ->addCheckCommandElements()
             ->addExtraInfoElements()
             ->setButtons();

        $this->setDefaultNameFromTemplate($imports);
    }

    /**
     * @param IcingaHost $host
     * @return $this
     */
    public function setHost(IcingaHost $host)
    {
        $this->host = $host;
        return $this;
    }

    /**
     * @throws \Zend_Form_Exception
     */
    protected function setupSetRelatedElements()
    {
        $this->addHidden('service_set', $this->set->getObjectName());
        $this->addHidden('object_type', 'apply');
        $this->addImportsElement();
        $this->setButtons();
        $imports = $this->getSentOrObjectValue('imports');

        if ($this->hasBeenSent()) {
            $imports = $this->getElement('imports')->setValue($imports)->getValue();
        }

        if ($this->isNew() && empty($imports)) {
            $this->groupMainProperties();
            return;
        }

        $this->addNameElement()
             ->addDisabledElement()
             ->addGroupsElement()
             ->groupMainProperties();

        if ($this->hasPermission(Permission::ADMIN)) {
            $this->addCheckCommandElements(true)
                ->addCheckExecutionElements(true)
                ->addExtraInfoElements();
        }

        $this->setDefaultNameFromTemplate($imports);
    }

    public function setServiceSet(IcingaServiceSet $set)
    {
        $this->set = $set;
        return $this;
    }

    /**
     * @return $this
     * @throws \Zend_Form_Exception
     */
    protected function addNameElement()
    {
        $this->addElement('text', 'object_name', array(
            'label'       => $this->translate('Name'),
            'required'    => !$this->object()->isApplyRule(),
            'description' => $this->translate(
                'Name for the Icinga service you are going to create'
            )
        ));

        if ($this->object()->isApplyRule()) {
            $this->eventuallyAddNameRestriction('director/service/apply/filter-by-name');
        }

        return $this;
    }

    /**
     * @return $this
     * @throws \Zend_Form_Exception
     */
    protected function addHostObjectElement()
    {
        if ($this->isObject()) {
            $this->addElement('select', 'host', [
                'label'       => $this->translate('Host'),
                'required'    => true,
                'multiOptions' => $this->optionalEnum($this->enumHostsAndTemplates()),
                'description' => $this->translate(
                    'Choose the host this single service should be assigned to'
                )
            ]);
        }

        return $this;
    }

    /**
     * @return $this
     * @throws \Zend_Form_Exception
     */
    protected function addApplyForElement()
    {
        if ($this->object->isApplyRule()) {
            $hostProperties = IcingaHost::enumProperties(
                $this->object->getConnection(),
                'host.',
                new ArrayCustomVariablesFilter()
            );

            $this->addElement('select', 'apply_for', array(
                'label' => $this->translate('Apply For'),
                'class' => 'assign-property autosubmit',
                'multiOptions' => $this->optionalEnum($hostProperties, $this->translate('None')),
                'description' => $this->translate(
                    'Evaluates the apply for rule for ' .
                    'all objects with the custom attribute specified. ' .
                    'E.g selecting "host.vars.custom_attr" will generate "for (config in ' .
                    'host.vars.array_var)" where "config" will be accessible through "$config$". ' .
                    'NOTE: only custom variables of type "Array" are eligible.'
                )
            ));
        }

        return $this;
    }

    /**
     * @return $this
     * @throws \Zend_Form_Exception
     */
    protected function addGroupsElement()
    {
        $groups = $this->enumServicegroups();

        if (! empty($groups)) {
            $this->addElement('extensibleSet', 'groups', array(
                'label'        => $this->translate('Groups'),
                'multiOptions' => $this->optionallyAddFromEnum($groups),
                'positional'   => false,
                'description'  => $this->translate(
                    'Service groups that should be directly assigned to this service.'
                    . ' Servicegroups can be useful for various reasons. They are'
                    . ' helpful to provided service-type specific view in Icinga Web 2,'
                    . ' either for custom dashboards or as an instrument to enforce'
                    . ' restrictions. Service groups can be directly assigned to'
                    . ' single services or to service templates.'
                )
            ));
        }

        return $this;
    }

    /**
     * @return $this
     * @throws \Zend_Form_Exception
     */
    protected function addAgentAndZoneElements()
    {
        if (!$this->isTemplate()) {
            return $this;
        }

        $this->optionalBoolean(
            'use_agent',
            $this->translate('Run on agent'),
            $this->translate(
                'Whether the check commmand for this service should be executed'
                . ' on the Icinga agent'
            )
        );
        $this->addZoneElement();

        $elements = array(
            'use_agent',
            'zone_id',
        );
        $this->addDisplayGroup($elements, 'clustering', array(
            'decorators' => array(
                'FormElements',
                array('HtmlTag', array('tag' => 'dl')),
                'Fieldset',
            ),
            'order' => self::GROUP_ORDER_CLUSTERING,
            'legend' => $this->translate('Icinga Agent and zone settings')
        ));

        return $this;
    }

    protected function enumHostsAndTemplates()
    {
        if ($this->branch && $this->branch->isBranch()) {
            return $this->enumHosts();
        }

        return [
            $this->translate('Templates') => $this->enumHostTemplates(),
            $this->translate('Hosts')     => $this->enumHosts(),
        ];
    }

    protected function enumHostTemplates()
    {
        $names = array_values($this->db->enumHostTemplates());
        return array_combine($names, $names);
    }

    protected function enumHosts()
    {
        $db = $this->db->getDbAdapter();
        $table = new ObjectsTableHost($this->db, $this->getAuth());
        if ($this->branch && $this->branch->isBranch()) {
            $table->setBranchUuid($this->branch->getUuid());
        }
        $result = [];
        foreach ($db->fetchAll($table->getQuery()->reset(\Zend_Db_Select::LIMIT_COUNT)) as $row) {
            $result[$row->object_name] = $row->object_name;
        }

        return $result;
    }

    protected function enumServicegroups()
    {
        $db = $this->db->getDbAdapter();
        $select = $db->select()->from(
            'icinga_servicegroup',
            array(
                'name'    => 'object_name',
                'display' => 'COALESCE(display_name, object_name)'
            )
        )->where('object_type = ?', 'object')->order('display');

        return $db->fetchPairs($select);
    }

    protected function succeedForOverrides()
    {
        $vars = array();
        foreach ($this->object->vars() as $key => $var) {
            $vars[$key] = $var->getValue();
        }

        $host = $this->host;
        $serviceName = $this->object->getObjectName();

        $this->host->overrideServiceVars($serviceName, (object) $vars);

        if ($host->hasBeenModified()) {
            $msg = sprintf(
                empty($vars)
                ? $this->translate('All overrides have been removed from "%s"')
                : $this->translate('The given properties have been stored for "%s"'),
                $this->translate($host->getObjectName())
            );

            $this->getDbObjectStore()->store($host);
        } else {
            if ($this->isApiRequest()) {
                $this->setHttpResponseCode(304);
            }

            $msg = $this->translate('No action taken, object has not been modified');
        }

        $this->redirectOnSuccess($msg);
    }

    public function onSuccess()
    {
        if ($this->providesOverrides()) {
            $this->succeedForOverrides();
            return;
        }

        parent::onSuccess();
    }

    /**
     * @param array $imports
     */
    protected function setDefaultNameFromTemplate($imports)
    {
        if ($this->hasBeenSent()) {
            $name = $this->getSentOrObjectValue('object_name');
            if ($name === null || !strlen($name)) {
                $this->setElementValue('object_name', end($imports));
                $this->object->set('object_name', end($imports));
            }
        }
    }
}