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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "replication/replicationcomponent.h"
|
|
|
|
#include "icinga/service.h"
|
|
|
|
#include "icinga/checkresultmessage.h"
|
|
|
|
#include "remoting/endpointmanager.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/dynamictype.h"
|
|
|
|
#include "base/objectlock.h"
|
|
|
|
#include "base/logger_fwd.h"
|
|
|
|
#include <boost/foreach.hpp>
|
2012-07-02 12:34:54 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-03-12 13:45:54 +01:00
|
|
|
REGISTER_TYPE(ReplicationComponent);
|
|
|
|
|
|
|
|
ReplicationComponent::ReplicationComponent(const Dictionary::Ptr& serializedUpdate)
|
|
|
|
: DynamicObject(serializedUpdate)
|
|
|
|
{ }
|
2013-02-08 07:11:14 +01:00
|
|
|
|
2012-07-02 12:34:54 +02:00
|
|
|
/**
|
|
|
|
* 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));
|
2013-02-27 12:44:51 +01:00
|
|
|
DynamicObject::OnTransactionClosing.connect(boost::bind(&ReplicationComponent::TransactionClosingHandler, this, _1, _2));
|
|
|
|
DynamicObject::OnFlushObject.connect(boost::bind(&ReplicationComponent::FlushObjectHandler, this, _1, _2));
|
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));
|
2013-02-08 23:40:28 +01: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 */
|
2013-01-31 16:49:40 +01:00
|
|
|
m_Endpoint->RegisterTopicHandler("checker::CheckResult",
|
|
|
|
boost::bind(&ReplicationComponent::CheckResultRequestHandler, _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-31 16:49:40 +01:00
|
|
|
void ReplicationComponent::CheckResultRequestHandler(const RequestMessage& request)
|
2012-07-02 12:34:54 +02:00
|
|
|
{
|
2013-01-31 16:49:40 +01:00
|
|
|
CheckResultMessage params;
|
2012-07-02 12:34:54 +02:00
|
|
|
if (!request.GetParams(¶ms))
|
|
|
|
return;
|
|
|
|
|
2013-01-31 16:49:40 +01:00
|
|
|
String svcname = params.GetService();
|
2012-07-27 16:05:02 +02:00
|
|
|
Service::Ptr service = Service::GetByName(svcname);
|
2012-07-02 12:34:54 +02:00
|
|
|
|
2013-01-31 16:49:40 +01:00
|
|
|
Dictionary::Ptr cr = params.GetCheckResult();
|
|
|
|
if (!cr)
|
|
|
|
return;
|
2012-07-18 11:52:22 +02:00
|
|
|
|
2013-02-26 10:13:54 +01:00
|
|
|
if (cr->Contains("current_checker") && cr->Get("current_checker") == EndpointManager::GetInstance()->GetIdentity())
|
2013-02-11 23:37:39 +01:00
|
|
|
return;
|
2013-01-28 09:01:47 +01:00
|
|
|
|
2013-02-11 23:37:39 +01:00
|
|
|
Service::UpdateStatistics(cr);
|
2012-07-02 12:34:54 +02:00
|
|
|
}
|
|
|
|
|
2012-09-14 10:52:57 +02:00
|
|
|
void ReplicationComponent::EndpointConnectedHandler(const Endpoint::Ptr& endpoint)
|
2012-07-02 15:08:15 +02:00
|
|
|
{
|
2013-04-01 17:21:57 +02:00
|
|
|
/* no need to sync the config with local endpoints */
|
|
|
|
if (endpoint->IsLocalEndpoint())
|
|
|
|
return;
|
2012-07-02 15:08:15 +02:00
|
|
|
|
2013-04-01 17:21:57 +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;
|
2013-02-18 23:44:24 +01:00
|
|
|
BOOST_FOREACH(const DynamicType::Ptr& dt, DynamicType::GetTypes()) {
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<DynamicObject::Ptr> objects;
|
2013-02-18 23:44:24 +01:00
|
|
|
|
2013-04-01 17:21:57 +02:00
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, dt->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);
|
2013-03-15 11:19:52 +01:00
|
|
|
EndpointManager::GetInstance()->SendUnicastMessage(m_Endpoint, endpoint, request);
|
2013-02-18 23:44:24 +01:00
|
|
|
|
2012-09-03 10:28:14 +02:00
|
|
|
}
|
|
|
|
}
|
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());
|
2013-04-05 12:09:26 +02:00
|
|
|
params.Set("type", object->GetType()->GetName());
|
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));
|
|
|
|
}
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
void ReplicationComponent::TransactionClosingHandler(double tx, const std::set<DynamicObject::WeakPtr>& modifiedObjects)
|
2012-08-02 09:38:08 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
if (modifiedObjects.empty())
|
|
|
|
return;
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream msgbuf;
|
2012-08-02 09:38:08 +02:00
|
|
|
msgbuf << "Sending " << modifiedObjects.size() << " replication updates.";
|
2013-04-05 12:09:26 +02:00
|
|
|
Log(LogInformation, "replication", msgbuf.str());
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-02-19 12:17:31 +01:00
|
|
|
BOOST_FOREACH(const DynamicObject::WeakPtr& wobject, modifiedObjects) {
|
|
|
|
DynamicObject::Ptr object = wobject.lock();
|
|
|
|
|
|
|
|
if (!object)
|
|
|
|
continue;
|
2013-02-08 23:40:28 +01:00
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
FlushObjectHandler(tx, object);
|
2013-02-19 23:02:08 +01:00
|
|
|
}
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
void ReplicationComponent::FlushObjectHandler(double tx, const DynamicObject::Ptr& object)
|
2013-02-19 23:02:08 +01:00
|
|
|
{
|
|
|
|
if (!ShouldReplicateObject(object))
|
|
|
|
return;
|
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
RequestMessage request = MakeObjectMessage(object, "config::ObjectUpdate", tx, true);
|
2013-03-15 11:19:52 +01:00
|
|
|
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) {
|
2013-03-04 15:52:42 +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()) {
|
2013-02-08 23:40:28 +01: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
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(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())
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::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
|
|
|
}
|
|
|
|
}
|