refactor downtime message handling (wip)

This commit is contained in:
Michael Friedrich 2013-06-28 16:08:43 +02:00
parent 6f7b231302
commit b906b51d3b
5 changed files with 42 additions and 12 deletions

View File

@ -68,11 +68,11 @@ void CompatLog::Start(void)
m_Endpoint = Endpoint::MakeEndpoint("compatlog_" + GetName(), false); m_Endpoint = Endpoint::MakeEndpoint("compatlog_" + GetName(), false);
m_Endpoint->RegisterTopicHandler("checker::CheckResult", m_Endpoint->RegisterTopicHandler("checker::CheckResult",
boost::bind(&CompatLog::CheckResultRequestHandler, this, _3)); boost::bind(&CompatLog::CheckResultRequestHandler, this, _3));
m_Endpoint->RegisterTopicHandler("icinga::Downtime",
boost::bind(&CompatLog::DowntimeRequestHandler, this, _3));
m_Endpoint->RegisterTopicHandler("icinga::NotificationSent", m_Endpoint->RegisterTopicHandler("icinga::NotificationSent",
boost::bind(&CompatLog::NotificationSentRequestHandler, this, _3)); boost::bind(&CompatLog::NotificationSentRequestHandler, this, _3));
Service::OnDowntimeChanged.connect(bind(&CompatLog::DowntimeHandler, this, _1, _2));
m_RotationTimer = boost::make_shared<Timer>(); m_RotationTimer = boost::make_shared<Timer>();
m_RotationTimer->OnTimerExpired.connect(boost::bind(&CompatLog::RotationTimerHandler, this)); m_RotationTimer->OnTimerExpired.connect(boost::bind(&CompatLog::RotationTimerHandler, this));
m_RotationTimer->Start(); m_RotationTimer->Start();
@ -208,21 +208,13 @@ void CompatLog::CheckResultRequestHandler(const RequestMessage& request)
/** /**
* @threadsafety Always. * @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(); Host::Ptr host = service->GetHost();
if (!host) if (!host)
return; return;
DowntimeState downtime_state = params.GetState();
String downtime_state_str; String downtime_state_str;
String downtime_output; String downtime_output;

View File

@ -20,6 +20,7 @@
#ifndef COMPATLOG_H #ifndef COMPATLOG_H
#define COMPATLOG_H #define COMPATLOG_H
#include "icinga/service.h"
#include "remoting/endpoint.h" #include "remoting/endpoint.h"
#include "base/dynamicobject.h" #include "base/dynamicobject.h"
#include "base/timer.h" #include "base/timer.h"
@ -63,8 +64,8 @@ private:
Endpoint::Ptr m_Endpoint; Endpoint::Ptr m_Endpoint;
void CheckResultRequestHandler(const RequestMessage& request); void CheckResultRequestHandler(const RequestMessage& request);
void DowntimeRequestHandler(const RequestMessage& request);
void NotificationSentRequestHandler(const RequestMessage& request); void NotificationSentRequestHandler(const RequestMessage& request);
void DowntimeHandler(const Service::Ptr& service, DowntimeState downtime_state);
Timer::Ptr m_RotationTimer; Timer::Ptr m_RotationTimer;
void RotationTimerHandler(void); void RotationTimerHandler(void);

View File

@ -39,6 +39,20 @@ static bool l_DowntimesCacheNeedsUpdate = false;
static Timer::Ptr l_DowntimesCacheTimer; static Timer::Ptr l_DowntimesCacheTimer;
static Timer::Ptr l_DowntimesExpireTimer; 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) int Service::GetNextDowntimeID(void)
{ {
boost::mutex::scoped_lock lock(l_DowntimeMutex); boost::mutex::scoped_lock lock(l_DowntimeMutex);

View File

@ -22,6 +22,7 @@
#include "icinga/checkcommand.h" #include "icinga/checkcommand.h"
#include "icinga/icingaapplication.h" #include "icinga/icingaapplication.h"
#include "icinga/macroprocessor.h" #include "icinga/macroprocessor.h"
#include "icinga/downtimemessage.h"
#include "config/configitembuilder.h" #include "config/configitembuilder.h"
#include "base/dynamictype.h" #include "base/dynamictype.h"
#include "base/objectlock.h" #include "base/objectlock.h"
@ -34,6 +35,8 @@ using namespace icinga;
REGISTER_TYPE(Service); REGISTER_TYPE(Service);
boost::once_flag Service::m_OnceFlag = BOOST_ONCE_INIT;
Service::Service(const Dictionary::Ptr& serializedObject) Service::Service(const Dictionary::Ptr& serializedObject)
: DynamicObject(serializedObject), m_CheckRunning(false) : DynamicObject(serializedObject), m_CheckRunning(false)
{ {
@ -90,6 +93,8 @@ Service::Service(const Dictionary::Ptr& serializedObject)
RegisterAttribute("enable_flapping", Attribute_Config, &m_EnableFlapping); RegisterAttribute("enable_flapping", Attribute_Config, &m_EnableFlapping);
SetSchedulingOffset(rand()); SetSchedulingOffset(rand());
boost::call_once(m_OnceFlag, &Service::Initialize);
} }
Service::~Service(void) Service::~Service(void)
@ -100,6 +105,13 @@ Service::~Service(void)
Service::InvalidateCommentsCache(); 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) void Service::OnRegistrationCompleted(void)
{ {
ASSERT(!OwnsLock()); ASSERT(!OwnsLock());

View File

@ -25,9 +25,13 @@
#include "icinga/host.h" #include "icinga/host.h"
#include "icinga/timeperiod.h" #include "icinga/timeperiod.h"
#include "icinga/notification.h" #include "icinga/notification.h"
#include "remoting/requestmessage.h"
#include "remoting/endpoint.h"
#include "base/i2-base.h"
#include "base/dynamicobject.h" #include "base/dynamicobject.h"
#include "base/array.h" #include "base/array.h"
#include <boost/signals2.hpp> #include <boost/signals2.hpp>
#include <boost/thread/once.hpp>
namespace icinga namespace icinga
{ {
@ -191,6 +195,7 @@ public:
static boost::signals2::signal<void (const Service::Ptr&)> OnCheckerChanged; 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&)> OnNextCheckChanged;
static boost::signals2::signal<void (const Service::Ptr&, DowntimeState)> OnDowntimeChanged;
virtual bool ResolveMacro(const String& macro, const Dictionary::Ptr& cr, String *result) const; virtual bool ResolveMacro(const String& macro, const Dictionary::Ptr& cr, String *result) const;
@ -311,10 +316,16 @@ private:
bool m_CheckRunning; bool m_CheckRunning;
long m_SchedulingOffset; long m_SchedulingOffset;
static boost::once_flag m_OnceFlag;
static Endpoint::Ptr m_Endpoint;
static void Initialize(void);
/* Downtimes */ /* Downtimes */
Attribute<Dictionary::Ptr> m_Downtimes; Attribute<Dictionary::Ptr> m_Downtimes;
static void DowntimesExpireTimerHandler(void); static void DowntimesExpireTimerHandler(void);
static void DowntimeRequestHandler(const RequestMessage& request);
void RemoveExpiredDowntimes(void); void RemoveExpiredDowntimes(void);