From: Michael Friedrich Date: Wed, 25 Sep 2013 16:01:08 +0000 (+0200) Subject: db_ido: Add statehistory. X-Git-Tag: v0.0.3~404 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=77abbc1556524f03d114ecaa73edca7dd4ede3a6;p=icinga2 db_ido: Add statehistory. refs #4379 --- diff --git a/lib/db_ido/servicedbobject.cpp b/lib/db_ido/servicedbobject.cpp index 212cb761b..e3ad7bb3d 100644 --- a/lib/db_ido/servicedbobject.cpp +++ b/lib/db_ido/servicedbobject.cpp @@ -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(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(); + 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); + } +} diff --git a/lib/db_ido/servicedbobject.h b/lib/db_ido/servicedbobject.h index 3b9eb269b..c35b8247b 100644 --- a/lib/db_ido/servicedbobject.h +++ b/lib/db_ido/servicedbobject.h @@ -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& 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); }; diff --git a/lib/icinga/service-check.cpp b/lib/icinga/service-check.cpp index 266fef2e9..5d4fe64ed 100644 --- a/lib/icinga/service-check.cpp +++ b/lib/icinga/service-check.cpp @@ -38,6 +38,7 @@ const double Service::DefaultCheckInterval = 5 * 60; const double Service::CheckIntervalDivisor = 5.0; boost::signals2::signal Service::OnNewCheckResult; +boost::signals2::signal Service::OnStateChange; boost::signals2::signal Service::OnNotificationsRequested; boost::signals2::signal Service::OnNextCheckChanged; boost::signals2::signal 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(); diff --git a/lib/icinga/service.h b/lib/icinga/service.h index cfece1f24..4d4203c5d 100644 --- a/lib/icinga/service.h +++ b/lib/icinga/service.h @@ -238,6 +238,7 @@ public: static boost::signals2::signal OnEnableNotificationsChanged; static boost::signals2::signal OnEnableFlappingChanged; static boost::signals2::signal OnNewCheckResult; + static boost::signals2::signal OnStateChange; static boost::signals2::signal OnNotificationsRequested; static boost::signals2::signal OnNotificationSentToUser; static boost::signals2::signal&, const NotificationType&, const Dictionary::Ptr&, const String&, const String&)> OnNotificationSentToAllUsers;