diff --git a/lib/ido/timeperioddbobject.cpp b/lib/ido/timeperioddbobject.cpp index 5ba0307ea..83c5e8a9f 100644 --- a/lib/ido/timeperioddbobject.cpp +++ b/lib/ido/timeperioddbobject.cpp @@ -21,8 +21,11 @@ #include "ido/dbtype.h" #include "ido/dbvalue.h" #include "icinga/timeperiod.h" +#include "icinga/legacytimeperiod.h" +#include "base/exception.h" #include "base/objectlock.h" #include +#include using namespace icinga; @@ -46,3 +49,70 @@ Dictionary::Ptr TimePeriodDbObject::GetStatusFields(void) const { return Empty; } + +void TimePeriodDbObject::OnConfigUpdate(void) +{ + TimePeriod::Ptr tp = static_pointer_cast(GetObject()); + + DbQuery query_del1; + query_del1.Table = GetType()->GetTable() + "_timeranges"; + query_del1.Type = DbQueryDelete; + query_del1.WhereCriteria = boost::make_shared(); + query_del1.WhereCriteria->Set("timeperiod_id", DbValue::FromObjectInsertID(tp)); + OnQuery(query_del1); + + Dictionary::Ptr ranges = tp->Get("ranges"); + + if (!ranges) + return; + + time_t refts = Utility::GetTime(); + ObjectLock olock(ranges); + String key; + Value value; + BOOST_FOREACH(boost::tie(key, value), ranges) { + int wday = LegacyTimePeriod::WeekdayFromString(key); + + if (wday == -1) + continue; + + tm reference; + +#ifdef _MSC_VER + tm *temp = localtime(&refts); + + if (temp == NULL) { + BOOST_THROW_EXCEPTION(posix_error() + << boost::errinfo_api_function("localtime") + << boost::errinfo_errno(errno)); + } + + reference = *temp; +#else /* _MSC_VER */ + if (localtime_r(&refts, &reference) == NULL) { + BOOST_THROW_EXCEPTION(posix_error() + << boost::errinfo_api_function("localtime_r") + << boost::errinfo_errno(errno)); + } +#endif /* _MSC_VER */ + + tm begin, end; + int stride; + + LegacyTimePeriod::ParseTimeRange(key, &begin, &end, &stride, &reference); + + if (stride != 1) + continue; + + DbQuery query; + query.Table = GetType()->GetTable() + "_timeranges"; + query.Type = DbQueryInsert; + query.Fields = boost::make_shared(); + query.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */ + query.Fields->Set("timeperiod_id", DbValue::FromObjectInsertID(tp)); + query.Fields->Set("day", wday); + query.Fields->Set("start_sec", mktime(&begin)); + query.Fields->Set("end_sec", mktime(&end)); + OnQuery(query); + } +} diff --git a/lib/ido/timeperioddbobject.h b/lib/ido/timeperioddbobject.h index dc9f0d1c1..fe54c5ca7 100644 --- a/lib/ido/timeperioddbobject.h +++ b/lib/ido/timeperioddbobject.h @@ -40,6 +40,7 @@ public: virtual Dictionary::Ptr GetConfigFields(void) const; virtual Dictionary::Ptr GetStatusFields(void) const; + virtual void OnConfigUpdate(void); }; }