Skip to content

Instantly share code, notes, and snippets.

@bbuivn
Forked from matt448/slack_nagios.sh
Last active March 21, 2017 17:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbuivn/d3b07af1da6ff1a2a806 to your computer and use it in GitHub Desktop.
Save bbuivn/d3b07af1da6ff1a2a806 to your computer and use it in GitHub Desktop.
Script to post Shinken service notifications into a Slack channel
#!/bin/bash
# This script is used by Shinken to post alerts for services into a Slack channel
# using the slackcli command line. Create the channel,
# and integration first and then add this notification script in your
# Shinken configuration.
#
# You have to set enable_environment_macros=1 in the shinken.cfg configuration file.
#
# All variables that start with NAGIOS_ are provided by Shinken as
# environment variables when a notification is generated.
# A list of the env variables is available here:
# http://shinken.readthedocs.org/en/latest/05_thebasics/macrolist.html
#
# More info on Slack
# Website: https://slack.com/
# Twitter: @slackhq, @slackapi
#
# More info on slackcli
# Github: https://github.com/candrholdings/slack-cli
#Modify these variables for your environment
SLACKCLI=$( which slackcli )
SLACK_TOKEN="<your slack token>"
SLACK_USER="$( hostname -f )"
SLACK_CHANNEL="shinken"
BOT_ICON=":shinto_shrine:"
#Set the message icon based on Nagios service state
if [ "$NAGIOS_SERVICESTATE" = "CRITICAL" ]
then
ICON=":red_circle:"
elif [ "$NAGIOS_SERVICESTATE" = "WARNING" ]
then
ICON=":warning:"
elif [ "$NAGIOS_SERVICESTATE" = "OK" ]
then
ICON=":white_check_mark:"
elif [ "$NAGIOS_SERVICESTATE" = "UNKNOWN" ]
then
ICON=":question:"
else
ICON=":white_medium_square:"
fi
#Send message to Slack
"${SLACKCLI}" -t "${SLACK_TOKEN}" -u "${SLACK_USER}" -h "${SLACK_CHANNEL}" -e "${BOT_ICON}" -m "${ICON} Type: ${NAGIOS_NOTIFICATIONTYPE}
${ICON} Service: ${NAGIOS_SERVICEDESC}
${ICON} Host: ${NAGIOS_HOSTNAME}
${ICON} State: ${NAGIOS_SERVICESTATE}
${ICON} Infos: ${NAGIOS_SERVICEOUTPUT}
${ICON} Date: ${NAGIOS_LONGDATETIME}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment