Sending Icinga2 notifications to Slack


(watermelon) #1

Integrating Icinga2 with Slack for notifications

Source:
GitHub - nisabek/icinga2-slack-notifications

Introduction

It’s good to have more than one form of notification with Icinga alerts, whether that’s through email, SMS, on-screen (dashboard), and through messaging applications like WhatsApp, Viber, and Slack. I wasn’t able to integrate WhatsApp with Icinga notifications because a recent update (as of 6/15/18) supposedly broke the library (GitHub - Yowsup) that allows for transmission of automated messages to WhatsApp. As for Viber, they have been making some changes as well and are removing the previous method of “chatbotting” (with Public Accounts) and moving forward with a new feature they call Viber Communities. This feature has not come out yet, so I decided to try to work with Slack. Perhaps in the future, I can get to writing guides for Viber and WhatsApp integrations.

I found that somebody had already integrated Slack into their workflow and shared it on GitHub, so I decided to write up a guide outlining the specific steps for how to get everything to work.

Requirements

  • Assuming you have Icinga2 up and running, all you need is curl!

Installation

Setting Slack up

  1. Create a Slack workspace
    • Within the workspace, create a new channel for Icinga notifications
  2. Go to https://api.slack.com/ to create an “App” for your workspace
    • This will be access the Slack API
    • Make sure it’s created under the newly created Slack workspace
  3. Add a web hook to the new Slack App
    • Look for the Features tab in the left-hand column and go to Incoming Webhooks
    • Switch the Active Incoming Webhooks slider to “On”
    • Click Add New Webhook to Workspace at the bottom of the page
      • Authorize the webhook to post to the previously created Icinga notifications channel within Slack
  4. Test out the web hook to make sure it works by using the sample curl request provided after the web hook is created
    • The command will look something like this:
curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}' https://hooks.slack.com/services/BLAHBLAH/BLAHWATERMELONBLAH/BLAHBLAHBLAH
  • Then, you should see a message in the Icinga notifications channel saying “Hello, World!”

Adding the config files

  1. Clone the repository containing the config files for the Slack notifications to your Icinga server
    • git clone https://github.com/nisabek/icinga2-slack-notifications.git
    • Note: there is an alternative way to install the icinga2-slack-notifications package with Debian, details located on nisabek’s repo
  2. Copy the slack-notifications-user-configuration.conf.template
    • cp slack-notifications/slack-notifications-user-configuration.conf.template slack-notifications/slack-notifications-user-configuration.conf
  3. In slack-notifications-user-configuration.conf, change the lines that require your Slack web hook URL as well as your Icinga2 base URL
template Notification "slack-notifications-user-configuration" {
    import "slack-notifications-default-configuration"

    vars.slack_notifications_webhook_url = "<YOUR SLACK WEBHOOK URL>, e.g. https://hooks.slack.com/services/TOKEN1/TOKEN2"
    vars.slack_notifications_icinga2_base_url = "<YOUR ICINGA2 BASE URL>, e.g. http://icinga-web.yourcompany.com/icingaweb2"
}
  • Note: You’ll find the web hook URL in that sample curl command from before
  1. Ensure the permissions are correct for the config files
chmod -R root:root slack-notifications/*
chmod 0640 slack-notifications/*
  • Note: This may vary depending on your setup
  1. Ensure the checker, command, and notification features are enabled through icinga2 feature list
    • If you need to enable a feature, do icinga2 feature enable FEATURE_NAME

Customizing to your environment

  1. To enable Slack notifications on a Host or Service, simply add the slack_notifications variable set to enabled to that object
    • vars.slack_notifications = “enabled”
    • You could do this at the template level as well to have your Host and Services inherit this property
  2. If you have a distributed environment, you’ll probably want to put this config (all three files in the slack-notifications directory) within the global-templates zone. Otherwise, you can put it in the conf.d directory.

Conclusion

And that’s it! You should now be able to test (using a custom notification for example) the sending of notifications directly to Slack via curl and the Slack API.

FAQ

There is more information about how everything works at nisabek’s icinga2-slack-notifications repository if you were curious. Lemme know if you have any questions and I can try to answer them.


How to create that command in Director?
(Gergo Schwahofer) #2

Any way to make this work trough Director? Since years nobody was able to solve that and make a tutorial or Guide, it would help for thousands of people as I see the google finds.

Thank you.


(watermelon) #3

Have you tried to do it yourself yet? If so, what problems are you having? If not, why not? You should be able to apply the principles covered in this howto for Director as well.


(Gergo Schwahofer) #4

I solved it, the trick was to Import to Director with Kickstart Run. ENV’s are not allowed in Director, so you cannot create notification scripts like this SLACK in Director, maybe it will be included in a newer version.