]> granicus.if.org Git - icinga2/commitdiff
refactor downtime message handling (wip)
authorMichael Friedrich <michael.friedrich@netways.de>
Fri, 28 Jun 2013 14:08:43 +0000 (16:08 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Mon, 1 Jul 2013 09:28:49 +0000 (11:28 +0200)
components/compat/compatlog.cpp
components/compat/compatlog.h
lib/icinga/service-downtime.cpp
lib/icinga/service.cpp
lib/icinga/service.h

index 89a3c65e4bae1bf53be23fad45aecba6133fa333..52fcfa06a629d2660c722a0a5699eda8bba69d04 100644 (file)
@@ -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;
 
index eb4a3a4167554dabf4ce9470d6020c89a33feca5..259842230796783676f9f9db0d079bd47cb0d6c7 100644 (file)
@@ -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);
index a3c18c0cb9a423f64330a85aa87acd7e256f9dd4..1552d0243cd505b94ed523376729988397fbc3bb 100644 (file)
@@ -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);
index d11fa40c1b6779ad983b2b070d2eba0868d1ae73..674e3de8c9f0ee9750538e3c46bdec5372806260 100644 (file)
@@ -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());
index 8c07698eb5632f55298d79165de9ce952d620a94..10b58ec9ca2c9892be1991fcde8de66538c78253 100644 (file)
 #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);