icinga2/components/configrpc/configrpccomponent.cpp

213 lines
6.3 KiB
C++
Raw Normal View History

2012-04-13 13:16:54 +02:00
#include "i2-configrpc.h"
2012-03-31 16:28:11 +02:00
using namespace icinga;
IcingaApplication::Ptr ConfigRpcComponent::GetIcingaApplication(void)
2012-03-31 16:28:11 +02:00
{
2012-04-02 16:25:43 +02:00
return static_pointer_cast<IcingaApplication>(GetApplication());
2012-03-31 16:28:11 +02:00
}
string ConfigRpcComponent::GetName(void)
{
return "configcomponent";
}
void ConfigRpcComponent::Start(void)
{
IcingaApplication::Ptr icingaApp = GetIcingaApplication();
2012-03-31 16:28:11 +02:00
EndpointManager::Ptr endpointManager = icingaApp->GetEndpointManager();
ConfigHive::Ptr configHive = icingaApp->GetConfigHive();
2012-03-31 16:28:11 +02:00
m_ConfigRpcEndpoint = make_shared<VirtualEndpoint>();
2012-04-01 19:32:41 +02:00
int configSource;
if (GetConfig()->GetPropertyInteger("configSource", &configSource) && configSource != 0) {
m_ConfigRpcEndpoint->RegisterMethodHandler("config::FetchObjects", bind_weak(&ConfigRpcComponent::FetchObjectsHandler, shared_from_this()));
2012-03-31 16:28:11 +02:00
2012-04-03 11:13:17 +02:00
configHive->OnObjectCreated += bind_weak(&ConfigRpcComponent::LocalObjectCreatedHandler, shared_from_this());
configHive->OnObjectRemoved += bind_weak(&ConfigRpcComponent::LocalObjectRemovedHandler, shared_from_this());
configHive->OnPropertyChanged += bind_weak(&ConfigRpcComponent::LocalPropertyChangedHandler, shared_from_this());
m_ConfigRpcEndpoint->RegisterMethodSource("config::ObjectCreated");
m_ConfigRpcEndpoint->RegisterMethodSource("config::ObjectRemoved");
m_ConfigRpcEndpoint->RegisterMethodSource("config::PropertyChanged");
2012-03-31 16:28:11 +02:00
}
m_ConfigRpcEndpoint->RegisterMethodHandler("config::ObjectCreated", bind_weak(&ConfigRpcComponent::RemoteObjectUpdatedHandler, shared_from_this()));
m_ConfigRpcEndpoint->RegisterMethodHandler("config::ObjectRemoved", bind_weak(&ConfigRpcComponent::RemoteObjectRemovedHandler, shared_from_this()));
m_ConfigRpcEndpoint->RegisterMethodHandler("config::PropertyChanged", bind_weak(&ConfigRpcComponent::RemoteObjectUpdatedHandler, shared_from_this()));
endpointManager->RegisterEndpoint(m_ConfigRpcEndpoint);
2012-03-31 16:28:11 +02:00
}
void ConfigRpcComponent::Stop(void)
{
// TODO: implement
}
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.SetVersion("2.0");
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-18 15:22:25 +02:00
params.GetDictionary()->SetValueString("name", object->GetName());
params.GetDictionary()->SetValueString("type", object->GetType());
2012-03-31 16:28:11 +02:00
if (includeProperties) {
2012-04-18 15:22:25 +02:00
Message properties;
params.GetDictionary()->SetValueDictionary("properties", properties.GetDictionary());
2012-03-31 16:28:11 +02:00
for (ConfigObject::ParameterIterator pi = object->Properties.begin(); pi != object->Properties.end(); pi++) {
2012-04-18 15:22:25 +02:00
properties.GetDictionary()->SetValueString(pi->first, pi->second);
2012-03-31 16:28:11 +02:00
}
}
return msg;
}
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;
ConfigHive::Ptr configHive = GetIcingaApplication()->GetConfigHive();
2012-03-31 16:28:11 +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-18 15:22:25 +02:00
client->ProcessRequest(m_ConfigRpcEndpoint, MakeObjectMessage(oi->second, "config::ObjectCreated", true));
2012-03-31 16:28:11 +02:00
}
}
return 0;
}
2012-04-18 15:22:25 +02:00
int ConfigRpcComponent::LocalObjectCreatedHandler(const ConfigObjectEventArgs& 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);
int replicate = 0;
object->GetPropertyInteger("replicate", &replicate);
if (replicate) {
EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager();
2012-04-18 15:22:25 +02:00
mgr->SendMulticastRequest(m_ConfigRpcEndpoint, MakeObjectMessage(object, "config::ObjectCreated", true));
}
2012-03-31 16:28:11 +02:00
return 0;
}
2012-04-18 15:22:25 +02:00
int ConfigRpcComponent::LocalObjectRemovedHandler(const ConfigObjectEventArgs& 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);
int replicate = 0;
object->GetPropertyInteger("replicate", &replicate);
if (replicate) {
EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager();
2012-04-18 15:22:25 +02:00
mgr->SendMulticastRequest(m_ConfigRpcEndpoint, MakeObjectMessage(object, "config::ObjectRemoved", false));
}
2012-03-31 16:28:11 +02:00
return 0;
}
2012-04-18 15:22:25 +02:00
int ConfigRpcComponent::LocalPropertyChangedHandler(const ConfigObjectEventArgs& 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);
int replicate = 0;
object->GetPropertyInteger("replicate", &replicate);
if (replicate) {
2012-04-18 15:22:25 +02:00
JsonRpcRequest msg = MakeObjectMessage(object, "config::PropertyChanged", false);
Message params;
msg.SetParams(params);
2012-04-18 15:22:25 +02:00
Message properties;
params.GetDictionary()->SetValueDictionary("properties", properties.GetDictionary());
string value;
2012-04-18 15:22:25 +02:00
object->GetProperty(ea.Property, &value);
2012-04-18 15:22:25 +02:00
properties.GetDictionary()->SetValueString(ea.Property, value);
EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager();
2012-04-18 15:22:25 +02:00
mgr->SendMulticastRequest(m_ConfigRpcEndpoint, msg);
}
2012-03-31 16:28:11 +02:00
return 0;
}
2012-04-18 15:22:25 +02:00
int ConfigRpcComponent::RemoteObjectUpdatedHandler(const NewRequestEventArgs& ea)
2012-03-31 16:28:11 +02:00
{
2012-04-18 15:22:25 +02:00
JsonRpcRequest message = ea.Request;
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(&params))
2012-03-31 16:28:11 +02:00
return 0;
2012-04-18 15:22:25 +02:00
string name;
if (!params.GetDictionary()->GetValueString("name", &name))
return 0;
string type;
if (!params.GetDictionary()->GetValueString("type", &type))
2012-03-31 16:28:11 +02:00
return 0;
ConfigHive::Ptr configHive = GetIcingaApplication()->GetConfigHive();
ConfigObject::Ptr object = configHive->GetObject(type, name);
2012-03-31 16:28: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;
if (!params.GetDictionary()->GetValueDictionary("properties", &properties))
return 0;
2012-04-18 15:22:25 +02:00
for (DictionaryIterator i = properties->Begin(); i != properties->End(); i++) {
object->SetProperty(i->first, i->second);
}
if (was_null)
configHive->AddObject(object);
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-18 15:22:25 +02:00
Message params;
if (!message.GetParams(&params))
return 0;
string name;
if (!params.GetDictionary()->GetValueString("name", &name))
2012-03-31 16:28:11 +02:00
return 0;
2012-04-18 15:22:25 +02:00
string type;
if (!params.GetDictionary()->GetValueString("type", &type))
2012-03-31 16:28:11 +02:00
return 0;
ConfigHive::Ptr configHive = GetIcingaApplication()->GetConfigHive();
ConfigObject::Ptr object = configHive->GetObject(type, name);
2012-03-31 16:28:11 +02:00
if (object.get() == NULL)
return 0;
configHive->RemoveObject(object);
2012-03-31 16:28:11 +02:00
return 0;
}
EXPORT_COMPONENT(ConfigRpcComponent);