-
Notifications
You must be signed in to change notification settings - Fork 1
/
haproxy.py
72 lines (63 loc) · 2.67 KB
/
haproxy.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
#utils file is required for running commands in linux
#sudo apt install haproxy -y
#copy config file to /etc/haproxy/haproxy.cfg
#edit frontend/backend ip, port, mode (tcp or http), etc.
#use this script to customize backends
#Haproxy requires a restart after each reconfiguration, but using haproxyadmin, this is not required.
import haproxyadmin
from haproxyadmin import haproxy
import utils
utils.shell('touch /run/haproxy/admin.sock')
utils.shell('sudo systemctl restart haproxy.service')
utils.shell('sudo chown ubuntu /run/haproxy/admin.sock')
hap = haproxy.HAProxy(socket_dir='/run/haproxy')
print(hap.show_acl())
print('frontends')
frontends = hap.frontends()
for frontend in frontends:
print('name={0}, requests={1}, process_nb={2}'.format(frontend.name, frontend.requests, frontend.process_nb))
# print(frontend.maxconn)
# frontend.setmaxconn(50000)
# frontend.status
# frontend.disable()
def backend_on(server):
server.setstate(haproxyadmin.STATE_ENABLE)
server.setstate(haproxyadmin.STATE_READY)
def backend_off(server):
server.setstate(haproxyadmin.STATE_DISABLE)
server.setstate(haproxyadmin.STATE_DRAIN)
server.setstate(haproxyadmin.STATE_MAINT)
# print(hap.info())
print('backends')
backends = hap.backends()
for backend in backends:
servers = backend.servers()
# new_server = haproxyadmin.server.Server([haproxyadmin.internal.server._Server(backend, 'ww1', 'eer2')],'backendnodes')
# servers.append(new_server)
for server in servers:
print('***********')
if server.name == 'w1':
backend_on(server)
# server.address = '10.43.249.158'
server.setweight(50)
elif server.name == 'w2':
backend_off(server)
# server.address = '10.43.249.158'
server.setweight(50)
elif server.name == 'w7':
backend_off(server)
# server.address = '10.43.111.70'
server.setweight(50)
else:
backend_off(server)
# server.port=8080
# server.address = ''
# server.setweight(0)
print('name={0}, requests={1}, weight={2}, address={3}, port={4}, last_status={5}, check_status={6}, requests_per_process={7}, status={8}'.
format(server.name, server.requests, server.weight, server.address, server.port,server.last_status, server.check_status, server.requests_per_process(), server.status))
#servers accross all backends
# servers = hap.servers()
# for server in servers:
# server.port=8080
# # server.address = ''
# print(server.name, server.requests, server.weight, server.address, server.port,server.last_status, server.requests_per_process(), server.status)