forked from ericoc/zabbix-slack-alertscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.sh
35 lines (29 loc) · 1.54 KB
/
slack.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/bash
# Slack incoming web-hook user name
username='Zabbix'
## Values received by this script:
# Url = $1 (example: https://hooks.slack.com/services/QW3R7Y/D34DC0D3/BCADFGabcDEF123)
# To = $2 (Slack channel or user to send the message to, specified in the Zabbix web interface; "@username" or "#channel")
# Subject = $3 (usually either PROBLEM or RECOVERY/OK)
# Message = $4 (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 or user ($1) and Zabbix subject ($2 - hopefully either PROBLEM or RECOVERY/OK)
url="$1"
to="$2"
subject="$3"
# Change message emoji depending on the subject - smile (RECOVERY/OK), frowning (PROBLEM), or ghost (for everything else)
recoversub='^RECOVER(Y|ED)?$'
if [[ "$subject" =~ ${recoversub} ]]; then
emoji=':smile:'
elif [ "$subject" == 'OK' ]; then
emoji=':smile:'
elif [ "$subject" == 'PROBLEM' ]; then
emoji=':frowning:'
else
emoji=':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}: $4"
# Build our JSON payload and send it as a POST request to the Slack incoming web-hook URL
payload="payload={\"channel\": \"${to//\"/\\\"}\", \"username\": \"${username//\"/\\\"}\", \"text\": \"${message//\"/\\\"}\", \"icon_emoji\": \"${emoji}\"}"
curl -m 5 --data-urlencode "${payload}" $url -A 'zabbix-slack-alertscript / https://github.com/ericoc/zabbix-slack-alertscript'