forked from talset/apache-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage-jk-balancer.py
More file actions
executable file
·308 lines (257 loc) · 10.9 KB
/
manage-jk-balancer.py
File metadata and controls
executable file
·308 lines (257 loc) · 10.9 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
#!/usr/bin/python -O
#
# Author: Vladislav Nazarenko <vnazarenko@codeberry.de>
# Source: https://github.com/talset/apache-tools
#
#
# File: httpd.conf
#
# JkWorkersFile /etc/httpd/conf.d/workers.properties
# JkLogFile "| /usr/sbin/rotatelogs -l /var/log/httpd/mod_jk-%Y-%m-%d.%H.log 3600"
# JkLogLevel error
# JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
# JkOptions +ForwardKeySize -ForwardDirectories +ForwardURICompatUnparsed
# JkShmFile [PATH]../jk.shm
# JkMount /jkstatus/* status
# JkMount /* [BALANCER NAME]
#
# File: workers.properties
#
# worker.status.type=status
#
# worker.template.type=ajp13
# worker.template.socket_timeout=180
# worker.template.socket_connect_timeout=5000
# worker.template.retries=2
# worker.template.recovery_options=3
# worker.template.distance=0
#
# worker.appsrv00.reference=worker.template
# worker.appsrv00.host=app01.example.com
# worker.appsrv00.port=8080
# worker.appsrv01.reference=worker.template
# worker.appsrv01.host=app01.example.com
# worker.appsrv01.port=9080
# worker.appsrv10.reference=worker.template
# worker.appsrv10.host=app02.example.com
# worker.appsrv10.port=8080
# worker.appsrv11.reference=worker.template
# worker.appsrv11.host=app02.example.com
# worker.appsrv11.port=9080
#
# worker.appsrv.type=lb
# worker.appsrv.method=B
# worker.appsrv.recover_time=30
# worker.appsrv.balance_workers=appsrv00 appsrv01 appsrv00 appsrv01
#
# worker.list=status appsrv
#
#
# You have to allow /jkstatus
#
# <Location /jkstatus>
# Order deny,allow
# Deny from all
# Allow from localhost 127.0.0.1
# </Location>
#
# Verify url="http....."
#
#
# HOW TO :
# balancer-manager.py -l
# balancer-manager.py -w ajp://10.152.45.1:8001 -a enable
#
#############################################################################
import argparse
import re
import HTMLParser
import sys
import urlparse
from urllib import urlencode
from urllib2 import Request
from urllib2 import urlopen
#############################################################################
# Get args
PARSER = argparse.ArgumentParser()
PARSER.add_argument("-H", "--host", help="Host connect to", type=str)
PARSER.add_argument("-p", "--port", help="Port connect to", type=str)
PARSER.add_argument("-l", "--list",
help="List Worker member and status", action='store_true')
PARSER.add_argument("-a", "--action",
help="\"enable\" or \"disable\" the specified Worker", type=str)
PARSER.add_argument("-w", "--worker",
help="Worker name : example ajp://127.0.0.1:8001", type=str)
ARGS = PARSER.parse_args()
#############################################################################
# default values
host='localhost'
port = 80
if ARGS.host:
host = ARGS.host
if ARGS.port: port = ARGS.port
if ARGS.port:
port = ARGS.port
url="http://%s:%s/jkstatus/" % (host, port)
#############################################################################
# functions
def getWorkersList():
reqUrl = "%s?cmd=list" % (url)
req = Request(reqUrl, None)
f = urlopen(req)
class LBParser(HTMLParser.HTMLParser):
def __init__(self):
self.workers = {}
self._wData = None
self.datas = []
self._tds = []
self._balancer = ''
self.in_td = False
self.in_th = False
self.in_h3 = False
self.in_bm_declaration = False
self.in_bm_status = False
HTMLParser.HTMLParser.__init__(self)
def handle_starttag(self, tag, attrs):
if tag == 'td':
self.in_td = True
elif tag == 'th':
self.in_th = True
elif tag == 'h3':
self.in_h3 = True
elif self.in_h3 and not self.in_td and tag == 'a':
b = self.get_balancer_name(attrs)
if b:
self._balancer = b
def handle_data(self, data):
if self.in_td:
self._tds.append(data)
self._wData = data
elif self.in_th and data == 'Address:Port':
self.in_bm_declaration = True
elif self.in_th and data == 'Route':
self.in_bm_status = True
def handle_endtag(self, tag):
# if no text data was enclosed by the tag, handle_data function
# is not called, we have to handle it with a hook
if self.in_td and self._wData == None:
self._tds.append('')
#print "ERROR: %s" % (self.get_starttag_text())
self._wData = None
self.in_td = False
self.in_th = False
self.in_h3 = False
if tag == 'table':
self.in_bm_declaration = False
self.in_bm_status = False
if tag == 'tr':
if self.in_bm_declaration and self._tds != []:
wName = self._tds[0]
worker = {}
# first workers table
worker['Type'] = self._tds[1]
worker['Hostname'] = self._tds[2]
worker['Address:Port'] = self._tds[3]
worker['ConnectionPoolTimeout'] = self._tds[4]
worker['ConnectTimeout'] = self._tds[5]
worker['PrepostTimeout'] = self._tds[6]
worker['ReplyTimeout'] = self._tds[7]
worker['Retries'] = self._tds[8]
worker['RecoveryOptions'] = self._tds[9]
worker['MaxPacketSize'] = self._tds[10]
worker['Balancer'] = self._balancer
self.workers[wName] = worker
elif self.in_bm_status and self._tds != []:
if len(self._tds) > 1:
# there is a legend table which we do not want to
# look in
wName = self._tds[2]
if self.workers.has_key(wName):
# second workers table
self.workers[wName]['ActConf'] = self._tds[3]
self.workers[wName]['State'] = self._tds[4]
self.workers[wName]['Distance'] = self._tds[5]
self.workers[wName]['Factor'] = self._tds[6]
self.workers[wName]['Multiplicity'] = self._tds[7]
self.workers[wName]['Value'] = self._tds[8]
self.workers[wName]['ReqNr'] = self._tds[9]
self.workers[wName]['SessNr'] = self._tds[10]
self.workers[wName]['ErrNr'] = self._tds[11]
self.workers[wName]['ClinetErrNr'] = self._tds[12]
self.workers[wName]['RyplyTO'] = self._tds[13]
self.workers[wName]['BytesWr'] = self._tds[14]
self.workers[wName]['BytesRead'] = self._tds[15]
self.workers[wName]['BusyConnNr'] = self._tds[16]
self.workers[wName]['BusyConnMaxNr'] = self._tds[17]
self.workers[wName]['BackendConnNr'] = self._tds[18]
self.workers[wName]['Route'] = self._tds[19]
self.workers[wName]['RouteRedirect'] = self._tds[20]
self.workers[wName]['ClusterDomain'] = self._tds[21]
self.workers[wName]['RecoverySched'] = self._tds[22]
self.workers[wName]['LastReset'] = self._tds[23]
self.workers[wName]['LastErr'] = self._tds[24]
else:
print "ERROR: could not find worker name in the status table"
self._tds = []
def get_balancer_name(self, attrs):
'''Just a convinience function'''
for prop, val in attrs:
if prop == 'href':
# extract balancer name from the query string
parsed = urlparse.urlparse(val)
if parsed.path and parsed.query:
r = urlparse.parse_qs(parsed.query)
if r.has_key('w'):
return r['w'][0]
return False
p = LBParser()
p.feed(f.read())
return p.workers
def balancer_status():
workers = getWorkersList()
if workers:
template = "{Worker:40} | {Route:16} | {Status:10}| {From:16} | {To:16} | {Balancer:80}"
# print workers.keys()
print template.format( Worker="Worker", Route="Route", Status="Status", From='From', To = 'To', Balancer='Balancer' )
for k in sorted(workers.keys()):
# print workers[k]
print template.format(
Worker = "%s://%s:%s" % (workers[k]['Type'], workers[k]['Hostname'], workers[k]['Address:Port'].split(':')[1]),
Route = workers[k]['Route'],
Status = workers[k]['ActConf'],
From = workers[k]['BytesRead'],
To = workers[k]['BytesWr'],
Balancer = workers[k]['Balancer']
)
return True
return False
def balancer_manage(sAction, worker):
if sAction not in ['enable', 'disable']:
print "Unknown action: %s" %(sAction)
return False
workers = getWorkersList()
found = False
for k in workers.keys():
if worker == "%s://%s:%s" % (workers[k]['Type'], workers[k]['Hostname'], workers[k]['Address:Port'].split(':')[1]):
found = True
action = (0,1)[sAction == 'disable']
balancer = workers[k]['Balancer']
params = urlencode({'cmd': 'update', 'from': 'list','w': balancer, 'sw': k, 'vwa': action})
req = Request(url+"?%s" % params, None)
urlopen(req)
print "Action\n Worker %s [%s]\n\nStatus" % (worker,sAction)
balancer_status()
if not found:
print "Could not find worker: %s" %(worker)
return False
#############################################################################
r = 0
if __name__ == "__main__":
#if ARGS.list is not None:
if ARGS.list :
r = balancer_status()
elif ARGS.action and ARGS.worker:
r = balancer_manage(ARGS.action,ARGS.worker)
else:
PARSER.print_help()
sys.exit( not r )