-
Notifications
You must be signed in to change notification settings - Fork 0
/
zabbix-teams.sh
115 lines (101 loc) · 3.49 KB
/
zabbix-teams.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
#!/bin/bash
#############################################################################
# ______ _ _ _ __ _______ #
# |___ / | | | | (_) \ \ |__ __| #
# / / __ _| |__ | |__ ___ __ _____\ \ | | ___ __ _ _ __ ___ ___ #
# / / / _` | '_ \| '_ \| \ \/ / |______> > | |/ _ \/ _` | '_ ` _ \/ __|#
# / /_| (_| | |_) | |_) | |> < / / | | __/ (_| | | | | | \__ \#
# /_____\__,_|_.__/|_.__/|_/_/\_\ /_/ |_|\___|\__,_|_| |_| |_|___/#
#############################################################################
# Title: zabbix-teams.sh
# Author: Jared Brogan
# Purpose: Integrate Zabbix alerts into Microsoft Teams
# Example webhook test message:
# WEBHOOK_URL=
# ./zabbix-teams.sh "${WEBHOOK_URL}" "HOST GROUP: Acknowledged - Test Alert" "Test message"
## Values received by this script:
# WebHook = $1 (URL generated by configuring an incoming webhook on a specific Teams channel. This should be configured as a user's media on Zabbix frontend.)
# Subject = $2 (usually contains either PROBLEM or Resolved/OK)
# Message = $3 (whatever message the Zabbix action sends, preferably something like "Zabbix server is unreachable for 5 minutes - Zabbix server (127.0.0.1)")
webhook_url="$1"
subject="$2"
message="$(echo "$3" | egrep -v '(ACK_URL|PROB_URL)')"
prob_url="$(echo "$3" | grep PROB_URL | sed 's/PROB_URL: //g ; s/ .*//g')"
ack_url="$(echo "$3" | grep ACK_URL | sed 's/ACK_URL: //g ; s/ .*//g')"
###############################################################################
# Payload formatted for PROBLEM issues
read -r -d '' problem_payload <<- EOM
{
"@context": "http://schema.org/extensions",
"@type": "MessageCard",
"themeColor": "EA4300",
"title": "${subject}",
"text": "${message}",
"potentialAction": [
{
"@type": "OpenUri",
"name": "Details",
"targets": [
{ "os": "default", "uri": "${prob_url}" }
]
},
{
"@type": "OpenUri",
"name": "Acknowledge",
"targets": [
{ "os": "default", "uri": "${ack_url}" }
]
}
]
}
EOM
###############################################################################
# Payload formatted for RESOLVED issues
read -r -d '' resolved_payload <<- EOM
{
"@context": "http://schema.org/extensions",
"@type": "MessageCard",
"themeColor": "43EA00",
"title": "${subject}",
"text": "${message}",
"potentialAction": [
{
"@type": "OpenUri",
"name": "Details",
"targets": [
{ "os": "default", "uri": "${prob_url}" }
]
}
]
}
EOM
###############################################################################
# payload formatted for RESOLVED issues
read -r -d '' ack_payload <<- EOM
{
"@context": "http://schema.org/extensions",
"@type": "MessageCard",
"themeColor": "0072C6",
"title": "${subject}",
"text": "${message}",
"potentialAction": [
{
"@type": "OpenUri",
"name": "Details",
"targets": [
{ "os": "default", "uri": "${prob_url}" }
]
}
]
}
EOM
###############################################################################
# Decide which payload to send to the webhook
if [[ $(echo "$subject" | egrep -qi '(problem)' ; echo $?) -eq 0 ]]; then
payload="${problem_payload}"
elif [[ $(echo "$subject" | egrep -qi '(resolved|ok)' ; echo $?) -eq 0 ]]; then
payload="${resolved_payload}"
elif [[ $(echo "$subject" | egrep -qi '(acknowledged)' ; echo $?) -eq 0 ]]; then
payload="${ack_payload}"
fi
curl -sSL -H "Content-Type: application/json" -d "${payload}" $webhook_url