-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhaproxyapi.py
More file actions
39 lines (33 loc) · 1.16 KB
/
haproxyapi.py
File metadata and controls
39 lines (33 loc) · 1.16 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
import requests
class haproxy:
def __init__(self, url, auth):
self.url = url
self.auth = auth
def enable(self, backend, server):
return self.post(backend, server, "enable")
def disable(self, backend, server):
return self.post(backend, server, "disable")
def post(self, backend, server, action):
payload = 's=%s&action=%s&b=%s' % (server, action, backend)
r = requests.post(self.url, auth=self.auth, data=payload, allow_redirects=False)
if r.status_code<>303:
return "Error (%d)" % int(r.status_code)
if 'DONE' not in r.headers['location']:
return "Error (%s)" % r.headers['location']
return "OK"
def stats(self):
r = requests.get("%s;csv" % self.url, auth=self.auth)
if r.status_code<>200:
return "Error (%d)" % int(r.status_code)
lines = r.text.split("\n")[:-1]
header = lines.pop(0).replace('# ','').strip(",").split(",")
rows = []
for l in lines:
rows.append(dict(zip(header, l.split(","))))
return rows
def status(self, backend, server):
stats = self.stats()
servers = {}
for s in stats:
servers[s['pxname']+':'+s['svname']] = s
return servers[backend+':'+server] if backend+':'+server in servers else "Not found"