]> granicus.if.org Git - icinga2/commitdiff
Implement support for using functions in custom attributes
authorGunnar Beutner <gunnar@beutner.name>
Tue, 27 Jan 2015 12:40:05 +0000 (13:40 +0100)
committerGunnar Beutner <gunnar@beutner.name>
Tue, 27 Jan 2015 12:40:05 +0000 (13:40 +0100)
refs #7564

doc/3-monitoring-basics.md
lib/icinga/macroprocessor.cpp

index 5ef81b105a64da64108fc7ef47d03adb1b7c9723..eafecb5f6863412188ed7d22e84f55bbe2dac4a8 100644 (file)
@@ -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`.
index 53b770322ba274228dfaf029d2857a7907e545b8..280ba938ae9ce4b28805711bf12d6a010d0a21a8 100644 (file)
@@ -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>
@@ -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);
@@ -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")