[icinga-checkins] icinga.org: icinga2/fix/refactor-downtime-message-4359: refactor downtime message handling (wip)

git at icinga.org git at icinga.org
Mon Jul 1 11:31:58 CEST 2013


Module: icinga2
Branch: fix/refactor-downtime-message-4359
Commit: b906b51d3b057995d31d332c92f64e4a5e1a2d13
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=b906b51d3b057995d31d332c92f64e4a5e1a2d13

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Fri Jun 28 16:08:43 2013 +0200

refactor downtime message handling (wip)

---

 components/compat/compatlog.cpp |   14 +++-----------
 components/compat/compatlog.h   |    3 ++-
 lib/icinga/service-downtime.cpp |   14 ++++++++++++++
 lib/icinga/service.cpp          |   12 ++++++++++++
 lib/icinga/service.h            |   11 +++++++++++
 5 files changed, 42 insertions(+), 12 deletions(-)

diff --git a/components/compat/compatlog.cpp b/components/compat/compatlog.cpp
index 89a3c65..52fcfa0 100644
--- a/components/compat/compatlog.cpp
+++ b/components/compat/compatlog.cpp
@@ -68,11 +68,11 @@ void CompatLog::Start(void)
 	m_Endpoint = Endpoint::MakeEndpoint("compatlog_" + GetName(), false);
 	m_Endpoint->RegisterTopicHandler("checker::CheckResult",
 	    boost::bind(&CompatLog::CheckResultRequestHandler, this, _3));
-	m_Endpoint->RegisterTopicHandler("icinga::Downtime",
-	    boost::bind(&CompatLog::DowntimeRequestHandler, this, _3));
 	m_Endpoint->RegisterTopicHandler("icinga::NotificationSent",
 	    boost::bind(&CompatLog::NotificationSentRequestHandler, this, _3));
 
+	Service::OnDowntimeChanged.connect(bind(&CompatLog::DowntimeHandler, this, _1, _2));
+
 	m_RotationTimer = boost::make_shared<Timer>();
 	m_RotationTimer->OnTimerExpired.connect(boost::bind(&CompatLog::RotationTimerHandler, this));
 	m_RotationTimer->Start();
@@ -208,21 +208,13 @@ void CompatLog::CheckResultRequestHandler(const RequestMessage& request)
 /**
  * @threadsafety Always.
  */
