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; protected $endpoint;
/**
* Next Icinga API command to be sent, if any
*
* @var static
*/
protected $next;
/** /**
* Create a new Icinga 2 API command * Create a new Icinga 2 API command
* *
@ -83,4 +90,37 @@ class IcingaApiCommand
return $this; 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\ProcessCheckResultCommand;
use Icinga\Module\Monitoring\Command\Object\PropagateHostDowntimeCommand; use Icinga\Module\Monitoring\Command\Object\PropagateHostDowntimeCommand;
use Icinga\Module\Monitoring\Command\Object\RemoveAcknowledgementCommand; 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\ScheduleServiceCheckCommand;
use Icinga\Module\Monitoring\Command\Object\ScheduleServiceDowntimeCommand; use Icinga\Module\Monitoring\Command\Object\ScheduleServiceDowntimeCommand;
use Icinga\Module\Monitoring\Command\Object\SendCustomNotificationCommand; use Icinga\Module\Monitoring\Command\Object\SendCustomNotificationCommand;
@ -149,14 +150,27 @@ class IcingaApiCommandRenderer implements IcingaCommandRendererInterface
'fixed' => $command->getFixed(), 'fixed' => $command->getFixed(),
'trigger_name' => $command->getTriggerId() 'trigger_name' => $command->getTriggerId()
); );
if ($command->getObject()->getType() === $command::TYPE_HOST $commandData = $data;
&& $command instanceof PropagateHostDowntimeCommand if ($command instanceof PropagateHostDowntimeCommand) {
) {
/** @var \Icinga\Module\Monitoring\Command\Object\PropagateHostDowntimeCommand $command */ /** @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()); $this->applyFilter($commandData, $command->getObject());
return IcingaApiCommand::create($endpoint, $data); $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) public function renderAcknowledgeProblem(AcknowledgeProblemCommand $command)

View File

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