2012-05-10 12:06:41 +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 *
|
2012-05-11 13:33:57 +02:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
2012-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2012-04-13 13:16:54 +02:00
|
|
|
#include "i2-configrpc.h"
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-04-19 11:29:36 +02:00
|
|
|
string ConfigRpcComponent::GetName(void) const
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
|
|
|
return "configcomponent";
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigRpcComponent::Start(void)
|
|
|
|
{
|
2012-04-24 07:16:34 +02:00
|
|
|
EndpointManager::Ptr endpointManager = GetEndpointManager();
|
|
|
|
ConfigHive::Ptr configHive = GetConfigHive();
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-06 08:56:52 +02:00
|
|
|
m_ConfigRpcEndpoint = make_shared<VirtualEndpoint>();
|
|
|
|
|
2012-04-20 13:49:04 +02:00
|
|
|
long configSource;
|
2012-04-01 19:32:41 +02:00
|
|
|
if (GetConfig()->GetPropertyInteger("configSource", &configSource) && configSource != 0) {
|
2012-04-24 15:56:48 +02:00
|
|
|
m_ConfigRpcEndpoint->RegisterMethodHandler("config::FetchObjects",
|
|
|
|
bind_weak(&ConfigRpcComponent::FetchObjectsHandler, shared_from_this()));
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-05-09 10:15:51 +02:00
|
|
|
configHive->OnObjectCommitted += bind_weak(&ConfigRpcComponent::LocalObjectCommittedHandler, shared_from_this());
|
2012-04-03 11:13:17 +02:00
|
|
|
configHive->OnObjectRemoved += bind_weak(&ConfigRpcComponent::LocalObjectRemovedHandler, shared_from_this());
|
2012-04-06 08:56:52 +02:00
|
|
|
|
2012-05-09 10:15:51 +02:00
|
|
|
m_ConfigRpcEndpoint->RegisterMethodSource("config::ObjectCommitted");
|
2012-04-06 08:56:52 +02:00
|
|
|
m_ConfigRpcEndpoint->RegisterMethodSource("config::ObjectRemoved");
|
2012-03-31 16:28:11 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 13:40:22 +02:00
|
|
|
endpointManager->OnNewEndpoint += bind_weak(&ConfigRpcComponent::NewEndpointHandler, shared_from_this());
|
2012-04-25 20:35:37 +02:00
|
|
|
|
2012-05-08 13:40:22 +02:00
|
|
|
m_ConfigRpcEndpoint->RegisterMethodSource("config::FetchObjects");
|
2012-05-09 10:15:51 +02:00
|
|
|
m_ConfigRpcEndpoint->RegisterMethodHandler("config::ObjectCommitted",
|
|
|
|
bind_weak(&ConfigRpcComponent::RemoteObjectCommittedHandler, shared_from_this()));
|
2012-04-24 15:56:48 +02:00
|
|
|
m_ConfigRpcEndpoint->RegisterMethodHandler("config::ObjectRemoved",
|
|
|
|
bind_weak(&ConfigRpcComponent::RemoteObjectRemovedHandler, shared_from_this()));
|
2012-04-06 08:56:52 +02:00
|
|
|
|
|
|
|
endpointManager->RegisterEndpoint(m_ConfigRpcEndpoint);
|
2012-03-31 16:28:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigRpcComponent::Stop(void)
|
|
|
|
{
|
2012-05-08 15:14:20 +02:00
|
|
|
EndpointManager::Ptr mgr = GetEndpointManager();
|
|
|
|
|
|
|
|
if (mgr)
|
|
|
|
mgr->UnregisterEndpoint(m_ConfigRpcEndpoint);
|
2012-03-31 16:28:11 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 13:40:22 +02:00
|
|
|
int ConfigRpcComponent::NewEndpointHandler(const NewEndpointEventArgs& ea)
|
|
|
|
{
|
|
|
|
ea.Endpoint->OnSessionEstablished += bind_weak(&ConfigRpcComponent::SessionEstablishedHandler, shared_from_this());
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ConfigRpcComponent::SessionEstablishedHandler(const EventArgs& ea)
|
2012-04-20 10:38:11 +02:00
|
|
|
{
|
2012-04-24 15:56:48 +02:00
|
|
|
JsonRpcRequest request;
|
|
|
|
request.SetMethod("config::FetchObjects");
|
|
|
|
|
2012-05-08 13:40:22 +02:00
|
|
|
Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(ea.Source);
|
|
|
|
GetEndpointManager()->SendUnicastRequest(m_ConfigRpcEndpoint, endpoint, request);
|
2012-04-20 10:38:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
JsonRpcRequest ConfigRpcComponent::MakeObjectMessage(const ConfigObject::Ptr& object, string method, bool includeProperties)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-18 15:22:25 +02:00
|
|
|
JsonRpcRequest msg;
|
|
|
|
msg.SetMethod(method);
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
Message params;
|
|
|
|
msg.SetParams(params);
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-20 13:49:04 +02:00
|
|
|
params.GetDictionary()->SetPropertyString("name", object->GetName());
|
|
|
|
params.GetDictionary()->SetPropertyString("type", object->GetType());
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-20 15:49:12 +02:00
|
|
|
if (includeProperties)
|
|
|
|
params.SetPropertyMessage("properties", Message(object));
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2012-04-20 14:20:25 +02:00
|
|
|
bool ConfigRpcComponent::ShouldReplicateObject(const ConfigObject::Ptr& object)
|
|
|
|
{
|
|
|
|
long replicate;
|
|
|
|
if (!object->GetPropertyInteger("replicate", &replicate))
|
2012-04-20 15:49:12 +02:00
|
|
|
return true;
|
2012-04-20 14:20:25 +02:00
|
|
|
return (replicate != 0);
|
|
|
|
}
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
int ConfigRpcComponent::FetchObjectsHandler(const NewRequestEventArgs& ea)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-18 15:22:25 +02:00
|
|
|
Endpoint::Ptr client = ea.Sender;
|
2012-04-24 07:16:34 +02:00
|
|
|
ConfigHive::Ptr configHive = GetConfigHive();
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-04 10:04:38 +02:00
|
|
|
for (ConfigHive::CollectionIterator ci = configHive->Collections.begin(); ci != configHive->Collections.end(); ci++) {
|
|
|
|
ConfigCollection::Ptr collection = ci->second;
|
|
|
|
|
|
|
|
for (ConfigCollection::ObjectIterator oi = collection->Objects.begin(); oi != collection->Objects.end(); oi++) {
|
2012-04-20 14:20:25 +02:00
|
|
|
ConfigObject::Ptr object = oi->second;
|
|
|
|
|
|
|
|
if (!ShouldReplicateObject(object))
|
|
|
|
continue;
|
|
|
|
|
2012-04-23 16:49:02 +02:00
|
|
|
JsonRpcRequest request = MakeObjectMessage(object, "config::ObjectCreated", true);
|
|
|
|
|
2012-04-24 07:16:34 +02:00
|
|
|
GetEndpointManager()->SendUnicastRequest(m_ConfigRpcEndpoint, client, request);
|
2012-03-31 16:28:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-09 10:15:51 +02:00
|
|
|
int ConfigRpcComponent::LocalObjectCommittedHandler(const EventArgs& ea)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-18 15:22:25 +02:00
|
|
|
ConfigObject::Ptr object = static_pointer_cast<ConfigObject>(ea.Source);
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2012-04-20 14:20:25 +02:00
|
|
|
if (!ShouldReplicateObject(object))
|
|
|
|
return 0;
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2012-04-24 07:16:34 +02:00
|
|
|
GetEndpointManager()->SendMulticastRequest(m_ConfigRpcEndpoint,
|
|
|
|
MakeObjectMessage(object, "config::ObjectCreated", true));
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-20 13:49:04 +02:00
|
|
|
int ConfigRpcComponent::LocalObjectRemovedHandler(const EventArgs& ea)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-18 15:22:25 +02:00
|
|
|
ConfigObject::Ptr object = static_pointer_cast<ConfigObject>(ea.Source);
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2012-04-20 14:20:25 +02:00
|
|
|
if (!ShouldReplicateObject(object))
|
|
|
|
return 0;
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2012-04-24 07:16:34 +02:00
|
|
|
GetEndpointManager()->SendMulticastRequest(m_ConfigRpcEndpoint,
|
|
|
|
MakeObjectMessage(object, "config::ObjectRemoved", false));
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-09 10:15:51 +02:00
|
|
|
int ConfigRpcComponent::RemoteObjectCommittedHandler(const NewRequestEventArgs& ea)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-18 15:22:25 +02:00
|
|
|
JsonRpcRequest message = ea.Request;
|
2012-04-04 14:30:11 +02:00
|
|
|
bool was_null = false;
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
Message params;
|
|
|
|
if (!message.GetParams(¶ms))
|
2012-03-31 16:28:11 +02:00
|
|
|
return 0;
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
string name;
|
2012-04-20 13:49:04 +02:00
|
|
|
if (!params.GetDictionary()->GetPropertyString("name", &name))
|
2012-04-18 15:22:25 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
string type;
|
2012-04-20 13:49:04 +02:00
|
|
|
if (!params.GetDictionary()->GetPropertyString("type", &type))
|
2012-03-31 16:28:11 +02:00
|
|
|
return 0;
|
|
|
|
|
2012-04-24 07:16:34 +02:00
|
|
|
ConfigHive::Ptr configHive = GetConfigHive();
|
2012-04-02 20:50:35 +02:00
|
|
|
ConfigObject::Ptr object = configHive->GetObject(type, name);
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-04 14:30:11 +02:00
|
|
|
if (!object) {
|
|
|
|
was_null = true;
|
|
|
|
object = make_shared<ConfigObject>(type, name);
|
|
|
|
}
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
Dictionary::Ptr properties;
|
2012-04-20 13:49:04 +02:00
|
|
|
if (!params.GetDictionary()->GetPropertyDictionary("properties", &properties))
|
2012-04-18 15:22:25 +02:00
|
|
|
return 0;
|
2012-04-04 14:30:11 +02:00
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
for (DictionaryIterator i = properties->Begin(); i != properties->End(); i++) {
|
2012-04-20 13:49:04 +02:00
|
|
|
object->SetPropertyString(i->first, i->second);
|
2012-04-04 14:30:11 +02:00
|
|
|
}
|
|
|
|
|
2012-04-20 14:20:25 +02:00
|
|
|
if (was_null) {
|
|
|
|
object->SetReplicated(true);
|
2012-04-04 14:30:11 +02:00
|
|
|
configHive->AddObject(object);
|
2012-04-20 14:20:25 +02:00
|
|
|
}
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
int ConfigRpcComponent::RemoteObjectRemovedHandler(const NewRequestEventArgs& ea)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-18 15:22:25 +02:00
|
|
|
JsonRpcRequest message = ea.Request;
|
2012-04-04 14:30:11 +02:00
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
Message params;
|
|
|
|
if (!message.GetParams(¶ms))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
string name;
|
2012-04-20 13:49:04 +02:00
|
|
|
if (!params.GetDictionary()->GetPropertyString("name", &name))
|
2012-03-31 16:28:11 +02:00
|
|
|
return 0;
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
string type;
|
2012-04-20 13:49:04 +02:00
|
|
|
if (!params.GetDictionary()->GetPropertyString("type", &type))
|
2012-03-31 16:28:11 +02:00
|
|
|
return 0;
|
|
|
|
|
2012-04-24 07:16:34 +02:00
|
|
|
ConfigHive::Ptr configHive = GetConfigHive();
|
2012-04-02 20:50:35 +02:00
|
|
|
ConfigObject::Ptr object = configHive->GetObject(type, name);
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-19 12:16:52 +02:00
|
|
|
if (!object)
|
2012-03-31 16:28:11 +02:00
|
|
|
return 0;
|
|
|
|
|
2012-04-20 14:20:25 +02:00
|
|
|
if (object->GetReplicated())
|
|
|
|
configHive->RemoveObject(object);
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_COMPONENT(ConfigRpcComponent);
|