]> granicus.if.org Git - icinga2/commitdiff
Add validator for time ranges in ScheduledDowntime objects
authorGunnar Beutner <gunnar@beutner.name>
Mon, 9 Mar 2015 06:51:55 +0000 (07:51 +0100)
committerGunnar Beutner <gunnar@beutner.name>
Mon, 9 Mar 2015 07:02:12 +0000 (08:02 +0100)
fixes #8600

lib/icinga/icinga-type.conf
lib/icinga/scheduleddowntime.cpp
lib/icinga/scheduleddowntime.hpp

index f32d378b538d110d78571bf75368807deedee63a..049eadc1d82b23689b056e933a5690c7032fb2f5 100644 (file)
 }
 
 %type ScheduledDowntime %inherits CustomVarObject {
+       %validator "ValidateScheduledDowntimeRanges",
+
        %require "host_name",
        %attribute %name(Host) "host_name",
        %attribute %string "service_name",
index 7ce5e7c198f099a0a6c495a01ac31162fc9a93e3..446e252812f0eb3e2d7ccb6c83370e6198232721 100644 (file)
@@ -34,6 +34,7 @@
 using namespace icinga;
 
 REGISTER_TYPE(ScheduledDowntime);
+REGISTER_SCRIPTFUNCTION(ValidateScheduledDowntimeRanges, &ScheduledDowntime::ValidateRanges);
 
 INITIALIZE_ONCE(&ScheduledDowntime::StaticInitialize);
 
@@ -179,3 +180,35 @@ void ScheduledDowntime::CreateNextDowntime(void)
        Downtime::Ptr downtime = Checkable::GetDowntimeByID(uid);
        downtime->SetConfigOwner(GetName());
 }
+
+void ScheduledDowntime::ValidateRanges(const String& location, const ScheduledDowntime::Ptr& object)
+{
+       Dictionary::Ptr ranges = object->GetRanges();
+
+       if (!ranges)
+               return;
+
+       /* create a fake time environment to validate the definitions */
+       time_t refts = Utility::GetTime();
+       tm reference = Utility::LocalTime(refts);
+       Array::Ptr segments = new Array();
+
+       ObjectLock olock(ranges);
+       BOOST_FOREACH(const Dictionary::Pair& kv, ranges) {
+               try {
+                       tm begin_tm, end_tm;
+                       int stride;
+                       LegacyTimePeriod::ParseTimeRange(kv.first, &begin_tm, &end_tm, &stride, &reference);
+               } catch (std::exception&) {
+                       BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
+                           location + ": Invalid time specification.", object->GetDebugInfo()));
+               }
+
+               try {
+                       LegacyTimePeriod::ProcessTimeRanges(kv.second, &reference, segments);
+               } catch (std::exception&) {
+                       BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
+                           location + ": Invalid time range definition.", object->GetDebugInfo()));
+               }
+       }
+}
index 22394c5a42bb70a0e59be56cb26e6c2d7636a967..a4f5273d34ecf19d3cd851083f11c946dccec586 100644 (file)
@@ -53,6 +53,8 @@ public:
        static void EvaluateApplyRules(const intrusive_ptr<Host>& host);
        static void EvaluateApplyRules(const intrusive_ptr<Service>& service);
 
+       static void ValidateRanges(const String& location, const ScheduledDowntime::Ptr& object);
+
 protected:
        virtual void OnAllConfigLoaded(void);
        virtual void Start(void);