fuel-plugin-ci/puppet-manifests/modules/fuel_project/templates/zabbix/slack.sh.erb

43 lines
1.5 KiB
Plaintext

#!/bin/bash
#
# Original sources: https://github.com/ericoc/zabbix-slack-alertscript
# Author: https://github.com/ericoc
#
set -e
# Slack incoming web-hook URL and user name
url='<%= @slack_web_hook_url %>'
username='<%= @slack_post_username %>'
## Values received by this script:
# To/Channel = $1 (Slack channel to send the message to, specified in the Zabbix
# web interface)
# Subject = $2 (usually either PROBLEM or RECOVERY)
# Message = $3 (whatever message the Zabbix action sends, preferably something
# like "Zabbix server is unreachable for 5 minutes - Zabbix server (127.0.0.1)")
# Get the Slack channel ($1) and Zabbix subject ($2 - hopefully either PROBLEM
# or RECOVERY)
channel="${1}"
subject="${2}"
# Change message emoji depending on the subject - smile (RECOVERY), frowning
# (PROBLEM), or ghost (for everything else)
if [ "${subject}" == 'OK' ]; then
emoji='<%= @slack_emoji_ok || ':smile:' %>'
elif [ "${subject}" == 'PROBLEM' ]; then
emoji='<%= @slack_emoji_problem || ':frowning:' %>'
else
emoji='<%= @slack_emoji_unknown || ':ghost:' %>'
fi
# The message that we want to send to Slack is the "subject" value ($2 /
# $subject - that we got earlier)
# followed by the message that Zabbix actually sent us ($3)
message="${subject}: ${3}"
# Build our JSON payload and send it as a POST request to the Slack incoming
# web-hook URL
payload="payload={\"channel\": \"${channel}\", \"username\": \"${username}\",\"text\": \"${message}\", \"icon_emoji\": \"${emoji}\"}"
curl -m 5 --data-urlencode "${payload}" "${url}"