From 09658f6d0e92db179301197d85067bf2fdc45a89 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Wed, 5 Oct 2016 17:36:43 +0200 Subject: [PATCH] Add child_options for API action 'schedule-downtime' fixes #10896 fixes #10897 --- doc/12-icinga2-api.md | 1 + lib/icinga/apiactions.cpp | 60 ++++++++++++++++++++++++++++++++++----- lib/icinga/downtime.cpp | 11 +++---- lib/icinga/downtime.ti | 2 +- 4 files changed, 61 insertions(+), 13 deletions(-) diff --git a/doc/12-icinga2-api.md b/doc/12-icinga2-api.md index 0ed18f25e..730eea177 100644 --- a/doc/12-icinga2-api.md +++ b/doc/12-icinga2-api.md @@ -986,6 +986,7 @@ Send a `POST` request to the URL endpoint `/v1/actions/schedule-downtime`. duration | integer | **Required.** Duration of the downtime in seconds if `fixed` is set to false. fixed | boolean | **Optional.** Defaults to `true`. If true, the downtime is `fixed` otherwise `flexible`. See [downtimes](8-advanced-topics.md#downtimes) for more information. trigger\_name | string | **Optional.** Sets the trigger for a triggered downtime. See [downtimes](8-advanced-topics.md#downtimes) for more information on triggered downtimes. + child\_options | integer | **Optional.** Schedule child downtimes. `0` does not do anything, `1` schedules child downtimes triggered by this downtime, `2` schedules non-triggered downtimes. Defaults to `0`. In addition to these parameters a [filter](12-icinga2-api.md#icinga2-api-filters) must be provided. The valid types for this action are `Host` and `Service`. diff --git a/lib/icinga/apiactions.cpp b/lib/icinga/apiactions.cpp index d3f52d8ce..2f71dfb51 100644 --- a/lib/icinga/apiactions.cpp +++ b/lib/icinga/apiactions.cpp @@ -315,13 +315,21 @@ Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object, if (!fixed && !params->Contains("duration")) return ApiActions::CreateResult(404, "Option 'duration' is required for flexible downtime"); - String downtimeName = Downtime::AddDowntime(checkable, - HttpUtility::GetLastParameter(params, "author"), - HttpUtility::GetLastParameter(params, "comment"), - HttpUtility::GetLastParameter(params, "start_time"), - HttpUtility::GetLastParameter(params, "end_time"), fixed, - HttpUtility::GetLastParameter(params, "trigger_name"), - HttpUtility::GetLastParameter(params, "duration")); + double duration = 0.0; + if (params->Contains("duration")) + duration = HttpUtility::GetLastParameter(params, "duration"); + + String triggerName; + if (params->Contains("trigger_name")) + triggerName = HttpUtility::GetLastParameter(params, "trigger_name"); + + String author = HttpUtility::GetLastParameter(params, "author"); + String comment = HttpUtility::GetLastParameter(params, "comment"); + double startTime = HttpUtility::GetLastParameter(params, "start_time"); + double endTime = HttpUtility::GetLastParameter(params, "end_time"); + + String downtimeName = Downtime::AddDowntime(checkable, author, comment, startTime, endTime, + fixed, triggerName, duration); Downtime::Ptr downtime = Downtime::GetByName(downtimeName); @@ -329,6 +337,44 @@ Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object, additional->Set("name", downtimeName); additional->Set("legacy_id", downtime->GetLegacyId()); + /* Schedule downtime for all child objects. */ + int childOptions = 0; + if (params->Contains("child_options")) + childOptions = HttpUtility::GetLastParameter(params, "child_options"); + + if (childOptions > 0) { + /* '1' schedules child downtimes triggered by the parent downtime. + * '2' schedules non-triggered downtimes for all children. + */ + if (childOptions == 1) + triggerName = downtimeName; + + Array::Ptr childDowntimes = new Array(); + + Log(LogCritical, "ApiActions") + << "Processing child options " << childOptions << " for downtime " << downtimeName; + + for (const Checkable::Ptr& child : checkable->GetAllChildren()) { + Log(LogCritical, "ApiActions") + << "Scheduling downtime for child object " << child->GetName(); + + String childDowntimeName = Downtime::AddDowntime(child, author, comment, startTime, endTime, + fixed, triggerName, duration); + + Log(LogCritical, "ApiActions") + << "Add child downtime '" << childDowntimeName << "'."; + + Downtime::Ptr childDowntime = Downtime::GetByName(childDowntimeName); + + Dictionary::Ptr additionalChild = new Dictionary(); + additionalChild->Set("name", childDowntimeName); + additionalChild->Set("legacy_id", childDowntime->GetLegacyId()); + childDowntimes->Add(additionalChild); + } + + additional->Set("child_downtimes", childDowntimes); + } + return ApiActions::CreateResult(200, "Successfully scheduled downtime '" + downtimeName + "' for object '" + checkable->GetName() + "'.", additional); } diff --git a/lib/icinga/downtime.cpp b/lib/icinga/downtime.cpp index d69f6e5fb..8adf30c5f 100644 --- a/lib/icinga/downtime.cpp +++ b/lib/icinga/downtime.cpp @@ -262,17 +262,18 @@ String Downtime::AddDowntime(const Checkable::Ptr& checkable, const String& auth } if (!triggeredBy.IsEmpty()) { - Downtime::Ptr triggerDowntime = Downtime::GetByName(triggeredBy); - Array::Ptr triggers = triggerDowntime->GetTriggers(); + Downtime::Ptr parentDowntime = Downtime::GetByName(triggeredBy); + Array::Ptr triggers = parentDowntime->GetTriggers(); - if (!triggers->Contains(triggeredBy)) - triggers->Add(triggeredBy); + ObjectLock olock(triggers); + if (!triggers->Contains(fullName)) + triggers->Add(fullName); } Downtime::Ptr downtime = Downtime::GetByName(fullName); if (!downtime) - BOOST_THROW_EXCEPTION(std::runtime_error("Could not create downtime.")); + BOOST_THROW_EXCEPTION(std::runtime_error("Could not create downtime object.")); Log(LogNotice, "Downtime") << "Added downtime '" << downtime->GetName() diff --git a/lib/icinga/downtime.ti b/lib/icinga/downtime.ti index bb2bd0958..b3126109a 100644 --- a/lib/icinga/downtime.ti +++ b/lib/icinga/downtime.ti @@ -76,7 +76,7 @@ class Downtime : ConfigObject < DowntimeNameComposer [state] Timestamp trigger_time; [config] bool fixed; [config] Timestamp duration; - [config] name(Downtime) triggered_by; + [config] String triggered_by; [config] String scheduled_by; [state] Array::Ptr triggers { default {{{ return new Array(); }}} -- 2.40.0