Autor: @watermelon
Original: https://monitoring-portal.org/t/sending-icinga2-notifications-to-slack/2901
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
- Create a Slack workspace
- Within the workspace, create a new channel for Icinga notifications
- 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
- 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
- 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
- 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
- Copy the
slack-notifications-user-configuration.conf.template
cp slack-notifications/slack-notifications-user-configuration.conf.template slack-notifications/slack-notifications-user-configuration.conf
- 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
- 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
- Ensure the
checker
,command
, andnotification
features are enabled throughicinga2 feature list
- If you need to enable a feature, do
icinga2 feature enable FEATURE_NAME
- If you need to enable a feature, do
Customizing to your environment
- To enable Slack notifications on a Host or Service, simply add the
slack_notifications
variable set toenabled
to that objectvars.slack_notifications = “enabled”
- You could do this at the template level as well to have your Host and Services inherit this property
- If you have a distributed environment, you’ll probably want to put this config (all three files in the
slack-notifications
directory) within theglobal-templates
zone. Otherwise, you can put it in theconf.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.
Notification use wrong options / variables
If you can’t override options or variables to your script by setting macros in Icinga 2, make sure that you don’t have any defaults or overrides in your notification script. While this is rather obvious we see it a lot that people tend to forget about the contents of the script itself.