-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangeNodeState.sh
More file actions
executable file
·392 lines (356 loc) · 10.2 KB
/
changeNodeState.sh
File metadata and controls
executable file
·392 lines (356 loc) · 10.2 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/bin/bash
# Author: Joshua Chen
# Date: 2016-01-22
# Location: Shenzhen
# Desc: set the node to be enabled/disabled on condition,
# using the OPS API.
login() {
resp=$(curl -m $CURL_MXTIME -i --url "$loginUrl" \
-d "username=$userName&password=$password" -c $cookie 2>/dev/null)
if test $? -ne 0; then
return $API_CALL_FAILED
elif grep -qE 'HTTP.* 200 OK' <<< "$resp"; then
if grep -qE "$LOGINOKCODE" <<< "$resp"; then
return $LOGIN_SUCCESS
else
return $LOGIN_FAILED
fi
else
return $API_CALL_FAILED
fi
}
change() {
local ip stat eventCode eventId
ip=$1
stat=$2
eventCode=$3
eventId=$4
resp=$(curl -m $CURL_MXTIME \
-i --url "$applyUrl/$ip/$stat/$eventCode/$eventId" -b $cookie 2>/dev/null)
if test $? -ne 0; then
return $API_CALL_FAILED
elif grep -qE 'HTTP.* 200 OK' <<< "$resp"; then
if grep -qE "$APPLYOKCODE" <<< "$resp"; then
return $CHANGE_APPLIED
elif grep -qE "$APPLYFAILEDCODE" <<< "$resp"; then
return $CHANGE_FAILED
else
return $CHANGE_IGNORED
fi
else
return $API_CALL_FAILED
fi
}
# any response determines a success result
pingOne() {
if ! ping -W1 -c3 $1 | grep -q '100% packet loss'; then
echo ok
fi
}
# simultaneously ping
simulPing() {
while read ip
do
pingOne $ip &
done <<< "$1"
wait
}
# more or equal to 80% success determines a GOOD stat
weAreGood() {
url="${refIpUrl}/$1"
ipList=$(wget -O - $url 2>/dev/null | sed '/^$/d')
if test -z "$ipList"; then
msg=$'Auto dispatch aborted.\n'
msg+="Reason: no reference IPs for $1"$'\n'
msg+="Task: $task"$'\n'
msg+="Event: $event"
log "$msg"
report warn "$msg"
exit 1
fi
resp=$(simulPing "$ipList")
listCount=$(echo "$ipList" | wc -l)
respCount=$(echo "$resp" | wc -l)
percnt=$(echo "scale=2;${respCount}/${listCount}*100" | bc | awk -F. '{print $1}')
test "$percnt" -ge 80
}
# whether the IP is in the excluded list or not
isExcluded() {
grep -q "^$1\$" "$xcldList"
}
getStat() {
awk '/^Trigger status:/{print $NF}' <<< "$1"
}
getToStat() {
if test "$1" = PROBLEM; then
toStat=0
else
toStat=1
fi
echo $toStat
}
getIp() {
awk '/^IP:/{print $NF}' <<< "$1"
}
log() {
logger -t "[AUTODISPATCH]" -p local0.info "$*"
}
# log the report state
logReport() {
local stat=$1 group=$2 api=$3 msg=$4 wd localLog ts
msg=$(xargs <<< "$msg")
wd=$(cd -P $(dirname $0); pwd)
localLog="$wd/data/sent_messages.log"
ts=$(date '+%F %T')
echo "$ts stat=$stat group=$group api=$api msg=$msg" >> "$localLog"
}
report() {
local groupName apiUrl msg stat
if test "$1" = "warn"; then
groupName="PLCDN-SUPPORT"
else
groupName="PLCDN-STATUS"
fi
apiUrl="http://push.plcdn.net:7890/20160128"
msg="$2"$'\n'
msg+="Time: $(date +'%F %T')"
wget -q --header="To: $groupName" \
--post-data="$msg" "$apiUrl" \
-O /dev/null
logReport $? "$groupName" "$apiUrl" "$msg"
}
# parse the input string, produce two
# variables for composing the report message.
parseTask() {
local subject=$1 ip=$2 toStat=$3
event=$(awk -F": " '{print $NF}' <<< "$subject")
if test "$toStat" = 1; then # to enable
task="enable $ip"
event="recover from $event"
else
task="disable $ip"
fi
}
cleanup() {
rm -f $cookie
}
# generate a unique id for the task
# the id is a 40-char sha1sum.
genEventId() {
head -c 1024 /dev/urandom | sha1sum | awk '{print $1}'
}
# log the ignored request for later query
# strip off the 'PROBLEM: ' and 'OK: ' characters from
# the subject, acquire a lock to prevent race condition.
logIgnored() {
local toStat=$1 ip=$2 subject=${3##*: } ts=$(date +%s)
(
flock -w $lockTimeOut 3 || return
echo "$ts,$ip,$toStat,$subject" >> "$igndHist"
) 3> $igndHistLock
}
# check if the request was ignored
# within a set period before.
# use index to match the subject rather
# than $3, because subject may contain commas.
# acquire a lock to prevent race condition.
wasIgnored() {
local stat=$1 ip=$2 subject=${3##*: } period=1800
(
flock -w $lockTimeOut 3 || return
awk -v now=$(date +%s) -v period=$period \
-v stat=$stat -v ip=$ip -v subject="$subject" \
-F, 'now - $1 < period && $2 == ip && $3 == stat && index($0, subject) != 0' \
"$igndHist" | grep -q .
) 3> $igndHistLock
}
# delete entries that match the given criteria
# acquire a lock to prevent race condition.
clearIgnored() {
local stat=$1 ip=$2 subject=${3##*: } tmpfile
(
flock -w $lockTimeOut 3 || return
tmpfile=$(mktemp)
awk -v stat=$stat -v ip=$ip -v subject="$subject" \
-F, '$2 != ip || $3 != stat || index($0, subject) == 0' "$igndHist" > $tmpfile
mv $tmpfile "$igndHist"
) 3> $igndHistLock
}
userName='xxxxx'
password='xxxx'
loginUrl='http://drms.xxxxx.com/index.php/admin/login'
applyUrl='http://drms.xxxxxx.com/index.php/portal/dispatchStatus'
refIpUrl='http://drms.xxxxxxxx.com/index.php/portal/getIPs'
xcldList='/etc/zabbix/zabbix_server.conf.d/data/excluded_ip'
igndHist='/etc/zabbix/zabbix_server.conf.d/data/ignored_hist'
igndHistLock='/etc/zabbix/zabbix_server.conf.d/data/ignored_hist_lock'
lockTimeOut=5
cookie=$(mktemp -u)
LOGINOKCODE=v00006
APPLYOKCODE=v30007
APPLYFAILEDCODE=030008
CURL_MXTIME=60
CURL_RETRY_DELAY=3
# return codes
API_CALL_FAILED=11
LOGIN_SUCCESS=12
LOGIN_FAILED=13
CHANGE_APPLIED=14
CHANGE_FAILED=15
CHANGE_IGNORED=16
# event definition
port80DownStr='80 port is down'
pingUnreachableStr='ping_unreachable'
port80DownCode=1
pingUnreachableCode=2
# 给username, password, loginUrl, applyUrl,
# refIpUrl, xcldList, igndHist 赋上适当的值之后,
# 把CONFIGOK 设置为1即可。
CONFIGOK=1
trap cleanup exit
# start to work
log "start to work"
subject=$1
message=$2
stat=$(getStat "$message")
toStat=$(getToStat "$stat")
ip=$(getIp "$message")
parseTask "$subject" "$ip" "$toStat" # produce two variables: task, event
if test "$CONFIGOK" -ne 1; then
log "config error: variable username, password, loginUrl, applyUrl shall be set"
errmsg=$'Auto dispatch aborted.\n'
errmsg+=$'Reason: config error\n'
errmsg+="Task: $task"$'\n'
errmsg+="Event: $event"
report warn "$errmsg"
exit 1
fi
# the igndHist must be set and the file can be created
(
flock -w $lockTimeOut 3 || exit 1
if test -z "$igndHist" || ! touch "$igndHist"; then
log "ignored history file not set or can not be created/changed"
errmsg="Auto dispatch aborted."$'\n'
errmsg+="Reason: runtime error"$'\n'
errmsg+="Task: $task"$'\n'
errmsg+="Event: $event"
report warn "$errmsg"
exit 1
else
exit 0
fi
) 3> $igndHistLock
test $? -ne 0 && exit 1
# exclude the ip in the excluded list
if test -z "$xcldList" -o ! -f "$xcldList"; then
log "excluded list not set or the file not exists"
errmsg="Auto dispatch aborted."$'\n'
errmsg+="Reason: runtime error"$'\n'
errmsg+="Task: $task"$'\n'
errmsg+="Event: $event"
report warn "$errmsg"
exit 1
fi
if isExcluded "$ip"; then
msg="$ip is in the excluded list"
log "$msg"
# 2016-05-23 12:47, temporarily disable this report
#errmsg="Auto dispatch aborted."$'\n'
#errmsg+="Reason: $msg"$'\n'
#errmsg+="Task: $task"$'\n'
#errmsg+="Event: $event"
#report warn "$errmsg"
exit 1
fi
# filter events
if grep -q "$port80DownStr" <<< "$subject"; then
code=$port80DownCode
elif grep -q "$pingUnreachableStr" <<< "$subject"; then
code=$pingUnreachableCode
else
msg="unrecognized event ($subject)"
log "$msg"
errmsg="Auto dispatch aborted."$'\n'
errmsg+="Reason: $msg"$'\n'
errmsg+="Task: $task"$'\n'
errmsg+="Event: $event"
report warn "$errmsg"
exit 1
fi
# do a sanity check before sending command
if test "$toStat" = 0 && ! weAreGood $ip; then
msg="sanity check failed"
log "$msg"
errmsg="Auto dispatch aborted."$'\n'
errmsg+="Reason: $msg"$'\n'
errmsg+="Task: $task"$'\n'
errmsg+="Event: $event"
report warn "$errmsg"
exit 1
fi
# login
while true
do
login
x=$?
if test "$x" = "$LOGIN_FAILED"; then
log "login failed"
msg="Task: $task"$'\n'
msg+="Event: $event"$'\n'
msg+="Result: login failed"
report warn "$msg"
exit 1
elif test "$x" = "$API_CALL_FAILED"; then
errmsg="Failed to call API for login"
log "$errmsg"
msg="Task: $task"$'\n'
msg+="Event: $event"$'\n'
msg+="Result: $errmsg (retrying)"
report warn "$msg"
sleep $CURL_RETRY_DELAY
continue # keep trying when call to api failed
fi
break
done
eventId=$(genEventId)
# apply change
while true
do
change "$ip" "$toStat" "$code" "$eventId"
x=$?
if test "$x" = "$CHANGE_APPLIED"; then # the node disabled/enabled, shall warn
log "changed successfully ($subject)"
msg="Task: $task"$'\n'
msg+="Event: $event"$'\n'
msg+="Result: success"
if test "$toStat" = 1 && wasIgnored 0 "$ip" "$subject"; then
clearIgnored 0 "$ip" "$subject"
report message "$msg (disable request ignored)"
else
report warn "$msg"
fi
elif test "$x" = "$CHANGE_FAILED"; then # API internal error, shall warn
log "change failed ($subject)"
msg="Task: $task"$'\n'
msg+="Event: $event"$'\n'
msg+="Result: failed"
report warn "$msg"
elif test "$x" = "$API_CALL_FAILED"; then
errmsg="Failed to call API for change"
log "$errmsg"
msg="Task: $task"$'\n'
msg+="Event: $event"$'\n'
msg+="Result: $errmsg (retrying)"
report warn "$msg"
sleep $CURL_RETRY_DELAY
continue # keep trying when call to api failed
else
log "apply request been ignored"
msg="Task: $task"$'\n'
msg+="Event: $event"$'\n'
msg+="Result: ignored"
report message "$msg"
logIgnored "$toStat" "$ip" "$subject"
fi
break
done