Integrating Nagios XI alerts to a Slack Channel

Recently, I have been exploring the use of Slack for messaging and collaboration and in particular integration with other tools. In this post I will discuss enabling integration with Nagios XI and providing the functionality to send alerts generated to a channel within Slack. Firstly, you need to have created a login and be an owner or a member of a team. Now, we can browse to the Slack App Directory and search for Nagios to enable and configure.

In order to integrate Nagios and configure the plugin there is a requirement to install the necessary perl modules and to download the plugin to the Nagios XI server, place the plugin in th directory ‘/usr/local/bin’ and change the access permissions to the file.

sudo yum install perl-libwww-perl
sudo yum install perl-Net-SSLeay
cd /tmp 
wget https://raw.github.com/tinyspeck/services-examples/master/nagios.pl
cp nagios.pl /usr/local/bin/slack_nagios.pl
chmod 755 /usr/local/bin/slack_nagios.pl

We will need to edit ‘/usr/local/bin/slack_nagios.pl’ and modify the $opt_domain and $opt_token variables as per your Slack configuration. In the below example, I am using the Slack team domain ‘deangrant.slack.com’ and the token for the Nagios integration has been generated as ‘BIRQpEaFMixAi6LsMMj80bcC’.

my $opt_domain = "dean.slack.com";
my $opt_token = "BIRQpEaFMixAi6LsMMj80bcC"; 

Now we will configure Nagios to define a contact and commands to use for the plugin. In this example, I will use the Slack channel ‘nagiosalerts’ for both the host and service notification command to send messages. Firstly, I will modify the file ‘/usr/local/nagios/etc/contacts.cfg’ to define the contact for slack and specify the host/server notification period, options and commands.

define contact {
      contact_name                             slack
      alias                                    Slack
      service_notification_period              24x7
      host_notification_period                 24x7
      service_notification_options             w,u,c,r
      host_notification_options                d,r
      service_notification_commands            notify-service-by-slack
      host_notification_commands               notify-host-by-slack
}

Now we will define commands for the notification settings by modifying the file ‘/usr/local/nagiosxi/tmp/nagiosxi/subcomponents/nagioscore/mods/cfg/objects/commands.cfg’ and including the following in the notification settings section. As per my example, I am configuring the command_line to use the Slack channel ‘nagiosalerts’ for for both the ‘notify-service-by-slack’ and ‘notify-host-by-slack’ commands.

define command {
      command_name     notify-service-by-slack
      command_line     /usr/local/bin/slack_nagios.pl -field slack_channel=#nagiosalerts
}

define command {
      command_name     notify-host-by-slack
      command_line     /usr/local/bin/slack_nagios.pl -field slack_channel=#nagiosalerts
}

We can also define contact group membership for the contact defined for Slack. In this example, I have modified an existing contact group for the ‘Nagios Administrators’ to include the slack contact as a member by modifying the file ‘/usr/local/nagios/etc/contactgroups.cfg’.

define contactgroup {
  contactgroup_name admins
  alias             Nagios Administrators
  members           nagiosadmin, slack
			}

We also need to ensure that the file ‘/usr/local/nagiosxi/tmp/nagiosxi/subcomponents/nagioscore/mods/cfg/nagios.cfg’ contains the following configuration value to enable environment macros.

enable_environment_macros=1

Finally, to apply the configuration to enable the plugin we will be required to restart Nagios. Following a restart alerts which should now be sent to the Slack channel when generated.

sudo service nagios restart
Advertisements

One thought on “Integrating Nagios XI alerts to a Slack Channel

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s