If appropriate, set the API parameter all_services for schedule-downtime
Since Icinga 2.11.0 the schedule-downtime API supports the all_services parameter. So far we've always sent a separate request for scheduling service downtimes. As of Icinga 2.13.0, these service downtimes are automatically removed when the host downtimes are removed. Of course, this doesn't work if we don't use the all_services parameter but send a separate request. With this commit we set this parameter if the transport is API and Icinga is equal to or greater than 2.11.0. In addition, if child_options and all_services were previously set, a request was sent per host and service. This is now also only a single request if an API command transport is requested or only API command transports are configured.
This commit is contained in:
parent
576833eaf9
commit
1e1b4b74ad
|
@ -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();
|
||||
};
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
/* Icinga Web 2 | (c) 2021 Icinga GmbH | GPLv2+ */
|
||||
|
||||
namespace Icinga\Module\Monitoring\Command\Object;
|
||||
|
||||
/**
|
||||
* Schedule host downtime command for API command transport and Icinga >= 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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue