object NotificationCommand "xm-host-notification" {
  command = [ SysconfDir + "/icinga2/scripts/xm-host-notification.sh" ]

  env = {
    HOSTNAME = "$host.name$"
    HOSTALIAS = "$host.display_name$"
    HOSTADDRESS = "$address$"
    HOSTSTATE = "$host.state$"
    HOSTSTATEID = "$host.state_id$"
    HOSTSTATETYPE = "$host.state_type$"
    HOSTCHECKATTEMPT = "$host.check_attempt$"
    HOSTMAXCHECKATTEMPTS = "$host.max_check_attempts$"
    HOSTLASTSTATE = "$host.last_state$"
    HOSTLASTSTATEID = "$host.last_state_id$"
    HOSTLASTSTATETYPE = "$host.last_state_type$"
    HOSTLASTSTATECHANGE = "$host.last_state_change$"
    HOSTDOWNTIMEDEPTH = "$host.downtime_depth$"
    HOSTDURATIONSEC = "$host.duration_sec$"
    HOSTLATENCY = "$host.latency$"
    HOSTEXECUTIONTIME = "$host.execution_time$"
    HOSTOUTPUT = "$host.output$"
    HOSTPERFDATA = "$host.perfdata$"
    HOSTLASTCHECK = "$host.last_check$"
    HOSTCHECKSOURCE = "$host.check_source$"
    HOSTNUMSERVICES = "$host.num_services$"
    HOSTNUMSERVICESOK = "$host.num_services_ok$"
    HOSTNUMSERVICESWARNING = "$host.num_services_warning$"
    HOSTNUMSERVICESUNKNOWN = "$host.num_services_unknown$"
    HOSTNUMSERVICESCRITICAL = "$host.num_services_critical$"
    NOTIFICATIONTYPE = "$notification.type$"
    NOTIFICATIONAUTHORNAME = "$notification.author$"
    NOTIFICATIONCOMMENT = "$notification.comment$"
    TIMET = "$icinga.timet$"
    LONGDATETIME = "$icinga.long_date_time$"
    SHORTDATETIME = "$icinga.short_date_time$"
    DATE = "$icinga.date$"
    TIME = "$icinga.time$"
    UPTIME = "$icinga.uptime$"
    NUMSERVICESOK = "$icinga.num_services_ok$"
    NUMSERVICESWARNING = "$icinga.num_services_warning$"
    NUMSERVICESCRITICAL = "$icinga.num_services_critical$"
    NUMSERVICESUNKNOWN = "$icinga.num_services_unknown$"
    NUMSERVICESPENDING = "$icinga.num_services_pending$"
    NUMSERVICESUNREACHABLE = "$icinga.num_services_unreachable$"
    NUMSERVICESFLAPPING = "$icinga.num_services_flapping$"
    NUMSERVICESINDOWNTIME = "$icinga.num_services_in_downtime$"
    NUMSERVICESACKNOWLEDGED = "$icinga.num_services_acknowledged$"
    NUMHOSTSUP = "$icinga.num_hosts_up$"
    NUMHOSTSDOWN = "$icinga.num_hosts_down$"
    NUMHOSTSUNREACHABLE = "$icinga.num_hosts_unreachable$"
    NUMHOSTSPENDING = "$icinga.num_hosts_pending$"
    NUMHOSTSFLAPPING = "$icinga.num_hosts_flapping$"
    NUMHOSTSINDOWNTIME = "$icinga.num_hosts_in_downtime$"
    NUMHOSTSACKNOWLEDGED = "$icinga.num_hosts_acknowledged$"
    XM_INTEGRATION_URL = "Replace this text with the URL from your configuration page in xMatters"
  }
}

object NotificationCommand "xm-service-notification" {
  command = [ SysconfDir + "/icinga2/scripts/xm-service-notification.sh" ]

