SHARE
TWEET

Untitled

a guest May 21st, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. import requests
  4. import socket
  5. import sys
  6.  
  7. BACKEND_PREFIX = 'backend_'
  8. BUFFER_SIZE = 16
  9. SOCKET_PATH = '/var/run/hapee-lb.sock'
  10. SOCKET_TIMEOUT = 10
  11.  
  12. def get_client_backends():
  13.     pass
  14.  
  15. def get_haproxy_backends(stats):
  16.     backends = {}
  17.        
  18.     for line in stats:
  19.         values = line.split(',')
  20.         if len(values) > 80 and backend_values[0].startswith(BACKEND_PREFIX):
  21.             server = get_server(values)
  22.             if server['backend'] not backends:
  23.                 backends[server['backend'] = { 'active': {}, 'inactive': [] }
  24.             if server['state'] == 'MAINT':
  25.                 backends[server['backend']['inactive'].append(server)
  26.             else:
  27.                 backends[server['backend']['active'][server['address']] = server
  28.  
  29.     return backends
  30.  
  31. def get_server(values):
  32.     server = {}
  33.  
  34.     server['address'] = values[73]
  35.     server['backend'] = values[0]
  36.     server['name'] = values[1]
  37.     server['state'] = values[17]
  38.  
  39.     return server
  40.  
  41. def send_haproxy_command(command):
  42.     socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  43.     socket.settimeout(SOCKET_TIMEOUT)
  44.     try:
  45.         answer = ''
  46.         socket.connect(SOCKET_PATH)
  47.         socket.send(command)
  48.         while True:
  49.             data = socket.recv(BUFFER_SIZE)
  50.             if data:
  51.                 answer += data
  52.             else:
  53.                 break
  54.     except:
  55.         pass
  56.     finally:
  57.         socket.close()
  58.  
  59.     return answer
  60.  
  61. def update_backends():
  62.     stats = send_haproxy_command(server, "show stat\n").split("\n")
  63.     if not stats:
  64.         print('Failed to get stats from HAProxy socket')
  65.         sys.exit(1)
  66.  
  67.     client_backends = get_client_backends()
  68.     haproxy_backends = get_haproxy_backends(stats)
  69.  
  70.     for backend in client_backend:
  71.         for server in backend
  72.             if server['address'] in haproxy_backends[backend]['active']:
  73.                 del haproxy_backends[backend]['active'][server['address']]
  74.             else:
  75.                 if haproxy_backends[backend]['inactive']:
  76.                     inactive_server = haproxy_backends[backend]['inactive'].pop(0)
  77.                     ip, port = server['address'].split(':')
  78.                     send_haproxy_command("set server %s/%s addr %s port %s\n" %
  79.                             (server['backend'], server['name'], ip, port))
  80.                     send_haproxy_command("set server %s/%s state ready\n" % (BACKEND_NAME, server['name']))
  81.    
  82.     for backend in haproxy_backend:
  83.         for server in backend:
  84.             send_haproxy_command("set server %s/%s state maint\n" % (BACKEND_NAME, server['name']))
  85.  
  86.  
  87. if __name__ == "__main__":
  88.     update_backends()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top