we’re looking to define a custom function (specifically urlencode) globally, so that it’s available across all zones in our Icinga 2 setup.
We are running icinga 2.14.5-1.
We’ve seen the documentation on registering global functions here:
However, as far as we understood it suggests modifying the icinga2.conf file manually on every involved host. In our case, this isn’t really feasible due to organizational constraints (and a large number of hosts). So far, we’ve been managing our entire configuration using top-down config sync and icinga director.
Is there a way to define such a function once and have it available globally via config sync?
Right now, defining the function per zone does technically work for us — but we’re concerned about maintaining consistency over time.
I tried by just using a global zone - but so far deployment just went through when the function was defined in the zone I also placed the service calling that function.
This does not work: (“… tried to access undefined script variable”) function urlencode(input) {…} in my-Global-Zone object Service "myDummyService" use(urlencode) {…} in my-Prod-Zone
This does work: function urlencode(input) {…} in my-Prod-Zone object Service "myDummyService" use(urlencode) {…} in my-Prod-Zone
thanks for that suggestion, I checked the manual you provided.
That is the way we are using it on a “define it in each zone seperately” basis, but this does not work for us: If I define the function in one (globally availiable) zone and try to use it by “use(…)” in another I always end up in the error “… tried to access undefined script variable”.
Hi, you can define your function e.g. in the zones.d/global-templates/functions.conf file like this:
globals.urlencode = function(url) {
var encodedUrl = ""
// Encode your URL here
// ...
return encodedUrl
}
This will then be accessible from all of your configured zones that trust the global-templates zone and call that function from everywhere without having to import it via the use(...) statement.
You can even omit the globals. prefix when calling the function, and it will still work, but it’s nicer to have it there, so that you know where the function comes from.