|
4 | 4 | import subprocess
|
5 | 5 | import re
|
6 | 6 | import socket
|
| 7 | +from gnlpy import ipvs |
7 | 8 |
|
8 |
| -# Ignore the input from the finger protocol |
9 |
| -sys.stdin.readline() |
| 9 | +# Ignore the HTTP request |
| 10 | +while sys.stdin.readline().strip(): |
| 11 | + pass |
10 | 12 |
|
11 |
| -print("HTTP/1.0 200 OK") |
12 |
| -print("Content-type: text/html") |
13 |
| -print("") |
| 13 | +print("HTTP/1.0 200 OK\r") |
| 14 | +print("Content-type: text/html\r") |
| 15 | +print("\r") |
14 | 16 | print("<html><head><title>scripts.mit.edu server status</title></head><body><h1>scripts.mit.edu server status</h1><p>The following table shows a list of the servers that are currently handling web requests for scripts.mit.edu:</p>")
|
15 |
| -lines = subprocess.check_output(['/sbin/ipvsadm', '-L', '-n']).split(b'\n') |
16 |
| -realserver = re.compile('( -> )([^:]*):([^ ]*) *[^ ]* *([^ ]*) *([^ ]*) *([^ ]*)') |
17 |
| -fwm = re.compile('^FWM ([0-9]*)') |
18 |
| -ipaddress = re.compile('[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+') |
19 |
| -show = 1 |
20 |
| -header = '' |
21 |
| -for line in lines: |
22 |
| - line = line.decode('utf8') |
23 |
| - fwmline = fwm.match(line) |
24 |
| - if fwmline: |
25 |
| - if show: |
26 |
| - print("</table>") |
27 |
| - mark = fwmline.group(1) |
28 |
| - show = 1 |
29 |
| - if mark == '22': |
30 |
| - pool = 'Fedora 20' |
31 |
| - elif mark == '32': |
32 |
| - pool = 'Fedora 30' |
33 |
| - else: |
34 |
| - show = 0 |
35 |
| - if show: |
36 |
| - print("<p><b>%s servers</b></p><table>" % (pool)) |
37 |
| - print(header) |
38 |
| - elif show: |
39 |
| - realline = realserver.match(line) |
40 |
| - if not realline: |
41 |
| - continue |
42 |
| - (preamble, ip, port, weight, inactive, active) = realline.groups() |
43 |
| - target = ip |
44 |
| - if ipaddress.match(ip): |
45 |
| - try: |
46 |
| - target = socket.gethostbyaddr(ip)[0] |
47 |
| - except: |
48 |
| - pass |
49 |
| - line = '<tr><td>' + target + '</td><td>' + weight + '</td><td>' |
50 |
| - line += inactive + '</td><td>' + active + '</td></tr>' |
51 |
| - if not header: # The header isn't set yet - this is it! |
52 |
| - header = line |
53 |
| - show = 0 |
54 |
| - else: |
55 |
| - print(line) |
56 |
| -if show: |
| 17 | + |
| 18 | +def row(target, weight, inactive, active): |
| 19 | + try: |
| 20 | + target = socket.gethostbyaddr(target)[0] |
| 21 | + except: |
| 22 | + pass |
| 23 | + line = '<tr><td>' + target + '</td><td>' + str(weight) + '</td><td>' |
| 24 | + line += str(inactive) + '</td><td>' + str(active) + '</td></tr>' |
| 25 | + return line |
| 26 | + |
| 27 | +pools = ipvs.IpvsClient().get_pools() |
| 28 | +for pool in pools: |
| 29 | + mark = pool.service().fwmark() |
| 30 | + if mark == 22: |
| 31 | + pool_name = 'Fedora 20' |
| 32 | + elif mark == 32: |
| 33 | + pool_name = 'Fedora 30' |
| 34 | + # TODO: Add query parameter/URL for all pools? |
| 35 | + else: |
| 36 | + continue |
| 37 | + print("<table><tr style='text-align: left'><th>%s servers</th><th>Weight</th><th>ActiveConn</th><th>InActConn</th></tr>" % (pool_name,)) |
| 38 | + for dest in pool.dests(): |
| 39 | + print(row(dest.ip(), dest.weight(), dest.counters().get('active_conns', 0), dest.counters().get('inact_conns', 0))) |
57 | 40 | print("</table>")
|
58 | 41 | print("</body></html>")
|
0 commit comments