summaryrefslogtreecommitdiffstats
path: root/modules/monitoring/application/forms/Command/Object/ProcessCheckResultCommandForm.php
blob: 7a196ec35d4844ace4c5bdea3087e8bc8a7fc798 (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
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Monitoring\Forms\Command\Object;

use Icinga\Web\Notification;
use Icinga\Module\Monitoring\Command\Object\ProcessCheckResultCommand;

/**
 * Form for submitting a passive host or service check result
 */
class ProcessCheckResultCommandForm extends ObjectsCommandForm
{
    /**
     * Initialize this form
     */
    public function init()
    {
        $this->addDescription($this->translate(
            'This command is used to submit passive host or service check results.'
        ));
    }

    /**
     * (non-PHPDoc)
     * @see \Icinga\Web\Form::getSubmitLabel() For the method documentation.
     */
    public function getSubmitLabel()
    {
        return $this->translatePlural(
            'Submit Passive Check Result',
            'Submit Passive Check Results',
            count($this->objects)
        );
    }

    /**
     * (non-PHPDoc)
     * @see \Icinga\Web\Form::createElements() For the method documentation.
     */
    public function createElements(array $formData)
    {
        $object = null;
        foreach ($this->getObjects() as $object) {
            /** @var \Icinga\Module\Monitoring\Object\MonitoredObject $object */
            // Nasty, but as getObjects() returns everything but an object with a real
            // iterator interface this is the only way to fetch just the first element
            break;
        }

        $this->addElement(
            'select',
            'status',
            array(
                'required'      => true,
                'label'         => $this->translate('Status'),
                'description'   => $this->translate('The state this check result should report'),
                'multiOptions'  => $object->getType() === $object::TYPE_HOST ? $this->getHostMultiOptions() : array(
                    ProcessCheckResultCommand::SERVICE_OK       => $this->translate('OK', 'icinga.state'),
                    ProcessCheckResultCommand::SERVICE_WARNING  => $this->translate('WARNING', 'icinga.state'),
                    ProcessCheckResultCommand::SERVICE_CRITICAL => $this->translate('CRITICAL', 'icinga.state'),
                    ProcessCheckResultCommand::SERVICE_UNKNOWN  => $this->translate('UNKNOWN', 'icinga.state')
                )
            )
        );
        $this->addElement(
            'text',
            'output',
            array(
                'required'      => true,
                'label'         => $this->translate('Output'),
                'description'   => $this->translate('The plugin output of this check result')
            )
        );
        $this->addElement(
            'text',
            'perfdata',
            array(
                'allowEmpty'    => true,
                'label'         => $this->translate('Performance Data'),
                'description'   => $this->translate(
                    'The performance data of this check result. Leave empty'
                    . ' if this check result has no performance data'
                )
            )
        );
    }

    /**
     * (non-PHPDoc)
     * @see \Icinga\Web\Form::onSuccess() For the method documentation.
     */
    public function onSuccess()
    {
        foreach ($this->objects as $object) {
            /** @var \Icinga\Module\Monitoring\Object\MonitoredObject $object */
            if (! $object->passive_checks_enabled) {
                continue;
            }

            $command = new ProcessCheckResultCommand();
            $command->setObject($object);
            $command->setStatus($this->getValue('status'));
            $command->setOutput($this->getValue('output'));

            if ($perfdata = $this->getValue('perfdata')) {
                $command->setPerformanceData($perfdata);
            }

            $this->getTransport($this->request)->send($command);
        }

        Notification::success($this->translatePlural(
            'Processing check result..',
            'Processing check results..',
            count($this->objects)
        ));

        return true;
    }

    /**
     * Returns the available host options based on the program version
     *
     * @return array
     */
    protected function getHostMultiOptions()
    {
        $options =  array(
            ProcessCheckResultCommand::HOST_UP => $this->translate('UP', 'icinga.state'),
            ProcessCheckResultCommand::HOST_DOWN => $this->translate('DOWN', 'icinga.state')
        );

        if (! $this->getBackend()->isIcinga2()) {
            $options[ProcessCheckResultCommand::HOST_UNREACHABLE] = $this->translate('UNREACHABLE', 'icinga.state');
        }

        return $options;
    }
}