Email notifications in icinga2

Hello,

so after almost 13 years, we decided to migrate from icinga 1 to icinga 2. We have many users and different notification approaches. For us, we use notifications via telegram, for some users, notifications via slack or email.

Telegram notifications are working great, so do email notifications. My question is, how can I disable or enable email notifications for specific user groups. For example, in icinga 1 we had like this:

define contactgroup {
        contactgroup_name       test
        alias                  Test
        members             test, test1
}

define contact {
  contact_name                    test 
  alias                           Test 
  service_notification_period     24x7
  host_notification_period        24x7
  service_notification_options    w,u,c,r
  host_notification_options       d,u,r
  service_notification_commands   notify-by-email
  host_notification_commands      host-notify-by-email
  email                           test@test.com
}

define service {
  	use                             generic-service
  	host_name                       test.server.com
  	service_description             PING
  	contact_groups                  test
  	check_command                   check_ping!40.0,10%!100.0,40% -p 5
}

So if i removed “service_notification_commands notify-by-email” from specific contact, this client wasnt alerted via email anymore and if I set this to “notify-service-by-telegram”, then this user was notified only by telegram.

How can I archive this in icinga2. I have usergroups, notificationcommands …

This is my example host/service definition in icinga2. It works great, but I cant find the way to do the same that I had in icinga 1. So that i can change notification type in user definition directly.

object Host "test.server.com" {
	import "generic-host"
	address = "1.1.1.1"
	vars.notification.groups = ["test"]
	vars.ssh_port =  22
	vars.os = "Linux"
	vars.check_nrpe_load = true
	vars.check_nrpe_disk_root = true
}

apply Notification "telegram-test" to Service {
  import "telegram-service-notification"
  user_groups = [ "test" ]
  vars.states = [ Critical ]
  assign where "test" in host.vars.notification.groups
}

Thank you in advance!

I’d create a user group per notification type and assign users accordingly. Notification objects will only have their user group configured. When a user wants to change the notification service, you simply remove it from one group and add him to the other group.

Hi Ronald.

Thank you for the answer. That is a great idea, but I have one problem. I have many user groups in which there are specific users (customers) for specific servers. This monitoring contains more than 800 hosts. But if user can be in different user groups, this could work.

For mail notifications, I added code below. But for example. I have ssl user that is part of two groups. Group “no-email-notification” is not defined in any host.

Is it possible to make assignment with logic that would apply mail notifications only for users which are not part of “no-email-notification” user group? But that group name is not defined in any host or service definition → “assign where “no-email-notification” !in host.vars.user.groups” something like this

apply Notification “mail-icingaadmin” to Host {
import “mail-host-notification”
user_groups = host.vars.notification.groups
assign where “no-email-notification” !in host.vars.notification.groups
}

Sure:

object User "xxx" {
  groups = [ "Linux", "Windows", "SQL" ]

assign where ! (“no-email-notification” in host.vars.notification.groups)

But those notification groups must be defined in host definition. I think this is not the same as “if user is not in some UserGroup, than dont use mail-icingaadmin”.

I’m trying different approach now. In every user which shouldn’t receive email alerts, i added var mailalert:

object User "test" {
        import "generic-user"
        display_name = "TEST monitor"
        groups = [ "test" ]
        period = "24x7"
        vars.mailalert = "yes"
        email = "test@test.si"
        pager = "-7603352343|6487682502:asd3wegwrg"
}

And then in notifications:

apply Notification "mail-icingaadmin" to Service {
  import "mail-service-notification"
  user_groups = host.vars.notification.groups
  assign where user.vars.mailalert == "yes"
}

But I get this error:
Aug 11 11:50:03 icinga2.test.lala.com icinga2[234335]: [2023-08-11 11:50:02 +0200] critical/config: Error: Error while evaluating expression: Tried to access undefined script variable ‘user’.

So, how can I pass the value of “vars.mailalert” from users.conf to Notifications?

This approach seems very logicaly to me but I dont know how to pass vars.mailalert value to Notification apply condition.