From b906b51d3b057995d31d332c92f64e4a5e1a2d13 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Fri, 28 Jun 2013 16:08:43 +0200 Subject: [PATCH] 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 89a3c65e4..52fcfa06a 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(); 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(¶ms)) - 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 eb4a3a416..259842230 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 a3c18c0cb..1552d0243 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 Service::OnDowntimeChanged; + +void Service::DowntimeRequestHandler(const RequestMessage& request) +{ + DowntimeMessage params; + if (!request.GetParams(¶ms)) + 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 d11fa40c1..674e3de8c 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 8c07698eb..10b58ec9c 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 +#include namespace icinga { @@ -191,6 +195,7 @@ public: static boost::signals2::signal OnCheckerChanged; static boost::signals2::signal OnNextCheckChanged; + static boost::signals2::signal 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 m_Downtimes; static void DowntimesExpireTimerHandler(void); + static void DowntimeRequestHandler(const RequestMessage& request); void RemoveExpiredDowntimes(void); -- 2.40.0