  env = {
    HOSTNAME = "$host.name$"
    HOSTALIAS = "$host.display_name$"
    HOSTADDRESS = "$address$"
    HOSTSTATE = "$host.state$"
    HOSTSTATEID = "$host.state_id$"
    HOSTSTATETYPE = "$host.state_type$"
    HOSTCHECKATTEMPT = "$host.check_attempt$"
    HOSTMAXCHECKATTEMPTS = "$host.max_check_attempts$"
    HOSTLASTSTATE = "$host.last_state$"
    HOSTLASTSTATEID = "$host.last_state_id$"
    HOSTLASTSTATETYPE = "$host.last_state_type$"
    HOSTLASTSTATECHANGE = "$host.last_state_change$"
    HOSTDOWNTIMEDEPTH = "$host.downtime_depth$"
    HOSTDURATIONSEC = "$host.duration_sec$"
    HOSTLATENCY = "$host.latency$"
    HOSTEXECUTIONTIME = "$host.execution_time$"
    HOSTOUTPUT = "$host.output$"
    HOSTPERFDATA = "$host.perfdata$"
    HOSTLASTCHECK = "$host.last_check$"
    HOSTCHECKSOURCE = "$host.check_source$"
    HOSTNUMSERVICES = "$host.num_services$"
    HOSTNUMSERVICESOK = "$host.num_services_ok$"
    HOSTNUMSERVICESWARNING = "$host.num_services_warning$"
    HOSTNUMSERVICESUNKNOWN = "$host.num_services_unknown$"
    HOSTNUMSERVICESCRITICAL = "$host.num_services_critical$"
    SERVICEDESC = "$service.name$"
    SERVICEDISPLAYNAME = "$service.display_name$"
    SERVICECHECKCOMMAND = "$service.check_command$"
    SERVICESTATE = "$service.state$"
    SERVICESTATEID = "$service.state_id$"
    SERVICESTATETYPE = "$service.state_type$"
    SERVICECHECKATTEMPT = "$service.check_attempt$"
    SERVICEMAXCHECKATTEMPTS = "$service.max_check_attempts$"
    SERVICELASTSTATE = "$service.last_state$"
    SERVICELASTSTATEID = "$service.last_state_id$"
    SERVICELASTSTATETYPE = "$service.last_state_type$"
    SERVICELASTSTATECHANGE = "$service.last_state_change$"
    SERVICEDOWNTIMEDEPTH = "$service.downtime_depth$"
    SERVICEDURATIONSEC = "$service.duration_sec$"
    SERVICELATENCY = "$service.latency$"
    SERVICEEXECUTIONTIME = "$service.execution_time$"
    SERVICEOUTPUT = "$service.output$"
    SERVICEPERFDATA = "$service.perfdata$"
    SERVICELASTCHECK = "$service.last_check$"
    SERVICECHECKSOURCE = "$service.check_source$"
    NOTIFICATIONTYPE = "$notification.type$"
    NOTIFICATIONAUTHORNAME = "$notification.author$"
    NOTIFICATIONCOMMENT = "$notification.comment$"
    TIMET = "$icinga.timet$"
    LONGDATETIME = "$icinga.long_date_time$"
    SHORTDATETIME = "$icinga.short_date_time$"
    DATE = "$icinga.date$"
    TIME = "$icinga.time$"
    UPTIME = "$icinga.uptime$"
    NUMSERVICESOK = "$icinga.num_services_ok$"
    NUMSERVICESWARNING = "$icinga.num_services_warning$"
    NUMSERVICESCRITICAL = "$icinga.num_services_critical$"
    NUMSERVICESUNKNOWN = "$icinga.num_services_unknown$"
    NUMSERVICESPENDING = "$icinga.num_services_pending$"
    NUMSERVICESUNREACHABLE = "$icinga.num_services_unreachable$"
    NUMSERVICESFLAPPING = "$icinga.num_services_flapping$"
    NUMSERVICESINDOWNTIME = "$icinga.num_services_in_downtime$"
    NUMSERVICESACKNOWLEDGED = "$icinga.num_services_acknowledged$"
    NUMHOSTSUP = "$icinga.num_hosts_up$"
    NUMHOSTSDOWN = "$icinga.num_hosts_down$"
    NUMHOSTSUNREACHABLE = "$icinga.num_hosts_unreachable$"
    NUMHOSTSPENDING = "$icinga.num_hosts_pending$"
    NUMHOSTSFLAPPING = "$icinga.num_hosts_flapping$"
    NUMHOSTSINDOWNTIME = "$icinga.num_hosts_in_downtime$"
    NUMHOSTSACKNOWLEDGED = "$icinga.num_hosts_acknowledged$"
    XM_INTEGRATION_URL = "Replace this text with the URL from your configuration page in xMatters"
  }
}

template Notification "xm-host-notification" {
  command = "xm-host-notification"

  states = [ Up, Down ]
  types = [ Problem, Acknowledgement, Recovery, Custom,
            FlappingStart, FlappingEnd,
            DowntimeStart, DowntimeEnd, DowntimeRemoved ]

  period = "24x7"
}

template Notification "xm-service-notification" {
  command = "xm-service-notification"

  states = [ OK, Warning, Critical, Unknown ]
  types = [ Problem, Acknowledgement, Recovery, Custom,
            FlappingStart, FlappingEnd,
            DowntimeStart, DowntimeEnd, DowntimeRemoved ]

  period = "24x7"
}

apply Notification "xm-notification" to Host {
  import "xm-host-notification"

  user_groups = host.vars.notification.xm.groups
  users = host.vars.notification.xm.users

  assign where host.vars.notification.xm
}

apply Notification "xm-notification" to Service {
  import "xm-service-notification"

  user_groups = host.vars.notification.xm.groups
  users = host.vars.notification.xm.users

  assign where host.vars.notification.xm
}
