Last active
November 13, 2020 15:28
-
-
Save asc-adean/3934e96fccf0eb31ec3c786446b93eb6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Slack incoming web-hook URL and user name | |
url=$1 # example: url='https://hooks.slack.com/services/QW3R7Y/D34DC0D3/BCADFGabcDEF123' | |
username='Zabbix' | |
## Values received by this script: | |
# To = $1 / Slack channel or user to send the message to, specified in the Zabbix web interface; "@username" or "#channel" | |
# Subject = $2 / subject of the message sent by Zabbix; by default, it is usually something like "(Problem|Resolved): Lack of free swap space on Zabbix server" | |
# Message = $3 / message body sent by Zabbix; by default, it is usually approximately 4 lines detailing the specific trigger involved | |
# Alternate URL = $4 (optional) / alternative Slack.com web-hook URL to replace the above hard-coded one; useful when multiple groups have seperate Slack teams | |
# Proxy = $5 (optional) / proxy host including port (such as "example.com:8080") | |
# Get the user/channel ($1), subject ($2), and message ($3) | |
to="$2" | |
subject="$3" | |
message="$4" | |
# Change message emoji and notification color depending on the subject indicating whether it is a trigger going in to problem state or recovering | |
recoversub='^RECOVER(Y|ED)?$|^OK$|^Resolved.*' | |
problemsub='^PROBLEM.*|^Problem.*' | |
if [[ "$subject" =~ $recoversub ]]; then | |
emoji=':smile:' | |
color='#0C7BDC' | |
elif [[ "$subject" =~ $problemsub ]]; then | |
emoji=':frowning:' | |
color='#FFC20A' | |
else | |
emoji=':question:' | |
color='#CCCCCC' | |
fi | |
payload="payload={\"channel\": \"${to//\"/\\\"}\", \ | |
\"username\": \"${username//\"/\\\"}\", \ | |
\"title\": \"${subject//\"/\\\"}\", \ | |
\"text\": \"${message//\"/\\\"}\", \ | |
\"color\": \"${color}\", \ | |
\"icon_emoji\": \"${emoji}\"}" | |
#Request of the payload to Slack via curl, storing stdout (the response body) | |
return=$(curl $proxy -sm 5 --data-urlencode "${payload}" $url -A 'zabbix-slack-alertscript / https://github.com/ericoc/zabbix-slack-alertscript') | |
# If the response body was not what was expected from Slack ("ok"), something went wrong so print the Slack error to stderr and exit with non-zero | |
if [[ "$return" != 'ok' ]]; then | |
>&2 echo "$return" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment