2021-02-12 06:21:55 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/* Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2 */
|
|
|
|
|
|
|
|
|
|
namespace Icinga\Module\Icingadb\Forms\Command\Object;
|
|
|
|
|
|
2023-03-01 07:11:15 -05:00
|
|
|
use Generator;
|
2021-02-12 06:21:55 -05:00
|
|
|
use Icinga\Module\Icingadb\Command\Object\ProcessCheckResultCommand;
|
|
|
|
|
use Icinga\Module\Icingadb\Forms\Command\CommandForm;
|
|
|
|
|
use Icinga\Module\Icingadb\Model\Host;
|
2021-08-31 11:12:22 -04:00
|
|
|
use Icinga\Web\Notification;
|
2021-06-22 04:29:25 -04:00
|
|
|
use ipl\Html\Attributes;
|
2021-02-12 06:21:55 -05:00
|
|
|
use ipl\Html\HtmlElement;
|
2021-06-22 04:29:25 -04:00
|
|
|
use ipl\Html\Text;
|
2021-02-12 06:21:55 -05:00
|
|
|
use ipl\Orm\Model;
|
|
|
|
|
use ipl\Web\FormDecorator\IcingaFormDecorator;
|
|
|
|
|
use ipl\Web\Widget\Icon;
|
2024-03-26 10:58:17 -04:00
|
|
|
use Iterator;
|
2023-03-01 04:22:13 -05:00
|
|
|
use Traversable;
|
2021-02-12 06:21:55 -05:00
|
|
|
|
2023-08-31 10:29:20 -04:00
|
|
|
use function ipl\Stdlib\iterable_value_first;
|
|
|
|
|
|
2021-02-12 06:21:55 -05:00
|
|
|
class ProcessCheckResultForm extends CommandForm
|
|
|
|
|
{
|
2021-08-31 11:12:22 -04:00
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->on(self::ON_SUCCESS, function () {
|
2021-09-20 04:24:01 -04:00
|
|
|
if ($this->errorOccurred) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 11:12:22 -04:00
|
|
|
$countObjects = count($this->getObjects());
|
2023-08-31 10:29:20 -04:00
|
|
|
if (iterable_value_first($this->getObjects()) instanceof Host) {
|
2021-08-31 11:12:22 -04:00
|
|
|
$message = sprintf(tp(
|
|
|
|
|
'Submitted passive check result successfully',
|
|
|
|
|
'Submitted passive check result for %d hosts successfully',
|
|
|
|
|
$countObjects
|
|
|
|
|
), $countObjects);
|
|
|
|
|
} else {
|
|
|
|
|
$message = sprintf(tp(
|
|
|
|
|
'Submitted passive check result successfully',
|
|
|
|
|
'Submitted passive check result for %d services successfully',
|
|
|
|
|
$countObjects
|
|
|
|
|
), $countObjects);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Notification::success($message);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-12 06:21:55 -05:00
|
|
|
protected function assembleElements()
|
|
|
|
|
{
|
2021-06-22 04:29:25 -04:00
|
|
|
$this->addHtml(new HtmlElement(
|
|
|
|
|
'div',
|
|
|
|
|
Attributes::create(['class' => 'form-description']),
|
2021-02-12 06:21:55 -05:00
|
|
|
new Icon('info-circle', ['class' => 'form-description-icon']),
|
2021-06-22 04:29:25 -04:00
|
|
|
new HtmlElement(
|
|
|
|
|
'ul',
|
|
|
|
|
null,
|
|
|
|
|
new HtmlElement(
|
|
|
|
|
'li',
|
|
|
|
|
null,
|
|
|
|
|
Text::create(t('This command is used to submit passive host or service check results.'))
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
));
|
2021-02-12 06:21:55 -05:00
|
|
|
|
|
|
|
|
$decorator = new IcingaFormDecorator();
|
|
|
|
|
|
2023-08-31 10:29:20 -04:00
|
|
|
/** @var Model $object */
|
|
|
|
|
$object = iterable_value_first($this->getObjects());
|
2021-02-12 06:21:55 -05:00
|
|
|
|
|
|
|
|
$this->addElement(
|
|
|
|
|
'select',
|
|
|
|
|
'status',
|
|
|
|
|
[
|
|
|
|
|
'required' => true,
|
|
|
|
|
'label' => t('Status'),
|
|
|
|
|
'description' => t('The state this check result should report'),
|
2022-11-08 09:35:40 -05:00
|
|
|
'options' => $object instanceof Host ? [
|
2021-02-12 06:21:55 -05:00
|
|
|
ProcessCheckResultCommand::HOST_UP => t('UP', 'icinga.state'),
|
|
|
|
|
ProcessCheckResultCommand::HOST_DOWN => t('DOWN', 'icinga.state')
|
|
|
|
|
] : [
|
|
|
|
|
ProcessCheckResultCommand::SERVICE_OK => t('OK', 'icinga.state'),
|
|
|
|
|
ProcessCheckResultCommand::SERVICE_WARNING => t('WARNING', 'icinga.state'),
|
|
|
|
|
ProcessCheckResultCommand::SERVICE_CRITICAL => t('CRITICAL', 'icinga.state'),
|
|
|
|
|
ProcessCheckResultCommand::SERVICE_UNKNOWN => t('UNKNOWN', 'icinga.state')
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
$decorator->decorate($this->getElement('status'));
|
|
|
|
|
|
|
|
|
|
$this->addElement(
|
|
|
|
|
'text',
|
|
|
|
|
'output',
|
|
|
|
|
[
|
|
|
|
|
'required' => true,
|
|
|
|
|
'label' => t('Output'),
|
|
|
|
|
'description' => t('The plugin output of this check result')
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
$decorator->decorate($this->getElement('output'));
|
|
|
|
|
|
|
|
|
|
$this->addElement(
|
|
|
|
|
'text',
|
|
|
|
|
'perfdata',
|
|
|
|
|
[
|
|
|
|
|
'allowEmpty' => true,
|
|
|
|
|
'label' => t('Performance Data'),
|
|
|
|
|
'description' => t(
|
|
|
|
|
'The performance data of this check result. Leave empty'
|
|
|
|
|
. ' if this check result has no performance data'
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
$decorator->decorate($this->getElement('perfdata'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function assembleSubmitButton()
|
|
|
|
|
{
|
|
|
|
|
$this->addElement(
|
|
|
|
|
'submit',
|
|
|
|
|
'btn_submit',
|
|
|
|
|
[
|
|
|
|
|
'required' => true,
|
|
|
|
|
'label' => tp(
|
|
|
|
|
'Submit Passive Check Result',
|
|
|
|
|
'Submit Passive Check Results',
|
|
|
|
|
count($this->getObjects())
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
(new IcingaFormDecorator())->decorate($this->getElement('btn_submit'));
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 10:58:17 -04:00
|
|
|
protected function getCommands(Iterator $objects): Traversable
|
2021-02-12 06:21:55 -05:00
|
|
|
{
|
2023-03-01 07:11:15 -05:00
|
|
|
$granted = (function () use ($objects): Generator {
|
|
|
|
|
foreach ($this->filterGrantedOn('icingadb/command/process-check-result', $objects) as $object) {
|
|
|
|
|
if ($object->passive_checks_enabled) {
|
|
|
|
|
yield $object;
|
|
|
|
|
}
|
2023-03-01 04:22:13 -05:00
|
|
|
}
|
2023-03-01 07:11:15 -05:00
|
|
|
})();
|
2021-03-16 11:34:13 -04:00
|
|
|
|
2023-03-01 07:11:15 -05:00
|
|
|
if ($granted->valid()) {
|
2023-03-01 04:22:13 -05:00
|
|
|
$command = new ProcessCheckResultCommand();
|
2023-03-01 07:11:15 -05:00
|
|
|
$command->setObjects($granted);
|
2023-03-01 04:22:13 -05:00
|
|
|
$command->setStatus($this->getValue('status'));
|
|
|
|
|
$command->setOutput($this->getValue('output'));
|
|
|
|
|
$command->setPerformanceData($this->getValue('perfdata'));
|
2021-02-12 06:21:55 -05:00
|
|
|
|
2023-03-01 04:22:13 -05:00
|
|
|
yield $command;
|
|
|
|
|
}
|
2021-02-12 06:21:55 -05:00
|
|
|
}
|
|
|
|
|
}
|