Am 27.05.2015 um 07:27 schrieb Klaus Muth:
Hi
I'm trying to define and use a global function in icinga2 and failing miserable:
in functions.conf:
function test(str) {
str = str.replace("X", "u")
return "bla"+str.replace("bla", "blubb")
}
this does reload without error.
In host object:
object Host "gnarf" {
import "generic-host"
# more config [...]
action_url = "bla" + test("XXbrrrm bla")
}
gives me a
critical/config: Error: Invalid field access (for value of type 'Host'): 'test'
Icinga 2 is right about that. Using user-defined functions in object
attributes are limited to a defined scope. Currently that's just
supported for
* custom attributes
* set_if in command arguments w/ boolean return values
* command attribute in *Command objects
* expressions (assign/ignore where) w/ boolean return values
Given that you are using string replace two times, it would be more
reasonable at the time to define a local variable and use built-in
prototype functions for the string type.
http://docs.icinga.org/icinga2/latest/doc/module/icinga2/chapter/library-reference#string-type
imagine /etc/icinga2/conf.d # cat test.conf
...
object Host "foo" {
import "generic-host"
vars.bla = "abcdefghijklmnopqrstuvwxyz"
var t = vars.bla.replace("a", "b")
var u = t.replace("b", "c")
action_url = "ß" + t
}
imagine /etc/icinga2/conf.d # icinga2 object list --name foo
Object 'foo' of type 'Host':
% declared in '/etc/icinga2/conf.d/test.conf', lines 6:1-6:17
* __name = "foo"
* action_url = "ßbbcdefghijklmnopqrstuvwxyz"
% = modified in '/etc/icinga2/conf.d/test.conf', lines 12:3-12:23
* address = ""
* address6 = ""
* check_command = "hostalive"
% = modified in '/etc/icinga2/conf.d/templates.conf', lines 19:3-19:29
* check_interval = 60
% = modified in '/etc/icinga2/conf.d/templates.conf', lines 16:3-16:21
* check_period = ""
* command_endpoint = ""
* display_name = "foo"
* enable_active_checks = true
* enable_event_handler = true
* enable_flapping = false
* enable_notifications = true
* enable_passive_checks = true
* enable_perfdata = true
* event_command = ""
* flapping_threshold = 30
* groups = [ ]
* icon_image = ""
* icon_image_alt = ""
* max_check_attempts = 5
% = modified in '/etc/icinga2/conf.d/templates.conf', lines 15:3-15:24
* name = "foo"
* notes = ""
* notes_url = ""
* retry_interval = 30
% = modified in '/etc/icinga2/conf.d/templates.conf', lines 17:3-17:22
* templates = [ "foo", "generic-host" ]
% = modified in '/etc/icinga2/conf.d/test.conf', lines 6:1-6:17
% = modified in '/etc/icinga2/conf.d/templates.conf', lines 14:1-14:28
* type = "Host"
* vars
* bla = "abcdefghijklmnopqrstuvwxyz"
% = modified in '/etc/icinga2/conf.d/test.conf', lines 8:3-8:41
* volatile = false
* zone = ""
Given user-defined functions in combination with object attributes I
remember discussing that with Gunnar a while ago, but IIRC this is not
that simple to support, neither context-wise nor implementation-wise.
Although I cannot find any referenced issue in the development tracker,
so feel free to propose a feature request, or sponsor a patch even.
So you'll see, Icinga 2 is more than just playing with stuff. But
certainly you'll acknowledge the fact that developers should enjoy their
software while granting you free license and usage.
Regards,
Michael
PS: https://twitter.com/dnsmichi/status/603984281514434561
--
https://twitter.com/dnsmichi
https://github.com/dnsmichi
http://www.legendiary.at
_______________________________________________
icinga-users mailing list
[email protected]
https://lists.icinga.org/mailman/listinfo/icinga-users