]> granicus.if.org Git - icinga2/commitdiff
Combine all_services with child_options for schedule-downtime API action
authorMichael Friedrich <michael.friedrich@icinga.com>
Tue, 4 Jun 2019 08:04:54 +0000 (10:04 +0200)
committerMichael Friedrich <michael.friedrich@icinga.com>
Thu, 6 Jun 2019 09:37:22 +0000 (11:37 +0200)
doc/12-icinga2-api.md
lib/icinga/apiactions.cpp

index b1c1b5b1b36872ca5776ec169de31c46abb87112..f05b94ecee56e20fc7ef50d65b3b7824d433b0de 100644 (file)
@@ -1381,7 +1381,7 @@ Send a `POST` request to the URL endpoint `/v1/actions/schedule-downtime`.
   end\_time     | Timestamp | **Required.** Timestamp marking the end of the downtime.
   fixed         | Boolean   | **Optional.** Defaults to `true`. If true, the downtime is `fixed` otherwise `flexible`. See [downtimes](08-advanced-topics.md#downtimes) for more information.
   duration      | Number    | **Required for flexible downtimes.** Duration of the downtime in seconds if `fixed` is set to false.
-  all\_services | Boolean   | **Optional for host downtimes.** Sets downtime for [all services](12-icinga2-api.md#icinga2-api-actions-schedule-downtime-host-all-services) for the matched host objects. Defaults to `false`.
+  all\_services | Boolean   | **Optional for host downtimes.** Sets downtime for [all services](12-icinga2-api.md#icinga2-api-actions-schedule-downtime-host-all-services) for the matched host objects. If `child_options` are set, all child hosts and their services will schedule a downtime too. Defaults to `false`.
   trigger\_name | String    | **Optional.** Sets the trigger for a triggered downtime. See [downtimes](08-advanced-topics.md#downtimes) for more information on triggered downtimes.
   child\_options| String    | **Optional.** Schedule child downtimes. `DowntimeNoChildren` does not do anything, `DowntimeTriggeredChildren` schedules child downtimes triggered by this downtime, `DowntimeNonTriggeredChildren` schedules non-triggered downtimes. Defaults to `DowntimeNoChildren`.
 
index 322236973e77da9159680e1f6fe0e618d78698a9..757b37eca68144bbaeaceefbf33a017f249053f9 100644 (file)
@@ -361,6 +361,33 @@ Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object,
                { "legacy_id", downtime->GetLegacyId() }
        });
 
+       /* Schedule downtime for all services for the host type. */
+       bool allServices = false;
+
+       if (params->Contains("all_services"))
+               allServices = HttpUtility::GetLastParameter(params, "all_services");
+
+       if (allServices && !service) {
+               ArrayData serviceDowntimes;
+
+               for (const Service::Ptr& hostService : host->GetServices()) {
+                       Log(LogNotice, "ApiActions")
+                               << "Creating downtime for service " << hostService->GetName() << " on host " << host->GetName();
+
+                       String serviceDowntimeName = Downtime::AddDowntime(hostService, author, comment, startTime, endTime,
+                               fixed, triggerName, duration);
+
+                       Downtime::Ptr serviceDowntime = Downtime::GetByName(serviceDowntimeName);
+
+                       serviceDowntimes.push_back(new Dictionary({
+                               { "name", serviceDowntimeName },
+                               { "legacy_id", serviceDowntime->GetLegacyId() }
+                       }));
+               }
+
+               additional->Set("service_downtimes", new Array(std::move(serviceDowntimes)));
+       }
+
        /* Schedule downtime for all child objects. */
        if (childOptions != DowntimeNoChildren) {
                /* 'DowntimeTriggeredChildren' schedules child downtimes triggered by the parent downtime.
@@ -386,40 +413,41 @@ Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object,
 
                        Downtime::Ptr childDowntime = Downtime::GetByName(childDowntimeName);
 
-                       childDowntimes.push_back(new Dictionary({
+                       Dictionary::Ptr childAdditional = new Dictionary({
                                { "name", childDowntimeName },
                                { "legacy_id", childDowntime->GetLegacyId() }
-                       }));
-               }
+                       });
 
-               additional->Set("child_downtimes", new Array(std::move(childDowntimes)));
-       }
+                       /* For a host, also schedule all service downtimes if requested. */
+                       Host::Ptr childHost;
+                       Service::Ptr childService;
+                       tie(childHost, childService) = GetHostService(child);
 
-       /* Schedule downtime for all services for the host type. */
-       bool allServices = false;
+                       if (allServices && !childService) {
+                               ArrayData childServiceDowntimes;
 
-       if (params->Contains("all_services"))
-               allServices = HttpUtility::GetLastParameter(params, "all_services");
+                               for (const Service::Ptr& hostService : host->GetServices()) {
+                                       Log(LogNotice, "ApiActions")
+                                               << "Creating downtime for service " << hostService->GetName() << " on child host " << host->GetName();
 
-       if (allServices && !service) {
-               ArrayData serviceDowntimes;
+                                       String serviceDowntimeName = Downtime::AddDowntime(hostService, author, comment, startTime, endTime,
+                                               fixed, triggerName, duration);
 
-               for (const Service::Ptr& hostService : host->GetServices()) {
-                       Log(LogNotice, "ApiActions")
-                               << "Creating downtime for service " << hostService->GetName() << " on host " << host->GetName();
+                                       Downtime::Ptr serviceDowntime = Downtime::GetByName(serviceDowntimeName);
 
-                       String serviceDowntimeName = Downtime::AddDowntime(hostService, author, comment, startTime, endTime,
-                               fixed, triggerName, duration);
+                                       childServiceDowntimes.push_back(new Dictionary({
+                                               { "name", serviceDowntimeName },
+                                               { "legacy_id", serviceDowntime->GetLegacyId() }
+                                       }));
+                               }
 
-                       Downtime::Ptr serviceDowntime = Downtime::GetByName(serviceDowntimeName);
+                               childAdditional->Set("service_downtimes", new Array(std::move(childServiceDowntimes)));
+                       }
 
-                       serviceDowntimes.push_back(new Dictionary({
-                               { "name", serviceDowntimeName },
-                               { "legacy_id", serviceDowntime->GetLegacyId() }
-                       }));
+                       childDowntimes.push_back(childAdditional);
                }
 
-               additional->Set("service_downtimes", new Array(std::move(serviceDowntimes)));
+               additional->Set("child_downtimes", new Array(std::move(childDowntimes)));
        }
 
        return ApiActions::CreateResult(200, "Successfully scheduled downtime '" +