}
Application::DeclareStatePath(Application::GetLocalStateDir() + "/lib/icinga2/icinga2.state");
+ Application::DeclareModAttrPath(Application::GetLocalStateDir() + "/lib/icinga2/modified-attributes.conf");
Application::DeclareObjectsPath(Application::GetLocalStateDir() + "/cache/icinga2/icinga2.debug");
Application::DeclareVarsPath(Application::GetLocalStateDir() + "/cache/icinga2/icinga2.vars");
Application::DeclarePidPath(Application::GetRunDir() + "/icinga2/icinga2.pid");
<< " Local state directory: " << GetLocalStateDir() << "\n"
<< " Package data directory: " << GetPkgDataDir() << "\n"
<< " State path: " << GetStatePath() << "\n"
+ << " Modified attributes path: " << GetModAttrPath() << "\n"
<< " Objects path: " << GetObjectsPath() << "\n"
<< " Vars path: " << GetVarsPath() << "\n"
<< " PID path: " << GetPidPath() << "\n";
ScriptGlobal::Set("StatePath", path);
}
+/**
+ * Retrieves the path for the modified attributes file.
+ *
+ * @returns The path.
+ */
+String Application::GetModAttrPath(void)
+{
+ return ScriptGlobal::Get("ModAttrPath", &Empty);
+}
+
+/**
+ * Sets the path for the modified attributes file.
+ *
+ * @param path The new path.
+ */
+void Application::DeclareModAttrPath(const String& path)
+{
+ if (!ScriptGlobal::Exists("ModAttrPath"))
+ ScriptGlobal::Set("ModAttrPath", path);
+}
+
/**
* Retrieves the path for the objects file.
*
static String GetStatePath(void);
static void DeclareStatePath(const String& path);
+ static String GetModAttrPath(void);
+ static void DeclareModAttrPath(const String& path);
+
static String GetObjectsPath(void);
static void DeclareObjectsPath(const String& path);
}
}
+void DynamicObject::DumpModifiedAttributes(const boost::function<void(const DynamicObject::Ptr&, const String&, const Value&)>& callback)
+{
+ BOOST_FOREACH(const DynamicType::Ptr& dt, DynamicType::GetTypes()) {
+ BOOST_FOREACH(const DynamicObject::Ptr& object, dt->GetObjects()) {
+ Dictionary::Ptr originalAttributes = object->GetOriginalAttributes();
+
+ if (!originalAttributes)
+ continue;
+
+ ObjectLock olock(originalAttributes);
+ BOOST_FOREACH(const Dictionary::Pair& kv, originalAttributes) {
+ // TODO-MA: vars.os
+ int fid = object->GetReflectionType()->GetFieldId(kv.first);
+ Value value = object->GetField(fid);
+ callback(object, kv.first, value);
+ }
+ }
+ }
+
+}
+
DynamicObject::Ptr DynamicObject::GetObject(const String& type, const String& name)
{
DynamicType::Ptr dtype = DynamicType::GetByName(type);
static void RestoreObjects(const String& filename, int attributeTypes = FAState);
static void StopObjects(void);
+ static void DumpModifiedAttributes(const boost::function<void(const DynamicObject::Ptr&, const String&, const Value&)>& callback);
+
static Object::Ptr GetPrototype(void);
protected:
#include "icinga/icingaapplication.hpp"
#include "icinga/icingaapplication.tcpp"
#include "icinga/cib.hpp"
+#include "config/configwriter.cpp"
#include "base/dynamictype.hpp"
#include "base/logger.hpp"
#include "base/objectlock.hpp"
l_RetentionTimer->OnTimerExpired.connect(boost::bind(&IcingaApplication::DumpProgramState, this));
l_RetentionTimer->Start();
+ /* restore modified attributes */
+ Expression *expression = ConfigCompiler::CompileFile(GetModAttrPath());
+
+ if (expression) {
+ try {
+ ScriptFrame frame;
+ expression->Evaluate(frame);
+ } catch (const std::exception& ex) {
+ Log(LogCritical, "config", DiagnosticInformation(ex));
+ }
+ }
+
RunEventLoop();
Log(LogInformation, "IcingaApplication", "Icinga has shut down.");
DumpProgramState();
}
+static void PersistModAttrHelper(const ConfigWriter::Ptr& cw, DynamicObject::Ptr& previousObject, const DynamicObject::Ptr& object, const String& attr, const Value& value)
+{
+ if (object != previousObject) {
+ if (previousObject)
+ cw->EmitRaw("}\n\n");
+
+ cw->EmitRaw("var obj = get_object(");
+ cw->EmitIdentifier(object->GetReflectionType()->GetName(), 0);
+ cw->EmitRaw(", ");
+ cw->EmitString(object->GetName());
+ cw->EmitRaw(")\n");
+
+ cw->EmitRaw("if (obj) {\n");
+ }
+
+ cw->EmitRaw("\tobj.");
+
+ Array::Ptr args2 = new Array();
+ args2->Add(attr);
+ args2->Add(value);
+ cw->EmitFunctionCall("modify_attribute", args2);
+
+ cw->EmitRaw("\n");
+
+ previousObject = object;
+}
+
void IcingaApplication::DumpProgramState(void)
{
DynamicObject::DumpObjects(GetStatePath());
+
+ ConfigWriter::Ptr cw = new ConfigWriter(GetModAttrPath());
+ DynamicObject::Ptr previousObject;
+ DynamicObject::DumpModifiedAttributes(boost::bind(&PersistModAttrHelper, cw, boost::ref(previousObject), _1, _2, _3));
+
+ if (previousObject)
+ cw->EmitRaw("\n}\n");
}
IcingaApplication::Ptr IcingaApplication::GetInstance(void)