Add validator for time ranges in ScheduledDowntime objects

fixes #8600
This commit is contained in:
Gunnar Beutner 2015-03-09 07:51:55 +01:00
parent 60d5959ba4
commit 22f3c5082d
3 changed files with 37 additions and 0 deletions

View File

@ -246,6 +246,8 @@
}
%type ScheduledDowntime %inherits CustomVarObject {
%validator "ValidateScheduledDowntimeRanges",
%require "host_name",
%attribute %name(Host) "host_name",
%attribute %string "service_name",

View 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()));
}
}
}

View 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);