[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
Wed Oct 12 17:49:08 CEST 2016


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

Author: Alexander A. Klimov <alexander.klimov at netways.de>
Date:   Wed Oct 12 17:47:08 2016 +0200

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

refs #12810

---

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

diff --git a/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php
index cb346bc..a45976a 100644
--- a/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php
+++ b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php
@@ -11,6 +11,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;
@@ -60,14 +61,22 @@ class IcingaApiCommandRenderer implements IcingaCommandRendererInterface
      *
      * @param   array           $data
      * @param   MonitoredObject $object
+     * @param   bool            $allServices
      *
      * @return  array
      */
-    protected function applyFilter(array &$data, MonitoredObject $object)
+    protected function applyFilter(array &$data, MonitoredObject $object, $allServices = false)
     {
         if ($object->getType() === $object::TYPE_HOST) {
             /** @var \Icinga\Module\Monitoring\Object\Host $object */
-            $data['host'] = $object->getName();
+            if ($allServices) {
+                $data += array(
+                    'type' => 'Service',
+                    'filter' => 'host.name==' . $this->escapeIcinga2String($object->getName())
+                );
+            } else {
+                $data['host'] = $object->getName();
+            }
         } else {
             /** @var \Icinga\Module\Monitoring\Object\Service $object */
             $data['service'] = sprintf('%s!%s', $object->getHost()->getName(), $object->getName());
@@ -75,6 +84,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
@@ -148,7 +173,11 @@ class IcingaApiCommandRenderer implements IcingaCommandRendererInterface
             'fixed'         => $command->getFixed(),
             'trigger_name'  => $command->getTriggerId()
         );
-        $this->applyFilter($data, $command->getObject());
+        $this->applyFilter(
+            $data,
+            $command->getObject(),
+            $command instanceof ScheduleHostDowntimeCommand && $command->getForAllServices()
+        );
         return IcingaApiCommand::create($endpoint, $data);
     }
 



More information about the icinga-checkins mailing list