[icinga-checkins] icinga.org: icingaweb2/master: Support scheduling a downtime for all services of a host w/ the Icinga 2 API as command transport

git at icinga.org git at icinga.org
Thu Dec 8 10:40:33 CET 2016


Module: icingaweb2
Branch: master
Commit: 4f6c54e62cb88496d3de5e977a89b13e13c1caa0
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=4f6c54e62cb88496d3de5e977a89b13e13c1caa0

Author: Eric Lippmann <eric.lippmann at icinga.com>
Date:   Wed Dec  7 15:54:56 2016 +0100

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

fixes #12810

---

 .../Monitoring/Command/IcingaApiCommand.php        |   40 ++++++++++++++++++++
 .../Command/Renderer/IcingaApiCommandRenderer.php  |   26 ++++++++++---
 .../Command/Transport/ApiCommandTransport.php      |   29 ++++++++------
 3 files changed, 78 insertions(+), 17 deletions(-)

diff --git a/modules/monitoring/library/Monitoring/Command/IcingaApiCommand.php b/modules/monitoring/library/Monitoring/Command/IcingaApiCommand.php
index b14f96b..c33157f 100644
--- a/modules/monitoring/library/Monitoring/Command/IcingaApiCommand.php
+++ b/modules/monitoring/library/Monitoring/Command/IcingaApiCommand.php
@@ -20,6 +20,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
      *
      * @param   string  $endpoint
@@ -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;
+    }
 }
diff --git a/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php
index 7f6b295..30dc414 100644
--- a/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php
+++ b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php
@@ -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)
diff --git a/modules/monitoring/library/Monitoring/Command/Transport/ApiCommandTransport.php b/modules/monitoring/library/Monitoring/Command/Transport/ApiCommandTransport.php
index 1058e91..8b00bd1 100644
--- a/modules/monitoring/library/Monitoring/Command/Transport/ApiCommandTransport.php
+++ b/modules/monitoring/library/Monitoring/Command/Transport/ApiCommandTransport.php
@@ -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));
     }
 }



More information about the icinga-checkins mailing list