From 16de7f853bde1db4d3fb65f44b2b79cd44ef8945 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Tue, 1 Oct 2013 11:04:30 +0200 Subject: [PATCH] db_ido: Add eventhandlers. refs #4768 --- lib/db_ido/servicedbobject.cpp | 48 ++++++++++++++++++++++++++++++++++ lib/db_ido/servicedbobject.h | 1 + lib/icinga/service-event.cpp | 5 ++++ lib/icinga/service.h | 1 + 4 files changed, 55 insertions(+) diff --git a/lib/db_ido/servicedbobject.cpp b/lib/db_ido/servicedbobject.cpp index 4193bd924..8c2c1fde8 100644 --- a/lib/db_ido/servicedbobject.cpp +++ b/lib/db_ido/servicedbobject.cpp @@ -66,6 +66,8 @@ void ServiceDbObject::StaticInitialize(void) Service::OnFlappingChanged.connect(bind(&ServiceDbObject::AddFlappingHistory, _1, _2)); Service::OnNewCheckResult.connect(bind(&ServiceDbObject::AddServiceCheckHistory, _1, _2)); + Service::OnEventCommandExecuted.connect(bind(&ServiceDbObject::AddEventHandlerHistory, _1)); + ExternalCommandProcessor::OnNewExternalCommand.connect(bind(&ServiceDbObject::AddExternalCommandHistory, _1, _2, _3)); } @@ -1322,6 +1324,52 @@ void ServiceDbObject::AddServiceCheckHistory(const Service::Ptr& service, const } } +/* eventhandlers */ +void ServiceDbObject::AddEventHandlerHistory(const Service::Ptr& service) +{ + Host::Ptr host = service->GetHost(); + + if (!host) + return; + + Log(LogDebug, "db_ido", "add eventhandler history for '" + service->GetName() + "'"); + + double now = Utility::GetTime(); + unsigned long event_time = static_cast(now); + unsigned long event_time_usec = (now - event_time) * 1000 * 1000; + + DbQuery query1; + query1.Table = "eventhandlers"; + query1.Type = DbQueryInsert; + + Dictionary::Ptr fields1 = boost::make_shared(); + + fields1->Set("eventhandler_type", 1); /* service */ + fields1->Set("object_id", service); + fields1->Set("state", service->GetState()); + fields1->Set("state_type", service->GetStateType()); + + fields1->Set("start_time", DbValue::FromTimestamp(event_time)); + fields1->Set("start_time_usec", event_time_usec); + fields1->Set("end_time", DbValue::FromTimestamp(event_time)); + fields1->Set("end_time_usec", event_time_usec); + fields1->Set("command_object_id", service->GetEventCommand()); + + fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */ + + query1.Fields = fields1; + OnQuery(query1); + + if (host->GetCheckService() == service) { + fields1->Set("eventhandler_type", 0); /* host */ + fields1->Set("object_id", host); + fields1->Set("state", host->GetState()); + fields1->Set("state_type", host->GetStateType()); + query1.Fields = fields1; + OnQuery(query1); + } +} + /* externalcommands */ void ServiceDbObject::AddExternalCommandHistory(double time, const String& command, const std::vector& arguments) { diff --git a/lib/db_ido/servicedbobject.h b/lib/db_ido/servicedbobject.h index 8a3228e5d..1b8fd7230 100644 --- a/lib/db_ido/servicedbobject.h +++ b/lib/db_ido/servicedbobject.h @@ -110,6 +110,7 @@ private: static void AddFlappingHistory(const Service::Ptr& service, FlappingState flapping_state); static void AddServiceCheckHistory(const Service::Ptr& service, const Dictionary::Ptr &cr); + static void AddEventHandlerHistory(const Service::Ptr& service); static void AddExternalCommandHistory(double time, const String& command, const std::vector& arguments); }; diff --git a/lib/icinga/service-event.cpp b/lib/icinga/service-event.cpp index f7574b9ab..084a0e463 100644 --- a/lib/icinga/service-event.cpp +++ b/lib/icinga/service-event.cpp @@ -22,6 +22,8 @@ using namespace icinga; +boost::signals2::signal Service::OnEventCommandExecuted; + EventCommand::Ptr Service::GetEventCommand(void) const { return EventCommand::GetByName(m_EventCommand); @@ -35,5 +37,8 @@ void Service::ExecuteEventHandler(void) return; Log(LogDebug, "icinga", "Executing event handler for service '" + GetName() + "'"); + ec->Execute(GetSelf()); + + OnEventCommandExecuted(GetSelf()); } \ No newline at end of file diff --git a/lib/icinga/service.h b/lib/icinga/service.h index 4d4203c5d..0371f79d7 100644 --- a/lib/icinga/service.h +++ b/lib/icinga/service.h @@ -250,6 +250,7 @@ public: static boost::signals2::signal OnDowntimeTriggered; static boost::signals2::signal OnAcknowledgementSet; static boost::signals2::signal OnAcknowledgementCleared; + static boost::signals2::signal OnEventCommandExecuted; virtual bool ResolveMacro(const String& macro, const Dictionary::Ptr& cr, String *result) const;