0

I'm trying to send different notifications in different time periods, basically I want to send all notifications during work hours, but only notifications for hosts marked emergency during off hours.

apply Notification "mike-on" to Host {
  import "mail-host-notification"
  users = [ "sms" ]
  period = "mikehours"
  assign where host.vars.notification.mail
}

apply Notification "mike-off" to Host {
  import "mail-host-notification"
  users = [ "sms" ]
  period = "nonmikehours"
  assign where host.vars.emergency == true
}

sms is simply a contact with the email set to mynumber@carrier.net, and the time periods are defined as follows

object TimePeriod "mikehours" {
  import "legacy-timeperiod"

  display_name = "Mike's work hours"
  ranges = {
    "monday"    = "06:00-23:00"
    "tuesday"   = "06:00-23:00"
    "wednesday" = "06:00-23:00"
    "thursday"  = "06:00-23:00"
    "friday"    = "06:00-23:00"
    "saturday"  = "07:00-22:00"
    "sunday"    = "07:00-22:00"
  }
}

object TimePeriod "nonmikehours" {
  import "legacy-timeperiod"
  display_name = "Mike's off hours"
  ranges = {
    "monday"    = "00:00-06:00,23:00-24:00"
    "tuesday"   = "00:00-06:00,23:00-24:00"
    "wednesday" = "00:00-06:00,23:00-24:00"
    "thursday"  = "00:00-06:00,23:00-24:00"
    "friday"    = "00:00-06:00,23:00-24:00"
    "saturday"  = "00:00-07:00,22:00-24:00"
    "sunday"    = "00:00-07:00,22:00-24:00"
  }
}

However, I still receive all notifications via sms even outside of the specified work hours.

fusorx
  • 121
  • 7

2 Answers2

1

Apply rules are evaluated on their own. If your assign/ignore where expressions match (as with "assign where host.vars.notification.mail" probably matching all hosts) they will generate a notification object. Verify that by 'object list' or the rest api endpoint /v1/objects/notifications. The second apply rule only matches on your custom attribute being set, and adds an additional notification object. So your comment's solution is perfectly fine.

You can omit the "== true" comparison btw. Icinga 2 assumes that automatically for boolean attributes.

dnsmichi
  • 845
  • 5
  • 12
  • I'm not sure I understand... I understand that it matched all hosts, but shouldn't period still prevent notifications from being sent outside of that time period? – fusorx Mar 24 '16 at 00:55
  • Your initial configuration from the original post did create notification objects for *all* hosts which had "vars.notification.mail" defined. So you would have received sms notification for "mikehours" period no matter what else you define. The additional apply rule "emergency" adds *another* notification. – dnsmichi Mar 28 '16 at 13:07
  • so then what does the period actually change? I understand I created a notification object for every host, and then another on the hosts with emergency defined, but shouldn't period define the period of time in which that notification is sent? – fusorx Mar 29 '16 at 16:10
  • Your two notifications match the entire 24x7 period, one 6-23 on monday e.g. the other one from 0-6 and 23-24. That way you'll always receive notifications from two different periods. Re-think your notification strategy, take pen and paper and draw your config items and their matched patters. – dnsmichi Mar 29 '16 at 22:12
  • I understand that there is always a notification for any given time, but during the night it should only be a notification for certain hosts. – fusorx Apr 01 '16 at 15:22
  • Additionally, the solution I proposed does not seem to be working correctly, upon further inspection it is now sending notifications via text at all times of night – fusorx Apr 01 '16 at 15:27
  • for anyone interested I found the issue in my proposed solution, a simple group assignment mistake that was sending the standard email notifications to the sms user as well – fusorx Apr 04 '16 at 18:36
0

I found this solution that works for me:

apply Notification "mike-on" to Host {
  import "mail-host-notification"

  users = [ "sms" ]

  period = "mikehours"

  assign where host.vars.notification.mail && !host.vars.emergency
}

apply Notification "emergency" to Host {
  import "mail-host-notification"
  users = [ "sms" ]
  period = "24x7"
  assign where host.vars.emergency == true
}

I'd still love an answer as to why my original solution failed, but this is a much better way to do it anyhow.

fusorx
  • 121
  • 7