[icinga-checkins] icinga.org: icingaweb2/bugfix/ schedule-a-downtime-for-all-services-of-a-host-w-the-icinga-2-api-12810: Make scheduling downtimes for all services of a host working via the Icinga 2 API

git at icinga.org git at icinga.org
Thu Oct 13 18:41:45 CEST 2016


Module: icingaweb2
Branch: bugfix/schedule-a-downtime-for-all-services-of-a-host-w-the-icinga-2-api-12810
Commit: 4c35e9dd3bb19efcd690737866b32c9bdfaf583c
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=4c35e9dd3bb19efcd690737866b32c9bdfaf583c

Author: Alexander A. Klimov <alexander.klimov at netways.de>
Date:   Thu Oct 13 18:40:47 2016 +0200

Make scheduling downtimes for all services of a host working via the Icinga 2 API

refs #12810

---

 .../Command/Renderer/IcingaApiCommandRenderer.php  |   33 ++++++++++++++++++--
 1 file changed, 30 insertions(+), 3 deletions(-)

diff --git a/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php
index 79f2125..5872e41 100644
--- a/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php
+++ b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php
@@ -3,6 +3,7 @@
 
 namespace Icinga\Module\Monitoring\Command\Renderer;
 
+use Icinga\Module\Monitoring\Command\IcingaApiCommand;
 use Icinga\Module\Monitoring\Command\IcingaApiCommandList;
 use Icinga\Module\Monitoring\Command\Instance\ToggleInstanceFeatureCommand;
 use Icinga\Module\Monitoring\Command\Object\AcknowledgeProblemCommand;
@@ -11,6 +12,7 @@ use Icinga\Module\Monitoring\Command\Object\DeleteCommentCommand;
 use Icinga\Module\Monitoring\Command\Object\DeleteDowntimeCommand;
 use Icinga\Module\Monitoring\Command\Object\ProcessCheckResultCommand;
 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;
@@ -75,6 +77,22 @@ class IcingaApiCommandRenderer implements IcingaCommandRendererInterface
     }
 
     /**
+     * Return $rawString as an Icinga 2 config language string literal
+     *
+     * @param   string  $rawString
+     *
+     * @return  string
+     */
+    protected function escapeIcinga2String($rawString)
+    {
+        return '"' . str_replace(
+            array('"', '\\', "\t", "\r", "\n", "\x07", "\f"),
+            array('\"', '\\\\', '\t', '\r', '\n', '\b', '\f'),
+            $rawString
+        ) . '"';
+    }
+
+    /**
      * Render a command
      *
      * @param   IcingaCommand   $command
@@ -139,7 +157,7 @@ class IcingaApiCommandRenderer implements IcingaCommandRendererInterface
     public function renderScheduleDowntime(ScheduleServiceDowntimeCommand $command)
     {
         $endpoint = 'actions/schedule-downtime';
-        $data = array(
+        $dataHost = $data = array(
             'author'        => $command->getAuthor(),
             'comment'       => $command->getComment(),
             'start_time'    => $command->getStart(),
@@ -148,8 +166,17 @@ class IcingaApiCommandRenderer implements IcingaCommandRendererInterface
             'fixed'         => $command->getFixed(),
             'trigger_name'  => $command->getTriggerId()
         );
-        $this->applyFilter($data, $command->getObject());
-        return IcingaApiCommandList::create($endpoint, $data);
+        $this->applyFilter($dataHost, $command->getObject());
+        $commands = array(IcingaApiCommand::create($endpoint, $dataHost));
+
+        if ($command instanceof ScheduleHostDowntimeCommand && $command->getForAllServices()) {
+            $commands[] = IcingaApiCommand::create($endpoint, $data + array(
+                'type'      => 'Service',
+                'filter'    => 'host.name==' . $this->escapeIcinga2String($command->getObject()->getName())
+            ));
+        }
+
+        return new IcingaApiCommandList($commands);
     }
 
     public function renderAcknowledgeProblem(AcknowledgeProblemCommand $command)



More information about the icinga-checkins mailing list