diff --git a/components/compat/compatcomponent.cpp b/components/compat/compatcomponent.cpp index 4af6b84cc..0e82e13ef 100644 --- a/components/compat/compatcomponent.cpp +++ b/components/compat/compatcomponent.cpp @@ -181,6 +181,9 @@ void CompatComponent::DumpComments(ofstream& fp, const DynamicObject::Ptr& owner String id; Dictionary::Ptr comment; BOOST_FOREACH(tie(id, comment), comments) { + if (CommentProcessor::IsCommentExpired(comment)) + continue; + /* There's no way for us to dump comments that haven't been * assigned a legacy ID yet. */ if (!comment->Contains("legacy_id")) @@ -228,6 +231,9 @@ void CompatComponent::DumpDowntimes(ofstream& fp, const DynamicObject::Ptr& owne String id; Dictionary::Ptr downtime; BOOST_FOREACH(tie(id, downtime), downtimes) { + if (DowntimeProcessor::IsDowntimeExpired(downtime)) + continue; + /* There's no way for us to dump downtimes that haven't been * assigned a legacy ID yet. */ if (!downtime->Contains("legacy_id")) diff --git a/lib/icinga/commentprocessor.cpp b/lib/icinga/commentprocessor.cpp index d0485f0a1..5703c2c9a 100644 --- a/lib/icinga/commentprocessor.cpp +++ b/lib/icinga/commentprocessor.cpp @@ -25,6 +25,7 @@ int CommentProcessor::m_NextCommentID = 1; map CommentProcessor::m_LegacyCommentCache; map CommentProcessor::m_CommentCache; bool CommentProcessor::m_CommentCacheValid; +Timer::Ptr CommentProcessor::m_CommentExpireTimer; int CommentProcessor::GetNextCommentID(void) { @@ -108,6 +109,13 @@ Dictionary::Ptr CommentProcessor::GetCommentByID(const String& id) return Dictionary::Ptr(); } +bool CommentProcessor::IsCommentExpired(const Dictionary::Ptr& comment) +{ + double expire_time = comment->Get("expire_time"); + + return (expire_time != 0 && expire_time < Utility::GetTime()); +} + void CommentProcessor::InvalidateCommentCache(void) { m_CommentCacheValid = false; @@ -170,5 +178,49 @@ void CommentProcessor::ValidateCommentCache(void) } m_CommentCacheValid = true; + + if (!m_CommentExpireTimer) { + m_CommentExpireTimer = boost::make_shared(); + m_CommentExpireTimer->SetInterval(300); + m_CommentExpireTimer->OnTimerExpired.connect(boost::bind(&CommentProcessor::CommentExpireTimerHandler)); + m_CommentExpireTimer->Start(); + } +} + +void CommentProcessor::RemoveExpiredComments(const DynamicObject::Ptr& owner) +{ + Dictionary::Ptr comments = owner->Get("comments"); + + if (!comments) + return; + + vector expiredComments; + + String id; + Dictionary::Ptr comment; + BOOST_FOREACH(tie(id, comment), comments) { + if (IsCommentExpired(comment)) + expiredComments.push_back(id); + } + + if (expiredComments.size() > 0) { + BOOST_FOREACH(id, expiredComments) { + comments->Remove(id); + } + + owner->Touch("comments"); + } +} + +void CommentProcessor::CommentExpireTimerHandler(void) +{ + DynamicObject::Ptr object; + BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Host")->GetObjects()) { + RemoveExpiredComments(object); + } + + BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Service")->GetObjects()) { + RemoveExpiredComments(object); + } } diff --git a/lib/icinga/commentprocessor.h b/lib/icinga/commentprocessor.h index 32ad57363..fb39ccdb5 100644 --- a/lib/icinga/commentprocessor.h +++ b/lib/icinga/commentprocessor.h @@ -52,6 +52,8 @@ public: static DynamicObject::Ptr GetOwnerByCommentID(const String& id); static Dictionary::Ptr GetCommentByID(const String& id); + static bool IsCommentExpired(const Dictionary::Ptr& comment); + static void InvalidateCommentCache(void); static void ValidateCommentCache(void); @@ -61,10 +63,14 @@ private: static map m_LegacyCommentCache; static map m_CommentCache; static bool m_CommentCacheValid; + static Timer::Ptr m_CommentExpireTimer; CommentProcessor(void); + static void CommentExpireTimerHandler(void); + static void AddCommentsToCache(const DynamicObject::Ptr& owner); + static void RemoveExpiredComments(const DynamicObject::Ptr& owner); }; } diff --git a/lib/icinga/downtimeprocessor.cpp b/lib/icinga/downtimeprocessor.cpp index a62931f01..979995722 100644 --- a/lib/icinga/downtimeprocessor.cpp +++ b/lib/icinga/downtimeprocessor.cpp @@ -25,6 +25,7 @@ int DowntimeProcessor::m_NextDowntimeID = 1; map DowntimeProcessor::m_LegacyDowntimeCache; map DowntimeProcessor::m_DowntimeCache; bool DowntimeProcessor::m_DowntimeCacheValid; +Timer::Ptr DowntimeProcessor::m_DowntimeExpireTimer; int DowntimeProcessor::GetNextDowntimeID(void) { @@ -109,19 +110,24 @@ bool DowntimeProcessor::IsDowntimeActive(const Dictionary::Ptr& downtime) { double now = Utility::GetTime(); - if (now < static_cast(downtime->Get("start_time")) || - now > static_cast(downtime->Get("end_time"))) + if (now < downtime->Get("start_time") || + now > downtime->Get("end_time")) return false; if (static_cast(downtime->Get("fixed"))) return true; - double triggerTime = static_cast(downtime->Get("trigger_time")); + double triggerTime = downtime->Get("trigger_time"); if (triggerTime == 0) return false; - return (triggerTime + static_cast(downtime->Get("duration")) < now); + return (triggerTime + downtime->Get("duration") < now); +} + +bool DowntimeProcessor::IsDowntimeExpired(const Dictionary::Ptr& downtime) +{ + return (downtime->Get("end_time") < Utility::GetTime()); } void DowntimeProcessor::InvalidateDowntimeCache(void) @@ -141,6 +147,8 @@ void DowntimeProcessor::AddDowntimesToCache(const DynamicObject::Ptr& owner) String id; Dictionary::Ptr downtime; BOOST_FOREACH(tie(id, downtime), downtimes) { + double end_time = downtime->Get("end_time"); + int legacy_id; if (!downtime->Contains("legacy_id")) { @@ -185,5 +193,49 @@ void DowntimeProcessor::ValidateDowntimeCache(void) } m_DowntimeCacheValid = true; + + if (!m_DowntimeExpireTimer) { + m_DowntimeExpireTimer = boost::make_shared(); + m_DowntimeExpireTimer->SetInterval(300); + m_DowntimeExpireTimer->OnTimerExpired.connect(boost::bind(&DowntimeProcessor::DowntimeExpireTimerHandler)); + m_DowntimeExpireTimer->Start(); + } +} + +void DowntimeProcessor::RemoveExpiredDowntimes(const DynamicObject::Ptr& owner) +{ + Dictionary::Ptr downtimes = owner->Get("downtimes"); + + if (!downtimes) + return; + + vector expiredDowntimes; + + String id; + Dictionary::Ptr downtime; + BOOST_FOREACH(tie(id, downtime), downtimes) { + if (IsDowntimeExpired(downtime)) + expiredDowntimes.push_back(id); + } + + if (expiredDowntimes.size() > 0) { + BOOST_FOREACH(id, expiredDowntimes) { + downtimes->Remove(id); + } + + owner->Touch("downtimes"); + } +} + +void DowntimeProcessor::DowntimeExpireTimerHandler(void) +{ + DynamicObject::Ptr object; + BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Host")->GetObjects()) { + RemoveExpiredDowntimes(object); + } + + BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Service")->GetObjects()) { + RemoveExpiredDowntimes(object); + } } diff --git a/lib/icinga/downtimeprocessor.h b/lib/icinga/downtimeprocessor.h index 6612a63ba..db8913d1b 100644 --- a/lib/icinga/downtimeprocessor.h +++ b/lib/icinga/downtimeprocessor.h @@ -45,6 +45,7 @@ public: static Dictionary::Ptr GetDowntimeByID(const String& id); static bool IsDowntimeActive(const Dictionary::Ptr& downtime); + static bool IsDowntimeExpired(const Dictionary::Ptr& downtime); static void InvalidateDowntimeCache(void); static void ValidateDowntimeCache(void); @@ -55,10 +56,14 @@ private: static map m_LegacyDowntimeCache; static map m_DowntimeCache; static bool m_DowntimeCacheValid; + static Timer::Ptr m_DowntimeExpireTimer; DowntimeProcessor(void); + static void DowntimeExpireTimerHandler(void); + static void AddDowntimesToCache(const DynamicObject::Ptr& owner); + static void RemoveExpiredDowntimes(const DynamicObject::Ptr& owner); }; }