RunDir |**Read-only.** Contains the path of the run directory. Defaults to LocalStateDir + "/run".
PkgDataDir |**Read-only.** Contains the path of the package data directory. Defaults to PrefixDir + "/share/icinga2".
StatePath |**Read-write.** Contains the path of the Icinga 2 state file. Defaults to LocalStateDir + "/lib/icinga2/icinga2.state".
+ObjectsPath |**Read-write.** Contains the path of the Icinga 2 objects file. Defaults to LocalStateDir + "/cache/icinga2/icinga2.debug".
PidPath |**Read-write.** Contains the path of the Icinga 2 PID file. Defaults to RunDir + "/icinga2/icinga2.pid".
Vars |**Read-write.** Contains a dictionary with global custom attributes. Not set by default.
NodeName |**Read-write.** Contains the cluster node name. Set to the local hostname by default.
IncludeZoneDirRecursive(zonePath);
}
-static bool LoadConfigFiles(const String& appType)
+static bool LoadConfigFiles(const String& appType, const String& objectsFile = String())
{
ConfigCompilerContext::GetInstance()->Reset();
ConfigItem::Ptr item = builder->Compile();
item->Register();
- bool result = ConfigItem::ValidateItems();
+ bool result = ConfigItem::ValidateItems(objectsFile);
int warnings = 0, errors = 0;
}
Application::DeclareStatePath(Application::GetLocalStateDir() + "/lib/icinga2/icinga2.state");
+ Application::DeclareObjectsPath(Application::GetLocalStateDir() + "/cache/icinga2/icinga2.debug");
Application::DeclarePidPath(Application::GetRunDir() + "/icinga2/icinga2.pid");
#ifndef _WIN32
}
}
- if (!LoadConfigFiles(appType))
+ if (!LoadConfigFiles(appType, Application::GetObjectsPath()))
return EXIT_FAILURE;
if (g_AppParams.count("validate")) {
<< " Local state directory: " << GetLocalStateDir() << std::endl
<< " Package data directory: " << GetPkgDataDir() << std::endl
<< " State path: " << GetStatePath() << std::endl
+ << " Objects path: " << GetObjectsPath() << std::endl
<< " PID path: " << GetPidPath() << std::endl
<< " Application type: " << GetApplicationType() << std::endl;
}
ScriptVariable::Set("StatePath", path, false);
}
+/**
+ * Retrieves the path for the objects file.
+ *
+ * @returns The path.
+ */
+String Application::GetObjectsPath(void)
+{
+ return ScriptVariable::Get("ObjectsPath");
+}
+
+/**
+ * Sets the path for the objects file.
+ *
+ * @param path The new path.
+ */
+void Application::DeclareObjectsPath(const String& path)
+{
+ ScriptVariable::Set("ObjectsPath", path, false);
+}
+
/**
* Retrieves the path for the PID file.
*
ScriptVariable::GetByName("LocalStateDir")->SetConstant(true);
ScriptVariable::GetByName("RunDir")->SetConstant(true);
ScriptVariable::GetByName("PkgDataDir")->SetConstant(true);
- ScriptVariable::GetByName("StatePath")->SetConstant(false);
- ScriptVariable::GetByName("PidPath")->SetConstant(false);
+ ScriptVariable::GetByName("StatePath")->SetConstant(true);
+ ScriptVariable::GetByName("ObjectsPath")->SetConstant(true);
+ ScriptVariable::GetByName("PidPath")->SetConstant(true);
ScriptVariable::GetByName("ApplicationType")->SetConstant(true);
}
static String GetStatePath(void);
static void DeclareStatePath(const String& path);
+ static String GetObjectsPath(void);
+ static void DeclareObjectsPath(const String& path);
+
static String GetPidPath(void);
static void DeclarePidPath(const String& path);
#include "base/debug.hpp"
#include "base/workqueue.hpp"
#include "base/exception.hpp"
+#include "base/stdiostream.hpp"
+#include "base/netstring.hpp"
#include <sstream>
+#include <fstream>
#include <boost/foreach.hpp>
using namespace icinga;
m_Validated = true;
}
-bool ConfigItem::ValidateItems(void)
+void ConfigItem::WriteObjectsFile(const String& filename)
+{
+ Log(LogInformation, "ConfigItem", "Dumping config items to file '" + filename + "'");
+
+ String tempFilename = filename + ".tmp";
+
+ std::fstream fp;
+ fp.open(tempFilename.CStr(), std::ios_base::out);
+
+ if (!fp)
+ BOOST_THROW_EXCEPTION(std::runtime_error("Could not open '" + tempFilename + "' file"));
+
+ StdioStream::Ptr sfp = make_shared<StdioStream>(&fp, false);
+
+ BOOST_FOREACH(const ItemMap::value_type& kv, m_Items) {
+ ConfigItem::Ptr item = kv.second;
+
+ Dictionary::Ptr persistentItem = make_shared<Dictionary>();
+
+ persistentItem->Set("type", item->GetType());
+ persistentItem->Set("name", item->GetName());
+ persistentItem->Set("abstract", item->IsAbstract());
+ persistentItem->Set("properties", item->GetProperties());
+
+ String json = JsonSerialize(persistentItem);
+
+ NetString::WriteStringToStream(sfp, json);
+ }
+
+ sfp->Close();
+
+ fp.close();
+
+#ifdef _WIN32
+ _unlink(filename.CStr());
+#endif /* _WIN32 */
+
+ if (rename(tempFilename.CStr(), filename.CStr()) < 0) {
+ BOOST_THROW_EXCEPTION(posix_error()
+ << boost::errinfo_api_function("rename")
+ << boost::errinfo_errno(errno)
+ << boost::errinfo_file_name(tempFilename));
+ }
+}
+
+bool ConfigItem::ValidateItems(const String& objectsFile)
{
if (ConfigCompilerContext::GetInstance()->HasErrors())
return false;
upq.Join();
+ if (!objectsFile.IsEmpty())
+ ConfigItem::WriteObjectsFile(objectsFile);
+
ConfigItem::DiscardItems();
ConfigType::DiscardTypes();
void ValidateItem(void);
- static bool ValidateItems(void);
+ static bool ValidateItems(const String& objectsFile = String());
static bool ActivateItems(void);
static void DiscardItems(void);
+ static void WriteObjectsFile(const String& filename);
+
private:
String m_Type; /**< The object type. */
String m_Name; /**< The name. */