]> granicus.if.org Git - icinga2/commitdiff
Add child_options to ScheduledDowntime
authorNoah Hilverling <noah@hilverling.com>
Wed, 8 Aug 2018 12:42:18 +0000 (14:42 +0200)
committerMichael Friedrich <michael.friedrich@icinga.com>
Fri, 24 Aug 2018 12:29:39 +0000 (14:29 +0200)
refs #3935

doc/09-object-types.md
doc/12-icinga2-api.md
lib/icinga/apiactions.cpp
lib/icinga/downtime.cpp
lib/icinga/downtime.hpp
lib/icinga/scheduleddowntime.cpp
lib/icinga/scheduleddowntime.hpp
lib/icinga/scheduleddowntime.ti

index d04f781569661e0e21cb274378705d9bfc5d4a21..8d91b7b30ad86f3abc9cd45600240dc1332ed625 100644 (file)
@@ -1385,6 +1385,7 @@ Configuration Attributes:
   fixed                     | Boolean               | **Optional.** Whether this is a fixed downtime. Defaults to `true`.
   duration                  | Duration              | **Optional.** How long the downtime lasts. Only has an effect for flexible (non-fixed) downtimes.
   ranges                    | Dictionary            | **Required.** A dictionary containing information which days and durations apply to this timeperiod.
+  child\_options            | String                | **Optional.** Schedule child downtimes. `DowntimeNoChildren` does not do anything, `DowntimeTriggeredChildren` schedules child downtimes triggered by this downtime, `DowntimeTriggeredChildren` schedules non-triggered downtimes. Defaults to `DowntimeNoChildren`.
 
 ScheduledDowntime objects have composite names, i.e. their names are based
 on the `host_name` and `service_name` attributes and the
index 722b88a2bca9f807957e9e682cc15eaffe7c2077..50b107c88631fb5876948f1bbbeee9b825cd68c7 100644 (file)
@@ -1117,7 +1117,7 @@ Send a `POST` request to the URL endpoint `/v1/actions/schedule-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.
   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 | Number   | **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`.
