diff --git a/modules/monitoring/application/forms/Command/Object/ScheduleHostDowntimeCommandForm.php b/modules/monitoring/application/forms/Command/Object/ScheduleHostDowntimeCommandForm.php index 73e820614..89db1cee2 100644 --- a/modules/monitoring/application/forms/Command/Object/ScheduleHostDowntimeCommandForm.php +++ b/modules/monitoring/application/forms/Command/Object/ScheduleHostDowntimeCommandForm.php @@ -6,9 +6,12 @@ namespace Icinga\Module\Monitoring\Forms\Command\Object; use DateInterval; use DateTime; use Icinga\Application\Config; +use Icinga\Module\Monitoring\Command\Object\ApiScheduleHostDowntimeCommand; use Icinga\Module\Monitoring\Command\Object\PropagateHostDowntimeCommand; use Icinga\Module\Monitoring\Command\Object\ScheduleHostDowntimeCommand; use Icinga\Module\Monitoring\Command\Object\ScheduleServiceDowntimeCommand; +use Icinga\Module\Monitoring\Command\Transport\ApiCommandTransport; +use Icinga\Module\Monitoring\Command\Transport\CommandTransport; use Icinga\Web\Notification; /** @@ -105,7 +108,37 @@ class ScheduleHostDowntimeCommandForm extends ScheduleServiceDowntimeCommandForm return false; } + // Send all_services API parameter if Icinga is equal to or greater than 2.11.0 + $allServicesNative = version_compare($this->getBackend()->getProgramVersion(), '2.11.0', '>='); + // Use ApiScheduleHostDowntimeCommand only when Icinga is equal to or greater than 2.11.0 and + // when an API command transport is requested or only API command transports are configured: + $useApiDowntime = $allServicesNative; + if ($useApiDowntime) { + $transport = $this->getTransport($this->getRequest()); + if ($transport instanceof CommandTransport) { + foreach ($transport::getConfig() as $config) { + if (strtolower($config->transport) !== 'api') { + $useApiDowntime = false; + break; + } + } + } elseif (! $transport instanceof ApiCommandTransport) { + $useApiDowntime = false; + } + } + foreach ($this->objects as $object) { + if ($useApiDowntime) { + $hostDowntime = (new ApiScheduleHostDowntimeCommand()) + ->setForAllServices($this->getElement('all_services')->isChecked()) + ->setChildOptions((int) $this->getElement('child_hosts')->getValue()); + // Code duplicated for readability and scope + $hostDowntime->setObject($object); + $this->scheduleDowntime($hostDowntime, $this->request); + + continue; + } + /** @var \Icinga\Module\Monitoring\Object\Host $object */ if (($childHostsEl = $this->getElement('child_hosts')) !== null) { $childHosts = (int) $childHostsEl->getValue(); @@ -114,7 +147,8 @@ class ScheduleHostDowntimeCommandForm extends ScheduleServiceDowntimeCommandForm } $allServices = $this->getElement('all_services')->isChecked(); if ($childHosts === 0) { - $hostDowntime = new ScheduleHostDowntimeCommand(); + $hostDowntime = (new ScheduleHostDowntimeCommand()) + ->setForAllServicesNative($allServicesNative); if ($allServices === true) { $hostDowntime->setForAllServices(); }; diff --git a/modules/monitoring/library/Monitoring/Command/Object/ApiScheduleHostDowntimeCommand.php b/modules/monitoring/library/Monitoring/Command/Object/ApiScheduleHostDowntimeCommand.php new file mode 100644 index 000000000..6495375a0 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Object/ApiScheduleHostDowntimeCommand.php @@ -0,0 +1,40 @@ += 2.11.0 that + * sends all_services and child_options in a single request + */ +class ApiScheduleHostDowntimeCommand extends ScheduleHostDowntimeCommand +{ + /** @var int Whether no, triggered, or non-triggered child downtimes should be scheduled */ + protected $childOptions; + + protected $forAllServicesNative = true; + + /** + * Get child options, i.e. whether no, triggered, or non-triggered child downtimes should be scheduled + * + * @return int + */ + public function getChildOptions() + { + return $this->childOptions; + } + + /** + * Set child options, i.e. whether no, triggered, or non-triggered child downtimes should be scheduled + * + * @param int $childOptions + * + * @return $this + */ + public function setChildOptions($childOptions) + { + $this->childOptions = $childOptions; + + return $this; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Object/ScheduleHostDowntimeCommand.php b/modules/monitoring/library/Monitoring/Command/Object/ScheduleHostDowntimeCommand.php index 9cc8d191d..3ac37d35e 100644 --- a/modules/monitoring/library/Monitoring/Command/Object/ScheduleHostDowntimeCommand.php +++ b/modules/monitoring/library/Monitoring/Command/Object/ScheduleHostDowntimeCommand.php @@ -23,6 +23,9 @@ class ScheduleHostDowntimeCommand extends ScheduleServiceDowntimeCommand */ protected $forAllServices = false; + /** @var bool Whether to send the all_services API parameter */ + protected $forAllServicesNative; + /** * Set whether to schedule a downtime for all services associated with a particular host * @@ -45,4 +48,28 @@ class ScheduleHostDowntimeCommand extends ScheduleServiceDowntimeCommand { return $this->forAllServices; } + + /** + * Get whether to send the all_services API parameter + * + * @return bool + */ + public function isForAllServicesNative() + { + return $this->forAllServicesNative; + } + + /** + * Get whether to send the all_services API parameter + * + * @param bool $forAllServicesNative + * + * @return $this + */ + public function setForAllServicesNative($forAllServicesNative = true) + { + $this->forAllServicesNative = (bool) $forAllServicesNative; + + return $this; + } } diff --git a/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php index 3eb9a84c4..8370314dc 100644 --- a/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php +++ b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php @@ -3,10 +3,12 @@ namespace Icinga\Module\Monitoring\Command\Renderer; +use Icinga\Module\Monitoring\Backend\MonitoringBackend; use Icinga\Module\Monitoring\Command\IcingaApiCommand; use Icinga\Module\Monitoring\Command\Instance\ToggleInstanceFeatureCommand; use Icinga\Module\Monitoring\Command\Object\AcknowledgeProblemCommand; use Icinga\Module\Monitoring\Command\Object\AddCommentCommand; +use Icinga\Module\Monitoring\Command\Object\ApiScheduleHostDowntimeCommand; use Icinga\Module\Monitoring\Command\Object\DeleteCommentCommand; use Icinga\Module\Monitoring\Command\Object\DeleteDowntimeCommand; use Icinga\Module\Monitoring\Command\Object\ProcessCheckResultCommand; @@ -156,25 +158,40 @@ class IcingaApiCommandRenderer implements IcingaCommandRendererInterface 'trigger_name' => $command->getTriggerId() ); $commandData = $data; + if ($command instanceof PropagateHostDowntimeCommand) { - /** @var \Icinga\Module\Monitoring\Command\Object\PropagateHostDowntimeCommand $command */ $commandData['child_options'] = $command->getTriggered() ? 1 : 2; + } elseif ($command instanceof ApiScheduleHostDowntimeCommand) { + // We assume that it has previously been verified that the Icinga version is + // equal to or greater than 2.11.0 + $commandData['child_options'] = $command->getChildOptions(); } + + $allServicesCompat = false; + if ($command instanceof ScheduleHostDowntimeCommand) { + if ($command->isForAllServicesNative()) { + // We assume that it has previously been verified that the Icinga version is + // equal to or greater than 2.11.0 + $commandData['all_services'] = $command->getForAllServices(); + } else { + $allServicesCompat = $command->getForAllServices(); + } + } + $this->applyFilter($commandData, $command->getObject()); $apiCommand = IcingaApiCommand::create($endpoint, $commandData); - if ($command instanceof ScheduleHostDowntimeCommand - /** @var \Icinga\Module\Monitoring\Command\Object\ScheduleHostDowntimeCommand $command */ - && $command->getForAllServices() - ) { - $commandData = $data + array( - 'type' => 'Service', - 'filter' => 'host.name == host_name', - 'filter_vars' => array( + + if ($allServicesCompat) { + $commandData = $data + [ + 'type' => 'Service', + 'filter' => 'host.name == host_name', + 'filter_vars' => [ 'host_name' => $command->getObject()->getName() - ) - ); + ] + ]; $apiCommand->setNext(IcingaApiCommand::create($endpoint, $commandData)); } + return $apiCommand; }