[icinga-checkins] icinga.org: icinga2/feature/cmd-propagate-10896: WIP: Add child_options for API action 'schedule-downtime'

git at icinga.org git at icinga.org
Wed Oct 26 09:26:09 CEST 2016


Module: icinga2
Branch: feature/cmd-propagate-10896
Commit: 4ae18610852020e4c57d7ffa3306f5cbe70eeeff
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=4ae18610852020e4c57d7ffa3306f5cbe70eeeff

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Wed Oct  5 17:36:43 2016 +0200

WIP: Add child_options for API action 'schedule-downtime'

---

 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 0ed18f2..730eea1 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 d3f52d8..2f71dfb 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 d69f6e5..8adf30c 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 bb2bd09..b312610 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(); }}}



More information about the icinga-checkins mailing list