How to not repeat myself in Notifications

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.

Any ideas are welcome! :slight_smile:

Hi @jhagg,

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
}

You could also use namespaces here instead of global: https://icinga.com/docs/icinga2/latest/doc/17-language-reference/#namespaces

Greetings
Noah

I tried using ‘global’, but icinga (2.12.0) complained:

critical/config: Error: Invalid field access (for value of type ‘Notification’): ‘global’

So I tested using a namespace instead of global and it worked perfectly!

Don’t know what I did wrong with global, but I’m satisfied with namespaces.

Thanks!

2 Likes