Integrate Slack and Icinga2

- 3 mins

This was originally posted by me on Glogster dev blog.

Since we’ve been using Slack for communication inside our team, I’ve started to play with different integrations. There are two ways to integrate Slack with Nagios-based monitoring systems. The first one is to use general connectors and write a custom script. The second one is to use the built-in Slack bot for Nagios. This option is better, because it gives a better integration level. In Slack, the documentation only describes how to setup this bot for Nagios or Icinga1 and there is nothing about Icinga2.

So, to setup Nagios integration follow these steps:

yum install perl-libwww-perl perl-Crypt-SSLeay perl-LWP-Protocol-https
wget https://raw.github.com/tinyspeck/services-examples/master/nagios.pl -O /etc/icinga2/scripts/slack_nagios.pl
chmod 755 /etc/icinga2/scripts/slack_nagios.pl
object NotificationCommand "slack-host-notification" {
  import "plugin-notification-command"

  command = [ SysconfDir + "/icinga2/scripts/slack_nagios.pl" ]
  arguments = {
    "-field" = "$user.vars.host_fields$"

  }
}


object NotificationCommand "slack-service-notification" {
  import "plugin-notification-command"

  command = [ SysconfDir + "/icinga2/scripts/slack_nagios.pl" ]
  arguments = {
    "-field" = "$user.vars.service_fields$"

  }
}

If you pass env variables with this command, Icinga2 will not propagate it to the script, so you must pass everything with -field key. If you only pass the slack_channel value to the script, Nagios bot will post only empty messages to the channel.

If you define multiple entries for -field key, Icinga2 will only use the last one, suppressing the others. Here you’ll need to define an array with those fields.

[2015-08-31 22:36:50 +0200] notice/Process: PID 19582 ('/etc/icinga2/scripts/slack_nagios.pl' '-field' 'NOTIFICATIONTYPE=RECOVERY') terminated with exit code 0

And surprisingly, you can’t define an array in the NotificationCommand definition, so something like this "-field" = ["a", "b, "c"] will give you nothing but errors.

However, you can define an array in another object definition connected to the NotificationCommand. We’ll define this variables in User object.

object User "slack" {
  display_name = "Slack"
  import "generic-user"
  vars.host_fields = ["slack_channel=#ops", "HOSTALIAS=$host.display_name$", "HOSTSTATE=$host.state$", "HOSTOUTPUT=$host.output$", "NOTIFICATIONTYPE=$notification.type$"]
  vars.service_fields = ["slack_channel=#ops", "HOSTALIAS=$host.display_name$", "SERVICEDESC=$service.name$", "SERVICESTATE=$service.state$", "SERVICEOUTPUT=$service.output$", "NOTIFICATIONTYPE=$notification.type$"]
}

Replace #ops with the name of your channel.

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

  users = [ "slack"  ]

  assign where host.vars.sla == "24x7"

}

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

  users = [ "slack"  ]

  assign where service.vars.sla == "24x7"

}

and this to /etc/icinga2/conf.d/templates.conf:

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

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

  period = "24x7"

}

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

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

  period = "24x7"

}
[2015-09-01 02:31:11 +0200] notice/Process: PID 12654 ('/etc/icinga2/scripts/slack_nagios.pl' '-field' 'slack_channel=#ops' '-field' 'HOSTALIAS=edu01' '-field' 'SERVICEDESC=load' '-field' 'SERVICESTATE=OK' '-field' 'SERVICEOUTPUT=OK - load average: 4.72, 5.71, 7.90' '-field' 'NOTIFICATIONTYPE=RECOVERY') terminated with exit code 0

Enjoy your teamwork!

Roman Komkov

Roman Komkov

Creating and maintaining modern solid infrastructures

comments powered by Disqus
rss facebook twitter github youtube mail spotify instagram linkedin google pinterest medium