2012-07-02 12:34:54 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "i2-cibsync.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the component.
|
|
|
|
*/
|
|
|
|
void CIBSyncComponent::Start(void)
|
|
|
|
{
|
2012-07-02 15:08:15 +02:00
|
|
|
m_SyncingConfig = false;
|
|
|
|
|
2012-07-02 12:34:54 +02:00
|
|
|
m_Endpoint = boost::make_shared<VirtualEndpoint>();
|
2012-07-02 15:08:15 +02:00
|
|
|
|
|
|
|
/* config objects */
|
|
|
|
m_Endpoint->RegisterTopicHandler("config::FetchObjects",
|
|
|
|
boost::bind(&CIBSyncComponent::FetchObjectsHandler, this, _2));
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
DynamicObject::OnRegistered.connect(boost::bind(&CIBSyncComponent::LocalObjectRegisteredHandler, this, _1));
|
|
|
|
DynamicObject::OnUnregistered.connect(boost::bind(&CIBSyncComponent::LocalObjectUnregisteredHandler, this, _1));
|
|
|
|
DynamicObject::OnTransactionClosing.connect(boost::bind(&CIBSyncComponent::TransactionClosingHandler, this, _1));
|
2012-07-02 15:08:15 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
m_Endpoint->RegisterPublication("config::ObjectUpdate");
|
2012-07-02 15:08:15 +02:00
|
|
|
m_Endpoint->RegisterPublication("config::ObjectRemoved");
|
|
|
|
|
|
|
|
EndpointManager::GetInstance()->OnNewEndpoint.connect(boost::bind(&CIBSyncComponent::NewEndpointHandler, this, _2));
|
|
|
|
|
|
|
|
m_Endpoint->RegisterPublication("config::FetchObjects");
|
2012-08-02 09:38:08 +02:00
|
|
|
m_Endpoint->RegisterTopicHandler("config::ObjectUpdate",
|
|
|
|
boost::bind(&CIBSyncComponent::RemoteObjectUpdateHandler, this, _2, _3));
|
2012-07-02 15:08:15 +02:00
|
|
|
m_Endpoint->RegisterTopicHandler("config::ObjectRemoved",
|
|
|
|
boost::bind(&CIBSyncComponent::RemoteObjectRemovedHandler, this, _3));
|
|
|
|
|
|
|
|
/* service status */
|
2012-07-25 09:03:02 +02:00
|
|
|
m_Endpoint->RegisterTopicHandler("checker::CheckResult",
|
|
|
|
boost::bind(&CIBSyncComponent::CheckResultRequestHandler, _2, _3));
|
2012-07-02 15:08:15 +02:00
|
|
|
|
2012-07-02 12:34:54 +02:00
|
|
|
EndpointManager::GetInstance()->RegisterEndpoint(m_Endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the component.
|
|
|
|
*/
|
|
|
|
void CIBSyncComponent::Stop(void)
|
|
|
|
{
|
|
|
|
EndpointManager::Ptr endpointManager = EndpointManager::GetInstance();
|
|
|
|
|
|
|
|
if (endpointManager)
|
|
|
|
endpointManager->UnregisterEndpoint(m_Endpoint);
|
|
|
|
}
|
|
|
|
|
2012-07-25 09:03:02 +02:00
|
|
|
void CIBSyncComponent::CheckResultRequestHandler(const Endpoint::Ptr& sender, const RequestMessage& request)
|
2012-07-02 12:34:54 +02:00
|
|
|
{
|
2012-07-25 09:03:02 +02:00
|
|
|
CheckResultMessage params;
|
2012-07-02 12:34:54 +02:00
|
|
|
if (!request.GetParams(¶ms))
|
|
|
|
return;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String svcname;
|
2012-07-03 14:18:46 +02:00
|
|
|
if (!params.GetService(&svcname))
|
2012-07-02 12:34:54 +02:00
|
|
|
return;
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
Service::Ptr service = Service::GetByName(svcname);
|
2012-07-02 12:34:54 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
//CheckResult cr;
|
|
|
|
//if (!params.GetCheckResult(&cr))
|
|
|
|
// return;
|
2012-07-25 09:03:02 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
//Service::OnCheckResultReceived(service, params);
|
|
|
|
//service->ApplyCheckResult(cr);
|
2012-07-18 11:52:22 +02:00
|
|
|
|
2012-07-25 12:59:17 +02:00
|
|
|
time_t now = Utility::GetTime();
|
2012-07-02 12:34:54 +02:00
|
|
|
CIB::UpdateTaskStatistics(now, 1);
|
|
|
|
}
|
|
|
|
|
2012-07-02 15:08:15 +02:00
|
|
|
void CIBSyncComponent::NewEndpointHandler(const Endpoint::Ptr& endpoint)
|
|
|
|
{
|
|
|
|
/* no need to sync the config with local endpoints */
|
|
|
|
if (endpoint->IsLocal())
|
|
|
|
return;
|
|
|
|
|
|
|
|
endpoint->OnSessionEstablished.connect(boost::bind(&CIBSyncComponent::SessionEstablishedHandler, this, _1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CIBSyncComponent::SessionEstablishedHandler(const Endpoint::Ptr& endpoint)
|
|
|
|
{
|
|
|
|
RequestMessage request;
|
|
|
|
request.SetMethod("config::FetchObjects");
|
|
|
|
|
|
|
|
EndpointManager::GetInstance()->SendUnicastMessage(m_Endpoint, endpoint, request);
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
RequestMessage CIBSyncComponent::MakeObjectMessage(const DynamicObject::Ptr& object, const String& method, double sinceTx, bool includeProperties)
|
2012-07-02 15:08:15 +02:00
|
|
|
{
|
|
|
|
RequestMessage msg;
|
|
|
|
msg.SetMethod(method);
|
|
|
|
|
|
|
|
MessagePart params;
|
|
|
|
msg.SetParams(params);
|
|
|
|
|
2012-07-09 16:19:56 +02:00
|
|
|
params.Set("name", object->GetName());
|
|
|
|
params.Set("type", object->GetType());
|
2012-07-02 15:08:15 +02:00
|
|
|
|
|
|
|
if (includeProperties)
|
2012-08-02 09:38:08 +02:00
|
|
|
params.Set("update", object->BuildUpdate(sinceTx, Attribute_Replicated | Attribute_Config));
|
2012-07-02 15:08:15 +02:00
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
bool CIBSyncComponent::ShouldReplicateObject(const DynamicObject::Ptr& object)
|
2012-07-02 15:08:15 +02:00
|
|
|
{
|
|
|
|
return (!object->IsLocal());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CIBSyncComponent::FetchObjectsHandler(const Endpoint::Ptr& sender)
|
|
|
|
{
|
2012-07-30 10:17:29 +02:00
|
|
|
pair<DynamicObject::TypeMap::iterator, DynamicObject::TypeMap::iterator> trange;
|
|
|
|
DynamicObject::TypeMap::iterator tt;
|
2012-07-27 16:05:02 +02:00
|
|
|
for (tt = trange.first; tt != trange.second; tt++) {
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::Ptr object;
|
2012-07-27 16:05:02 +02:00
|
|
|
BOOST_FOREACH(tie(tuples::ignore, object), tt->second) {
|
|
|
|
if (!ShouldReplicateObject(object))
|
|
|
|
continue;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
RequestMessage request = MakeObjectMessage(object, "config::ObjectUpdate", 0, true);
|
2012-07-27 16:05:02 +02:00
|
|
|
|
|
|
|
EndpointManager::GetInstance()->SendUnicastMessage(m_Endpoint, sender, request);
|
|
|
|
}
|
2012-07-02 15:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
void CIBSyncComponent::LocalObjectRegisteredHandler(const DynamicObject::Ptr& object)
|
2012-07-02 15:08:15 +02:00
|
|
|
{
|
|
|
|
/* don't send messages when we're currently processing a remote update */
|
|
|
|
if (m_SyncingConfig)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!ShouldReplicateObject(object))
|
|
|
|
return;
|
|
|
|
|
|
|
|
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint,
|
2012-08-02 09:38:08 +02:00
|
|
|
MakeObjectMessage(object, "config::ObjectCommitted", 0, true));
|
2012-07-02 15:08:15 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
void CIBSyncComponent::LocalObjectUnregisteredHandler(const DynamicObject::Ptr& object)
|
2012-07-02 15:08:15 +02:00
|
|
|
{
|
|
|
|
/* don't send messages when we're currently processing a remote update */
|
|
|
|
if (m_SyncingConfig)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!ShouldReplicateObject(object))
|
|
|
|
return;
|
|
|
|
|
|
|
|
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint,
|
2012-08-02 09:38:08 +02:00
|
|
|
MakeObjectMessage(object, "config::ObjectRemoved", 0, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CIBSyncComponent::TransactionClosingHandler(const set<DynamicObject::Ptr>& modifiedObjects)
|
|
|
|
{
|
|
|
|
stringstream msgbuf;
|
|
|
|
msgbuf << "Sending " << modifiedObjects.size() << " replication updates.";
|
|
|
|
Logger::Write(LogInformation, "cibsync", msgbuf.str());
|
|
|
|
|
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, modifiedObjects) {
|
|
|
|
if (!ShouldReplicateObject(object))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
RequestMessage request = MakeObjectMessage(object, "config::ObjectUpdate", DynamicObject::GetCurrentTx(), true);
|
|
|
|
|
|
|
|
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint, request);
|
|
|
|
}
|
2012-07-02 15:08:15 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
void CIBSyncComponent::RemoteObjectUpdateHandler(const Endpoint::Ptr& sender, const RequestMessage& request)
|
2012-07-02 15:08:15 +02:00
|
|
|
{
|
|
|
|
MessagePart params;
|
|
|
|
if (!request.GetParams(¶ms))
|
|
|
|
return;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String name;
|
2012-07-09 16:19:56 +02:00
|
|
|
if (!params.Get("name", &name))
|
2012-07-02 15:08:15 +02:00
|
|
|
return;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String type;
|
2012-07-09 16:19:56 +02:00
|
|
|
if (!params.Get("type", &type))
|
2012-07-02 15:08:15 +02:00
|
|
|
return;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Dictionary::Ptr update;
|
|
|
|
if (!params.Get("update", &update))
|
2012-07-02 15:08:15 +02:00
|
|
|
return;
|
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::Ptr object = DynamicObject::GetObject(type, name);
|
2012-07-02 15:08:15 +02:00
|
|
|
|
|
|
|
if (!object) {
|
2012-08-02 09:38:08 +02:00
|
|
|
object = DynamicObject::Create(type, update);
|
2012-07-02 15:08:15 +02:00
|
|
|
|
|
|
|
if (object->GetSource() == EndpointManager::GetInstance()->GetIdentity()) {
|
|
|
|
/* the peer sent us an object that was originally created by us -
|
2012-08-02 09:38:08 +02:00
|
|
|
* however it was deleted locally so we have to tell the peer to destroy
|
2012-07-02 15:08:15 +02:00
|
|
|
* its copy of the object. */
|
|
|
|
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint,
|
2012-08-02 09:38:08 +02:00
|
|
|
MakeObjectMessage(object, "config::ObjectRemoved", 0, false));
|
2012-07-02 15:08:15 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2012-08-02 09:38:08 +02:00
|
|
|
if (object->IsLocal())
|
|
|
|
throw_exception(invalid_argument("Replicated remote object is marked as local."));
|
2012-07-02 19:25:33 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
if (object->GetSource().IsEmpty())
|
|
|
|
object->SetSource(sender->GetIdentity());
|
2012-07-02 15:08:15 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
object->ApplyUpdate(update, true);
|
2012-07-02 15:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CIBSyncComponent::RemoteObjectRemovedHandler(const RequestMessage& request)
|
|
|
|
{
|
|
|
|
MessagePart params;
|
|
|
|
if (!request.GetParams(¶ms))
|
|
|
|
return;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String name;
|
2012-07-09 16:19:56 +02:00
|
|
|
if (!params.Get("name", &name))
|
2012-07-02 15:08:15 +02:00
|
|
|
return;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String type;
|
2012-07-09 16:19:56 +02:00
|
|
|
if (!params.Get("type", &type))
|
2012-07-02 15:08:15 +02:00
|
|
|
return;
|
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::Ptr object = DynamicObject::GetObject(type, name);
|
2012-07-02 15:08:15 +02:00
|
|
|
|
|
|
|
if (!object)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!object->IsLocal()) {
|
|
|
|
try {
|
|
|
|
m_SyncingConfig = true;
|
|
|
|
object->Unregister();
|
|
|
|
m_SyncingConfig = false;
|
2012-07-23 08:57:19 +02:00
|
|
|
} catch (...) {
|
2012-07-02 15:08:15 +02:00
|
|
|
m_SyncingConfig = false;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-02 12:34:54 +02:00
|
|
|
EXPORT_COMPONENT(cibsync, CIBSyncComponent);
|