]> granicus.if.org Git - icinga2/commitdiff
db_ido: Add statehistory.
authorMichael Friedrich <michael.friedrich@netways.de>
Wed, 25 Sep 2013 16:01:08 +0000 (18:01 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Wed, 25 Sep 2013 16:01:08 +0000 (18:01 +0200)
refs #4379

lib/db_ido/servicedbobject.cpp
lib/db_ido/servicedbobject.h
lib/icinga/service-check.cpp
lib/icinga/service.h

index 212cb761b656280b1db5394d07491741e153a7ce..e3ad7bb3de1044a6bedc8bd277fe544672eb94b3 100644 (file)
@@ -52,6 +52,8 @@ void ServiceDbObject::StaticInitialize(void)
        Service::OnDowntimeAdded.connect(boost::bind(&ServiceDbObject::AddDowntimeHistory, _1, _2));
        Service::OnAcknowledgementSet.connect(boost::bind(&ServiceDbObject::AddAcknowledgementHistory, _1, _2, _3, _4, _5));
        Service::OnNotificationSentToAllUsers.connect(bind(&ServiceDbObject::AddNotificationHistory, _1, _2, _3, _4, _5, _6));
+
+       Service::OnStateChange.connect(boost::bind(&ServiceDbObject::AddStateChangeHistory, _1, _2, _3));
 }
 
 ServiceDbObject::ServiceDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
@@ -877,3 +879,56 @@ void ServiceDbObject::AddNotificationHistory(const Service::Ptr& service, const
                OnQuery(query2);
        }
 }
+
+void ServiceDbObject::AddStateChangeHistory(const Service::Ptr& service, const Dictionary::Ptr& cr, StateType type)
+{
+       Host::Ptr host = service->GetHost();
+
+       if (!host)
+               return;
+
+       Log(LogDebug, "db_ido", "add state change for '" + service->GetName() + "'");
+
+       double now = Utility::GetTime();
+       unsigned long state_time = static_cast<long>(now);
+       unsigned long state_time_usec = (now - state_time) * 1000 * 1000;
+
+       DbQuery query1;
+       query1.Table = "statehistory";
+       query1.Type = DbQueryInsert;
+
+       Dictionary::Ptr fields1 = boost::make_shared<Dictionary>();
+       fields1->Set("state_time", DbValue::FromTimestamp(state_time));
+       fields1->Set("state_time_usec", state_time_usec);
+       fields1->Set("object_id", service);
+       fields1->Set("state_change", 1); /* service */
+       fields1->Set("state", service->GetState());
+       fields1->Set("state_type", service->GetStateType());
+       fields1->Set("current_check_attempt", service->GetCurrentCheckAttempt());
+       fields1->Set("max_check_attempts", service->GetMaxCheckAttempts());
+       fields1->Set("last_state", service->GetLastState());
+       fields1->Set("last_hard_state", service->GetLastHardState());
+
+       if (cr) {
+               Dictionary::Ptr output_bag = CompatUtility::GetCheckResultOutput(cr);
+               fields1->Set("output", output_bag->Get("output"));
+               fields1->Set("long_output", output_bag->Get("long_output"));
+       }
+
+       fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
+
+       query1.Fields = fields1;
+       OnQuery(query1);
+
+       if (host->GetCheckService() == service) {
+               fields1->Set("object_id", host);
+               fields1->Set("state_change", 0); /* host */
+               /* get host states instead */
+               fields1->Set("state", host->GetState());
+               fields1->Set("state_type", host->GetStateType());
+               fields1->Set("last_state", host->GetLastState());
+               fields1->Set("last_hard_state", host->GetLastHardState());
+               query1.Fields = fields1;
+               OnQuery(query1);
+       }
+}
index 3b9eb269b4accbbc7135477c81c7004ef984110f..c35b8247bd11b348c4b664d9616c22f93b39319a 100644 (file)
@@ -75,6 +75,7 @@ private:
         static void AddDowntimeHistory(const Service::Ptr& service, const Dictionary::Ptr& downtime);
         static void AddAcknowledgementHistory(const Service::Ptr& service, const String& author, const String& comment, AcknowledgementType type, double expiry);
         static void AddNotificationHistory(const Service::Ptr& service, const std::set<User::Ptr>& users, NotificationType type, const Dictionary::Ptr& cr, const String& author, const String& text);
+        static void AddStateChangeHistory(const Service::Ptr& service, const Dictionary::Ptr& cr, StateType type);
 
 };
 
index 266fef2e9b7f8b48e2299227f6092060b6978014..5d4fe64edd7e5dd7d990ba9965620b5484c2ff1d 100644 (file)
@@ -38,6 +38,7 @@ const double Service::DefaultCheckInterval = 5 * 60;
 const double Service::CheckIntervalDivisor = 5.0;
 
 boost::signals2::signal<void (const Service::Ptr&, const Dictionary::Ptr&, const String&)> Service::OnNewCheckResult;
+boost::signals2::signal<void (const Service::Ptr&, const Dictionary::Ptr&, StateType, const String&)> Service::OnStateChange;
 boost::signals2::signal<void (const Service::Ptr&, NotificationType, const Dictionary::Ptr&, const String&, const String&)> Service::OnNotificationsRequested;
 boost::signals2::signal<void (const Service::Ptr&, double, const String&)> Service::OnNextCheckChanged;
 boost::signals2::signal<void (const Service::Ptr&, bool, const String&)> Service::OnForceNextCheckChanged;
@@ -620,6 +621,13 @@ void Service::ProcessCheckResult(const Dictionary::Ptr& cr, const String& author
        Utility::QueueAsyncCallback(boost::bind(boost::ref(OnNewCheckResult), GetSelf(), cr, authority));
        OnStateChanged(GetSelf());
 
+       if (hardChange) {
+               Utility::QueueAsyncCallback(boost::bind(boost::ref(OnStateChange), GetSelf(), cr, StateTypeHard, authority));
+       }
+       else if (stateChange) {
+               Utility::QueueAsyncCallback(boost::bind(boost::ref(OnStateChange), GetSelf(), cr, StateTypeSoft, authority));
+       }
+
        if (call_eventhandler)
                ExecuteEventHandler();
 
index cfece1f24556bbb9ff86c96a8454cf17ca3683da..4d4203c5df7c7da207ed57ac5dc47d227b3235a0 100644 (file)
@@ -238,6 +238,7 @@ public:
        static boost::signals2::signal<void (const Service::Ptr&, bool, const String&)> OnEnableNotificationsChanged;
        static boost::signals2::signal<void (const Service::Ptr&, bool, const String&)> OnEnableFlappingChanged;
        static boost::signals2::signal<void (const Service::Ptr&, const Dictionary::Ptr&, const String&)> OnNewCheckResult;
+       static boost::signals2::signal<void (const Service::Ptr&, const Dictionary::Ptr&, StateType, const String&)> OnStateChange;
        static boost::signals2::signal<void (const Service::Ptr&, NotificationType, const Dictionary::Ptr&, const String&, const String&)> OnNotificationsRequested;
        static boost::signals2::signal<void (const Service::Ptr&, const User::Ptr&, const NotificationType&, const Dictionary::Ptr&, const String&, const String&)> OnNotificationSentToUser;
        static boost::signals2::signal<void (const Service::Ptr&, const std::set<User::Ptr>&, const NotificationType&, const Dictionary::Ptr&, const String&, const String&)> OnNotificationSentToAllUsers;