+  child\_options| String    | **Optional.** Schedule child downtimes. `DowntimeNoChildren` does not do anything, `DowntimeTriggeredChildren` schedules child downtimes triggered by this downtime, `DowntimeTriggeredChildren` schedules non-triggered downtimes. Defaults to `DowntimeNoChildren`.
 
 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`.
 
index 2a373eb1c794e33345da3baa17205a70b62303d7..0c28fe9443badcac4efd0a8bd07b6ddca3d32d2e 100644 (file)
@@ -366,30 +366,30 @@ Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object,
        });
 
        /* Schedule downtime for all child objects. */
-       int childOptions = 0;
+       DowntimeChildOptions childOptions = DowntimeNoChildren;
        if (params->Contains("child_options"))
-               childOptions = HttpUtility::GetLastParameter(params, "child_options");
+               childOptions = Downtime::ChildOptionsFromValue(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 != DowntimeNoChildren) {
+               /* 'DowntimeTriggeredChildren' schedules child downtimes triggered by the parent downtime.
+                * 'DowntimeNonTriggeredChildren' schedules non-triggered downtimes for all children.
                 */
-               if (childOptions == 1)
+               if (childOptions == DowntimeTriggeredChildren)
                        triggerName = downtimeName;
 
-               Log(LogCritical, "ApiActions")
+               Log(LogNotice, "ApiActions")
                        << "Processing child options " << childOptions << " for downtime " << downtimeName;
 
                ArrayData childDowntimes;
 
                for (const Checkable::Ptr& child : checkable->GetAllChildren()) {
-                       Log(LogCritical, "ApiActions")
+                       Log(LogNotice, "ApiActions")
                                << "Scheduling downtime for child object " << child->GetName();
 
                        String childDowntimeName = Downtime::AddDowntime(child, author, comment, startTime, endTime,
                                fixed, triggerName, duration);
 
-                       Log(LogCritical, "ApiActions")
+                       Log(LogNotice, "ApiActions")
                                << "Add child downtime '" << childDowntimeName << "'.";
 
                        Downtime::Ptr childDowntime = Downtime::GetByName(childDowntimeName);
index 8c3584a4bae4a6b1d8de4290565d0c868b9295d3..6e5bc0210a3fd7fd4e292db318f546940a295dae 100644 (file)
@@ -42,6 +42,15 @@ boost::signals2::signal<void (const Downtime::Ptr&)> Downtime::OnDowntimeTrigger
 
 REGISTER_TYPE(Downtime);
 
+INITIALIZE_ONCE(&Downtime::StaticInitialize);
+
+void Downtime::StaticInitialize()
+{
+       ScriptGlobal::Set("DowntimeNoChildren", "DowntimeNoChildren");
+       ScriptGlobal::Set("DowntimeTriggeredChildren", "DowntimeTriggeredChildren");
+       ScriptGlobal::Set("DowntimeNonTriggeredChildren", "DowntimeNonTriggeredChildren");
+}
+
 String DowntimeNameComposer::MakeName(const String& shortName, const Object::Ptr& context) const
 {
        Downtime::Ptr downtime = dynamic_pointer_cast<Downtime>(context);
@@ -420,3 +429,20 @@ void Downtime::ValidateEndTime(const Lazy<Timestamp>& lvalue, const ValidationUt
        if (lvalue() <= 0)
                BOOST_THROW_EXCEPTION(ValidationError(this, { "end_time" }, "End time must be greater than 0."));
 }
+
+DowntimeChildOptions Downtime::ChildOptionsFromValue(const Value& options)
+{
+       if (options == "DowntimeNoChildren")
+               return DowntimeNoChildren;
+       else if (options == "DowntimeTriggeredChildren")
+               return DowntimeTriggeredChildren;
+       else if (options == "DowntimeNonTriggeredChildren")
+               return DowntimeNonTriggeredChildren;
+       else if (options.IsNumber()) {
+               int number = options;
+               if (number >= 0 && number <= 2)
+                       return static_cast<DowntimeChildOptions>(number);
+       }
+
+       BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid child option specified"));
+}
index 6d4ba0a21086bf57b518811d2ab4be07cfa0b4c3..c4646eabb17cc75db0f344a34814c6f1dfbc2cab 100644 (file)
 namespace icinga
 {
 
+enum DowntimeChildOptions
+{
+       DowntimeNoChildren,
+       DowntimeTriggeredChildren,
+       DowntimeNonTriggeredChildren
+};
+
 /**
  * A downtime.
  *
@@ -51,6 +58,8 @@ public:
        bool IsExpired() const;
        bool HasValidConfigOwner() const;
 
+       static void StaticInitialize();
+
        static int GetNextDowntimeID();
 
        static String AddDowntime(const intrusive_ptr<Checkable>& checkable, const String& author,
@@ -65,6 +74,8 @@ public:
 
        static String GetDowntimeIDFromLegacyID(int id);
 
+       static DowntimeChildOptions ChildOptionsFromValue(const Value& options);
+
 protected:
        void OnAllConfigLoaded() override;
        void Start(bool runtimeCreated) override;
index 6a715fc401ae1f4c04a620a71dc4a4660ab40cec..80d7617ba500e05c2d186db699c7fce39d1b5d9c 100644 (file)
@@ -191,9 +191,35 @@ void ScheduledDowntime::CreateNextDowntime()
                return;
        }
 
-       Downtime::AddDowntime(GetCheckable(), GetAuthor(), GetComment(),
+       String downtimeName = Downtime::AddDowntime(GetCheckable(), GetAuthor(), GetComment(),
                segment.first, segment.second,
                GetFixed(), String(), GetDuration(), GetName(), GetName());
+
+       Downtime::Ptr downtime = Downtime::GetByName(downtimeName);
+
+       int childOptions = Downtime::ChildOptionsFromValue(GetChildOptions());
+       if (childOptions > 0) {
+               /* 'DowntimeTriggeredChildren' schedules child downtimes triggered by the parent downtime.
+                * 'DowntimeNonTriggeredChildren' schedules non-triggered downtimes for all children.
+                */
+               String triggerName;
+               if (childOptions == 1)
+                       triggerName = downtimeName;
+
+               Log(LogNotice, "ScheduledDowntime")
+                               << "Processing child options " << childOptions << " for downtime " << downtimeName;
+
+               for (const Checkable::Ptr& child : GetCheckable()->GetAllChildren()) {
+                       Log(LogNotice, "ScheduledDowntime")
+                               << "Scheduling downtime for child object " << child->GetName();
+
+                       String childDowntimeName = Downtime::AddDowntime(child, GetAuthor(), GetComment(),
+                               segment.first, segment.second, GetFixed(), triggerName, GetDuration(), GetName(), GetName());
+
+                       Log(LogNotice, "ScheduledDowntime")
+                               << "Add child downtime '" << childDowntimeName << "'.";
+               }
+       }
 }
 
 void ScheduledDowntime::ValidateRanges(const Lazy<Dictionary::Ptr>& lvalue, const ValidationUtils& utils)
@@ -226,3 +252,13 @@ void ScheduledDowntime::ValidateRanges(const Lazy<Dictionary::Ptr>& lvalue, cons
        }
 }
 
+void ScheduledDowntime::ValidateChildOptions(const Lazy<Value>& lvalue, const ValidationUtils& utils)
+{
+       ObjectImpl<ScheduledDowntime>::ValidateChildOptions(lvalue, utils);
+
+       try {
+               Downtime::ChildOptionsFromValue(lvalue());
+       } catch (const std::exception&) {
+               BOOST_THROW_EXCEPTION(ValidationError(this, { "child_options" }, "Invalid child_options specified"));
+       }
+}
\ No newline at end of file
index 86138fa0442111a684894b37dfa6db0a4be52840..7fbcd1e126b74ce5b8899ecb8954cd5fb86536b6 100644 (file)
@@ -49,6 +49,7 @@ public:
        static void EvaluateApplyRules(const intrusive_ptr<Service>& service);
 
        void ValidateRanges(const Lazy<Dictionary::Ptr>& lvalue, const ValidationUtils& utils) override;
+       void ValidateChildOptions(const Lazy<Value>& lvalue, const ValidationUtils& utils) override;
 
 protected:
        void OnAllConfigLoaded() override;
index e119eaa11b73323aa9575ce0a3b499cb142bf5de..e2d2aca3065b0e4ceb30264bf3ab3a2d0d826873 100644 (file)
@@ -77,6 +77,10 @@ class ScheduledDowntime : CustomVarObject < ScheduledDowntimeNameComposer
                default {{{ return true; }}}
        };
 
+       [config] Value child_options {
+           default {{{ return "DowntimeNoChildren"; }}}
+       };
+
        [config, required] Dictionary::Ptr ranges;
 };