forked from muokata/zabbix-mattermost-alertscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MattermostWebHooks.sh
67 lines (55 loc) · 2.83 KB
/
MattermostWebHooks.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
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
#Enable DEBUG to '1'
Debug='0'
# Mattermost incoming web-hook URL and user name
url='<incoming webbook uri>' # example: httpsL//mattermost.example.com/hooks/ere5h9gfbbbk8gdxsei1tt8ewewechjsd
username='zabbix'
icon='<icon_url>'
## Values received by this script:
# To = $1 / Mattermost 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|Update): 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")
to="${1}"
subject="${2}"
# The message that we want to send to Mattermost is the "subject" value ($2 / $subject - that we got earlier)
# followed by the message that Zabbix actually sent us ($3)
message="${subject}\n\n${3}"
# Change color emoji depending on the subject - Green (Resolved), Red (Problem)
if [[ "$subject" == *"Resolved"* ]]; then
emoji=':smile:'
color="#1BFF00"
elif [[ "$subject" == *"Problem"* ]]; then
emoji=':frowning:'
color="#ff2a00"
elif [[ "$subject" == *"Updated"* ]]; then
color="#ccff00"
else
# Default color
emoji=':question:'
color="#1258b5"
fi
# Replace the above hard-coded Mattermost web-hook URL entirely, if one was passed via the optional 4th parameter
url=${4-$url}
# Use optional 5th parameter as proxy server for curl
proxy=${5-""}
if [[ "$proxy" != '' ]]; then
proxy="-x $proxy"
fi
## Debug
if [[ "$Debug" == "1" ]]; then
echo "to - $to" >> /tmp/SortieMattermost.txt
echo "sujet - $subject" >> /tmp/SortieMattermost.txt
echo "message - $message" >> /tmp/SortieMattermost.txt
echo "........." >> /tmp/SortieMattermost.txt
fi
# Build our JSON payload and send it as a POST request to the Mattermost incoming web-hook URL
payload="payload={\"icon_url\": \"$icon\", \"attachments\": [ {\"color\": \"${color}\", \"text\": \"${message}\"} ], \"channel\": \"${to}\", \"username\": \"${username}\", \"icon_emoji\": \"${emoji}\"}"
# Execute the HTTP POST request of the payload to Slack via curl, storing stdout (the response body)
return=$(curl $proxy -sm 5 --data-urlencode "${payload}" $url -A 'zabbix-mattermost-alertscript / https://github.com/vincent1890/zabbix-mattermost-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