-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbalancer-manager.py
executable file
·112 lines (90 loc) · 3.1 KB
/
balancer-manager.py
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
#!/usr/bin/python -O
# Author: Florian Lambert <[email protected]>
# Allow you to manage Worker/BalancerMember defined in your apache2 mod_proxy conf :
# <Proxy balancer://tomcatservers>
# BalancerMember ajp://10.152.45.1:8001 route=web1 retry=60
# BalancerMember ajp://10.152.45.2:8001 route=web2 retry=60
# </Proxy>
# You have to allow /balancer-manager
#Like :
# ProxyPass /balancer-manager !
# <Location /balancer-manager>
# SetHandler balancer-manager
# Order Deny,Allow
# Deny from all
# Allow from 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
from urllib import urlencode
from urllib2 import Request, urlopen
# Get args
PARSER = argparse.ArgumentParser()
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()
#Fix if necessary
#vhostname
headers = {"Host": '127.0.0.1' }
#ip to reach apache
url="http://127.0.0.1/balancer-manager"
def balancer_status():
req = Request(url, None, headers)
f = urlopen(req)
#print f.read()
class TableParser(HTMLParser.HTMLParser):
def __init__(self):
self.datas=[]
self._tds=[]
HTMLParser.HTMLParser.__init__(self)
self.in_td = False
def handle_starttag(self, tag, attrs):
if tag == 'td' or tag == 'th':
self.in_td = True
def handle_data(self, data):
if self.in_td:
self._tds.append(data)
def handle_endtag(self, tag):
self.in_td = False
if tag == 'tr':
self.datas.append(self._tds)
self._tds = []
p = TableParser()
p.feed(f.read())
template = " {Worker:40} | {Status:10} | {Elected:10}"
print template.format(Worker="Worker",Status="Status",Elected="Elected")
for v in p.datas[2:]:
print template.format(Worker=v[0],Status=v[4],Elected=v[5])
def balancer_manage(action, worker):
#Read informations
req = Request(url, None, headers)
f = urlopen(req)
#Find balancer and nonce
result = re.search("b=([^&]+)&w="+worker+"&nonce=([^\"]+)", f.read())
if result is not None:
balancer = result.group(1)
nonce = result.group(2)
#Generate URL
params = urlencode({'b': balancer, 'w': worker, 'dw': action, 'nonce': nonce})
req = Request(url+"?%s" % params, None, headers)
f = urlopen(req)
print "Action\n Worker %s [%s]\n\nStatus" % (worker,action)
balancer_status()
if __name__ == "__main__":
#if ARGS.list is not None:
if ARGS.list :
balancer_status()
elif ARGS.action and ARGS.worker:
balancer_manage(ARGS.action,ARGS.worker)
else : PARSER.print_help()