]> granicus.if.org Git - icinga2/commitdiff
Implemented global macros.
authorGunnar Beutner <gunnar.beutner@netways.de>
Fri, 13 Jul 2012 09:29:23 +0000 (11:29 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Fri, 13 Jul 2012 09:30:11 +0000 (11:30 +0200)
cib/host.cpp
cib/host.h
cib/macroprocessor.cpp
cib/macroprocessor.h
cib/nagioschecktask.cpp
doc/icinga2-config.odt
icinga/icingaapplication.cpp
icinga/icingaapplication.h

index 680d194d1612e90551fdf1ea5f1fe0237506103a..2b59cc98fd037d19c0f3b36da73c742e41523a9d 100644 (file)
@@ -86,6 +86,13 @@ set<string> Host::GetParents(void) const
        return parents;
 }
 
+Dictionary::Ptr Host::GetMacros(void) const
+{
+       Dictionary::Ptr value;
+       GetProperty("macros", &value);
+       return value;
+}
+
 bool Host::IsReachable(void) const
 {
        Dictionary::Ptr dependencies;
index 765831745c7eac6ecd7cea70051f86384eaf7a7e..6a8ff828cc0e522d4ec8557343e835f0a12a8024 100644 (file)
@@ -34,6 +34,7 @@ public:
        string GetAlias(void) const;
        Dictionary::Ptr GetGroups(void) const;
        set<string> GetParents(void) const;
+       Dictionary::Ptr GetMacros(void) const;
 
        bool IsReachable(void) const;
        bool IsUp(void) const;
index 413b499dbc57ca42c9d7a5953678e26975506cde..de5a2522849e2121097a937559fd4f0154af56d6 100644 (file)
@@ -21,7 +21,7 @@
 
 using namespace icinga;
 
-string MacroProcessor::ResolveMacros(const string& str, const Dictionary::Ptr& macros)
+string MacroProcessor::ResolveMacros(const string& str, const vector<Dictionary::Ptr>& macroDicts)
 {
        string::size_type offset, pos_first, pos_second;
 
@@ -36,7 +36,18 @@ string MacroProcessor::ResolveMacros(const string& str, const Dictionary::Ptr& m
 
                string name = result.substr(pos_first + 1, pos_second - pos_first - 1);
                string value;
-               if (!macros || !macros->Get(name, &value))
+               bool resolved = false;
+
+               vector<Dictionary::Ptr>::const_iterator it;
+               for (it = macroDicts.begin(); it != macroDicts.end(); it++) {
+                       Dictionary::Ptr macros = *it;
+                       if (macros && macros->Get(name, &value)) {
+                               resolved = true;
+                               break;
+                       }
+               }
+
+               if (!resolved)
                        throw runtime_error("Macro '" + name + "' is not defined.");
 
                result.replace(pos_first, pos_second - pos_first + 1, value);
index 5dbe4e1cbcf20d0ee28ab619274bace3bfc72bbf..224485bb73fea13afeb6000e6bd47f0b08141edf 100644 (file)
@@ -26,9 +26,9 @@ namespace icinga
 class I2_CIB_API MacroProcessor
 {
 public:
-       static string ResolveMacros(const string& str, const Dictionary::Ptr& macros);
+       static string ResolveMacros(const string& str, const vector<Dictionary::Ptr>& macroDicts);
 };
 
 }
 
-#endif /* MACROPROCESSOR_H */
\ No newline at end of file
+#endif /* MACROPROCESSOR_H */
index f82a6f2c693c3183399a8375b5a0f68d02044a5c..ae96f95944d3117967ff27b8d8b936cd71f7f3e6 100644 (file)
@@ -34,7 +34,12 @@ NagiosCheckTask::NagiosCheckTask(const Service& service)
        : CheckTask(service), m_FP(NULL), m_UsePopen(false)
 {
        string checkCommand = service.GetCheckCommand();
-       m_Command = MacroProcessor::ResolveMacros(checkCommand, service.GetMacros());
+
+       vector<Dictionary::Ptr> macroDicts;
+       macroDicts.push_back(service.GetMacros());
+       macroDicts.push_back(service.GetHost().GetMacros());
+       macroDicts.push_back(IcingaApplication::GetInstance()->GetMacros());
+       m_Command = MacroProcessor::ResolveMacros(checkCommand, macroDicts);
 }
 
 void NagiosCheckTask::Enqueue(void)
index 39db6b130a1e708d471c22b871bd24cfb6622d6f..2b59128663e3de8e05524d4a0efed9520e8b6963 100644 (file)
Binary files a/doc/icinga2-config.odt and b/doc/icinga2-config.odt differ
index 5d98f1687d424470a7982f4777ab92e271f3c1be..a6b97bdcad842c4f9ac57f1f564f0dfa79b22bd8 100644 (file)
@@ -170,6 +170,7 @@ int IcingaApplication::Main(const vector<string>& args)
        icingaConfig->GetProperty("node", &m_Node);
        icingaConfig->GetProperty("service", &m_Service);
        icingaConfig->GetProperty("pidpath", &m_PidPath);
+       icingaConfig->GetProperty("macros", &m_Macros);
 
        string logpath;
        if (icingaConfig->GetProperty("logpath", &logpath)) {
@@ -314,6 +315,11 @@ string IcingaApplication::GetPidPath(void) const
        return m_PidPath;
 }
 
+Dictionary::Ptr IcingaApplication::GetMacros(void) const
+{
+       return m_Macros;
+}
+
 time_t IcingaApplication::GetStartTime(void) const
 {
        return m_StartTime;
index bf15401f090c232a0ceaa816bb4bea9ae6bc9215..dc40af1473feef784990c54261c2f6a3c536b274 100644 (file)
@@ -45,6 +45,7 @@ public:
        string GetNode(void) const;
        string GetService(void) const;
        string GetPidPath(void) const;
+       Dictionary::Ptr GetMacros(void) const;
 
        time_t GetStartTime(void) const;
 
@@ -56,6 +57,7 @@ private:
        string m_Node;
        string m_Service;
        string m_PidPath;
+       Dictionary::Ptr m_Macros;
 
        time_t m_StartTime;