Update haproxy example
parent
e31af763fd
commit
71fd22f2f9
|
@ -2,23 +2,22 @@
|
||||||
# Authors: yeasy.github.com
|
# Authors: yeasy.github.com
|
||||||
# Date: 2015-11-15
|
# Date: 2015-11-15
|
||||||
|
|
||||||
|
version: "3.9"
|
||||||
|
services:
|
||||||
weba:
|
weba:
|
||||||
build: ./web
|
build: ./web
|
||||||
expose:
|
expose:
|
||||||
- 80
|
- 80
|
||||||
|
|
||||||
webb:
|
webb:
|
||||||
build: ./web
|
build: ./web
|
||||||
expose:
|
expose:
|
||||||
- 80
|
- 80
|
||||||
|
|
||||||
webc:
|
webc:
|
||||||
build: ./web
|
build: ./web
|
||||||
expose:
|
expose:
|
||||||
- 80
|
- 80
|
||||||
|
|
||||||
haproxy:
|
haproxy:
|
||||||
image: haproxy:1.6
|
image: haproxy:2.7
|
||||||
volumes:
|
volumes:
|
||||||
- ./haproxy:/haproxy-override
|
- ./haproxy:/haproxy-override
|
||||||
- ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
- ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||||
|
@ -28,5 +27,4 @@ haproxy:
|
||||||
- webc
|
- webc
|
||||||
ports:
|
ports:
|
||||||
- "80:80"
|
- "80:80"
|
||||||
- "70:70"
|
- "70:70" # haproxy admin console, login with user:pass
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
global
|
global
|
||||||
log 127.0.0.1 local0
|
log /dev/log local0
|
||||||
log 127.0.0.1 local1 notice
|
log /dev/log local1 notice
|
||||||
maxconn 4096
|
maxconn 4096
|
||||||
|
|
||||||
defaults
|
defaults
|
||||||
|
@ -8,9 +8,9 @@ defaults
|
||||||
mode http
|
mode http
|
||||||
option httplog
|
option httplog
|
||||||
option dontlognull
|
option dontlognull
|
||||||
timeout connect 5000ms
|
timeout connect 5000
|
||||||
timeout client 50000ms
|
timeout client 50000
|
||||||
timeout server 50000ms
|
timeout server 50000
|
||||||
|
|
||||||
listen stats
|
listen stats
|
||||||
bind 0.0.0.0:70
|
bind 0.0.0.0:70
|
||||||
|
@ -29,10 +29,10 @@ frontend balancer
|
||||||
|
|
||||||
backend web_backends
|
backend web_backends
|
||||||
mode http
|
mode http
|
||||||
option forwardfor
|
|
||||||
balance roundrobin
|
balance roundrobin
|
||||||
|
option forwardfor
|
||||||
|
#option httpchk GET /
|
||||||
|
option httpchk HEAD /healthcheck.html HTTP/1.1
|
||||||
server weba weba:80 check
|
server weba weba:80 check
|
||||||
server webb webb:80 check
|
server webb webb:80 check
|
||||||
server webc webc:80 check
|
server webc webc:80 check
|
||||||
option httpchk GET /
|
|
||||||
http-check expect status 200
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
FROM python:2.7
|
FROM python:3.9
|
||||||
WORKDIR /code
|
WORKDIR /code
|
||||||
ADD . /code
|
ADD . /code
|
||||||
EXPOSE 80
|
EXPOSE 80
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
|
||||||
|
Got request from HAProxy.
|
|
@ -1,37 +1,46 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# authors: yeasy.github.com
|
# authors: yeasy.github.com
|
||||||
# date: 2013-07-05
|
# date: 2013-07-05
|
||||||
|
# update: 2023-03-02
|
||||||
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import BaseHTTPServer
|
|
||||||
from SimpleHTTPServer import SimpleHTTPRequestHandler
|
|
||||||
import socket
|
|
||||||
import fcntl
|
|
||||||
import struct
|
|
||||||
import pickle
|
|
||||||
from datetime import datetime
|
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
from datetime import datetime
|
||||||
|
import fcntl
|
||||||
|
from http.server import HTTPServer, SimpleHTTPRequestHandler
|
||||||
|
import pickle
|
||||||
|
import socket
|
||||||
|
import struct
|
||||||
|
|
||||||
|
|
||||||
class HandlerClass(SimpleHTTPRequestHandler):
|
class HandlerClass(SimpleHTTPRequestHandler):
|
||||||
def get_ip_address(self, ifname):
|
def get_ip_address(self, ifname):
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
"""
|
||||||
|
Get the IP from the network interface
|
||||||
|
"""
|
||||||
|
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
|
||||||
return socket.inet_ntoa(fcntl.ioctl(
|
return socket.inet_ntoa(fcntl.ioctl(
|
||||||
s.fileno(),
|
s.fileno(),
|
||||||
0x8915, # SIOCGIFADDR
|
0x8915, # SIOCGIFADDR
|
||||||
struct.pack('256s', ifname[:15])
|
struct.pack('256s', ifname[:15].encode())
|
||||||
)[20:24])
|
)[20:24])
|
||||||
|
|
||||||
def log_message(self, format, *args):
|
def log_message(self, format, *args):
|
||||||
if len(args) < 3 or "200" not in args[1]:
|
"""
|
||||||
return
|
Generate the index.html
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
request = pickle.load(open("pickle_data.txt","r"))
|
request = pickle.load(open("pickle_data.txt", "rb"))
|
||||||
except:
|
except:
|
||||||
request = OrderedDict()
|
request = OrderedDict()
|
||||||
|
if len(args) < 3 or "200" not in args[1]:
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
time_now = datetime.now()
|
time_now = datetime.now()
|
||||||
ts = time_now.strftime('%Y-%m-%d %H:%M:%S')
|
ts = time_now.strftime('%H:%M:%S')
|
||||||
server = self.get_ip_address('eth0')
|
|
||||||
host = self.address_string()
|
host = self.address_string()
|
||||||
addr_pair = (host,server)
|
addr_pair = (host, self.get_ip_address('eth0'))
|
||||||
if addr_pair not in request:
|
if addr_pair not in request:
|
||||||
request[addr_pair] = [1, ts]
|
request[addr_pair] = [1, ts]
|
||||||
else:
|
else:
|
||||||
|
@ -39,30 +48,40 @@ class HandlerClass(SimpleHTTPRequestHandler):
|
||||||
del request[addr_pair]
|
del request[addr_pair]
|
||||||
request[addr_pair] = [num, ts]
|
request[addr_pair] = [num, ts]
|
||||||
file = open("index.html", "w")
|
file = open("index.html", "w")
|
||||||
file.write("<!DOCTYPE html> <html> <body><center><h1><font color=\"blue\" face=\"Georgia, Arial\" size=8><em>HA</em></font> Webpage Visit Results</h1>");
|
file.write(
|
||||||
|
"<!DOCTYPE html> <html> <body><center><h1><font color=\"blue\" face=\"Georgia, Arial\" size=8><em>HA</em></font> Webpage Visit Results</h1>")
|
||||||
for pair in request:
|
for pair in request:
|
||||||
if pair[0] == host:
|
if pair[0] == host:
|
||||||
guest = "LOCAL: " + pair[0]
|
guest = "LOCAL: " + pair[0]
|
||||||
else:
|
else:
|
||||||
guest = pair[0]
|
guest = pair[0]
|
||||||
if (time_now-datetime.strptime(request[pair][1],'%Y-%m-%d %H:%M:%S')).seconds < 3:
|
if (time_now - datetime.strptime(request[pair][1],
|
||||||
file.write("<p style=\"font-size:150%\" >#"+ str(request[pair][1]) +": <font color=\"red\">"+str(request[pair][0])+ "</font> requests " + "from <<font color=\"blue\">"+guest+"</font>> to WebServer <<font color=\"blue\">"+pair[1]+"</font>></p>")
|
'%H:%M:%S')).seconds < 3:
|
||||||
|
file.write("<p style=\"font-size:150%\" >" + str(
|
||||||
|
request[pair][1]) + ": <font color=\"red\">" + str(
|
||||||
|
request[pair][
|
||||||
|
0]) + "</font> requests " + "from <<font color=\"blue\">" + guest + "</font>> to WebServer <<font color=\"blue\">" +
|
||||||
|
pair[1] + "</font>> </p>")
|
||||||
else:
|
else:
|
||||||
file.write("<p style=\"font-size:150%\" >#"+ str(request[pair][1]) +": <font color=\"maroon\">"+str(request[pair][0])+ "</font> requests " + "from <<font color=\"navy\">"+guest+"</font>> to WebServer <<font color=\"navy\">"+pair[1]+"</font>></p>")
|
file.write("<p style=\"font-size:150%\" >" + str(
|
||||||
file.write("</center></body> </html>");
|
request[pair][1]) + ": <font color=\"maroon\">" + str(
|
||||||
|
request[pair][
|
||||||
|
0]) + "</font> requests " + "from <<font color=\"navy\">" + guest + "</font>> to WebServer <<font color=\"navy\">" +
|
||||||
|
pair[1] + "</font>> </p>")
|
||||||
|
file.write("</center></body> </html>")
|
||||||
file.close()
|
file.close()
|
||||||
pickle.dump(request,open("pickle_data.txt","w"))
|
with open('pickle_data.txt', 'wb') as handle:
|
||||||
|
pickle.dump(request, handle, protocol=pickle.HIGHEST_PROTOCOL)
|
||||||
|
except Exception as e:
|
||||||
|
print("Error:", e)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
try:
|
try:
|
||||||
ServerClass = BaseHTTPServer.HTTPServer
|
httpd = HTTPServer(('0.0.0.0', 80), HandlerClass)
|
||||||
Protocol = "HTTP/1.0"
|
|
||||||
addr = len(sys.argv) < 2 and "0.0.0.0" or sys.argv[1]
|
print("serving at port", 80)
|
||||||
port = len(sys.argv) < 3 and 80 or int(sys.argv[2])
|
|
||||||
HandlerClass.protocol_version = Protocol
|
|
||||||
httpd = ServerClass((addr, port), HandlerClass)
|
|
||||||
sa = httpd.socket.getsockname()
|
|
||||||
print "Serving HTTP on", sa[0], "port", sa[1], "..."
|
|
||||||
httpd.serve_forever()
|
httpd.serve_forever()
|
||||||
except:
|
except Exception as e:
|
||||||
exit()
|
print("Error:", e)
|
||||||
|
exit(1)
|
||||||
|
|
Loading…
Reference in New Issue