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-02 20:50:35 +02:00
|
|
|
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)
|
|
|
|
{
|
2012-04-02 20:50:35 +02:00
|
|
|
IcingaApplication::Ptr icingaApp = GetIcingaApplication();
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-06 08:56:52 +02:00
|
|
|
EndpointManager::Ptr endpointManager = icingaApp->GetEndpointManager();
|
2012-04-02 20:50:35 +02:00
|
|
|
ConfigHive::Ptr configHive = icingaApp->GetConfigHive();
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-06 08:56:52 +02:00
|
|
|
m_ConfigRpcEndpoint = make_shared<VirtualEndpoint>();
|
|
|
|
|
2012-04-01 19:32:41 +02:00
|
|
|
int configSource;
|
|
|
|
if (GetConfig()->GetPropertyInteger("configSource", &configSource) && configSource != 0) {
|
2012-04-06 08:56:52 +02:00
|
|
|
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());
|
2012-04-06 08:56:52 +02:00
|
|
|
|
|
|
|
m_ConfigRpcEndpoint->RegisterMethodSource("config::ObjectCreated");
|
|
|
|
m_ConfigRpcEndpoint->RegisterMethodSource("config::ObjectRemoved");
|
|
|
|
m_ConfigRpcEndpoint->RegisterMethodSource("config::PropertyChanged");
|
2012-03-31 16:28:11 +02:00
|
|
|
}
|
|
|
|
|
2012-04-06 08:56:52 +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-02 20:50:35 +02:00
|
|
|
JsonRpcMessage::Ptr ConfigRpcComponent::MakeObjectMessage(const ConfigObject::Ptr& object, string method, bool includeProperties)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-03 15:16:11 +02:00
|
|
|
JsonRpcMessage::Ptr msg = make_shared<JsonRpcMessage>();
|
2012-03-31 16:28:11 +02:00
|
|
|
msg->SetVersion("2.0");
|
|
|
|
msg->SetMethod(method);
|
|
|
|
cJSON *params = msg->GetParams();
|
|
|
|
|
|
|
|
string name = object->GetName();
|
|
|
|
cJSON_AddStringToObject(params, "name", name.c_str());
|
|
|
|
|
|
|
|
string type = object->GetType();
|
|
|
|
cJSON_AddStringToObject(params, "type", type.c_str());
|
|
|
|
|
|
|
|
if (includeProperties) {
|
2012-04-04 14:30:11 +02:00
|
|
|
cJSON *properties = cJSON_CreateObject();
|
|
|
|
cJSON_AddItemToObject(params, "properties", properties);
|
|
|
|
|
2012-03-31 16:28:11 +02:00
|
|
|
for (ConfigObject::ParameterIterator pi = object->Properties.begin(); pi != object->Properties.end(); pi++) {
|
2012-04-04 14:30:11 +02:00
|
|
|
cJSON_AddStringToObject(properties, pi->first.c_str(), pi->second.c_str());
|
2012-03-31 16:28:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
int ConfigRpcComponent::FetchObjectsHandler(NewMessageEventArgs::Ptr ea)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-02 20:50:35 +02:00
|
|
|
JsonRpcClient::Ptr client = static_pointer_cast<JsonRpcClient>(ea->Source);
|
|
|
|
ConfigHive::Ptr configHive = GetIcingaApplication()->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-02 20:50:35 +02:00
|
|
|
JsonRpcMessage::Ptr msg = MakeObjectMessage(oi->second, "config::ObjectCreated", true);
|
2012-03-31 16:28:11 +02:00
|
|
|
client->SendMessage(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-04 10:04:38 +02:00
|
|
|
int ConfigRpcComponent::LocalObjectCreatedHandler(ConfigObjectEventArgs::Ptr ea)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-04 10:04:38 +02:00
|
|
|
ConfigObject::Ptr object = static_pointer_cast<ConfigObject>(ea->Source);
|
2012-04-04 12:22:46 +02:00
|
|
|
|
|
|
|
int replicate = 0;
|
|
|
|
object->GetPropertyInteger("replicate", &replicate);
|
|
|
|
|
|
|
|
if (replicate) {
|
2012-04-06 08:56:52 +02:00
|
|
|
EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager();
|
2012-04-13 11:58:05 +02:00
|
|
|
mgr->SendMessage(m_ConfigRpcEndpoint, Endpoint::Ptr(), MakeObjectMessage(object, "config::ObjectCreated", true));
|
2012-04-04 12:22:46 +02:00
|
|
|
}
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-04 10:04:38 +02:00
|
|
|
int ConfigRpcComponent::LocalObjectRemovedHandler(ConfigObjectEventArgs::Ptr ea)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-04 10:04:38 +02:00
|
|
|
ConfigObject::Ptr object = static_pointer_cast<ConfigObject>(ea->Source);
|
2012-04-04 12:22:46 +02:00
|
|
|
|
|
|
|
int replicate = 0;
|
|
|
|
object->GetPropertyInteger("replicate", &replicate);
|
|
|
|
|
|
|
|
if (replicate) {
|
2012-04-06 08:56:52 +02:00
|
|
|
EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager();
|
2012-04-13 11:58:05 +02:00
|
|
|
mgr->SendMessage(m_ConfigRpcEndpoint, Endpoint::Ptr(), MakeObjectMessage(object, "config::ObjectRemoved", false));
|
2012-04-04 12:22:46 +02:00
|
|
|
}
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-04 10:04:38 +02:00
|
|
|
int ConfigRpcComponent::LocalPropertyChangedHandler(ConfigObjectEventArgs::Ptr ea)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-04 10:04:38 +02:00
|
|
|
ConfigObject::Ptr object = static_pointer_cast<ConfigObject>(ea->Source);
|
2012-04-04 12:22:46 +02:00
|
|
|
|
|
|
|
int replicate = 0;
|
|
|
|
object->GetPropertyInteger("replicate", &replicate);
|
|
|
|
|
|
|
|
if (replicate) {
|
2012-04-04 14:30:11 +02:00
|
|
|
JsonRpcMessage::Ptr msg = MakeObjectMessage(object, "config::PropertyChanged", false);
|
2012-04-04 12:22:46 +02:00
|
|
|
cJSON *params = msg->GetParams();
|
2012-04-04 14:30:11 +02:00
|
|
|
|
|
|
|
cJSON *properties = cJSON_CreateObject();
|
|
|
|
cJSON_AddItemToObject(params, "properties", properties);
|
|
|
|
|
2012-04-04 12:22:46 +02:00
|
|
|
string value;
|
|
|
|
object->GetProperty(ea->Property, &value);
|
2012-04-04 14:30:11 +02:00
|
|
|
|
|
|
|
cJSON_AddStringToObject(properties, ea->Property.c_str(), value.c_str());
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2012-04-06 08:56:52 +02:00
|
|
|
EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager();
|
2012-04-13 11:58:05 +02:00
|
|
|
mgr->SendMessage(m_ConfigRpcEndpoint, Endpoint::Ptr(), msg);
|
2012-04-04 12:22:46 +02:00
|
|
|
}
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-04 14:30:11 +02:00
|
|
|
int ConfigRpcComponent::RemoteObjectUpdatedHandler(NewMessageEventArgs::Ptr ea)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-02 20:50:35 +02:00
|
|
|
JsonRpcMessage::Ptr message = ea->Message;
|
2012-04-04 14:30:11 +02:00
|
|
|
string name, type, value;
|
|
|
|
bool was_null = false;
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
if (!message->GetParamString("name", &name))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!message->GetParamString("type", &type))
|
|
|
|
return 0;
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
ConfigHive::Ptr configHive = GetIcingaApplication()->GetConfigHive();
|
|
|
|
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-04 14:30:11 +02:00
|
|
|
cJSON *properties = message->GetParam("properties");
|
|
|
|
|
|
|
|
if (properties != NULL) {
|
|
|
|
for (cJSON *prop = properties->child; prop != NULL; prop = prop->next) {
|
|
|
|
if (prop->type != cJSON_String)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
object->SetProperty(prop->string, prop->valuestring);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (was_null)
|
|
|
|
configHive->AddObject(object);
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-04 14:30:11 +02:00
|
|
|
int ConfigRpcComponent::RemoteObjectRemovedHandler(NewMessageEventArgs::Ptr ea)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-16 16:27:41 +02:00
|
|
|
JsonRpcRequest::Ptr message = ea->Message->Cast<JsonRpcRequest>();
|
|
|
|
Message::Ptr params = message->GetParams();
|
2012-04-04 14:30:11 +02:00
|
|
|
string name, type;
|
|
|
|
|
2012-03-31 16:28:11 +02:00
|
|
|
if (!message->GetParamString("name", &name))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!message->GetParamString("type", &type))
|
|
|
|
return 0;
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
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;
|
|
|
|
|
2012-04-04 14:30:11 +02:00
|
|
|
configHive->RemoveObject(object);
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_COMPONENT(ConfigRpcComponent);
|