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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-09-14 10:52:57 +02:00
|
|
|
#include "i2-replication.h"
|
2012-07-02 12:34:54 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the component.
|
|
|
|
*/
|
2012-09-14 10:52:57 +02:00
|
|
|
void ReplicationComponent::Start(void)
|
2012-07-02 12:34:54 +02:00
|
|
|
{
|
2013-01-18 09:36:28 +01:00
|
|
|
m_Endpoint = Endpoint::MakeEndpoint("replication", false);
|
2012-07-02 15:08:15 +02:00
|
|
|
|
2012-09-14 10:52:57 +02:00
|
|
|
DynamicObject::OnRegistered.connect(boost::bind(&ReplicationComponent::LocalObjectRegisteredHandler, this, _1));
|
|
|
|
DynamicObject::OnUnregistered.connect(boost::bind(&ReplicationComponent::LocalObjectUnregisteredHandler, this, _1));
|
|
|
|
DynamicObject::OnTransactionClosing.connect(boost::bind(&ReplicationComponent::TransactionClosingHandler, this, _1));
|
2012-07-02 15:08:15 +02:00
|
|
|
|
2012-09-14 10:52:57 +02:00
|
|
|
Endpoint::OnConnected.connect(boost::bind(&ReplicationComponent::EndpointConnectedHandler, this, _1));
|
2012-09-03 10:28:14 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
m_Endpoint->RegisterTopicHandler("config::ObjectUpdate",
|
2013-01-22 10:14:52 +01:00
|
|
|
boost::bind(&ReplicationComponent::RemoteObjectUpdateHandler, this, _3));
|
2012-07-02 15:08:15 +02:00
|
|
|
m_Endpoint->RegisterTopicHandler("config::ObjectRemoved",
|
2012-09-14 10:52:57 +02:00
|
|
|
boost::bind(&ReplicationComponent::RemoteObjectRemovedHandler, this, _3));
|
2012-07-02 15:08:15 +02:00
|
|
|
|
|
|
|
/* service status */
|
2012-08-03 14:07:25 +02:00
|
|
|
m_Endpoint->RegisterTopicHandler("checker::ServiceStateChange",
|
2013-01-22 10:14:52 +01:00
|
|
|
boost::bind(&ReplicationComponent::ServiceStateChangeRequestHandler, _3));
|
2012-07-02 12:34:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the component.
|
|
|
|
*/
|
2012-09-14 10:52:57 +02:00
|
|
|
void ReplicationComponent::Stop(void)
|
2012-07-02 12:34:54 +02:00
|
|
|
{
|
2012-09-03 10:28:14 +02:00
|
|
|
m_Endpoint->Unregister();
|
2012-07-02 12:34:54 +02:00
|
|
|
}
|
|
|
|
|
2013-01-22 10:14:52 +01:00
|
|
|
void ReplicationComponent::ServiceStateChangeRequestHandler(const RequestMessage& request)
|
2012-07-02 12:34:54 +02:00
|
|
|
{
|
2012-08-03 14:07:25 +02:00
|
|
|
ServiceStateChangeMessage 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-08-03 18:17:47 +02:00
|
|
|
time_t now = static_cast<time_t>(Utility::GetTime());
|
2012-07-02 12:34:54 +02:00
|
|
|
CIB::UpdateTaskStatistics(now, 1);
|
|
|
|
}
|
|
|
|
|
2012-09-14 10:52:57 +02:00
|
|
|
void ReplicationComponent::EndpointConnectedHandler(const Endpoint::Ptr& endpoint)
|
2012-07-02 15:08:15 +02:00
|
|
|
{
|
|
|
|
/* no need to sync the config with local endpoints */
|
2012-09-03 10:28:14 +02:00
|
|
|
if (endpoint->IsLocalEndpoint())
|
2012-07-02 15:08:15 +02:00
|
|
|
return;
|
|
|
|
|
2012-09-03 10:28:14 +02:00
|
|
|
/* we just assume the other endpoint wants object updates */
|
|
|
|
endpoint->RegisterSubscription("config::ObjectUpdate");
|
|
|
|
endpoint->RegisterSubscription("config::ObjectRemoved");
|
2012-07-02 15:08:15 +02:00
|
|
|
|
2012-12-04 08:42:24 +01:00
|
|
|
DynamicType::Ptr type;
|
|
|
|
BOOST_FOREACH(tie(tuples::ignore, type), DynamicType::GetTypes()) {
|
2012-09-03 10:28:14 +02:00
|
|
|
DynamicObject::Ptr object;
|
2012-12-04 08:42:24 +01:00
|
|
|
BOOST_FOREACH(tie(tuples::ignore, object), type->GetObjects()) {
|
2012-09-03 10:28:14 +02:00
|
|
|
if (!ShouldReplicateObject(object))
|
|
|
|
continue;
|
2012-07-02 15:08:15 +02:00
|
|
|
|
2012-09-03 10:28:14 +02:00
|
|
|
RequestMessage request = MakeObjectMessage(object, "config::ObjectUpdate", 0, true);
|
|
|
|
EndpointManager::GetInstance()->SendUnicastMessage(m_Endpoint, endpoint, request);
|
|
|
|
}
|
|
|
|
}
|
2012-07-02 15:08:15 +02:00
|
|
|
}
|
|
|
|
|
2012-09-14 10:52:57 +02:00
|
|
|
RequestMessage ReplicationComponent::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
|
|
|
|
2012-10-12 09:58:34 +02:00
|
|
|
String source = object->GetSource();
|
|
|
|
|
|
|
|
if (source.IsEmpty())
|
|
|
|
source = EndpointManager::GetInstance()->GetIdentity();
|
|
|
|
|
|
|
|
params.Set("source", source);
|
|
|
|
|
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-09-14 10:52:57 +02:00
|
|
|
bool ReplicationComponent::ShouldReplicateObject(const DynamicObject::Ptr& object)
|
2012-07-02 15:08:15 +02:00
|
|
|
{
|
|
|
|
return (!object->IsLocal());
|
|
|
|
}
|
|
|
|
|
2012-09-14 10:52:57 +02:00
|
|
|
void ReplicationComponent::LocalObjectRegisteredHandler(const DynamicObject::Ptr& object)
|
2012-07-02 15:08:15 +02:00
|
|
|
{
|
|
|
|
if (!ShouldReplicateObject(object))
|
|
|
|
return;
|
|
|
|
|
|
|
|
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint,
|
2012-08-02 12:12:59 +02:00
|
|
|
MakeObjectMessage(object, "config::ObjectUpdate", 0, true));
|
2012-07-02 15:08:15 +02:00
|
|
|
}
|
|
|
|
|
2012-09-14 10:52:57 +02:00
|
|
|
void ReplicationComponent::LocalObjectUnregisteredHandler(const DynamicObject::Ptr& object)
|
2012-07-02 15:08:15 +02:00
|
|
|
{
|
|
|
|
if (!ShouldReplicateObject(object))
|
|
|
|
return;
|
|
|
|
|
|
|
|
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint,
|
2012-08-02 09:38:08 +02:00
|
|
|
MakeObjectMessage(object, "config::ObjectRemoved", 0, false));
|
|
|
|
}
|
|
|
|
|
2012-09-14 10:52:57 +02:00
|
|
|
void ReplicationComponent::TransactionClosingHandler(const set<DynamicObject::Ptr>& modifiedObjects)
|
2012-08-02 09:38:08 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
if (modifiedObjects.empty())
|
|
|
|
return;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
stringstream msgbuf;
|
|
|
|
msgbuf << "Sending " << modifiedObjects.size() << " replication updates.";
|
2012-09-14 10:52:57 +02:00
|
|
|
Logger::Write(LogDebug, "replication", msgbuf.str());
|
2012-08-02 09:38:08 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-01-22 10:14:52 +01:00
|
|
|
void ReplicationComponent::RemoteObjectUpdateHandler(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-10-12 09:58:34 +02:00
|
|
|
String source;
|
|
|
|
if (!params.Get("source", &source))
|
|
|
|
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-12-04 08:42:24 +01:00
|
|
|
DynamicType::Ptr dtype = DynamicType::GetByName(type);
|
|
|
|
DynamicObject::Ptr object = dtype->GetObject(name);
|
2012-07-02 15:08:15 +02:00
|
|
|
|
2012-08-03 15:35:27 +02:00
|
|
|
// TODO: sanitize update, disallow __local
|
|
|
|
|
2012-07-02 15:08:15 +02:00
|
|
|
if (!object) {
|
2012-12-04 08:42:24 +01:00
|
|
|
object = dtype->CreateObject(update);
|
2012-07-02 15:08:15 +02:00
|
|
|
|
2012-10-12 09:58:34 +02:00
|
|
|
if (source == EndpointManager::GetInstance()->GetIdentity()) {
|
2012-07-02 15:08:15 +02:00
|
|
|
/* 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;
|
|
|
|
}
|
2012-08-02 12:21:44 +02:00
|
|
|
|
2012-10-15 18:25:56 +02:00
|
|
|
Logger::Write(LogDebug, "replication", "Received object from source: " + source);
|
2012-08-07 12:47:18 +02:00
|
|
|
|
2012-10-12 09:58:34 +02:00
|
|
|
object->SetSource(source);
|
2012-08-02 12:21:44 +02:00
|
|
|
object->Register();
|
2012-07-02 15:08:15 +02:00
|
|
|
} 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-03 23:03:58 +02:00
|
|
|
// TODO: disallow config updates depending on endpoint config
|
|
|
|
|
|
|
|
object->ApplyUpdate(update, Attribute_All);
|
2012-07-02 15:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-14 10:52:57 +02:00
|
|
|
void ReplicationComponent::RemoteObjectRemovedHandler(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-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()) {
|
2012-08-04 10:23:18 +02:00
|
|
|
object->Unregister();
|
2012-07-02 15:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-14 10:52:57 +02:00
|
|
|
EXPORT_COMPONENT(replication, ReplicationComponent);
|