I’m trying to setup notifications and ended up with four entries; sending mail for host & service and xmpp for host & service.
This works quite well, but I have to update four entries whenever I add a group.
This is an example of one entry:
apply Notification "xmpp-icingaadmin" to Service {
import "xmpp-service-notification"
user_groups = ["icingaadmins"]
for (var i => grp in {
"group1" = "group1-team"
"group2" = "group2-team"
"group3" = "group3-team"
"group4" = "group4-team"
}) {
if (i in host.vars.adm.group) {
user_groups += [grp]
}
}
assign where host.vars.notification.xmpp
}
I really would like an global array for at least the notifications, but couldn’t find out how. So I experimented with functions, but it seems I can’t just define a function outside the objects.
you can indeed define variables and functions in a global scope. Following your current approach, you could do something like this:
global.notification_user_groups = {
"group1" = "group1-team"
"group2" = "group2-team"
"group3" = "group3-team"
"group4" = "group4-team"
}
global.generate_notification_user_groups = function(host) {
var groups = ["icingaadmins"]
for (var i => grp in global.notification_user_groups) {
if (i in host.vars.adm.group) {
groups += [grp]
}
}
return groups
}
apply Notification "xmpp-icingaadmin" to Service {
import "xmpp-service-notification"
user_groups = global.generate_notification_user_groups(host) // For hosts use "this" instead host "host" here
assign where host.vars.notification.xmpp
}