-
Notifications
You must be signed in to change notification settings - Fork 4
/
is_server_up.sh
executable file
·112 lines (97 loc) · 2.25 KB
/
is_server_up.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
#!/bin/bash
# Test whether the Zanata server is up.
# Author: [email protected]
function print_usage(){
cat << END
Usage: $0 [-i interval] [-r retries] [-p up_pattern] [-v verboseLevel] <ZANATA_SERVER_URL>
$0 -h
ZANATA_SERVER_URL: URL to Zanata server
Options:
-h: Show usage
-i: Interval between retries in seconds
-r: Times of retries.
-p: Grep-like patten to be captured.
-v: VerboseLevel (Default is 1)
0: Nothing is output
1: Only the final results
2: Print result for each check.
3: curl message as well.
Return value:
0: Zanata server is up eventually.
1: Zanata server is still down after the retries.
END
}
# platform >= 4 has /account/.*login
# server <= 3 has /account/sign_in
UP_PATTERN="/account/(.*login|sign_in)"
# Retry interval: Default: 30 sec
INTERVAL=30
RETRIES=5
VERBOSE=1
check_connection(){
if [ $VERBOSE -lt 3 ]; then
QUIET="--silent"
else
QUIET=""
fi
if curl ${QUIET} --insecure --location $ZANATA_SERVER_URL | grep -q -E -e "$UP_PATTERN" ; then
UP=1
if [ $VERBOSE -ge 1 ]; then
echo "Zanata server on $ZANATA_SERVER_URL is [UP]"
fi
else
UP=0
if [ $VERBOSE -ge 2 ]; then
echo "Zanata server on $ZANATA_SERVER_URL is [DOWN]"
fi
fi
if [ "$UP" = "0" ];then
return 1
fi
return 0;
}
while getopts "hi:r:p:v:" opt; do
case $opt in
h)
print_usage
exit 0
;;
i)
INTERVAL=$OPTARG
;;
r)
RETRIES=$OPTARG
;;
p)
UP_PATTERN=$OPTARG
;;
v)
VERBOSE=$OPTARG
;;
*)
;;
esac
done
shift $((OPTIND-1));
ZANATA_SERVER_URL=$1
if [ -z $ZANATA_SERVER_URL ]; then
print_usage
exit -1
fi
if check_connection; then
exit 0;
fi
retries=0
up=0
until [ "$retries" = "$RETRIES" ]; do
let retries++
echo "Checking pattern: $UP_PATTERN, retries $retries in $INTERVAL seconds"
sleep $INTERVAL
if check_connection; then
exit 0
fi
done
if [ $VERBOSE -ge 1 ]; then
echo "Zanata server on $ZANATA_SERVER_URL is still [DOWN] after $retries retries"
fi
exit 1