mirror of https://github.com/Icinga/icinga2.git
Implemented rudimentary config file parser.
This commit is contained in:
parent
1661a1b363
commit
6e441a26e9
|
@ -1,170 +0,0 @@
|
|||
#include "i2-configcomponent.h"
|
||||
|
||||
using namespace icinga;
|
||||
using std::dynamic_pointer_cast;
|
||||
|
||||
IcingaApplication::RefType ConfigComponent::GetIcingaApplication(void)
|
||||
{
|
||||
return dynamic_pointer_cast<IcingaApplication>(GetApplication());
|
||||
}
|
||||
|
||||
string ConfigComponent::GetName(void)
|
||||
{
|
||||
return "configcomponent";
|
||||
}
|
||||
|
||||
void ConfigComponent::Start(void)
|
||||
{
|
||||
IcingaApplication::RefType icingaApp = GetIcingaApplication();
|
||||
|
||||
if (icingaApp.get() == NULL)
|
||||
throw exception(/*"Component loaded by incompatible application."*/);
|
||||
|
||||
ConnectionManager::RefType connectionManager = icingaApp->GetConnectionManager();
|
||||
ConfigHive::RefType configHive = icingaApp->GetConfigHive();
|
||||
|
||||
if (GetConfig()->GetPropertyInteger("configSource") != 0) {
|
||||
connectionManager->RegisterMethod("config::FetchObjects", bind_weak(&ConfigComponent::FetchObjectsHandler, shared_from_this()));
|
||||
|
||||
configHive->OnObjectCreated.bind(bind_weak(&ConfigComponent::LocalObjectCreatedHandler, shared_from_this()));
|
||||
configHive->OnObjectRemoved.bind(bind_weak(&ConfigComponent::LocalObjectRemovedHandler, shared_from_this()));
|
||||
configHive->OnPropertyChanged.bind(bind_weak(&ConfigComponent::LocalPropertyChangedHandler, shared_from_this()));
|
||||
}
|
||||
|
||||
connectionManager->RegisterMethod("config::ObjectCreated", bind_weak(&ConfigComponent::RemoteObjectCreatedHandler, shared_from_this()));
|
||||
connectionManager->RegisterMethod("config::ObjectRemoved", bind_weak(&ConfigComponent::RemoteObjectRemovedHandler, shared_from_this()));
|
||||
connectionManager->RegisterMethod("config::PropertyChanged", bind_weak(&ConfigComponent::RemotePropertyChangedHandler, shared_from_this()));
|
||||
}
|
||||
|
||||
void ConfigComponent::Stop(void)
|
||||
{
|
||||
// TODO: implement
|
||||
}
|
||||
|
||||
JsonRpcMessage::RefType ConfigComponent::MakeObjectMessage(const ConfigObject::RefType& object, string method, bool includeProperties)
|
||||
{
|
||||
JsonRpcMessage::RefType msg = new_object<JsonRpcMessage>();
|
||||
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) {
|
||||
for (ConfigObject::ParameterIterator pi = object->Properties.begin(); pi != object->Properties.end(); pi++) {
|
||||
cJSON_AddStringToObject(params, pi->first.c_str(), pi->second.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
int ConfigComponent::FetchObjectsHandler(NewMessageEventArgs::RefType ea)
|
||||
{
|
||||
JsonRpcClient::RefType client = static_pointer_cast<JsonRpcClient>(ea->Source);
|
||||
ConfigHive::RefType configHive = GetIcingaApplication()->GetConfigHive();
|
||||
|
||||
for (ConfigHive::TypeIterator ti = configHive->Objects.begin(); ti != configHive->Objects.end(); ti++) {
|
||||
for (ConfigHive::ObjectIterator oi = ti->second.begin(); oi != ti->second.end(); oi++) {
|
||||
JsonRpcMessage::RefType msg = MakeObjectMessage(oi->second, "config::ObjectCreated", true);
|
||||
client->SendMessage(msg);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConfigComponent::LocalObjectCreatedHandler(ConfigHiveEventArgs::RefType ea)
|
||||
{
|
||||
ConnectionManager::RefType connectionManager = GetIcingaApplication()->GetConnectionManager();
|
||||
connectionManager->SendMessage(MakeObjectMessage(ea->ConfigObject, "config::ObjectCreated", true));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConfigComponent::LocalObjectRemovedHandler(ConfigHiveEventArgs::RefType ea)
|
||||
{
|
||||
ConnectionManager::RefType connectionManager = GetIcingaApplication()->GetConnectionManager();
|
||||
connectionManager->SendMessage(MakeObjectMessage(ea->ConfigObject, "config::ObjectRemoved", false));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConfigComponent::LocalPropertyChangedHandler(ConfigHiveEventArgs::RefType ea)
|
||||
{
|
||||
JsonRpcMessage::RefType msg = MakeObjectMessage(ea->ConfigObject, "config::ObjectRemoved", false);
|
||||
cJSON *params = msg->GetParams();
|
||||
cJSON_AddStringToObject(params, "property", ea->Property.c_str());
|
||||
string value = ea->ConfigObject->GetProperty(ea->Property);
|
||||
cJSON_AddStringToObject(params, "value", value.c_str());
|
||||
|
||||
ConnectionManager::RefType connectionManager = GetIcingaApplication()->GetConnectionManager();
|
||||
connectionManager->SendMessage(msg);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConfigComponent::RemoteObjectCreatedHandler(NewMessageEventArgs::RefType ea)
|
||||
{
|
||||
JsonRpcMessage::RefType message = ea->Message;
|
||||
|
||||
// TODO: update hive
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConfigComponent::RemoteObjectRemovedHandler(NewMessageEventArgs::RefType ea)
|
||||
{
|
||||
JsonRpcMessage::RefType message = ea->Message;
|
||||
string name, type;
|
||||
|
||||
if (!message->GetParamString("name", &name))
|
||||
return 0;
|
||||
|
||||
if (!message->GetParamString("type", &type))
|
||||
return 0;
|
||||
|
||||
ConfigHive::RefType configHive = GetIcingaApplication()->GetConfigHive();
|
||||
ConfigObject::RefType object = configHive->GetObject(type, name);
|
||||
|
||||
if (object.get() == NULL)
|
||||
return 0;
|
||||
|
||||
configHive->RemoveObject(object);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConfigComponent::RemotePropertyChangedHandler(NewMessageEventArgs::RefType ea)
|
||||
{
|
||||
JsonRpcMessage::RefType message = ea->Message;
|
||||
string name, type, property, value;
|
||||
|
||||
if (!message->GetParamString("name", &name))
|
||||
return 0;
|
||||
|
||||
if (!message->GetParamString("type", &type))
|
||||
return 0;
|
||||
|
||||
if (!message->GetParamString("property", &property))
|
||||
return 0;
|
||||
|
||||
if (!message->GetParamString("value", &value))
|
||||
return 0;
|
||||
|
||||
ConfigHive::RefType configHive = GetIcingaApplication()->GetConfigHive();
|
||||
ConfigObject::RefType object = configHive->GetObject(type, name);
|
||||
|
||||
if (object.get() == NULL)
|
||||
return 0;
|
||||
|
||||
object->SetProperty(property, value);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
EXPORT_COMPONENT(ConfigComponent);
|
|
@ -1,32 +0,0 @@
|
|||
#ifndef I2_CONFIGCOMPONENT_H
|
||||
#define I2_CONFIGCOMPONENT_H
|
||||
|
||||
namespace icinga
|
||||
{
|
||||
|
||||
class ConfigComponent : public Component
|
||||
{
|
||||
private:
|
||||
IcingaApplication::RefType GetIcingaApplication(void);
|
||||
|
||||
int FetchObjectsHandler(NewMessageEventArgs::RefType ea);
|
||||
|
||||
int LocalObjectCreatedHandler(ConfigHiveEventArgs::RefType ea);
|
||||
int LocalObjectRemovedHandler(ConfigHiveEventArgs::RefType ea);
|
||||
int LocalPropertyChangedHandler(ConfigHiveEventArgs::RefType ea);
|
||||
|
||||
int RemoteObjectCreatedHandler(NewMessageEventArgs::RefType ea);
|
||||
int RemoteObjectRemovedHandler(NewMessageEventArgs::RefType ea);
|
||||
int RemotePropertyChangedHandler(NewMessageEventArgs::RefType ea);
|
||||
|
||||
JsonRpcMessage::RefType MakeObjectMessage(const ConfigObject::RefType& object, string method, bool includeProperties);
|
||||
|
||||
public:
|
||||
virtual string GetName(void);
|
||||
virtual void Start(void);
|
||||
virtual void Stop(void);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* I2_CONFIGCOMPONENT_H */
|
|
@ -1,10 +0,0 @@
|
|||
#ifndef I2_I2CONFIGCOMPONENT_H
|
||||
#define I2_I2CONFIGCOMPONENT_H
|
||||
|
||||
#include <i2-base.h>
|
||||
#include <i2-jsonrpc.h>
|
||||
#include <i2-icinga.h>
|
||||
|
||||
#include "configcomponent.h"
|
||||
|
||||
#endif /* I2_I2CONFIGCOMPONENT_H */
|
|
@ -0,0 +1,75 @@
|
|||
#include "i2-configfilecomponent.h"
|
||||
#include <cJSON.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
using namespace icinga;
|
||||
using std::ifstream;
|
||||
|
||||
string ConfigFileComponent::GetName(void)
|
||||
{
|
||||
return "configfilecomponent";
|
||||
}
|
||||
|
||||
void ConfigFileComponent::Start(void)
|
||||
{
|
||||
ifstream fp;
|
||||
FIFO::RefType fifo = new_object<FIFO>();
|
||||
|
||||
fp.open(GetConfig()->GetProperty("filename"), ifstream::in);
|
||||
if (fp.fail())
|
||||
throw exception(/*"Could not open config file"*/);
|
||||
|
||||
while (!fp.eof()) {
|
||||
size_t bufferSize = 1024;
|
||||
char *buffer = (char *)fifo->GetWriteBuffer(&bufferSize);
|
||||
fp.read(buffer, bufferSize);
|
||||
if (fp.bad())
|
||||
throw exception(/*"Could not read from config file"*/);
|
||||
fifo->Write(NULL, fp.gcount());
|
||||
}
|
||||
|
||||
fp.close();
|
||||
|
||||
fifo->Write("\0", 1);
|
||||
|
||||
/* TODO: implement config parsing, for now we just use JSON */
|
||||
cJSON *jsonobj = cJSON_Parse((const char *)fifo->GetReadBuffer());
|
||||
fifo->Read(NULL, fifo->GetSize());
|
||||
|
||||
if (jsonobj == NULL)
|
||||
throw exception(/*"Could not parse config file."*/);
|
||||
|
||||
for (cJSON *typeobj = jsonobj->child; typeobj != NULL; typeobj = typeobj->next) {
|
||||
string type = typeobj->string;
|
||||
|
||||
for (cJSON *object = typeobj->child; object != NULL; object = object->next) {
|
||||
string name = object->string;
|
||||
|
||||
ConfigObject::RefType cfgobj = new_object<ConfigObject>();
|
||||
cfgobj->SetName(name);
|
||||
cfgobj->SetType(type);
|
||||
|
||||
for (cJSON *property = object->child; property != NULL; property = property->next) {
|
||||
string key = property->string;
|
||||
|
||||
if (property->type != cJSON_String)
|
||||
continue;
|
||||
|
||||
string value = property->valuestring;
|
||||
|
||||
cfgobj->SetProperty(key, value);
|
||||
}
|
||||
|
||||
GetApplication()->GetConfigHive()->AddObject(cfgobj);
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_Delete(jsonobj);
|
||||
}
|
||||
|
||||
void ConfigFileComponent::Stop(void)
|
||||
{
|
||||
}
|
||||
|
||||
EXPORT_COMPONENT(ConfigFileComponent);
|
|
@ -0,0 +1,20 @@
|
|||
#ifndef I2_CONFIGFILECOMPONENT_H
|
||||
#define I2_CONFIGFILECOMPONENT_H
|
||||
|
||||
namespace icinga
|
||||
{
|
||||
|
||||
class ConfigFileComponent : public Component
|
||||
{
|
||||
public:
|
||||
typedef shared_ptr<ConfigFileComponent> RefType;
|
||||
typedef weak_ptr<ConfigFileComponent> WeakRefType;
|
||||
|
||||
virtual string GetName(void);
|
||||
virtual void Start(void);
|
||||
virtual void Stop(void);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* I2_CONFIGFILECOMPONENT_H */
|
|
@ -10,8 +10,15 @@
|
|||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="configfilecomponent.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="configfilecomponent.h" />
|
||||
<ClInclude Include="i2-configfilecomponent.h" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{697C6D7E-3109-484C-A7AF-384D28711610}</ProjectGuid>
|
||||
<ProjectGuid>{E58F1DA7-B723-412B-B2B7-7FF58E2A944E}</ProjectGuid>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<RootNamespace>icinga</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
@ -38,13 +45,11 @@
|
|||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
<IncludePath>$(ProjectDir)\..\base;$(ProjectDir)\..\jsonrpc;$(ProjectDir)\..\config;$(ProjectDir)\..\icinga;$(IncludePath)</IncludePath>
|
||||
<IncludePath>$(ProjectDir)\..\base;$(ProjectDir)\..\jsonrpc;$(IncludePath)</IncludePath>
|
||||
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
<IncludePath>$(ProjectDir)\..\base;$(ProjectDir)\..\jsonrpc;$(ProjectDir)\..\config;$(ProjectDir)\..\componentloader;$(IncludePath)</IncludePath>
|
||||
<IncludePath>$(ProjectDir)\..\base;$(ProjectDir)\..\jsonrpc;$(IncludePath)</IncludePath>
|
||||
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
|
@ -53,13 +58,17 @@
|
|||
</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;CONFIGCOMPONENT_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>base.lib;jsonrpc.lib;config.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>base.lib;jsonrpc.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
<Lib>
|
||||
<AdditionalDependencies>
|
||||
</AdditionalDependencies>
|
||||
</Lib>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
|
@ -69,23 +78,19 @@
|
|||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;CONFIGCOMPONENT_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<AdditionalDependencies>base.lib;jsonrpc.lib;config.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>base.lib;jsonrpc.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
<Lib>
|
||||
<AdditionalDependencies>$(OutDir)\base.lib;$(OutDir)\jsonrpc.lib</AdditionalDependencies>
|
||||
</Lib>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="configcomponent.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="configcomponent.h" />
|
||||
<ClInclude Include="i2-configcomponent.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef I2_I2CONFIGFILECOMPONENT_H
|
||||
#define I2_I2CONFIGFILECOMPONENT_H
|
||||
|
||||
#include <i2-base.h>
|
||||
|
||||
#include "configfilecomponent.h"
|
||||
|
||||
#endif /* I2_I2CONFIGFILECOMPONENT_H */
|
Loading…
Reference in New Issue