Skip to content

Instantly share code, notes, and snippets.

@lxcodes
Forked from anarchivist/slack_nagios.sh
Last active August 29, 2015 14:15
Show Gist options
  • Save lxcodes/e19f66d0134cee1d4178 to your computer and use it in GitHub Desktop.
Save lxcodes/e19f66d0134cee1d4178 to your computer and use it in GitHub Desktop.
#!/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)
# Modifications by Alexander Ambrose (@al3xnull)
#Modify these variables for your environment
MY_NAGIOS_HOSTNAME="icinga.yourdomain.com"
SLACK_HOSTNAME="domain.slack.com"
SLACK_TOKEN="xyxyxyourslackkey"
SLACK_CHANNEL="#alerts"
SLACK_USERNAME="icinga"
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/xxxxxxxxx/xxxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxx"
#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-urlencode "payload=${PAYLOAD}" ${SLACK_WEBHOOK_URL}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment