-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzabbix-notify.sh
executable file
·162 lines (141 loc) · 3.36 KB
/
zabbix-notify.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/sh
# Login and get token
TMPL_LOGIN=$(cat << EOF
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "${ZBX_LOGIN:-Admin}",
"password": "${ZBX_PASSWORD:-zabbix}"
},
"id": 1,
"auth": null
}
EOF
)
TOKEN=$(curl -s -X POST -H 'Content-Type: application/json-rpc' -d "$TMPL_LOGIN" http://$ZBX_API_URL/api_jsonrpc.php | jq -r '.result')
# Find out default node hostid
TMPL_HOSTS=$(cat << EOF
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"hostid"
],
"filter": {
"host": "Zabbix server"
}
},
"id": 2,
"auth": "$TOKEN"
}
EOF
)
HOST_ID=$(curl -s -X POST -H 'Content-Type: application/json-rpc' -d "$TMPL_HOSTS" http://$ZBX_API_URL/api_jsonrpc.php | jq '.result[0]' | jq -r '.hostid')
# Ensure host monitoring is enabled
TMPL_MONIT_CHECK=$(cat << EOF
{
"jsonrpc": "2.0",
"method": "host.update",
"params": {
"hostid": "$HOST_ID",
"status": 0
},
"auth": "$TOKEN",
"id": 1
}
EOF
)
curl -s -X POST -H 'Content-Type: application/json-rpc' -d "$TMPL_MONIT_CHECK" http://$ZBX_API_URL/api_jsonrpc.php | jq
# Loop over all hosts in file
while read EP; do
# Check if web scenario already exist
TMPL_VHOST_CHECK=$(cat << EOF
{
"jsonrpc": "2.0",
"method": "httptest.get",
"params": {
"output":
[
"httptestid"
],
"filter": {
"name": "$EP check"
}
},
"auth": "$TOKEN",
"id": 1
}
EOF
)
IF_VHOST_EXISTS=$(curl -s -X POST -H 'Content-Type: application/json-rpc' -d "$TMPL_VHOST_CHECK" http://$ZBX_API_URL/api_jsonrpc.php | jq ".result[0]" | jq -r ".httptestid")
if [ $IF_VHOST_EXISTS = "null" ]
then
TMPL_VHOST_ADD=$(cat << EOF
{
"jsonrpc": "2.0",
"method": "httptest.create",
"params": {
"name": "$EP check",
"hostid": "$HOST_ID",
"delay": "${ZBX_WEB_DELAY:-60}",
"timeout": "${ZBX_WEB_TIMEOUT:-15}",
"steps": [
{
"name": "Homepage",
"url": "http://$EP",
"status_codes": 200,
"no": 1
}
]
},
"auth": "$TOKEN",
"id": 1
}
EOF
)
curl -s -X POST -H 'Content-Type: application/json-rpc' -d "$TMPL_VHOST_ADD" http://$ZBX_API_URL/api_jsonrpc.php
else
echo "'$EP check' already exists"
fi
# Check if web scenario trigger already exist
TMPL_TRIGGER_CHECK=$(cat << EOF
{
"jsonrpc": "2.0",
"method": "trigger.get",
"params": {
"output": [
"triggerid"
],
"filter": {
"description": "$EP unreachable"
}
},
"auth": "$TOKEN",
"id": 1
}
EOF
)
IF_TRIGGER_EXISTS=$(curl -s -X POST -H 'Content-Type: application/json-rpc' -d "$TMPL_TRIGGER_CHECK" http://$ZBX_API_URL/api_jsonrpc.php | jq ".result[0]" | jq -r ".triggerid")
if [ $IF_TRIGGER_EXISTS = "null" ]
then
TMPL_TRIGGER_ADD=$(cat << EOF
{
"jsonrpc": "2.0",
"method": "trigger.create",
"params": {
"description": "$EP unreachable",
"expression": "{Zabbix server:web.test.fail[$EP check].min(2m)}<>0",
"priority": 4
},
"auth": "$TOKEN",
"id": 1
}
EOF
)
curl -s -X POST -H 'Content-Type: application/json-rpc' -d "$TMPL_TRIGGER_ADD" http://$ZBX_API_URL/api_jsonrpc.php
else
echo "'$EP trigger' already exists"
fi
done < endpoints.list