Apply notification with more than one notification type by user preference

I have a system set up with multiple notification types and I would like to be able to send notifications by the users’ preferred method where the users are in the same group. I know the apply Notification doesn’t allow direct reference to user attributes. Is there some other way to do so? Effectively I’m trying to do this:

apply Notification "email-service" to Service {
  import "generic-email-service"
  vars += config
  assign where service.vars.notify.email && user.vars.email
}

The only way I have figured out so far is a horrible hack where I have user groups by type of notification, something I am hoping to avoid, ie: “server-admins-sms” and “server-admins-email” instead of “server-admins”

Hello @sbusgs,

The way you chose with the two different groups for Email and SMS is how we roll here as well.
When logically thinking about it, you can’t differentiate between users if they are in the same group.

furthermore, the things complicate if the same user wants to get different notifications type for different servers (or different notification levels i.e. warning and critical)

I don’t think there is a solution for this but please do share if you get a lead.

Best :slight_smile:

I’m not sure I understand you use case correctly.
But have you tried playing around with the Icinga DSL?

With that I could imagine that you create a function that queries a variable of the user object (“preferred_contact_method”). And if that variable is == "email" then import generic-email-service.

Thanks for the comments.

I poked at it a little with something like this but got nowhere:

apply Notification "email-service" to Service {
  ...
  vars.message_transport = {{
     var res = {}

     for (u in get_objects(User)) {
        if (u.email) {
          res = "email"
        }
     }

    return res
  }}
assign where service.vars.notify.email && service.vars.message_transport

Code plagarized in part from:
Execute a notificationCommand with multiple users - Icinga 2 - Icinga Community

The use case is when you have a user that has two notification methods for one service, perhaps warning goes to email and critical and unknown go to pager. The alternative is to have a group for each notification method.

And both variants use the same notification command?
I would expect two different commands, or at least two notification templates and then create two notification apply rules. One for “email-warning”, the other for “pager-critical-unknown”.

No, there are two notification commands, two users, one group. I’ve already gone ahead and set up a test instance with two groups and that solves the problem in an inelegant but a relatively easy to understand way.