diff --git a/lib/icinga/icinga-type.conf b/lib/icinga/icinga-type.conf index f32d378b5..049eadc1d 100644 --- a/lib/icinga/icinga-type.conf +++ b/lib/icinga/icinga-type.conf @@ -246,6 +246,8 @@ } %type ScheduledDowntime %inherits CustomVarObject { + %validator "ValidateScheduledDowntimeRanges", + %require "host_name", %attribute %name(Host) "host_name", %attribute %string "service_name", diff --git a/lib/icinga/scheduleddowntime.cpp b/lib/icinga/scheduleddowntime.cpp index 7ce5e7c19..446e25281 100644 --- a/lib/icinga/scheduleddowntime.cpp +++ b/lib/icinga/scheduleddowntime.cpp @@ -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())); + } + } +} diff --git a/lib/icinga/scheduleddowntime.hpp b/lib/icinga/scheduleddowntime.hpp index 22394c5a4..a4f5273d3 100644 --- a/lib/icinga/scheduleddowntime.hpp +++ b/lib/icinga/scheduleddowntime.hpp @@ -53,6 +53,8 @@ public: static void EvaluateApplyRules(const intrusive_ptr& host); static void EvaluateApplyRules(const intrusive_ptr& service); + static void ValidateRanges(const String& location, const ScheduledDowntime::Ptr& object); + protected: virtual void OnAllConfigLoaded(void); virtual void Start(void);