]> granicus.if.org Git - icinga2/commitdiff
Implement support for writing the icinga2.debug file
authorGunnar Beutner <gunnar.beutner@netways.de>
Tue, 12 Aug 2014 11:46:54 +0000 (13:46 +0200)
committerGunnar Beutner <gunnar@beutner.name>
Fri, 15 Aug 2014 19:00:26 +0000 (21:00 +0200)
refs #6702

doc/6-configuring-icinga-2.md
icinga-app/icinga.cpp
lib/base/application.cpp
lib/base/application.hpp
lib/config/configitem.cpp
lib/config/configitem.hpp

index 97f18a40ce13ad8ea6fbcb6578eb4c969e80c8fd..c2840f76cc549ac5a8b49d49b1fc0f7113e4ed2f 100644 (file)
@@ -13,6 +13,7 @@ LocalStateDir       |**Read-only.** Contains the path of the local state directo
 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.
index 5cec83f9195d40af0a2e8103c269da3bc97ab5d7..7f54d10401afa98f2dff7660c941982490499499 100644 (file)
@@ -82,7 +82,7 @@ static void IncludeNonLocalZone(const String& zonePath)
        IncludeZoneDirRecursive(zonePath);
 }
 
-static bool LoadConfigFiles(const String& appType)
+static bool LoadConfigFiles(const String& appType, const String& objectsFile = String())
 {
        ConfigCompilerContext::GetInstance()->Reset();
 
@@ -113,7 +113,7 @@ static bool LoadConfigFiles(const String& appType)
        ConfigItem::Ptr item = builder->Compile();
        item->Register();
 
-       bool result = ConfigItem::ValidateItems();
+       bool result = ConfigItem::ValidateItems(objectsFile);
 
        int warnings = 0, errors = 0;
 
@@ -382,6 +382,7 @@ int Main(void)
        }
 
        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
@@ -543,7 +544,7 @@ int Main(void)
                }
        }
 
-       if (!LoadConfigFiles(appType))
+       if (!LoadConfigFiles(appType, Application::GetObjectsPath()))
                return EXIT_FAILURE;
 
        if (g_AppParams.count("validate")) {
index e2a00039db6a1f60be4809b4a17d59cd8b35f8f0..49d2d39a079fbc6829d682f2cb03f6b3c223427f 100644 (file)
@@ -478,6 +478,7 @@ void Application::DisplayInfoMessage(bool skipVersion)
                  << "  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;
 }
@@ -976,6 +977,26 @@ void Application::DeclareStatePath(const String& path)
        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.
  *
@@ -1023,8 +1044,9 @@ void Application::MakeVariablesConstant(void)
        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);
 }
 
index 2213fc6698d6b792c0cd3554205fe19979c27239..b1d043b6b9911d79f22153e8497800496bf6d5b8 100644 (file)
@@ -104,6 +104,9 @@ public:
        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);
 
index 80e9d99f9448959d35b70850920b28d62fb2cfba..ece7c9b4c9629a1d8aa3ba401a4ff243903ba2a9 100644 (file)
 #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;
@@ -264,7 +267,52 @@ void ConfigItem::ValidateItem(void)
        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;
@@ -323,6 +371,9 @@ bool ConfigItem::ValidateItems(void)
 
        upq.Join();
 
+       if (!objectsFile.IsEmpty())
+               ConfigItem::WriteObjectsFile(objectsFile);
+
        ConfigItem::DiscardItems();
        ConfigType::DiscardTypes();
 
index 7c79cbb03e063266aab94e81b57cfb484c54181c..4053161875b331a75c36df2bbf3a298e617ab67d 100644 (file)
@@ -64,10 +64,12 @@ public:
 
        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. */