-
-
Save anarchivist/db05943dced76e1f80a7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script is used by Icinga to post alerts into a Slack channel | |
# using the Incoming WebHooks integration. Create the channel, botname | |
# and integration first and then add this notification script in your | |
# Icinga configuration. | |
# | |
# All variables that start with NAGIOS_ are provided by Icinga as | |
# environment variables when an notification is generated. | |
# A list of the env variables is available here: | |
# http://nagios.sourceforge.net/docs/3_0/macrolist.html | |
# | |
# More info on Slack | |
# Website: https://slack.com/ | |
# Twitter: @slackhq, @slackapi | |
# | |
# Original author: | |
# Website: matthewmcmillan.blogspot.com | |
# Twitter: @matthewmcmillan | |
# | |
# Modifications by Mark Matienzo (@anarchivist) | |
#Modify these variables for your environment | |
MY_NAGIOS_HOSTNAME="nagios.yourdomain.com" | |
SLACK_HOSTNAME="yourslack.slack.com" | |
SLACK_TOKEN="xyxyxyourslackkey" | |
SLACK_CHANNEL="#alerts" | |
SLACK_USERNAME="nagios" | |
#Set the message icon based on Icinga service state | |
if [ "$ICINGA_SERVICESTATE" = "CRITICAL" ] | |
then | |
ICON=":exclamation:" | |
COLOR="danger" | |
elif [ "$ICINGA_SERVICESTATE" = "WARNING" ] | |
then | |
ICON=":warning:" | |
COLOR="warning" | |
elif [ "$ICINGA_SERVICESTATE" = "OK" ] | |
then | |
ICON=":white_check_mark:" | |
COLOR="good" | |
elif [ "$ICINGA_SERVICESTATE" = "UNKNOWN" ] | |
then | |
ICON=":question:" | |
COLOR="#00FFFF" | |
else | |
ICON=":white_medium_square:" | |
COLOR="#FFFFFF" | |
fi | |
#Send message to Slack | |
PAYLOAD="{\"username\": \"${SLACK_USERNAME}\", \"channel\": \"${SLACK_CHANNEL}\", \"icon_url\": \"https://slack.com/img/services/nagios_48.png\", \"attachments\": [{\"fallback\": \"${ICINGA_NOTIFICATIONTYPE} Service Alert: ${ICINGA_SERVICEDISPLAYNAME} on ${ICINGA_HOSTNAME} is ${ICINGA_SERVICESTATE}\", \"pretext\": \"${ICON} ${ICINGA_NOTIFICATIONTYPE} Service Alert: ${ICINGA_SERVICEDISPLAYNAME} on ${ICINGA_HOSTNAME} is ${ICINGA_SERVICESTATE} - <https://${MY_NAGIOS_HOSTNAME}/icinga|View Icinga>\", \"color\": \"${COLOR}\", \"fields\": [{\"title\": \"Host\", \"value\": \"${ICINGA_HOSTNAME}\", \"short\": true }, {\"title\": \"Service\", \"value\": \"${ICINGA_SERVICEDISPLAYNAME}\", \"short\": true }, {\"title\": \"State\", \"value\": \"${ICINGA_SERVICESTATE}\", \"short\": true }, {\"title\": \"Message\", \"value\": \"${ICINGA_SERVICEOUTPUT}\", \"short\": false }, {\"title\": \"Comment\", \"value\": \"${ICINGA_NOTIFICATIONCOMMENT}\", \"short\": false} ] }]}" | |
curl -sX POST --data "payload=${PAYLOAD}" https://${SLACK_HOSTNAME}/services/hooks/incoming-webhook?token=${SLACK_TOKEN} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment