From 35c829c0177a497b5d6559f92d31ddeb9242a655 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Fri, 20 Nov 2015 16:22:28 +0100 Subject: [PATCH] doc/5-advanced-topics: add example how to use custom functions in attributes Signed-off-by: Jean Flach fixes #10947 --- doc/5-advanced-topics.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/doc/5-advanced-topics.md b/doc/5-advanced-topics.md index c5a09f19e..95ae6e413 100644 --- a/doc/5-advanced-topics.md +++ b/doc/5-advanced-topics.md @@ -366,6 +366,32 @@ You can omit the `log()` calls, they only help debugging. } } +### Use Custom Functions as Attribute + +To use custom functions as attributes, the function must be defined in a +slightly unexpected way. The following example shows how to assign values +depending on group membership. All hosts in the `slow-lan` host group use 300 +as value for `ping_wrta`, all other hosts use 100. + + globals.group_specific_value = function(group, group_value, non_group_value) { + return function() use (group, group_value, non_group_value) { + if (group in host.groups) { + return group_value + } else { + return non_group_value + } + } + } + + apply Service "ping4" { + import "generic-service" + check_command = "ping4" + + vars.ping_wrta = group_specific_value("slow-lan", 300, 100) + vars.ping_crta = group_specific_value("slow-lan", 500, 200) + + assign where true + } ## Access Object Attributes at Runtime -- 2.40.0