Tony Mack | 81b1e7f | 2016-05-10 16:27:55 +0000 | [diff] [blame] | 1 | #!/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 |
| 22 | MY_NAGIOS_HOSTNAME="" # This server's hostname |
| 23 | SLACK_HOSTNAME="" |
| 24 | SLACK_CHANNEL="#alerts" |
| 25 | SLACK_BOTNAME="nagios" |
| 26 | WEBHOOK_URL="" # Incomming webhook url for the slack account |
| 27 | |
| 28 | #Set the message icon based on Nagios service state |
| 29 | if [ "$NAGIOS_SERVICESTATE" = "CRITICAL" ] |
| 30 | then |
| 31 | ICON=":exclamation:" |
| 32 | elif [ "$NAGIOS_SERVICESTATE" = "WARNING" ] |
| 33 | then |
| 34 | ICON=":warning:" |
| 35 | elif [ "$NAGIOS_SERVICESTATE" = "OK" ] |
| 36 | then |
| 37 | ICON=":white_check_mark:" |
| 38 | elif [ "$NAGIOS_SERVICESTATE" = "UNKNOWN" ] |
| 39 | then |
| 40 | ICON=":question:" |
| 41 | else |
| 42 | ICON=":white_medium_square:" |
| 43 | fi |
| 44 | |
| 45 | #Send message to Slack |
| 46 | curl -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 |