From 836c26da660a2e8b3137ec9c2cd043c9797b4479 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 16 Apr 2013 10:12:53 +0200 Subject: [PATCH] Bugfixes for the timeperiod feature. --- lib/icinga/host.cpp | 2 +- lib/icinga/legacytimeperiod.cpp | 4 +++- lib/icinga/notification.cpp | 2 +- lib/icinga/timeperiod.cpp | 41 +++++++++++++++++++++++++-------- lib/icinga/timeperiod.h | 8 ++++--- 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/lib/icinga/host.cpp b/lib/icinga/host.cpp index e694146c7..4c486f24c 100644 --- a/lib/icinga/host.cpp +++ b/lib/icinga/host.cpp @@ -539,7 +539,7 @@ String Host::StateToString(HostState state) } } -bool Host::ResolveMacro(const String& macro, const Dictionary::Ptr& cr, String *result) const +bool Host::ResolveMacro(const String& macro, const Dictionary::Ptr&, String *result) const { if (macro == "HOSTNAME" || macro == "HOSTALIAS") { *result = GetName(); diff --git a/lib/icinga/legacytimeperiod.cpp b/lib/icinga/legacytimeperiod.cpp index a990487fc..51173626a 100644 --- a/lib/icinga/legacytimeperiod.cpp +++ b/lib/icinga/legacytimeperiod.cpp @@ -21,6 +21,7 @@ #include "base/scriptfunction.h" #include "base/convert.h" #include "base/exception.h" +#include "base/objectlock.h" #include "base/logger_fwd.h" #include #include @@ -140,6 +141,7 @@ Array::Ptr LegacyTimePeriod::ScriptFunc(const TimePeriod::Ptr& tp, double begin, if (ranges) { for (int i = 0; i <= (end - begin) / (24 * 60 * 60); i++) { + ObjectLock olock(ranges); String key; Value value; BOOST_FOREACH(boost::tie(key, value), ranges) { @@ -149,7 +151,7 @@ Array::Ptr LegacyTimePeriod::ScriptFunc(const TimePeriod::Ptr& tp, double begin, ProcessTimeRanges(value, &reference, segments); } - reference.tm_wday++; + reference.tm_mday++; } } diff --git a/lib/icinga/notification.cpp b/lib/icinga/notification.cpp index e8c3be2a2..eafd8b417 100644 --- a/lib/icinga/notification.cpp +++ b/lib/icinga/notification.cpp @@ -280,7 +280,7 @@ void Notification::OnAttributeChanged(const String& name) Service::InvalidateNotificationsCache(); } -bool Notification::ResolveMacro(const String& macro, const Dictionary::Ptr& cr, String *result) const +bool Notification::ResolveMacro(const String& macro, const Dictionary::Ptr&, String *result) const { Dictionary::Ptr macros = GetMacros(); diff --git a/lib/icinga/timeperiod.cpp b/lib/icinga/timeperiod.cpp index 5a67322a4..f2d57aea1 100644 --- a/lib/icinga/timeperiod.cpp +++ b/lib/icinga/timeperiod.cpp @@ -55,7 +55,8 @@ void TimePeriod::Start(void) { /* Pre-fill the time period for the next 24 hours. */ double now = Utility::GetTime(); - UpdateRegion(now, now + 24 * 3600); + UpdateRegion(now, now + 24 * 3600, true); + Dump(); } TimePeriod::Ptr TimePeriod::GetByName(const String& name) @@ -196,13 +197,15 @@ void TimePeriod::PurgeSegments(double end) Touch("segments"); } -void TimePeriod::UpdateRegion(double begin, double end) +void TimePeriod::UpdateRegion(double begin, double end, bool clearExisting) { - if (begin < m_ValidEnd) - begin = m_ValidEnd; + if (!clearExisting) { + if (begin < m_ValidEnd) + begin = m_ValidEnd; - if (end < m_ValidEnd) - return; + if (end < m_ValidEnd) + return; + } TimePeriod::Ptr self = GetSelf(); @@ -288,18 +291,18 @@ void TimePeriod::UpdateTimerHandler(void) valid_end = tp->m_ValidEnd; } - if (valid_end < now + 3 * 3600) - tp->UpdateRegion(valid_end, now + 24 * 3600); + tp->UpdateRegion(valid_end, now + 24 * 3600, false); + tp->Dump(); } } -Array::Ptr TimePeriod::EmptyTimePeriodUpdate(const TimePeriod::Ptr tp, double begin, double end) +Array::Ptr TimePeriod::EmptyTimePeriodUpdate(const TimePeriod::Ptr&, double, double) { Array::Ptr segments = boost::make_shared(); return segments; } -Array::Ptr TimePeriod::EvenMinutesTimePeriodUpdate(const TimePeriod::Ptr tp, double begin, double end) +Array::Ptr TimePeriod::EvenMinutesTimePeriodUpdate(const TimePeriod::Ptr&, double begin, double end) { Array::Ptr segments = boost::make_shared(); @@ -315,3 +318,21 @@ Array::Ptr TimePeriod::EvenMinutesTimePeriodUpdate(const TimePeriod::Ptr tp, dou return segments; } + +void TimePeriod::Dump(void) +{ + Array::Ptr segments = m_Segments; + + Log(LogDebug, "icinga", "Dumping TimePeriod '" + GetName() + "'"); + + if (segments) { + ObjectLock dlock(segments); + BOOST_FOREACH(const Dictionary::Ptr& segment, segments) { + Log(LogDebug, "icinga", "Segment: " + + Utility::FormatDateTime("%c", segment->Get("begin")) + " <-> " + + Utility::FormatDateTime("%c", segment->Get("end"))); + } + } + + Log(LogDebug, "icinga", "---"); +} diff --git a/lib/icinga/timeperiod.h b/lib/icinga/timeperiod.h index c9c29094e..9f4036b64 100644 --- a/lib/icinga/timeperiod.h +++ b/lib/icinga/timeperiod.h @@ -44,13 +44,13 @@ public: virtual void Start(void); - void UpdateRegion(double begin, double end); + void UpdateRegion(double begin, double end, bool clearExisting); bool IsInside(double ts) const; double FindNextTransition(double begin); - static Array::Ptr EmptyTimePeriodUpdate(const TimePeriod::Ptr tp, double begin, double end); - static Array::Ptr EvenMinutesTimePeriodUpdate(const TimePeriod::Ptr tp, double begin, double end); + static Array::Ptr EmptyTimePeriodUpdate(const TimePeriod::Ptr& tp, double begin, double end); + static Array::Ptr EvenMinutesTimePeriodUpdate(const TimePeriod::Ptr& tp, double begin, double end); private: Attribute m_ValidBegin; @@ -62,6 +62,8 @@ private: void RemoveSegment(double begin, double end); void PurgeSegments(double end); + void Dump(void); + static void UpdateTimerHandler(void); };