Centralise default icinga.* and env.* macros

This commit is contained in:
Alexander A. Klimov 2023-02-17 15:33:36 +01:00
parent b2b49caf61
commit f2974c07cf
17 changed files with 88 additions and 137 deletions

View File

@ -1,7 +1,6 @@
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "db_ido/idochecktask.hpp"
#include "icinga/envresolver.hpp"
#include "icinga/host.hpp"
#include "icinga/checkcommand.hpp"
#include "icinga/macroprocessor.hpp"
@ -61,8 +60,6 @@ void IdoCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
resolvers.emplace_back("command", commandObj);
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
resolvers.emplace_back("env", new EnvResolver(), false);
String idoType = MacroProcessor::ResolveMacros("$ido_type$", resolvers, checkable->GetLastCheckResult(),
nullptr, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);

View File

@ -2,7 +2,6 @@
#include "icinga/apiactions.hpp"
#include "icinga/checkable.hpp"
#include "icinga/envresolver.hpp"
#include "icinga/service.hpp"
#include "icinga/servicegroup.hpp"
#include "icinga/hostgroup.hpp"
@ -668,8 +667,6 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object, cons
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
resolvers.emplace_back("env", new EnvResolver(), false);
String resolved_endpoint = MacroProcessor::ResolveMacros(
endpoint, resolvers, checkable->GetLastCheckResult(),

View File

@ -3,6 +3,8 @@
#include "icinga/macroprocessor.hpp"
#include "icinga/macroresolver.hpp"
#include "icinga/customvarobject.hpp"
#include "icinga/envresolver.hpp"
#include "icinga/icingaapplication.hpp"
#include "base/array.hpp"
#include "base/objectlock.hpp"
#include "base/logger.hpp"
@ -73,6 +75,16 @@ Value MacroProcessor::ResolveMacros(const Value& str, const ResolverList& resolv
return result;
}
static const EnvResolver::Ptr l_EnvResolver = new EnvResolver();
static MacroProcessor::ResolverList GetDefaultResolvers()
{
return {
{ "icinga", IcingaApplication::GetInstance() },
{ "env", l_EnvResolver, false }
};
}
bool MacroProcessor::ResolveMacro(const String& macro, const ResolverList& resolvers,
const CheckResult::Ptr& cr, Value *result, bool *recursive_macro)
{
@ -88,80 +100,84 @@ bool MacroProcessor::ResolveMacro(const String& macro, const ResolverList& resol
tokens.erase(tokens.begin());
}
for (const ResolverSpec& resolver : resolvers) {
if (!objName.IsEmpty() && objName != resolver.Name)
continue;
const auto defaultResolvers (GetDefaultResolvers());
if (objName.IsEmpty()) {
if (!resolver.ResolveShortMacros)
for (auto resolverList : {&resolvers, &defaultResolvers}) {
for (auto& resolver : *resolverList) {
if (!objName.IsEmpty() && objName != resolver.Name)
continue;
CustomVarObject::Ptr dobj = dynamic_pointer_cast<CustomVarObject>(resolver.Obj);
if (dobj) {
Dictionary::Ptr vars = dobj->GetVars();
if (vars && vars->Contains(macro)) {
*result = vars->Get(macro);
*recursive_macro = true;
return true;
}
}
}
auto *mresolver = dynamic_cast<MacroResolver *>(resolver.Obj.get());
if (mresolver && mresolver->ResolveMacro(boost::algorithm::join(tokens, "."), cr, result))
return true;
Value ref = resolver.Obj;
bool valid = true;
for (const String& token : tokens) {
if (ref.IsObjectType<Dictionary>()) {
Dictionary::Ptr dict = ref;
if (dict->Contains(token)) {
ref = dict->Get(token);
if (objName.IsEmpty()) {
if (!resolver.ResolveShortMacros)
continue;
} else {
valid = false;
break;
CustomVarObject::Ptr dobj = dynamic_pointer_cast<CustomVarObject>(resolver.Obj);
if (dobj) {
Dictionary::Ptr vars = dobj->GetVars();
if (vars && vars->Contains(macro)) {
*result = vars->Get(macro);
*recursive_macro = true;
return true;
}
}
} else if (ref.IsObject()) {
Object::Ptr object = ref;
Type::Ptr type = object->GetReflectionType();
if (!type) {
valid = false;
break;
}
int field = type->GetFieldId(token);
if (field == -1) {
valid = false;
break;
}
ref = object->GetField(field);
Field fieldInfo = type->GetFieldInfo(field);
if (strcmp(fieldInfo.TypeName, "Timestamp") == 0)
ref = static_cast<long>(ref);
}
}
if (valid) {
if (tokens[0] == "vars" ||
tokens[0] == "action_url" ||
tokens[0] == "notes_url" ||
tokens[0] == "notes")
*recursive_macro = true;
auto *mresolver = dynamic_cast<MacroResolver *>(resolver.Obj.get());
*result = ref;
return true;
if (mresolver && mresolver->ResolveMacro(boost::algorithm::join(tokens, "."), cr, result))
return true;
Value ref = resolver.Obj;
bool valid = true;
for (const String& token : tokens) {
if (ref.IsObjectType<Dictionary>()) {
Dictionary::Ptr dict = ref;
if (dict->Contains(token)) {
ref = dict->Get(token);
continue;
} else {
valid = false;
break;
}
} else if (ref.IsObject()) {
Object::Ptr object = ref;
Type::Ptr type = object->GetReflectionType();
if (!type) {
valid = false;
break;
}
int field = type->GetFieldId(token);
if (field == -1) {
valid = false;
break;
}
ref = object->GetField(field);
Field fieldInfo = type->GetFieldInfo(field);
if (strcmp(fieldInfo.TypeName, "Timestamp") == 0)
ref = static_cast<long>(ref);
}
}
if (valid) {
if (tokens[0] == "vars" ||
tokens[0] == "action_url" ||
tokens[0] == "notes_url" ||
tokens[0] == "notes")
*recursive_macro = true;
*result = ref;
return true;
}
}
}
@ -173,9 +189,12 @@ Value MacroProcessor::EvaluateFunction(const Function::Ptr& func, const Resolver
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros, int recursionLevel)
{
Dictionary::Ptr resolvers_this = new Dictionary();
const auto defaultResolvers (GetDefaultResolvers());
for (const ResolverSpec& resolver : resolvers) {
resolvers_this->Set(resolver.Name, resolver.Obj);
for (auto resolverList : {&resolvers, &defaultResolvers}) {
for (auto& resolver: *resolverList) {
resolvers_this->Set(resolver.Name, resolver.Obj);
}
}
auto internalResolveMacrosShim = [resolvers, cr, resolvedMacros, useResolvedMacros, recursionLevel](const std::vector<Value>& args) {

View File

@ -1,7 +1,6 @@
/* Icinga 2 | (c) 2022 Icinga GmbH | GPLv2+ */
#include "icingadb/icingadbchecktask.hpp"
#include "icinga/envresolver.hpp"
#include "icinga/host.hpp"
#include "icinga/checkcommand.hpp"
#include "icinga/macroprocessor.hpp"
@ -62,8 +61,6 @@ void IcingadbCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckR
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
resolvers.emplace_back("command", commandObj);
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
resolvers.emplace_back("env", new EnvResolver(), false);
auto resolve ([&](const String& macro) {
return MacroProcessor::ResolveMacros(macro, resolvers, checkable->GetLastCheckResult(),

View File

@ -3,7 +3,6 @@
#include "livestatus/hoststable.hpp"
#include "livestatus/hostgroupstable.hpp"
#include "livestatus/endpointstable.hpp"
#include "icinga/envresolver.hpp"
#include "icinga/host.hpp"
#include "icinga/service.hpp"
#include "icinga/hostgroup.hpp"
@ -11,7 +10,6 @@
#include "icinga/eventcommand.hpp"
#include "icinga/timeperiod.hpp"
#include "icinga/macroprocessor.hpp"
#include "icinga/icingaapplication.hpp"
#include "icinga/compatutility.hpp"
#include "icinga/pluginutility.hpp"
#include "base/configtype.hpp"
@ -316,8 +314,6 @@ Value HostsTable::NotesExpandedAccessor(const Value& row)
MacroProcessor::ResolverList resolvers {
{ "host", host },
{ "icinga", IcingaApplication::GetInstance() },
{ "env", new EnvResolver(), false }
};
return MacroProcessor::ResolveMacros(host->GetNotes(), resolvers);
@ -342,8 +338,6 @@ Value HostsTable::NotesUrlExpandedAccessor(const Value& row)
MacroProcessor::ResolverList resolvers {
{ "host", host },
{ "icinga", IcingaApplication::GetInstance() },
{ "env", new EnvResolver(), false }
};
return MacroProcessor::ResolveMacros(host->GetNotesUrl(), resolvers);
@ -368,8 +362,6 @@ Value HostsTable::ActionUrlExpandedAccessor(const Value& row)
MacroProcessor::ResolverList resolvers {
{ "host", host },
{ "icinga", IcingaApplication::GetInstance() },
{ "env", new EnvResolver(), false }
};
return MacroProcessor::ResolveMacros(host->GetActionUrl(), resolvers);
@ -426,8 +418,6 @@ Value HostsTable::IconImageExpandedAccessor(const Value& row)
MacroProcessor::ResolverList resolvers {
{ "host", host },
{ "icinga", IcingaApplication::GetInstance() },
{ "env", new EnvResolver(), false }
};
return MacroProcessor::ResolveMacros(host->GetIconImage(), resolvers);

View File

@ -9,11 +9,9 @@
#include "icinga/servicegroup.hpp"
#include "icinga/hostgroup.hpp"
#include "icinga/checkcommand.hpp"
#include "icinga/envresolver.hpp"
#include "icinga/eventcommand.hpp"
#include "icinga/timeperiod.hpp"
#include "icinga/macroprocessor.hpp"
#include "icinga/icingaapplication.hpp"
#include "icinga/compatutility.hpp"
#include "icinga/pluginutility.hpp"
#include "base/configtype.hpp"
@ -373,8 +371,6 @@ Value ServicesTable::NotesExpandedAccessor(const Value& row)
MacroProcessor::ResolverList resolvers {
{ "service", service },
{ "host", service->GetHost() },
{ "icinga", IcingaApplication::GetInstance() },
{ "env", new EnvResolver(), false }
};
return MacroProcessor::ResolveMacros(service->GetNotes(), resolvers);
@ -400,8 +396,6 @@ Value ServicesTable::NotesUrlExpandedAccessor(const Value& row)
MacroProcessor::ResolverList resolvers {
{ "service", service },
{ "host", service->GetHost() },
{ "icinga", IcingaApplication::GetInstance() },
{ "env", new EnvResolver(), false }
};
return MacroProcessor::ResolveMacros(service->GetNotesUrl(), resolvers);
@ -427,8 +421,6 @@ Value ServicesTable::ActionUrlExpandedAccessor(const Value& row)
MacroProcessor::ResolverList resolvers {
{ "service", service },
{ "host", service->GetHost() },
{ "icinga", IcingaApplication::GetInstance() },
{ "env", new EnvResolver(), false }
};
return MacroProcessor::ResolveMacros(service->GetActionUrl(), resolvers);
@ -454,8 +446,6 @@ Value ServicesTable::IconImageExpandedAccessor(const Value& row)
MacroProcessor::ResolverList resolvers {
{ "service", service },
{ "host", service->GetHost() },
{ "icinga", IcingaApplication::GetInstance() },
{ "env", new EnvResolver(), false }
};
return MacroProcessor::ResolveMacros(service->GetIconImage(), resolvers);

View File

@ -2,7 +2,6 @@
#include "methods/clusterzonechecktask.hpp"
#include "icinga/checkcommand.hpp"
#include "icinga/envresolver.hpp"
#include "icinga/macroprocessor.hpp"
#include "remote/apilistener.hpp"
#include "remote/endpoint.hpp"
@ -64,8 +63,6 @@ void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const Che
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
resolvers.emplace_back("command", command);
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
resolvers.emplace_back("env", new EnvResolver(), false);
String zoneName = MacroProcessor::ResolveMacros("$cluster_zone$", resolvers, checkable->GetLastCheckResult(),
nullptr, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);

View File

@ -4,8 +4,6 @@
# include <stdlib.h>
#endif /* _WIN32 */
#include "methods/dummychecktask.hpp"
#include "icinga/envresolver.hpp"
#include "icinga/icingaapplication.hpp"
#include "icinga/pluginutility.hpp"
#include "base/utility.hpp"
#include "base/perfdatavalue.hpp"
@ -38,8 +36,6 @@ void DummyCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResu
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
resolvers.emplace_back("command", command);
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
resolvers.emplace_back("env", new EnvResolver(), false);
int dummyState = MacroProcessor::ResolveMacros("$dummy_state$", resolvers, checkable->GetLastCheckResult(),
nullptr, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);

View File

@ -2,11 +2,9 @@
#include "methods/icingachecktask.hpp"
#include "icinga/cib.hpp"
#include "icinga/envresolver.hpp"
#include "icinga/service.hpp"
#include "icinga/checkcommand.hpp"
#include "icinga/macroprocessor.hpp"
#include "icinga/icingaapplication.hpp"
#include "icinga/clusterevents.hpp"
#include "icinga/checkable.hpp"
#include "remote/apilistener.hpp"
@ -42,8 +40,6 @@ void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
resolvers.emplace_back("command", command);
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
resolvers.emplace_back("env", new EnvResolver(), false);
String missingIcingaMinVersion;

View File

@ -1,11 +1,9 @@
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "methods/pluginchecktask.hpp"
#include "icinga/envresolver.hpp"
#include "icinga/pluginutility.hpp"
#include "icinga/checkcommand.hpp"
#include "icinga/macroprocessor.hpp"
#include "icinga/icingaapplication.hpp"
#include "base/configtype.hpp"
#include "base/logger.hpp"
#include "base/function.hpp"
@ -38,8 +36,6 @@ void PluginCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
resolvers.emplace_back("command", commandObj);
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
resolvers.emplace_back("env", new EnvResolver(), false);
int timeout = commandObj->GetTimeout();

View File

@ -1,11 +1,9 @@
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "methods/plugineventtask.hpp"
#include "icinga/envresolver.hpp"
#include "icinga/eventcommand.hpp"
#include "icinga/macroprocessor.hpp"
#include "icinga/pluginutility.hpp"
#include "icinga/icingaapplication.hpp"
#include "base/configtype.hpp"
#include "base/logger.hpp"
#include "base/function.hpp"
@ -37,8 +35,6 @@ void PluginEventTask::ScriptFunc(const Checkable::Ptr& checkable,
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
resolvers.emplace_back("command", commandObj);
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
resolvers.emplace_back("env", new EnvResolver(), false);
int timeout = commandObj->GetTimeout();
std::function<void(const Value& commandLine, const ProcessResult&)> callback;

View File

@ -1,13 +1,11 @@
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "methods/pluginnotificationtask.hpp"
#include "icinga/envresolver.hpp"
#include "icinga/notification.hpp"
#include "icinga/notificationcommand.hpp"
#include "icinga/pluginutility.hpp"
#include "icinga/service.hpp"
#include "icinga/macroprocessor.hpp"
#include "icinga/icingaapplication.hpp"
#include "base/function.hpp"
#include "base/logger.hpp"
#include "base/utility.hpp"
@ -54,8 +52,6 @@ void PluginNotificationTask::ScriptFunc(const Notification::Ptr& notification,
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
resolvers.emplace_back("command", commandObj);
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
resolvers.emplace_back("env", new EnvResolver(), false);
int timeout = commandObj->GetTimeout();
std::function<void(const Value& commandLine, const ProcessResult&)> callback;

View File

@ -1,8 +1,6 @@
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "methods/sleepchecktask.hpp"
#include "icinga/envresolver.hpp"
#include "icinga/icingaapplication.hpp"
#include "icinga/pluginutility.hpp"
#include "base/utility.hpp"
#include "base/convert.hpp"
@ -34,8 +32,6 @@ void SleepCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResu
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
resolvers.emplace_back("command", commandObj);
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
resolvers.emplace_back("env", new EnvResolver(), false);
double sleepTime = MacroProcessor::ResolveMacros("$sleep_time$", resolvers, checkable->GetLastCheckResult(),
nullptr, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);

View File

@ -2,7 +2,6 @@
#include "perfdata/graphitewriter.hpp"
#include "perfdata/graphitewriter-ti.cpp"
#include "icinga/envresolver.hpp"
#include "icinga/service.hpp"
#include "icinga/checkcommand.hpp"
#include "icinga/macroprocessor.hpp"
@ -294,8 +293,6 @@ void GraphiteWriter::CheckResultHandlerInternal(const Checkable::Ptr& checkable,
if (service)
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
resolvers.emplace_back("env", new EnvResolver(), false);
String prefix;

View File

@ -3,7 +3,6 @@
#include "perfdata/influxdbcommonwriter.hpp"
#include "perfdata/influxdbcommonwriter-ti.cpp"
#include "remote/url.hpp"
#include "icinga/envresolver.hpp"
#include "icinga/service.hpp"
#include "icinga/macroprocessor.hpp"
#include "icinga/icingaapplication.hpp"
@ -225,8 +224,6 @@ void InfluxdbCommonWriter::CheckResultHandlerWQ(const Checkable::Ptr& checkable,
if (service)
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
resolvers.emplace_back("env", new EnvResolver(), false);
String prefix;

View File

@ -2,7 +2,6 @@
#include "perfdata/opentsdbwriter.hpp"
#include "perfdata/opentsdbwriter-ti.cpp"
#include "icinga/envresolver.hpp"
#include "icinga/service.hpp"
#include "icinga/checkcommand.hpp"
#include "icinga/macroprocessor.hpp"
@ -196,8 +195,6 @@ void OpenTsdbWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C
if (service)
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
resolvers.emplace_back("env", new EnvResolver(), false);
// Resolve macros for the service and host template config line
if (config_tmpl_tags) {

View File

@ -2,7 +2,6 @@
#include "perfdata/perfdatawriter.hpp"
#include "perfdata/perfdatawriter-ti.cpp"
#include "icinga/envresolver.hpp"
#include "icinga/service.hpp"
#include "icinga/macroprocessor.hpp"
#include "icinga/icingaapplication.hpp"
@ -117,8 +116,6 @@ void PerfdataWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C
if (service)
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
resolvers.emplace_back("env", new EnvResolver(), false);
if (service) {
String line = MacroProcessor::ResolveMacros(GetServiceFormatTemplate(), resolvers, cr, nullptr, &PerfdataWriter::EscapeMacroMetric);