From: Michael Friedrich Date: Tue, 4 Jun 2019 08:04:54 +0000 (+0200) Subject: Combine all_services with child_options for schedule-downtime API action X-Git-Tag: v2.11.0-rc1~67^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f6fc81c6c3049fc8eaf2625e40628c45a9461116;p=icinga2 Combine all_services with child_options for schedule-downtime API action --- diff --git a/doc/12-icinga2-api.md b/doc/12-icinga2-api.md index b1c1b5b1b..f05b94ece 100644 --- a/doc/12-icinga2-api.md +++ b/doc/12-icinga2-api.md @@ -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`. diff --git a/lib/icinga/apiactions.cpp b/lib/icinga/apiactions.cpp index 322236973..757b37eca 100644 --- a/lib/icinga/apiactions.cpp +++ b/lib/icinga/apiactions.cpp @@ -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 '" +