-
Notifications
You must be signed in to change notification settings - Fork 20
/
slack.sh
executable file
·127 lines (116 loc) · 3.7 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash -x
# config
slack_url='https://hooks.slack.com/services/XXX/XXXX/XXXXX'
slack_username='Zabbix'
channel="$1"
title="$2"
params="$3"
emoji=':ghost:'
timeout="5"
cmd_curl="/usr/bin/curl"
cmd_wget="/usr/bin/wget"
zabbix_baseurl="http://zabbix.example.com"
zabbix_username="yourzabbixusername"
zabbix_password="zabbixpassword"
# chart settings
chart_period=3600
chart_width=1280
chart_height=390
chart_baseurl="${zabbix_baseurl}/slack_charts"
chart_basedir="/tmp/slack_charts"
chart_cookie="/tmp/zcookies.txt"
# set params
host="`echo \"${params}\" | grep 'HOST: ' | awk -F'HOST: ' '{print $2}' | sed -e 's///g'`"
trigger_name="`echo \"${params}\" | grep 'TRIGGER_NAME: ' | awk -F'TRIGGER_NAME: ' '{print $2}' | sed -e 's///g'`"
trigger_status="`echo \"${params}\" | grep 'TRIGGER_STATUS: ' | awk -F'TRIGGER_STATUS: ' '{print $2}' | sed -e 's///g'`"
trigger_severity="`echo \"${params}\" | grep 'TRIGGER_SEVERITY: ' | awk -F'TRIGGER_SEVERITY: ' '{print $2}' | sed -e 's///g'`"
trigger_url="`echo \"${params}\" | grep 'TRIGGER_URL: ' | awk -F'TRIGGER_URL: ' '{print $2}' | sed -e 's///g'`"
datetime="`echo \"${params}\" | grep 'DATETIME: ' | awk -F'DATETIME: ' '{print $2}' | sed -e 's///g'`"
item_value="`echo \"${params}\" | grep 'ITEM_VALUE: ' | awk -F'ITEM_VALUE: ' '{print $2}' | sed -e 's///g'`"
event_id="`echo \"${params}\" | grep 'EVENT_ID: ' | awk -F'EVENT_ID: ' '{print $2}' | sed -e 's///g'`"
item_id="`echo \"${params}\" | grep 'ITEM_ID: ' | awk -F'ITEM_ID: ' '{print $2}' | sed -e 's///g'`"
# get charts
if [ "${item_id}" != "" ]; then
timestamp=$(date +%s)
${cmd_wget} --save-cookies="${chart_cookie}_${timestamp}" --keep-session-cookies --post-data "name=${zabbix_username}&password=${zabbix_password}&enter=Sign+in" -O /dev/null -q "${zabbix_baseurl}/index.php?login=1"
${cmd_wget} --load-cookies="${chart_cookie}_${timestamp}" -O "${chart_basedir}/graph-${item_id}-${timestamp}.png" -q "${zabbix_baseurl}/chart.php?&itemids=${item_id}&width=${chart_width}&period=${chart_period}"
chart_url="${chart_baseurl}/graph-${item_id}-${timestamp}.png"
rm -f ${chart_cookie}_${timestamp}
# if triger url is empty then we link to the graph with the item_id
if [ "${trigger_url}" == "" ]; then
trigger_url="${zabbix_baseurl}/history.php?action=showgraph&itemids=${item_id}"
fi
fi
# set color
if [ "${trigger_status}" == 'OK' ]; then
case "${trigger_severity}" in
'Information')
color="#439FE0"
;;
*)
color="good"
;;
esac
elif [ "${trigger_status}" == 'PROBLEM' ]; then
case "${trigger_severity}" in
'Information')
color="#439FE0"
;;
'Warning')
color="warning"
;;
*)
color="danger"
;;
esac
else
color="#808080"
fi
# set payload
payload="payload={
\"channel\": \"${channel}\",
\"username\": \"${slack_username}\",
\"icon_emoji\": \"${emoji}\",
\"attachments\": [
{
\"fallback\": \"Date / Time: ${datetime} - ${title}\",
\"title\": \"${title}\",
\"title_link\": \"${trigger_url}\",
\"color\": \"${color}\",
\"fields\": [
{
\"title\": \"Date / Time\",
\"value\": \"${datetime}\",
\"short\": true
},
{
\"title\": \"Status\",
\"value\": \"${trigger_status}\",
\"short\": true
},
{
\"title\": \"Host\",
\"value\": \"${host}\",
\"short\": true
},
{
\"title\": \"Trigger\",
\"value\": \"${trigger_name}: ${item_value}\",
\"short\": true
}
],
\"image_url\": \"${chart_url}\"
}
]
}"
# send to slack
${cmd_curl} -m ${timeout} --data-urlencode "${payload}" "${slack_url}"