This repository has been archived by the owner on May 17, 2022. It is now read-only.
forked from ericoc/zabbix-slack-alertscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.sh
executable file
·50 lines (42 loc) · 1.84 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash
# Slack incoming web-hook URL and user name
url='CHANGEME' # example: 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 (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 or user ($1) and Zabbix subject ($2 - hopefully either PROBLEM or RECOVERY)
to="$1"
subject="$2"
# Change message emoji depending on the subject - smile (RECOVERY), frowning (PROBLEM), or ghost (for everything else)
if [[ "$subject" =~ RECOVERY* ]]; then
color="good"
emoji=':suspect:'
elif [[ "$subject" =~ PROBLEM* ]]; then
if [[ "$subject" =~ Warning* ]]; then
color="warning"
emoji=':goberserk:'
elif [[ "$subject" =~ Average* ]]; then
color="#d87047"
emoji=':rage4:'
elif [[ "$subject" =~ High* ]]; then
color="danger"
emoji=':feelsgood:'
elif [[ "$subject" =~ Disaster* ]]; then
color="#000000"
emoji=':finnadie:'
else
color="#0080ff"
emoji=':rage3:'
fi
else
color="warning"
emoji=':godmode:'
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="$3"
# Build our JSON payload and send it as a POST request to the Slack incoming web-hook URL
payload="payload={\"channel\": \"${to}\", \"username\": \"${username}\", \"icon_emoji\": \"${emoji}\", \"attachments\":[{\"color\": \"${color}\", \"fallback\": \"${message}\", \"fields\":[{\"title\":\"${subject}\", \"value\": \"${message}\"}]}]}"
curl -m 5 --data-urlencode "${payload}" $url