#/usr/local/etc/haproxy.conf global daemon stats socket /var/run/haproxy.sock mode 600 level admin stats timeout 2m defaults log global mode tcp option tcplog option dontlognull frontend fr_rdp mode tcp bind *:3389 name rdp log global option tcplog tcp-request inspect-delay 2s tcp-request content accept if RDP_COOKIE default_backend BK_RDP backend BK_RDP mode tcp balance leastconn timeout server 5s timeout connect 4s log global option tcplog stick-table type string len 32 size 10k expire 8h stick on rdp_cookie(mstshash),bytes(0,6) stick on rdp_cookie(mstshash) option tcp-check tcp-check connect port 3389 default-server inter 3s rise 2 fall 3 server TS01 172.16.50.11:3389 weight 10 check server TS02 172.16.50.12:3389 weight 20 check server TS03 172.16.50.13:3389 weight 10 check server TS04 172.16.50.14:3389 weight 20 check server TS05 172.16.50.15:3389 weight 10 check server TS06 172.16.50.16:3389 weight 10 check server TS07 172.16.50.17:3389 weight 20 check server TS08 172.16.50.18:3389 weight 20 check listen stats bind *:9000 mode http stats enable #stats hide-version stats show-node stats realm Haproxy\ Statistics stats uri /
#!/usr/bin/env python import sys import socket import re import subprocess haproxyConf = '/usr/local/etc/haproxy.conf' def restart(): backends = [] with open(haproxyConf) as f: for line in f: lines = line.split(' ') if lines[0] == 'backend': backends.append(lines[1].strip('\n')) for backend in backends: getDataTables(backend) rebootHa() for backend in backends: insertDataTables(backend) # Writes data from stik-tables to external files def getDataTables(table): print table tmp_f = open('/tmp/tmp.' + table,'w') tableVal = {} c = socket.socket( socket.AF_UNIX ) c.connect("/var/run/haproxy.sock") c.send("prompt\r\n") c.send("show table " + table + "\r\n") d = c.recv(10240) for line in d.split('\n'): if re.search('^[a-zA-Z_0-9]',line): line = line.split(' ') del line[0] for item in line: key = item.split('=')[0] val = item.split('=')[1] tableVal[key] = val print tableVal['key'] print tableVal['server_id'] tmp_f.write(tableVal['key'] + ',' + tableVal['server_id'] + '\n') tmp_f.close() def rebootHa(): subprocess.call("/usr/local/etc/rc.d/haproxy reload", shell=True) # Writes data from files to stik-tables def insertDataTables(table): tmp_f = open('/tmp/tmp.' + table,'r') c = socket.socket( socket.AF_UNIX ) c.connect("/var/run/haproxy.sock") c.send("prompt\r\n") for line in tmp_f: line = line.split(',') print "set table " + table + " key " + line[0] + " data.server_id " + line[1] c.send("set table " + table + " key " + line[0] + " data.server_id " + line[1] +"\r\n") c.recv(10240) c.close() restart()
#/usr/local/etc/haproxy.conf global daemon stats socket /var/run/haproxy.sock mode 600 level admin stats timeout 2m defaults log global mode tcp option tcplog option dontlognull frontend fr_rdp mode tcp bind *:3389 name rdp #timeout client 1h log global option tcplog tcp-request inspect-delay 2s tcp-request content accept if RDP_COOKIE acl Accounting_ACL rdp_cookie(mstshash),bytes(0,6) -m str -i -f /usr/local/etc/haproxy/Accounting acl Marketing_ACL rdp_cookie(mstshash),bytes(0,6) -m str -i -f /usr/local/etc/haproxy/Marketing acl Sales_ACL rdp_cookie(mstshash),bytes(0,6) -m str -i -f /usr/local/etc/haproxy/Sales use_backend Accounting_BK if Accounting_ACL use_backend Marketing_BK if Marketing_ACL use_backend Sales_BK if Sales_ACL default_backend DF_RDP backend DF_RDP mode tcp balance leastconn log global option tcplog stick-table type string len 32 size 10k expire 8h stick on rdp_cookie(mstshash),bytes(0,6) option tcp-check tcp-check connect port 3389 default-server inter 3s rise 2 fall 3 server TS01 172.16.50.11:3389 weight 10 check server TS02 172.16.50.12:3389 weight 10 check backend Accounting_BK mode tcp balance leastconn log global stick-table type string len 32 size 10k expire 8h stick on rdp_cookie(mstshash),bytes(0,6) option tcplog tcp-check connect port 3389 default-server inter 3s rise 2 fall 3 server TS03 172.16.50.13:3389 weight 10 check server TS04 172.16.50.14:3389 weight 10 check backend Marketing_BK mode tcp balance leastconn log global stick-table type string len 32 size 10k expire 8h stick on rdp_cookie(mstshash),bytes(0,6) option tcplog tcp-check connect port 3389 default-server inter 3s rise 2 fall 3 server TS05 172.16.50.15:3389 weight 10 check server TS06 172.16.50.16:3389 weight 10 check backend Sales_BK mode tcp balance leastconn log global stick-table type string len 32 size 10k expire 8h stick on rdp_cookie(mstshash),bytes(0,6) option tcplog tcp-check connect port 3389 default-server inter 3s rise 2 fall 3 server TS07 172.16.50.17:3389 weight 10 check server TS08 172.16.50.18:3389 weight 10 check listen stats bind *:9000 mode http stats enable #stats hide-version stats show-node stats realm Haproxy\ Statistics stats uri /
#!/usr/bin/env python import sys import ldap import socket import re import subprocess ldapDomain = '' ldapUser = '' ldapPass = '' ldapDN = '' # OU=GROUPS,DC=domain,DC=tld' haproxyConf = '/usr/local/etc/haproxy.conf' action = sys.argv[1] # Get users from Active Directory Groups and store it to files def getADGroups(): l = ldap.open(ldapDomain) l.simple_bind_s(ldapUser,ldapPass) f = open('/usr/local/etc/haproxy/' + groupName,'w') results = l.search_s("cn=%s, %s" % (groupName, ldapDN), ldap.SCOPE_BASE) for result in results: result_dn = result[0] result_attrs = result[1] if "member" in result_attrs: for member in result_attrs["member"]: f.write(member.split(',')[0].split('=')[1] + '\n') f.close() restart() # Searching stik-tables to save it and to restore after reload def restart(): backends = [] with open(haproxyConf) as f: for line in f: lines = line.split(' ') if lines[0] == 'backend': backends.append(lines[1].strip('\n')) for backend in backends: getDataTables(backend) rebootHa() for backend in backends: insertDataTables(backend) # Writes data from stik-tables to external files def getDataTables(table): print table tmp_f = open('/tmp/tmp.' + table,'w') tableVal = {} c = socket.socket( socket.AF_UNIX ) c.connect("/var/run/haproxy.sock") c.send("prompt\r\n") c.send("show table " + table + "\r\n") d = c.recv(10240) for line in d.split('\n'): if re.search('^[a-zA-Z_0-9]',line): line = line.split(' ') del line[0] for item in line: key = item.split('=')[0] val = item.split('=')[1] tableVal[key] = val print tableVal['key'] print tableVal['server_id'] tmp_f.write(tableVal['key'] + ',' + tableVal['server_id'] + '\n') tmp_f.close() def rebootHa(): #pass subprocess.call("/usr/local/etc/rc.d/haproxy reload", shell=True) # Writes data from files to stik-tables def insertDataTables(table): #pass tmp_f = open('/tmp/tmp.' + table,'r') #tableVal = {} c = socket.socket( socket.AF_UNIX ) c.connect("/var/run/haproxy.sock") c.send("prompt\r\n") for line in tmp_f: line = line.split(',') print "set table " + table + " key " + line[0] + " data.server_id " + line[1] c.send("set table " + table + " key " + line[0] + " data.server_id " + line[1] +"\r\n") c.recv(10240) c.close() if action == 'restart': restart() if action == 'group': groupName = sys.argv[2] getADGroups()
Source: https://habr.com/ru/post/335872/
All Articles