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 [email protected], 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.

2 Answers 2

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.

7
  • 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, 2016 at 0: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, 2016 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, 2016 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, 2016 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 1, 2016 at 15:22
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .