diff --git a/haproxy_web/docker-compose.yml b/haproxy_web/docker-compose.yml index 70cd6edb..5af09990 100644 --- a/haproxy_web/docker-compose.yml +++ b/haproxy_web/docker-compose.yml @@ -2,31 +2,29 @@ # Authors: yeasy.github.com # Date: 2015-11-15 -weba: - build: ./web - expose: - - 80 - -webb: - build: ./web - expose: - - 80 - -webc: - build: ./web - expose: - - 80 - -haproxy: - image: haproxy:1.6 - volumes: - - ./haproxy:/haproxy-override - - ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro - links: - - weba - - webb - - webc - ports: - - "80:80" - - "70:70" - +version: "3.9" +services: + weba: + build: ./web + expose: + - 80 + webb: + build: ./web + expose: + - 80 + webc: + build: ./web + expose: + - 80 + haproxy: + image: haproxy:2.7 + volumes: + - ./haproxy:/haproxy-override + - ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro + links: + - weba + - webb + - webc + ports: + - "80:80" + - "70:70" # haproxy admin console, login with user:pass \ No newline at end of file diff --git a/haproxy_web/haproxy/haproxy.cfg b/haproxy_web/haproxy/haproxy.cfg index cf12d344..e67280db 100644 --- a/haproxy_web/haproxy/haproxy.cfg +++ b/haproxy_web/haproxy/haproxy.cfg @@ -1,6 +1,6 @@ global - log 127.0.0.1 local0 - log 127.0.0.1 local1 notice + log /dev/log local0 + log /dev/log local1 notice maxconn 4096 defaults @@ -8,9 +8,9 @@ defaults mode http option httplog option dontlognull - timeout connect 5000ms - timeout client 50000ms - timeout server 50000ms + timeout connect 5000 + timeout client 50000 + timeout server 50000 listen stats bind 0.0.0.0:70 @@ -29,10 +29,10 @@ frontend balancer backend web_backends mode http - option forwardfor balance roundrobin + option forwardfor + #option httpchk GET / + option httpchk HEAD /healthcheck.html HTTP/1.1 server weba weba:80 check server webb webb:80 check server webc webc:80 check - option httpchk GET / - http-check expect status 200 diff --git a/haproxy_web/web/Dockerfile b/haproxy_web/web/Dockerfile index d0e74cd6..b72af3eb 100644 --- a/haproxy_web/web/Dockerfile +++ b/haproxy_web/web/Dockerfile @@ -1,4 +1,4 @@ -FROM python:2.7 +FROM python:3.9 WORKDIR /code ADD . /code EXPOSE 80 diff --git a/haproxy_web/web/index.html b/haproxy_web/web/index.html index e69de29b..f603828b 100644 --- a/haproxy_web/web/index.html +++ b/haproxy_web/web/index.html @@ -0,0 +1,2 @@ + +Got request from HAProxy. \ No newline at end of file diff --git a/haproxy_web/web/index.py b/haproxy_web/web/index.py index 2bc5d486..279a6a01 100644 --- a/haproxy_web/web/index.py +++ b/haproxy_web/web/index.py @@ -1,68 +1,87 @@ #!/usr/bin/python -#authors: yeasy.github.com -#date: 2013-07-05 +# authors: yeasy.github.com +# 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 datetime import datetime +import fcntl +from http.server import HTTPServer, SimpleHTTPRequestHandler +import pickle +import socket +import struct + class HandlerClass(SimpleHTTPRequestHandler): - def get_ip_address(self,ifname): - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - return socket.inet_ntoa(fcntl.ioctl( - s.fileno(), - 0x8915, # SIOCGIFADDR - struct.pack('256s', ifname[:15]) - )[20:24]) - def log_message(self, format, *args): - if len(args) < 3 or "200" not in args[1]: - return - try: - request = pickle.load(open("pickle_data.txt","r")) - except: - request=OrderedDict() - time_now = datetime.now() - ts = time_now.strftime('%Y-%m-%d %H:%M:%S') - server = self.get_ip_address('eth0') - host=self.address_string() - addr_pair = (host,server) - if addr_pair not in request: - request[addr_pair]=[1,ts] - else: - num = request[addr_pair][0]+1 - del request[addr_pair] - request[addr_pair]=[num,ts] - file=open("index.html", "w") - file.write("

HA Webpage Visit Results

"); - for pair in request: - if pair[0] == host: - guest = "LOCAL: "+pair[0] - else: - guest = pair[0] - if (time_now-datetime.strptime(request[pair][1],'%Y-%m-%d %H:%M:%S')).seconds < 3: - file.write("

#"+ str(request[pair][1]) +": "+str(request[pair][0])+ " requests " + "from <"+guest+"> to WebServer <"+pair[1]+">

") - else: - file.write("

#"+ str(request[pair][1]) +": "+str(request[pair][0])+ " requests " + "from <"+guest+"> to WebServer <"+pair[1]+">

") - file.write("
"); - file.close() - pickle.dump(request,open("pickle_data.txt","w")) + def get_ip_address(self, ifname): + """ + Get the IP from the network interface + """ + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: + return socket.inet_ntoa(fcntl.ioctl( + s.fileno(), + 0x8915, # SIOCGIFADDR + struct.pack('256s', ifname[:15].encode()) + )[20:24]) + + def log_message(self, format, *args): + """ + Generate the index.html + """ + try: + request = pickle.load(open("pickle_data.txt", "rb")) + except: + request = OrderedDict() + if len(args) < 3 or "200" not in args[1]: + return + + try: + time_now = datetime.now() + ts = time_now.strftime('%H:%M:%S') + host = self.address_string() + addr_pair = (host, self.get_ip_address('eth0')) + if addr_pair not in request: + request[addr_pair] = [1, ts] + else: + num = request[addr_pair][0] + 1 + del request[addr_pair] + request[addr_pair] = [num, ts] + file = open("index.html", "w") + file.write( + "

HA Webpage Visit Results

") + for pair in request: + if pair[0] == host: + guest = "LOCAL: " + pair[0] + else: + guest = pair[0] + if (time_now - datetime.strptime(request[pair][1], + '%H:%M:%S')).seconds < 3: + file.write("

" + str( + request[pair][1]) + ": " + str( + request[pair][ + 0]) + " requests " + "from <" + guest + "> to WebServer <" + + pair[1] + "

") + else: + file.write("

" + str( + request[pair][1]) + ": " + str( + request[pair][ + 0]) + " requests " + "from <" + guest + "> to WebServer <" + + pair[1] + "

") + file.write("
") + file.close() + 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__': - try: - ServerClass = BaseHTTPServer.HTTPServer - Protocol = "HTTP/1.0" - addr = len(sys.argv) < 2 and "0.0.0.0" or sys.argv[1] - 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() - except: - exit() + try: + httpd = HTTPServer(('0.0.0.0', 80), HandlerClass) + + print("serving at port", 80) + httpd.serve_forever() + except Exception as e: + print("Error:", e) + exit(1)