Implemented global macros.

This commit is contained in:
Gunnar Beutner 2012-07-13 11:29:23 +02:00
parent eebd660d4f
commit 530819d658
8 changed files with 37 additions and 5 deletions

View File

@ -86,6 +86,13 @@ set<string> Host::GetParents(void) const
return parents; return parents;
} }
Dictionary::Ptr Host::GetMacros(void) const
{
Dictionary::Ptr value;
GetProperty("macros", &value);
return value;
}
bool Host::IsReachable(void) const bool Host::IsReachable(void) const
{ {
Dictionary::Ptr dependencies; Dictionary::Ptr dependencies;

View File

@ -34,6 +34,7 @@ public:
string GetAlias(void) const; string GetAlias(void) const;
Dictionary::Ptr GetGroups(void) const; Dictionary::Ptr GetGroups(void) const;
set<string> GetParents(void) const; set<string> GetParents(void) const;
Dictionary::Ptr GetMacros(void) const;
bool IsReachable(void) const; bool IsReachable(void) const;
bool IsUp(void) const; bool IsUp(void) const;

View File

@ -21,7 +21,7 @@
using namespace icinga; 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; 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 name = result.substr(pos_first + 1, pos_second - pos_first - 1);
string value; 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."); throw runtime_error("Macro '" + name + "' is not defined.");
result.replace(pos_first, pos_second - pos_first + 1, value); result.replace(pos_first, pos_second - pos_first + 1, value);

View File

@ -26,9 +26,9 @@ namespace icinga
class I2_CIB_API MacroProcessor class I2_CIB_API MacroProcessor
{ {
public: 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 */ #endif /* MACROPROCESSOR_H */

View File

@ -34,7 +34,12 @@ NagiosCheckTask::NagiosCheckTask(const Service& service)
: CheckTask(service), m_FP(NULL), m_UsePopen(false) : CheckTask(service), m_FP(NULL), m_UsePopen(false)
{ {
string checkCommand = service.GetCheckCommand(); 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) void NagiosCheckTask::Enqueue(void)

Binary file not shown.

View File

@ -170,6 +170,7 @@ int IcingaApplication::Main(const vector<string>& args)
icingaConfig->GetProperty("node", &m_Node); icingaConfig->GetProperty("node", &m_Node);
icingaConfig->GetProperty("service", &m_Service); icingaConfig->GetProperty("service", &m_Service);
icingaConfig->GetProperty("pidpath", &m_PidPath); icingaConfig->GetProperty("pidpath", &m_PidPath);
icingaConfig->GetProperty("macros", &m_Macros);
string logpath; string logpath;
if (icingaConfig->GetProperty("logpath", &logpath)) { if (icingaConfig->GetProperty("logpath", &logpath)) {
@ -314,6 +315,11 @@ string IcingaApplication::GetPidPath(void) const
return m_PidPath; return m_PidPath;
} }
Dictionary::Ptr IcingaApplication::GetMacros(void) const
{
return m_Macros;
}
time_t IcingaApplication::GetStartTime(void) const time_t IcingaApplication::GetStartTime(void) const
{ {
return m_StartTime; return m_StartTime;

View File

@ -45,6 +45,7 @@ public:
string GetNode(void) const; string GetNode(void) const;
string GetService(void) const; string GetService(void) const;
string GetPidPath(void) const; string GetPidPath(void) const;
Dictionary::Ptr GetMacros(void) const;
time_t GetStartTime(void) const; time_t GetStartTime(void) const;
@ -56,6 +57,7 @@ private:
string m_Node; string m_Node;
string m_Service; string m_Service;
string m_PidPath; string m_PidPath;
Dictionary::Ptr m_Macros;
time_t m_StartTime; time_t m_StartTime;