Support scheduling a downtime for all services of a host w/ the Icinga 2 API as command transport

fixes #12810
This commit is contained in:
Eric Lippmann 2016-12-07 15:54:56 +01:00
parent 2156eb1a8c
commit 4f6c54e62c
3 changed files with 78 additions and 17 deletions

View File

@ -19,6 +19,13 @@ class IcingaApiCommand
*/
protected $endpoint;
/**
* Next Icinga API command to be sent, if any
*
* @var static
*/
protected $next;
/**
* Create a new Icinga 2 API command
*
@ -83,4 +90,37 @@ class IcingaApiCommand
return $this;
}
/**
* Get whether another Icinga API command should be sent after this one
*
* @return bool
*/
public function hasNext()
{
return $this->next !== null;
}
/**
* Get the next Icinga API command
*
* @return IcingaApiCommand
*/
public function getNext()
{
return $this->next;
}
/**
* Set the next Icinga API command
*
* @param IcingaApiCommand $next
*
* @return IcingaApiCommand
*/
public function setNext(IcingaApiCommand $next)
{
$this->next = $next;
return $next;
}
}

View File

@ -12,6 +12,7 @@ use Icinga\Module\Monitoring\Command\Object\DeleteDowntimeCommand;
use Icinga\Module\Monitoring\Command\Object\ProcessCheckResultCommand;
use Icinga\Module\Monitoring\Command\Object\PropagateHostDowntimeCommand;
use Icinga\Module\Monitoring\Command\Object\RemoveAcknowledgementCommand;
use Icinga\Module\Monitoring\Command\Object\ScheduleHostDowntimeCommand;
use Icinga\Module\Monitoring\Command\Object\ScheduleServiceCheckCommand;
use Icinga\Module\Monitoring\Command\Object\ScheduleServiceDowntimeCommand;
use Icinga\Module\Monitoring\Command\Object\SendCustomNotificationCommand;
@ -149,14 +150,27 @@ class IcingaApiCommandRenderer implements IcingaCommandRendererInterface
'fixed' => $command->getFixed(),
'trigger_name' => $command->getTriggerId()
);
if ($command->getObject()->getType() === $command::TYPE_HOST
&& $command instanceof PropagateHostDowntimeCommand
) {
$commandData = $data;
if ($command instanceof PropagateHostDowntimeCommand) {
/** @var \Icinga\Module\Monitoring\Command\Object\PropagateHostDowntimeCommand $command */
$data['child_options'] = $command->getTriggered() ? 1 : 2;
$commandData['child_options'] = $command->getTriggered() ? 1 : 2;
}
$this->applyFilter($data, $command->getObject());
return IcingaApiCommand::create($endpoint, $data);
$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(
'host_name' => $command->getObject()->getName()
)
);
$apiCommand->setNext(IcingaApiCommand::create($endpoint, $commandData));
}
return $apiCommand;
}
public function renderAcknowledgeProblem(AcknowledgeProblemCommand $command)

View File

@ -4,6 +4,7 @@
namespace Icinga\Module\Monitoring\Command\Transport;
use Icinga\Application\Logger;
use Icinga\Module\Monitoring\Command\IcingaApiCommand;
use Icinga\Module\Monitoring\Command\IcingaCommand;
use Icinga\Module\Monitoring\Command\Renderer\IcingaApiCommandRenderer;
use Icinga\Module\Monitoring\Exception\CommandTransportException;
@ -184,17 +185,8 @@ class ApiCommandTransport implements CommandTransportInterface
return sprintf('https://%s:%u/v1/%s', $this->getHost(), $this->getPort(), $endpoint);
}
/**
* Send the Icinga command over the Icinga 2 API
*
* @param IcingaCommand $command
* @param int|null $now
*
* @throws CommandTransportException
*/
public function send(IcingaCommand $command, $now = null)
protected function sendCommand(IcingaApiCommand $command)
{
$command = $this->renderer->render($command);
Logger::debug(
'Sending Icinga command "%s" to the API "%s:%u"',
$command->getEndpoint(),
@ -215,7 +207,6 @@ class ApiCommandTransport implements CommandTransportInterface
);
}
$result = array_pop($response['results']);
if ($result['code'] < 200 || $result['code'] >= 300) {
throw new CommandTransportException(
'Can\'t send external Icinga command: %u %s',
@ -223,5 +214,21 @@ class ApiCommandTransport implements CommandTransportInterface
$result['status']
);
}
if ($command->hasNext()) {
$this->sendCommand($command->getNext());
}
}
/**
* Send the Icinga command over the Icinga 2 API
*
* @param IcingaCommand $command
* @param int|null $now
*
* @throws CommandTransportException
*/
public function send(IcingaCommand $command, $now = null)
{
$this->sendCommand($this->renderer->render($command));
}
}