Skip to content

Commit

Permalink
Implement support for using functions in custom attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarbeutner committed Jan 27, 2015
1 parent c36d785 commit fb44744
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/3-monitoring-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,7 @@ There are several ways of using custom attributes with [apply rules](3-monitorin
[boolean](15-language-reference.md#boolean-literals)) for expression conditions (`assign where`, `ignore where`)
* As [array](15-language-reference.md#array) or [dictionary](15-language-reference.md#dictionary) attribute with nested values
(e.g. dictionaries in dictionaries) in [apply for](3-monitoring-basics.md#using-apply-for) rules.
* As a [function object](#functions)

Features like [DB IDO](3-monitoring-basics.md#db-ido), Livestatus(#setting-up-livestatus) or StatusData(#status-data)
dump this column as encoded JSON string, and set `is_json` resp. `cv_is_json` to `1`.
Expand Down
19 changes: 19 additions & 0 deletions lib/icinga/macroprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "base/logger.hpp"
#include "base/context.hpp"
#include "base/dynamicobject.hpp"
#include "base/scriptframe.hpp"
#include <boost/foreach.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/join.hpp>
Expand Down Expand Up @@ -166,6 +167,8 @@ Value MacroProcessor::InternalResolveMacros(const String& str, const ResolverLis
size_t offset, pos_first, pos_second;
offset = 0;

Dictionary::Ptr resolvers_this;

String result = str;
while ((pos_first = result.FindFirstOf("$", offset)) != String::NPos) {
pos_second = result.FindFirstOf("$", pos_first + 1);
Expand Down Expand Up @@ -194,6 +197,22 @@ Value MacroProcessor::InternalResolveMacros(const String& str, const ResolverLis
found = true;
}

if (resolved_macro.IsObjectType<Function>()) {
Function::Ptr func = resolved_macro;

if (!resolvers_this) {
resolvers_this = new Dictionary();

BOOST_FOREACH(const ResolverSpec& resolver, resolvers) {
resolvers_this->Set(resolver.first, resolver.second);
}
}

ScriptFrame frame(resolvers_this);
std::vector<Value> args;
resolved_macro = func->Invoke(args);
}

if (!found) {
if (!missingMacro)
Log(LogWarning, "MacroProcessor")
Expand Down

0 comments on commit fb44744

Please sign in to comment.