-void CompatLog::DowntimeRequestHandler(const RequestMessage& request)
+void CompatLog::DowntimeHandler(const Service::Ptr& service, DowntimeState downtime_state)
 {
-	DowntimeMessage params;
-	if (!request.GetParams(&params))
-		return;
-
-	String svcname = params.GetService();
-	Service::Ptr service = Service::GetByName(svcname);
-
 	Host::Ptr host = service->GetHost();
 
 	if (!host)
 		return;
 
-	DowntimeState downtime_state = params.GetState();
 	String downtime_state_str;
 	String downtime_output;
 
diff --git a/components/compat/compatlog.h b/components/compat/compatlog.h
index eb4a3a4..2598422 100644
--- a/components/compat/compatlog.h
+++ b/components/compat/compatlog.h
@@ -20,6 +20,7 @@
 #ifndef COMPATLOG_H
 #define COMPATLOG_H
 
+#include "icinga/service.h"
 #include "remoting/endpoint.h"
 #include "base/dynamicobject.h"
 #include "base/timer.h"
@@ -63,8 +64,8 @@ private:
 
 	Endpoint::Ptr m_Endpoint;
 	void CheckResultRequestHandler(const RequestMessage& request);
-	void DowntimeRequestHandler(const RequestMessage& request);
 	void NotificationSentRequestHandler(const RequestMessage& request);
+	void DowntimeHandler(const Service::Ptr& service, DowntimeState downtime_state);
 
 	Timer::Ptr m_RotationTimer;
 	void RotationTimerHandler(void);
diff --git a/lib/icinga/service-downtime.cpp b/lib/icinga/service-downtime.cpp
index a3c18c0..1552d02 100644
--- a/lib/icinga/service-downtime.cpp
+++ b/lib/icinga/service-downtime.cpp
@@ -39,6 +39,20 @@ static bool l_DowntimesCacheNeedsUpdate = false;
 static Timer::Ptr l_DowntimesCacheTimer;
 static Timer::Ptr l_DowntimesExpireTimer;
 
+boost::signals2::signal<void (const Service::Ptr&, DowntimeState)> Service::OnDowntimeChanged;
+
+void Service::DowntimeRequestHandler(const RequestMessage& request)
+{
+	DowntimeMessage params;
+	if (!request.GetParams(&params))
+		return;
+
+	String svcname = params.GetService();
+	Service::Ptr service = Service::GetByName(svcname);
+
+	OnDowntimeChanged(service, params.GetState());
+}
+
 int Service::GetNextDowntimeID(void)
 {
 	boost::mutex::scoped_lock lock(l_DowntimeMutex);
diff --git a/lib/icinga/service.cpp b/lib/icinga/service.cpp
index d11fa40..674e3de 100644
--- a/lib/icinga/service.cpp
+++ b/lib/icinga/service.cpp
@@ -22,6 +22,7 @@
 #include "icinga/checkcommand.h"
 #include "icinga/icingaapplication.h"
 #include "icinga/macroprocessor.h"
+#include "icinga/downtimemessage.h"
 #include "config/configitembuilder.h"
 #include "base/dynamictype.h"
 #include "base/objectlock.h"
@@ -34,6 +35,8 @@ using namespace icinga;
 
 REGISTER_TYPE(Service);
 
+boost::once_flag Service::m_OnceFlag = BOOST_ONCE_INIT;
+
 Service::Service(const Dictionary::Ptr& serializedObject)
 	: DynamicObject(serializedObject), m_CheckRunning(false)
 {
@@ -90,6 +93,8 @@ Service::Service(const Dictionary::Ptr& serializedObject)
 	RegisterAttribute("enable_flapping", Attribute_Config, &m_EnableFlapping);
 
 	SetSchedulingOffset(rand());
+
+	boost::call_once(m_OnceFlag, &Service::Initialize);
 }
 
 Service::~Service(void)
@@ -100,6 +105,13 @@ Service::~Service(void)
 	Service::InvalidateCommentsCache();
 }
 
+void Service::Initialize(void)
+{
+	m_Endpoint = Endpoint::MakeEndpoint("service", false);
+	m_Endpoint->RegisterTopicHandler("icinga::Downtime",
+	    boost::bind(&Service::DowntimeRequestHandler, _3));
+}
+
 void Service::OnRegistrationCompleted(void)
 {
 	ASSERT(!OwnsLock());
diff --git a/lib/icinga/service.h b/lib/icinga/service.h
index 8c07698..10b58ec 100644
--- a/lib/icinga/service.h
+++ b/lib/icinga/service.h
@@ -25,9 +25,13 @@
 #include "icinga/host.h"
 #include "icinga/timeperiod.h"
 #include "icinga/notification.h"
+#include "remoting/requestmessage.h"
+#include "remoting/endpoint.h"
+#include "base/i2-base.h"
 #include "base/dynamicobject.h"
 #include "base/array.h"
 #include <boost/signals2.hpp>
+#include <boost/thread/once.hpp>
 
 namespace icinga
 {
@@ -191,6 +195,7 @@ public:
 
 	static boost::signals2::signal<void (const Service::Ptr&)> OnCheckerChanged;
 	static boost::signals2::signal<void (const Service::Ptr&)> OnNextCheckChanged;
+	static boost::signals2::signal<void (const Service::Ptr&, DowntimeState)> OnDowntimeChanged;
 
 	virtual bool ResolveMacro(const String& macro, const Dictionary::Ptr& cr, String *result) const;
 
@@ -311,10 +316,16 @@ private:
 	bool m_CheckRunning;
 	long m_SchedulingOffset;
 
+	static boost::once_flag m_OnceFlag;
+	static Endpoint::Ptr m_Endpoint;
+
+	static void Initialize(void);
+
 	/* Downtimes */
 	Attribute<Dictionary::Ptr> m_Downtimes;
 
 	static void DowntimesExpireTimerHandler(void);
+	static void DowntimeRequestHandler(const RequestMessage& request);
 
 	void RemoveExpiredDowntimes(void);
 





More information about the icinga-checkins mailing list