diff --git a/cib/host.cpp b/cib/host.cpp index 680d194d1..2b59cc98f 100644 --- a/cib/host.cpp +++ b/cib/host.cpp @@ -86,6 +86,13 @@ set 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; diff --git a/cib/host.h b/cib/host.h index 765831745..6a8ff828c 100644 --- a/cib/host.h +++ b/cib/host.h @@ -34,6 +34,7 @@ public: string GetAlias(void) const; Dictionary::Ptr GetGroups(void) const; set GetParents(void) const; + Dictionary::Ptr GetMacros(void) const; bool IsReachable(void) const; bool IsUp(void) const; diff --git a/cib/macroprocessor.cpp b/cib/macroprocessor.cpp index 413b499db..de5a25228 100644 --- a/cib/macroprocessor.cpp +++ b/cib/macroprocessor.cpp @@ -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& 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::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); diff --git a/cib/macroprocessor.h b/cib/macroprocessor.h index 5dbe4e1cb..224485bb7 100644 --- a/cib/macroprocessor.h +++ b/cib/macroprocessor.h @@ -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& macroDicts); }; } -#endif /* MACROPROCESSOR_H */ \ No newline at end of file +#endif /* MACROPROCESSOR_H */ diff --git a/cib/nagioschecktask.cpp b/cib/nagioschecktask.cpp index f82a6f2c6..ae96f9594 100644 --- a/cib/nagioschecktask.cpp +++ b/cib/nagioschecktask.cpp @@ -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 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) diff --git a/doc/icinga2-config.odt b/doc/icinga2-config.odt index 39db6b130..2b5912866 100644 Binary files a/doc/icinga2-config.odt and b/doc/icinga2-config.odt differ diff --git a/icinga/icingaapplication.cpp b/icinga/icingaapplication.cpp index 5d98f1687..a6b97bdca 100644 --- a/icinga/icingaapplication.cpp +++ b/icinga/icingaapplication.cpp @@ -170,6 +170,7 @@ int IcingaApplication::Main(const vector& 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; diff --git a/icinga/icingaapplication.h b/icinga/icingaapplication.h index bf15401f0..dc40af147 100644 --- a/icinga/icingaapplication.h +++ b/icinga/icingaapplication.h @@ -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;