-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalancer-cli.sh
executable file
·176 lines (162 loc) · 3.75 KB
/
balancer-cli.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
#!/bin/bash
tmpfile=/tmp/balancer-$$
protocol=http
host=localhost
port=80
path=balancer-manager
url=
# nonce - will be populated automatically
declare -A nonce
# load factor
lf=
# load balance set
ls=
# route
wr=
# route redirect
rr=
# status ignore
statusi=
# status disable
statusd=
# status standby
statush=
# status drain
statusn=
# if defined, debug messages can be emited
#DEBUG=1
#
# Print usage
#
help() {
cat <<HELP
Usage: $0 [OPTION]... command [ARG]
Executes command in apache balancer manager.
Options:
--host host host of balancer manager (default localhost)
--port port of the balancer manager (default 80)
--path path of the balancer manager (default balancer-manager)
-h --help show this help
Commands:
balancers list all balancers
enable <balancer> <worker> enable the worker in the specified balancer, clears the drain flag
disable <balancer> <worker> disable the worker in the specified balancer
drain <balancer> <worker> set the worker to drain mode in the specified balancer
HELP
}
#
# Read default parameters like host, port, etc.
#
readdefaults() {
configfile=$(dirname $0)/.balancer-cli
if [ -e $configfile ]; then
source $configfile
elif [ -e ~/.balancer-cli ]; then
source ~/.balancer-cli
fi
return
}
#
# list balancers
#
balancers() {
verbose=$1
balancers=$(cat $tmpfile | grep "LoadBalancer Status for" | sed -e "s|.*balancer://||;s|</a>.*</h3>||")
[ 1 -eq "$1" ] && echo balancers: $balancers
for b in $balancers; do
[ 1 -eq "$1" ] && echo reading balancer $b
workers=$(cat $tmpfile | grep -e "b=$b" | grep -v "LoadBalancer Status" | sed -e "s/\(.*w=\)\([^\&\"]*\)\(.*\)/\2/")
nonce[$b]=$(cat $tmpfile | grep -e "b=$b" | grep nonce | sed -e "s/\(.*nonce=\)\([0-9a-zA-Z\-]\+\)\(.*\)/\2/" | head -n 1)
[ 1 -eq "$1" ] && echo workers: $workers
[ 1 -eq "$1" ] && echo nonce : ${nonce[$b]}
done
}
#
# configureworker
#
configureworker() {
# balancer
balancer=$1
# worker
worker=$2
# action
action=$3
case $action in
"enable")
statusd=0
statusn=0
;;
"disable")
statusd=1
;;
"drain")
statusn=1
;;
*)
;;
esac
echo configuring $worker @ $balancer with action $action
# echo statusd=$statusd statusn=$statusn
# if variables are empty, do not pass them,
#- otherwise the values will be rewritten by default values and old settings will be lost
if [ -n "$lf" ]; then uloadfactor="lf=${lf}&"; fi
if [ -n "$ls" ]; then uloadset="ls=${ls}&"; fi
if [ -n "$wr" ]; then uroute="wr=${wr}&"; fi
if [ -n "$rr" ]; then uredirect="rr=${rr}&"; fi
wget "${url}" --post-data "${uloadfactor}${uloadset}${uroute}${uredirect}w_status_I=${statusi}&w_status_D=${statusd}&w_status_H=${statush}&w_status_N=${statusn}&w=${worker}&b=${balancer}&nonce=${nonce[$balancer]}" -O - 1>/dev/null 2>&1
}
if [ $# -eq 0 ]; then
help
exit 0
fi
readdefaults
# read options first
while (( $# > 0 )); do
case $1 in
"--port")
port=$2
shift 2
;;
"--host")
host=$2
shift 2
;;
"--path")
path=$2
shift 2
;;
"-h" | "--help")
help
shift
;;
*)
break
;;
esac
done
# first read initial page with balancers, workers and also nonce
url=${protocol}://${host}:${port}/${path}
[ -n "$DEBUG" ] && echo wget $url -O $tmpfile
wget $url -O $tmpfile 2>/dev/null
trap "rm $tmpfile" EXIT
# now read commands
while (( $# > 0 )); do
case $1 in
"balancers" | "list")
balancers 1
;;
"enable" | "disable" | "drain")
# configure <balancer> <worker>
balancers 0
configureworker $2 $3 $1
shift 2
;;
*)
echo Unknown command $1
exit 1
;;
esac
shift
done
echo done
#eof