blob: 7c5e205e9fe0e5e7e70065244cea6e4afbbfd125 [file] [log] [blame]
Tony Mack81b1e7f2016-05-10 16:27:55 +00001#!/bin/bash
2
3# This script is used by Nagios to post alerts into a Slack channel
4# using the Incoming WebHooks integration. Create the channel, botname
5# and integration first and then add this notification script in your
6# Nagios configuration.
7#
8# All variables that start with NAGIOS_ are provided by Nagios as
9# environment variables when an notification is generated.
10# A list of the env variables is available here:
11# http://nagios.sourceforge.net/docs/3_0/macrolist.html
12#
13# More info on Slack
14# Website: https://slack.com/
15# Twitter: @slackhq, @slackapi
16#
17# My info
18# Website: http://matthewcmcmillan.blogspot.com/
19# Twitter: @matthewmcmillan
20
21#Modify these variables for your environment
22MY_NAGIOS_HOSTNAME="" # This server's hostname
23SLACK_HOSTNAME=""
24SLACK_CHANNEL="#alerts"
25SLACK_BOTNAME="nagios"
26WEBHOOK_URL="" # Incomming webhook url for the slack account
27
28#Set the message icon based on Nagios service state
29if [ "$NAGIOS_SERVICESTATE" = "CRITICAL" ]
30then
31 ICON=":exclamation:"
32elif [ "$NAGIOS_SERVICESTATE" = "WARNING" ]
33then
34 ICON=":warning:"
35elif [ "$NAGIOS_SERVICESTATE" = "OK" ]
36then
37 ICON=":white_check_mark:"
38elif [ "$NAGIOS_SERVICESTATE" = "UNKNOWN" ]
39then
40 ICON=":question:"
41else
42 ICON=":white_medium_square:"
43fi
44
45#Send message to Slack
46curl -X POST --data-urlencode "payload={\"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_USERNAME}\", \"text\": \"${ICON} HOST: ${MY_NAGIOS_HOSTNAME} SERVICE: ${NAGIOS_SERVICEDISPLAYNAME} MESSAGE: ${NAGIOS_SERVICEOUTPUT} <https://${MY_NAGIOS_HOSTNAME}/cgi-bin/nagios3/extinfo.cgi?type=1&host=${NAGIOS_HOSTNAME}|See Nagios>\"}" $WEBHOOK_URL