#!/bin/bash
WEBHOST_NAGIOS="<Host that nagios is runing on>"
SLACK_CHANNEL="#<Channel name>"
SLACK_BOTNAME="<Bot Name>"
WEBHOOK_URL="<Webhook URL>" #Get it from Slack Incoming WebHooks setup instruction
#Set the message icon based on Nagios service state
if [ "$NAGIOS_SERVICESTATE" = "OK" ]
then
ICON_EMOJI=":thumbsup:"
elif [ "$NAGIOS_SERVICESTATE" = "WARNING" ]
then
ICON_EMOJI=":loundspeaker:"
elif [ "$NAGIOS_SERVICESTATE" = "CRITICAL" ]
then
ICON_EMOJI=":broken_heart:"
elif [ "$NAGIOS_SERVICESTATE" = "UNKNOWN" ]
then
ICON_EMOJI=":troll:"
else
ICON_EMOJI=":octocat:"
fi
#request for posting to a channel
curl -X POST --data "payload={\"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_BOTNAME}\", \"icon_emoji\": \":vertical_traffic_light:\", \"text\": \"${ICON_EMOJI} HOST: ${NAGIOS_HOSTNAME} SERVICE: ${NAGIOS_SERVICEDISPLAYNAME} STATE: ${NAGIOS_SERVICESTATE} MESSAGE: ${NAGIOS_SERVICEOUTPUT} <http://${WEBHOST_NAGIOS}/cgi-bin/nagios3/extinfo.cgi?host=${NAGIOS_HOSTNAME}|See Nagios>\"}" ${WEBHOOK_URL}