forked from ericoc/zabbix-slack-alertscript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslack.sh
54 lines (49 loc) · 1.63 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
50
51
52
53
54
#!/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//\"/\\\"}"
case "$subject" in
OK*)
emoji=':white_check_mark:'
# Can either be one of 'good', 'warning', 'danger'}"
color='good'
;;
PROBLEM*)
emoji=':frowning:'
color='danger'
;;
*)
emoji=':ghost:'
color='warning'
;;
esac
# 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
# https://itclubs.slack.com/services/B33333V35#message_attachments
payload=$(cat <<EOF
{
"attachments":
[{
"fallback":"${subject}",
"pretext":"${subject}",
"color":"${color}",
"fields":[{
"title":"${subject}",
"value":"${message}",
"short":false
}]
}]
}
EOF
)
/usr/bin/curl -m 5 -X POST --data "${payload}" $url \
--user-agent 'zabbix-slack-alertscript / https://github.com/ericoc/zabbix-slack-alertscript'