]> granicus.if.org Git - icinga2/commitdiff
Remove expired comments and downtimes.
authorGunnar Beutner <gunnar.beutner@netways.de>
Wed, 30 Jan 2013 13:28:13 +0000 (14:28 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Wed, 30 Jan 2013 13:28:13 +0000 (14:28 +0100)
components/compat/compatcomponent.cpp
lib/icinga/commentprocessor.cpp
lib/icinga/commentprocessor.h
lib/icinga/downtimeprocessor.cpp
lib/icinga/downtimeprocessor.h

index 4af6b84cc659d43e203583faca9cfba1ee5a5c31..0e82e13ef77edc81710962984887b0d913877c18 100644 (file)
@@ -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"))
index d0485f0a12f55b0908d0e0fd2e36d8d00ef5fa66..5703c2c9a2e84c68a5eeaa33b9eb751612af6536 100644 (file)
@@ -25,6 +25,7 @@ int CommentProcessor::m_NextCommentID = 1;
 map<int, String> CommentProcessor::m_LegacyCommentCache;
 map<String, DynamicObject::WeakPtr> 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<Timer>();
+               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<String> 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);
+       }
 }
 
index 32ad573638d7d03c4eb698620497626bd65def04..fb39ccdb51cad0a91ec269d564b6e15ac02526b5 100644 (file)
@@ -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<int, String> m_LegacyCommentCache;
        static map<String, DynamicObject::WeakPtr> 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);
 };
 
 }
index a62931f01411d7f4fb6fb2768c9dbfe236649a0a..97999572260f045e0fdb0f8c1182c6d47c8511a1 100644 (file)
@@ -25,6 +25,7 @@ int DowntimeProcessor::m_NextDowntimeID = 1;
 map<int, String> DowntimeProcessor::m_LegacyDowntimeCache;
 map<String, DynamicObject::WeakPtr> 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<double>(downtime->Get("start_time")) ||
-           now > static_cast<double>(downtime->Get("end_time")))
+       if (now < downtime->Get("start_time") ||
+           now > downtime->Get("end_time"))
                return false;
 
        if (static_cast<bool>(downtime->Get("fixed")))
                return true;
 
-       double triggerTime = static_cast<double>(downtime->Get("trigger_time"));
+       double triggerTime = downtime->Get("trigger_time");
 
        if (triggerTime == 0)
                return false;
 
-       return (triggerTime + static_cast<double>(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<Timer>();
+               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<String> 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);
+       }
 }
 
index 6612a63ba38a999b28c3a77d5fb8c6a68f680409..db8913d1b26b5a37f45bf4ae0cfffa607ba6dcb9 100644 (file)
@@ -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<int, String> m_LegacyDowntimeCache;
        static map<String, DynamicObject::WeakPtr> 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);
 };
 
 }