]> granicus.if.org Git - icinga2/commitdiff
Implement triggered downtime for services.
authorGunnar Beutner <gunnar.beutner@netways.de>
Thu, 31 Jan 2013 12:57:14 +0000 (13:57 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Thu, 31 Jan 2013 12:57:14 +0000 (13:57 +0100)
Fixes #3583

components/compat/compatcomponent.cpp
lib/icinga/downtimeprocessor.cpp
lib/icinga/downtimeprocessor.h
lib/icinga/service.cpp

index 70e1dfb7fa102986f00f1184c852607b8dcdd9c3..75f93e468e68bc9467860c3cb270748e10ec870a 100644 (file)
@@ -231,13 +231,10 @@ void CompatComponent::DumpDowntimes(ofstream& fp, const DynamicObject::Ptr& owne
                        fp << "servicedowntime {" << "\n"
                           << "\t" << "service_description=" << service->GetAlias() << "\n";
 
-               String triggeredBy = downtime->Get("triggered_by");
+               Dictionary::Ptr triggeredByObj = DowntimeProcessor::GetDowntimeByID(downtime->Get("triggered_by"));
                int triggeredByLegacy = 0;
-               if (!triggeredBy.IsEmpty()) {
-                       Dictionary::Ptr triggeredByObj = DowntimeProcessor::GetDowntimeByID(triggeredBy);
-                       if (triggeredByObj->Contains("legacy_id"))
-                               triggeredByLegacy = triggeredByObj->Get("legacy_id");
-               }
+               if (triggeredByObj)
+                       triggeredByLegacy = triggeredByObj->Get("legacy_id");
 
                fp << "\t" << "host_name=" << host->GetName() << "\n"
                   << "\t" << "downtime_id=" << static_cast<String>(downtime->Get("legacy_id")) << "\n"
index 17b2939903096f6976c42248b778f83eb3a909a2..d21a44fc741bc9ba4bd3f65a31b652190d1cbc34 100644 (file)
@@ -46,9 +46,19 @@ String DowntimeProcessor::AddDowntime(const DynamicObject::Ptr& owner,
        downtime->Set("fixed", fixed);
        downtime->Set("duration", duration);
        downtime->Set("triggered_by", triggeredBy);
+       downtime->Set("triggers", boost::make_shared<Dictionary>());
        downtime->Set("trigger_time", 0);
        downtime->Set("legacy_id", m_NextDowntimeID++);
 
+       if (!triggeredBy.IsEmpty()) {
+               DynamicObject::Ptr otherOwner = GetOwnerByDowntimeID(triggeredBy);
+               Dictionary::Ptr otherDowntimes = otherOwner->Get("downtimes");
+               Dictionary::Ptr otherDowntime = otherDowntimes->Get(triggeredBy);
+               Dictionary::Ptr triggers = otherDowntime->Get("triggers");
+               triggers->Set(triggeredBy, triggeredBy);
+               otherOwner->Touch("downtimes");
+       }
+       
        Dictionary::Ptr downtimes = owner->Get("downtimes");
 
        if (!downtimes)
@@ -65,12 +75,52 @@ void DowntimeProcessor::RemoveDowntime(const String& id)
 {
        DynamicObject::Ptr owner = GetOwnerByDowntimeID(id);
 
+       if (!owner)
+               return;
+       
        Dictionary::Ptr downtimes = owner->Get("downtimes");
 
-       if (downtimes) {
-               downtimes->Remove(id);
-               owner->Touch("downtimes");
+       if (!downtimes)
+               return;
+
+       downtimes->Remove(id);
+       owner->Touch("downtimes");
+}
+
+void DowntimeProcessor::TriggerDowntimes(const DynamicObject::Ptr& owner)
+{
+       Dictionary::Ptr downtimes = owner->Get("downtimes");
+
+       if (!downtimes)
+               return;
+       
+       String id;
+       BOOST_FOREACH(tie(id, tuples::ignore), downtimes) {
+               TriggerDowntime(id);
+       }
+}
+
+void DowntimeProcessor::TriggerDowntime(const String& id)
+{
+       DynamicObject::Ptr owner = GetOwnerByDowntimeID(id);
+       Dictionary::Ptr downtime = GetDowntimeByID(id);
+
+       double now = Utility::GetTime();
+
+       if (now < downtime->Get("start_time") ||
+           now > downtime->Get("end_time"))
+               return;
+
+       if (downtime->Get("trigger_time") == 0)
+               downtime->Set("trigger_time", now);
+       
+       Dictionary::Ptr triggers = downtime->Get("triggers");
+       String tid;
+       BOOST_FOREACH(tie(tid, tuples::ignore), triggers) {
+               TriggerDowntime(tid);
        }
+       
+       owner->Touch("downtimes");
 }
 
 String DowntimeProcessor::GetIDFromLegacyID(int id)
@@ -78,7 +128,7 @@ String DowntimeProcessor::GetIDFromLegacyID(int id)
        map<int, String>::iterator it = m_LegacyDowntimeCache.find(id);
 
        if (it == m_LegacyDowntimeCache.end())
-               throw_exception(invalid_argument("Invalid legacy downtime ID specified."));
+               return Empty;
 
        return it->second;
 }
@@ -95,7 +145,7 @@ Dictionary::Ptr DowntimeProcessor::GetDowntimeByID(const String& id)
        DynamicObject::Ptr owner = GetOwnerByDowntimeID(id);
 
        if (!owner)
-               throw_exception(invalid_argument("Downtime ID does not exist."));
+               return Dictionary::Ptr();
 
        Dictionary::Ptr downtimes = owner->Get("downtimes");
 
index db8913d1b26b5a37f45bf4ae0cfffa607ba6dcb9..731d3aa5bd59d5c093b88cd91aa062a9812ca883 100644 (file)
@@ -40,6 +40,9 @@ public:
 
        static void RemoveDowntime(const String& id);
 
+        static void TriggerDowntimes(const DynamicObject::Ptr& owner);
+       static void TriggerDowntime(const String& id);
+
        static String GetIDFromLegacyID(int id);
        static DynamicObject::Ptr GetOwnerByDowntimeID(const String& id);
        static Dictionary::Ptr GetDowntimeByID(const String& id);
index b9848bbd0495b524ce3ca3d3b1e704afe5681ce0..7d314d931fb1385ea05d9391fddac133717725af 100644 (file)
@@ -564,6 +564,9 @@ void Service::ApplyCheckResult(const Dictionary::Ptr& cr)
                        }
                }
        }
+       
+       if (GetState() != StateOK)
+               DowntimeProcessor::TriggerDowntimes(GetSelf());
 }
 
 ServiceState Service::StateFromString(const String& state)