-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzabbix_trigger_dependency.sh
206 lines (157 loc) · 3.54 KB
/
zabbix_trigger_dependency.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/sh
# Script para adicionar dependencia de trigger
URL='https://url_zabbix/zabbix/api_jsonrpc.php'
HEADER='Content-Type:application/json'
read -p "Digite seu usuário Zabbix: " USER
read -s -p "Digite sua senha Zabbix: " PASS
MENU=$(mktemp)
HOST_FILHO=$(mktemp)
TRIGGER_FILHO=$(mktemp)
HOST_PAI=$(mktemp)
TRIGGER_PAI=$(mktemp)
GRUPO=$1
autenticacao()
{
JSON='
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "'$USER'",
"password": "'$PASS'"
},
"id": 0
}
'
curl -s -X POST -H "$HEADER" -d "$JSON" "$URL" | cut -d '"' -f8
}
TOKEN=$(autenticacao)
trigger_get()
{
JSON='
{
"jsonrpc": "2.0",
"method": "trigger.get",
"params": {
"output": [
"description"
],
"group": "'$GRUPO'",
"expandDescription": "True",
"filter": {
"value": "1"
},
"skipDependent": "True",
"search": {
"description": "unavailable"
},
"active": "True"
},
"auth": "'$TOKEN'",
"id": 1
}
'
curl -s -X POST -H "$HEADER" -d "$JSON" "$URL" | python -m json.tool | grep unavailable | wc -l
}
menu()
{
JSON='{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"host"
]
},
"auth": "'$TOKEN'",
"id": 1
}
'
curl -s -X POST -H "$HEADER" -d "$JSON" "$URL" | jq -r '.result[] | "\(.hostid)|\(.host)"' > $MENU
}
menu
while true
do
clear
read -p "Selecione os Hosts Filhos"
cat $MENU | percol > $HOST_FILHO
read -p "Selecione o Host Pai"
cat $MENU | percol > $HOST_PAI
clear
cat $HOST_FILHO
echo -e "\n\n\n======> $(cat $HOST_PAI)"
read -p "confirma a dependencia"
HOST_PAI=$(cat $HOST_PAI | cut -d"|" -f1)
for i in $(cut -d"|" -f1 $HOST_FILHO)
do
JSON='{
"jsonrpc": "2.0",
"method": "trigger.get",
"params": {
"output":["triggerid"],
"hostids":"'$i'",
"search": {
"description": "unavailable"
}
},
"auth": "'$TOKEN'",
"id": 1
}
'
#read -p antes
#curl -s -X POST -H "$HEADER" -d "$JSON" "$URL" | jq '.[]'
#read -p depois
#curl -s -X POST -H "$HEADER" -d "$JSON" "$URL" | jq '.result[].triggerid' >> $TRIGGER_FILHO
RESULT=$(curl -s -X POST -H "$HEADER" -d "$JSON" "$URL" | jq '.result[].triggerid')
[ -z $RESULT ] && read -p "Não foi encontrada trigger de indisponibilidade para o Host $i"
[ -z $RESULT ] || echo "$RESULT" >> $TRIGGER_FILHO
done
JSON='{
"jsonrpc": "2.0",
"method": "trigger.get",
"params": {
"output":["triggerid"],
"hostids":"'$HOST_PAI'",
"search": {
"description": "unavailable"
}
},
"auth": "'$TOKEN'",
"id": 1
}
'
TRIGGER_PAI=$(curl -s -X POST -H "$HEADER" -d "$JSON" "$URL" | jq '.result[].triggerid' | tr -d "\"")
for j in $(cat $TRIGGER_FILHO | tr -d "\"")
do
addDependencies='{
"jsonrpc": "2.0",
"method": "trigger.adddependencies",
"params": {
"triggerid":"'$j'",
"dependsOnTriggerid": "'$TRIGGER_PAI'"
},
"auth": "'$TOKEN'",
"id": 1
}
'
deleteDependencies='{
"jsonrpc": "2.0",
"method": "trigger.deletedependencies",
"params": {
"triggerid":"'$j'"
},
"auth": "'$TOKEN'",
"id": 1
}
'
curl -s -X POST -H "$HEADER" -d "$deleteDependencies" "$URL"
curl -s -X POST -H "$HEADER" -d "$addDependencies" "$URL"
done
done
rm -rf $MENU $HOST_FILHO $TRIGGER_FILHO $HOST_PAI $TRIGGER_PAI
#clear
#cat /tmp/host_pai
#echo ${SPLIT[@]}
#echo $SPLIT | tr -s [:space:] " "
#echo ${SPLIT[1]}
#host_get