icinga2/components/configrpc/configrpccomponent.cpp

192 lines
6.3 KiB
C++
Raw Normal View History

/******************************************************************************
* 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 *
2012-05-11 13:33:57 +02:00
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
2012-04-13 13:16:54 +02:00
#include "i2-configrpc.h"
2012-03-31 16:28:11 +02:00
using namespace icinga;
string ConfigRpcComponent::GetName(void) const
2012-03-31 16:28:11 +02:00
{
return "configcomponent";
}
void ConfigRpcComponent::Start(void)
{
2012-06-27 18:43:34 +02:00
EndpointManager::Ptr endpointManager = EndpointManager::GetInstance();
2012-03-31 16:28:11 +02:00
2012-06-15 19:32:41 +02:00
m_ConfigRpcEndpoint = boost::make_shared<VirtualEndpoint>();
2012-04-20 13:49:04 +02:00
long configSource;
2012-05-16 11:30:54 +02:00
if (GetConfig()->GetProperty("configSource", &configSource) && configSource != 0) {
m_ConfigRpcEndpoint->RegisterTopicHandler("config::FetchObjects",
2012-06-16 03:42:54 +02:00
boost::bind(&ConfigRpcComponent::FetchObjectsHandler, this, _2));
2012-03-31 16:28:11 +02:00
2012-06-16 03:42:54 +02:00
ConfigObject::GetAllObjects()->OnObjectAdded.connect(boost::bind(&ConfigRpcComponent::LocalObjectCommittedHandler, this, _2));
ConfigObject::GetAllObjects()->OnObjectCommitted.connect(boost::bind(&ConfigRpcComponent::LocalObjectCommittedHandler, this, _2));
ConfigObject::GetAllObjects()->OnObjectRemoved.connect(boost::bind(&ConfigRpcComponent::LocalObjectRemovedHandler, this, _2));
m_ConfigRpcEndpoint->RegisterPublication("config::ObjectCommitted");
m_ConfigRpcEndpoint->RegisterPublication("config::ObjectRemoved");
2012-03-31 16:28:11 +02:00
}
2012-06-16 03:42:54 +02:00
endpointManager->OnNewEndpoint.connect(boost::bind(&ConfigRpcComponent::NewEndpointHandler, this, _2));
2012-04-25 20:35:37 +02:00
m_ConfigRpcEndpoint->RegisterPublication("config::FetchObjects");
m_ConfigRpcEndpoint->RegisterTopicHandler("config::ObjectCommitted",
2012-06-16 03:42:54 +02:00
boost::bind(&ConfigRpcComponent::RemoteObjectCommittedHandler, this, _3));
m_ConfigRpcEndpoint->RegisterTopicHandler("config::ObjectRemoved",
2012-06-16 03:42:54 +02:00
boost::bind(&ConfigRpcComponent::RemoteObjectRemovedHandler, this, _3));
endpointManager->RegisterEndpoint(m_ConfigRpcEndpoint);
2012-03-31 16:28:11 +02:00
}
void ConfigRpcComponent::Stop(void)
{
2012-06-27 18:43:34 +02:00
EndpointManager::Ptr mgr = EndpointManager::GetInstance();
if (mgr)
mgr->UnregisterEndpoint(m_ConfigRpcEndpoint);
2012-03-31 16:28:11 +02:00
}
2012-06-16 03:42:54 +02:00
void ConfigRpcComponent::NewEndpointHandler(const Endpoint::Ptr& endpoint)
{
2012-06-21 17:39:16 +02:00
/* no need to sync the config with local endpoints */
if (endpoint->IsLocal())
return;
2012-06-16 03:42:54 +02:00
endpoint->OnSessionEstablished.connect(boost::bind(&ConfigRpcComponent::SessionEstablishedHandler, this, _1));
}
void ConfigRpcComponent::SessionEstablishedHandler(const Endpoint::Ptr& endpoint)
{
RequestMessage request;
request.SetMethod("config::FetchObjects");
2012-06-27 18:43:34 +02:00
EndpointManager::GetInstance()->SendUnicastMessage(m_ConfigRpcEndpoint, endpoint, request);
}
RequestMessage ConfigRpcComponent::MakeObjectMessage(const ConfigObject::Ptr& object, string method, bool includeProperties)
2012-03-31 16:28:11 +02:00
{
RequestMessage msg;
2012-04-18 15:22:25 +02:00
msg.SetMethod(method);
2012-03-31 16:28:11 +02:00
2012-05-16 11:30:54 +02:00
MessagePart params;
2012-04-18 15:22:25 +02:00
msg.SetParams(params);
2012-03-31 16:28:11 +02:00
2012-05-16 11:30:54 +02:00
params.SetProperty("name", object->GetName());
params.SetProperty("type", object->GetType());
2012-03-31 16:28:11 +02:00
2012-04-20 15:49:12 +02:00
if (includeProperties)
params.SetProperty("properties", object->GetProperties());
2012-03-31 16:28:11 +02:00
return msg;
}
bool ConfigRpcComponent::ShouldReplicateObject(const ConfigObject::Ptr& object)
{
return (!object->IsLocal());
}
2012-06-16 03:42:54 +02:00
void ConfigRpcComponent::FetchObjectsHandler(const Endpoint::Ptr& sender)
2012-03-31 16:28:11 +02:00
{
ConfigObject::Set::Ptr allObjects = ConfigObject::GetAllObjects();
2012-03-31 16:28:11 +02:00
for (ConfigObject::Set::Iterator ci = allObjects->Begin(); ci != allObjects->End(); ci++) {
ConfigObject::Ptr object = *ci;
if (!ShouldReplicateObject(object))
continue;
2012-06-29 11:59:53 +02:00
RequestMessage request = MakeObjectMessage(object, "config::ObjectCommitted", true);
2012-06-27 18:43:34 +02:00
EndpointManager::GetInstance()->SendUnicastMessage(m_ConfigRpcEndpoint, sender, request);
2012-03-31 16:28:11 +02:00
}
}
2012-06-16 03:42:54 +02:00
void ConfigRpcComponent::LocalObjectCommittedHandler(const ConfigObject::Ptr& object)
2012-03-31 16:28:11 +02:00
{
if (!ShouldReplicateObject(object))
2012-06-15 19:32:41 +02:00
return;
2012-06-27 18:43:34 +02:00
EndpointManager::GetInstance()->SendMulticastMessage(m_ConfigRpcEndpoint,
2012-06-29 11:59:53 +02:00
MakeObjectMessage(object, "config::ObjectCommitted", true));
2012-03-31 16:28:11 +02:00
}
2012-06-16 03:42:54 +02:00
void ConfigRpcComponent::LocalObjectRemovedHandler(const ConfigObject::Ptr& object)
2012-03-31 16:28:11 +02:00
{
if (!ShouldReplicateObject(object))
2012-06-15 19:32:41 +02:00
return;
2012-06-27 18:43:34 +02:00
EndpointManager::GetInstance()->SendMulticastMessage(m_ConfigRpcEndpoint,
MakeObjectMessage(object, "config::ObjectRemoved", false));
2012-03-31 16:28:11 +02:00
}
2012-06-16 03:42:54 +02:00
void ConfigRpcComponent::RemoteObjectCommittedHandler(const RequestMessage& request)
2012-03-31 16:28:11 +02:00
{
2012-05-16 11:30:54 +02:00
MessagePart params;
2012-06-16 03:42:54 +02:00
if (!request.GetParams(&params))
2012-06-15 19:32:41 +02:00
return;
2012-03-31 16:28:11 +02:00
2012-04-18 15:22:25 +02:00
string name;
2012-05-16 11:30:54 +02:00
if (!params.GetProperty("name", &name))
2012-06-15 19:32:41 +02:00
return;
2012-04-18 15:22:25 +02:00
string type;
2012-05-16 11:30:54 +02:00
if (!params.GetProperty("type", &type))
2012-06-15 19:32:41 +02:00
return;
2012-03-31 16:28:11 +02:00
2012-05-16 11:30:54 +02:00
MessagePart properties;
if (!params.GetProperty("properties", &properties))
2012-06-15 19:32:41 +02:00
return;
ConfigObject::Ptr object = ConfigObject::GetObject(type, name);
if (!object)
2012-06-15 19:32:41 +02:00
object = boost::make_shared<ConfigObject>(properties.GetDictionary());
else
object->SetProperties(properties.GetDictionary());
object->Commit();
2012-03-31 16:28:11 +02:00
}
2012-06-16 03:42:54 +02:00
void ConfigRpcComponent::RemoteObjectRemovedHandler(const RequestMessage& request)
2012-03-31 16:28:11 +02:00
{
2012-05-16 11:30:54 +02:00
MessagePart params;
2012-06-16 03:42:54 +02:00
if (!request.GetParams(&params))
2012-06-15 19:32:41 +02:00
return;
2012-04-18 15:22:25 +02:00
string name;
2012-05-16 11:30:54 +02:00
if (!params.GetProperty("name", &name))
2012-06-15 19:32:41 +02:00
return;
2012-03-31 16:28:11 +02:00
2012-04-18 15:22:25 +02:00
string type;
2012-05-16 11:30:54 +02:00
if (!params.GetProperty("type", &type))
2012-06-15 19:32:41 +02:00
return;
2012-03-31 16:28:11 +02:00
ConfigObject::Ptr object = ConfigObject::GetObject(type, name);
2012-03-31 16:28:11 +02:00
if (!object)
2012-06-15 19:32:41 +02:00
return;
2012-03-31 16:28:11 +02:00
if (!object->IsLocal())
object->Unregister();
2012-03-31 16:28:11 +02:00
}
2012-05-12 16:12:26 +02:00
EXPORT_COMPONENT(configrpc, ConfigRpcComponent);