Notifications: filtering based on user (target) attributes

  • Version used: 2.12.3
  • Operating System and version: Linux (Debian Bullseye)

I am happily using icinga2 for monitoring quite a lot of services.
Notifications are sent to a variety of users.
Notifications are sent via a variety of channels (e.g. email, XMPP, Matrix).

I think, I am using a quite typical apply sections for defining targets for notifications (here for Matrix notifications):

apply Notification "matrix-host" to Host {
  import "matrix-host-notification"
  user_groups = host.vars.notification.matrix.groups
  assign where host.vars.notification.matrix
}

apply Notification "matrix-service" to Service {
  import "matrix-service-notification"
  user_groups = host.vars.notification.matrix.groups
  assign where ( service.vars.notification.matrix || host.vars.notification.matrix.all_services )
}

But not all users are interested in all notification channels, thus some users lack the attribute for an email address, an XMPP address or a matrix room ID.

This results in error messages like the one below:

Notification command for object 'foo' (PID: -1, arguments: 'sh' '-c' '') terminated with exit code 3, output: Error: Non-optional macro 'user.vars.matrix.room' used in argument '-m' is missing.

(here the NotificationCommand refers to a specific attribute of the user, which simply does not exist)

I am not sure, how to handle this situation properly.

I see two options, but neither of them feels really desirable for me:

  • A) split the user accounts (and groups) into multiple notification-specific objects (e.g. foo is split into foo-email, foo-xmpp, …)
  • B) ensure that the required attributes exist (even if empty) for each user and handle an empty target gracefully in the notification scripts (exiting early with success → doing nothing)

But I hope, that there is a better way for achieving my goal? Maybe some kind of conditional execution of the notification command (based on specific user attributes)?

Thank you for your thoughts!

C) Add eMail address(es) pointing to /dev/null to the respective users.

Thanks for sharing your thoughts!

C) Add eMail address(es) pointing to /dev/null to the respective users.

I would consider this to be equivalent to (B): you need to assign magic values (null, "", /dev/null, …) and interprete them as “do nothing” indicators in the respective notification scripts.

I was hoping, that I could avoid filling all these user attributes manually. Some kind of filtered/conditional execution (or application) of the notification commands would feel so much better …