Skip to content

Instantly share code, notes, and snippets.

@davidnewhall
Created September 21, 2017 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidnewhall/42ed98f9d08375d99b14e0ebd5ce3bbd to your computer and use it in GitHub Desktop.
Save davidnewhall/42ed98f9d08375d99b14e0ebd5ce3bbd to your computer and use it in GitHub Desktop.
Simple Nagios Notification Handler to Send Pretty Slack Notifications
#!/bin/bash
# Send Nagios alert to a Slack Channel.
# Add something like this to Nagios commands.cfg:
#define command {
# command_name notify-host-by-slack
# command_line $USER2$/notify-slack.sh HOST "$CONTACTPAGER$" "$HOSTNAME$" "$TIMET$" "$NOTIFICATIONTYPE$" "$HOSTSTATE$" "$HOSTOUTPUT$" "$LONGHOSTOUTPUT$" "$HOSTNOTESURL$"
# register 1
#}
#define command {
# command_name notify-service-by-slack
# command_line $USER2$/notify-slack.sh SERVICE "$CONTACTPAGER$" "$HOSTNAME$" "$TIMET$" $NOTIFICATIONTYPE$ $SERVICESTATE$ "$SERVICEOUTPUT$" "$LONGSERVICEOUTPUT$" "$SERVICENOTESURL$" "$SERVICEDESC$"
# register 1
#}
#And add something like this to your contact/template:
#host_notification_commands notify-host-by-slack
#service_notification_commands notify-service-by-slack
#pager #slack_channel_name
declare -A ICONS
ICONS[WARNING]="warning"
ICONS[CRITICAL]="x"
ICONS[OK]="white_check_mark"
ICONS[UNKNOWN]="question"
ICONS[UP]="white_check_mark"
ICONS[DOWN]="x"
ICONS[UNREACHABLE]="x"
declare -A COLORS
COLORS[WARNING]="warning"
COLORS[CRITICAL]="danger"
COLORS[OK]="good"
COLORS[UNKNOWN]="#7E01EE" #purple
COLORS[UP]="good"
COLORS[DOWN]="danger"
COLORS[UNREACHABLE]="danger"
# The hostname is added to the end of this URL.
NAGIOS_URL="https://your.server/thruk/#cgi-bin/status.cgi?host="
SLACK_URL="<fill this in>"
# Slack will re-render and cache these icons.
NAGIOS_ICON="https://pbs.twimg.com/profile_images/2652489899/01344a9dba8a62a1e5d4312b6b3afb1d_200x200.png"
RUNBOOK_ICON="https://confluence.atlassian.com/conf53/files/411109329/411174577/1/1380772449002/Confluence_appicon.png"
TYPE="${1}"
CHANNEL="${2}"
HOSTNAME="${3}"
TIME="${4}"
NOTIFICATIONTYPE="${5}"
STATE="${6}"
OUTPUT="${7}"
LONGOUTPUT=${8}
WIKI_URL="${9}"
SERVICENAME="${10}"
send_slack_msg() {
FOOTER="<${WIKI_URL}|Runbook>"
FOOTER_ICON="${RUNBOOK_ICON}"
if [ -z "$WIKI_URL"]; then
FOOTER="${TYPE} Missing Runbook"
fi
TEXT="${OUTPUT} \`\`\`${LONGOUTPUT}\`\`\`"
test -z "$LONGOUTPUT" && TEXT="${OUTPUT}"
curl -X POST "${SLACK_URL}" --data "payload={ \
\"channel\": \"${CHANNEL}\", \
\"attachments\": [{ \
\"color\": \"${COLORS[$STATE]}\", \
\"title\": \"${1}\", \
\"title_link\": \"${NAGIOS_URL}${HOSTNAME}\", \
\"pretext\": \":${ICONS[$STATE]}: *${NOTIFICATIONTYPE}*: <${NAGIOS_URL}${HOSTNAME}|${HOSTNAME}> \", \
\"text\": \"${TEXT}\", \
\"ts\": \"$TIME\", \
\"footer\": \"${FOOTER} - Nagios Time:\", \
\"thumb_url\": \"${NAGIOS_ICON}\", \
\"footer_icon\": \"${FOOTER_ICON}\", \
\"mrkdwn_in\": [\"text\",\"pretext\"] \
}] \
}"
}
# Start here.
if [ "$TYPE" = "HOST" ]; then
send_slack_msg "Host '${HOSTNAME}' is ${STATE}!"
elif [ "$TYPE" = "SERVICE" ]; then
send_slack_msg "Service '${SERVICENAME}' is ${STATE}!"
else
echo "This script should be used with a command in Nagios. See script comments."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment