Skip to content

Instantly share code, notes, and snippets.

@npeters
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save npeters/b143de7322bbbe4204f6 to your computer and use it in GitHub Desktop.
Save npeters/b143de7322bbbe4204f6 to your computer and use it in GitHub Desktop.
HaProxy Config
global
log 127.0.0.1 local1 notice
#chroot /var/lib/haproxy
user haproxy
group haproxy
# daemon
stats socket /var/run/haproxy_${ENV}.sock mode 600 level admin
defaults
log global
mode http
option httplog
option dontlognull
option http-server-close
option forwardfor
contimeout 5000
clitimeout 50000
srvtimeout 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
unique-id-format %{+X}o\ %ci:%cp_%fi:%fp_%Ts_%rt:%pid
unique-id-header X-Unique-ID
rate-limit sessions 50
listen stats :80
stats enable
stats uri /stats
peers mypeers
peer haproxy1 127.0.0.1:1024
peer haproxy2 127.0.0.1:1023
frontend http-in
# PORT variable ENV du Bash
bind *:${PORT}
maxconn 10
option httplog
log global
tcp-request content track-sc0 src
# Stick Table Definitions
# - conn_cur: count active connections
# - conn_rate(10s): average incoming connection rate over 10 seconds
# - http_req_rate(10s): Monitors the number of request sent by an IP over a period of 10 seconds
# - http_err_rate(10s): Monitors the number of errors generated by an IP over a period of 10 seconds
stick-table type ip size 500k expire 30s store conn_cur,conn_rate(10s),http_req_rate(10m),http_err_rate(10s) peers mypeers
# parse les requests HTTP: GET /ctx/eck/xtc/content HTTP/1.1 =>
reqrep ^([^\ :]*)\ /ctx/(.*)/xtc/([^\ :]*)\ ([^\ :]*) \1\ /\3&\2\ \4
# urlp parameter de l'url
acl version1 urlp(version) -i 1
# redirige vers une erreur si l'acl est tre
http-request deny if { sc0_http_req_rate() gt 100 }
use_backend neo4j-slaves1
default_backend neo4j-slaves2
backend neo4j-slaves1
# type de headchek
option httpchk GET / HTTP/1.1\r\n
# activation du headchek sur un port
# maxconn
server s1 127.0.0.1:8081 maxconn 32 check port 8081
server s2 127.0.0.1:8082 maxconn 32
backend neo4j-slaves2
server s3 127.0.0.1:8083 maxconn 32
import sys
import BaseHTTPServer
import urlparse
from SimpleHTTPServer import SimpleHTTPRequestHandler
myname=""
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_HEAD(s):
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
def do_GET(s):
parsed_path = urlparse.urlparse(s.path)
message_parts = [
'CLIENT VALUES:',
'client_address=%s (%s)' % (s.client_address,
s.address_string()),
'command=%s' % s.command,
'path=%s' % s.path,
'real path=%s' % parsed_path.path,
'query=%s' % parsed_path.query,
'request_version=%s' % s.request_version,
'',
'SERVER VALUES:',
'server_version=%s' % s.server_version,
'sys_version=%s' % s.sys_version,
'protocol_version=%s' % s.protocol_version,
'',
'HEADERS RECEIVED:',
]
for name, value in sorted(s.headers.items()):
message_parts.append('%s=%s' % (name, value.rstrip()))
message_parts.append('')
message = '\r\n'.join(message_parts)
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
s.wfile.write(message)
s.wfile.write(myname)
HandlerClass = SimpleHTTPRequestHandler
ServerClass = BaseHTTPServer.HTTPServer
Protocol = "HTTP/1.0"
if sys.argv[1:]:
port = int(sys.argv[1])
myname = str(sys.argv[2])
else:
port = 8000
server_address = ('0.0.0.0', port)
HandlerClass.protocol_version = Protocol
httpd = ServerClass(server_address, MyHandler)
sa = httpd.socket.getsockname()
print "Serving HTTP on", sa[0], "port", sa[1], "..."
httpd.serve_forever()
import sys
import BaseHTTPServer
import urlparse
from SimpleHTTPServer import SimpleHTTPRequestHandler
from pprint import pprint
import urllib
#import signal
import subprocess
# import filecmp
myname=""
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_HEAD(s):
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
def do_POST(s):
parsed_path = urlparse.urlparse(s.path)
pprint(parsed_path)
if parsed_path.path == "/conf":
length = int(s.headers['Content-Length'])
post_data = urlparse.parse_qs(s.rfile.read(length).decode('utf-8'))
url = post_data['url']
f = urllib.urlopen(url[0])
conf = f.read()
with open('workfile', 'w') as configFile:
configFile.write(conf)
configFile.close
with open('haproxy.pid', 'r') as pidFile:
pid=pidFile.read
configFile.close
cmd = ["/home/npeters/Data/git/haproxy-manager/hello.sh"]
p = subprocess.Popen(cmd)
returncode = p.wait()
if returncode == 0 :
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
s.wfile.write("OK")
# for key, value in post_data.iteritems():
# print "%s=%s" % (key, value)
def do_GET(s):
parsed_path = urlparse.urlparse(s.path)
message_parts = [
'CLIENT VALUES:',
'client_address=%s (%s)' % (s.client_address,
s.address_string()),
'command=%s' % s.command,
'path=%s' % s.path,
'real path=%s' % parsed_path.path,
'query=%s' % parsed_path.query,
'request_version=%s' % s.request_version,
'',
'SERVER VALUES:',
'server_version=%s' % s.server_version,
'sys_version=%s' % s.sys_version,
'protocol_version=%s' % s.protocol_version,
'',
'HEADERS RECEIVED:',
]
for name, value in sorted(s.headers.items()):
message_parts.append('%s=%s' % (name, value.rstrip()))
message_parts.append('')
message = '\r\n'.join(message_parts)
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
s.wfile.write(message)
s.wfile.write(myname)
HandlerClass = SimpleHTTPRequestHandler
ServerClass = BaseHTTPServer.HTTPServer
Protocol = "HTTP/1.0"
if sys.argv[1:]:
port = int(sys.argv[1])
myname = str(sys.argv[2])
else:
port = 8001
server_address = ('0.0.0.0', port)
HandlerClass.protocol_version = Protocol
httpd = ServerClass(server_address, MyHandler)
sa = httpd.socket.getsockname()
print "Serving HTTP on %s port %s" % (sa[0],sa[1])
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment