Skip to content

Commit 0777e0a

Browse files
committed
Add server status update python script
1 parent 487dd45 commit 0777e0a

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

src/update-server-statuses.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env python3
2+
3+
# @project bnetdocs-web <https://github.com/BNETDocs/bnetdocs-web/>
4+
5+
import argparse
6+
import requests
7+
import socket
8+
import json
9+
from concurrent.futures import ThreadPoolExecutor, as_completed
10+
11+
CONFIG_FILE = "../etc/config.phoenix.json"
12+
13+
def load_config(config_file):
14+
with open(config_file) as f:
15+
return json.load(f)
16+
17+
def fetch_servers(api_url):
18+
response = requests.get(api_url)
19+
response.raise_for_status()
20+
servers = response.json().get("servers", [])
21+
return servers
22+
23+
def check_server_status(server, updates, timeout):
24+
ip = server["address"]
25+
port = server["port"]
26+
status_bitmask = server["status_bitmask"]
27+
id = server["id"]
28+
29+
if status_bitmask & 2: # Disabled
30+
print(f"Skipping {ip}:{port}")
31+
return
32+
33+
try:
34+
with socket.create_connection((ip, port), timeout=timeout):
35+
new_status = 1 # online
36+
if not (status_bitmask & 1): # was offline
37+
updates[id] = new_status
38+
print(f"{ip}:{port} status updated to online")
39+
except socket.timeout:
40+
print(f"Timeout occurred while checking {ip}:{port}")
41+
new_status = 0 # offline
42+
if status_bitmask & 1: # was online
43+
updates[id] = new_status
44+
print(f"{ip}:{port} status updated to offline due to timeout")
45+
except socket.error:
46+
print(f"Error occurred while checking {ip}:{port}")
47+
new_status = 0 # offline
48+
if status_bitmask & 1: # was online
49+
updates[id] = new_status
50+
print(f"{ip}:{port} status updated to offline due to error")
51+
52+
def submit_updates(job_url, job_token, updates):
53+
for server_id, status in updates.items():
54+
data = {'id': server_id, 'job_token': job_token, 'status': status}
55+
response = requests.post(job_url, data=data)
56+
response.raise_for_status()
57+
print(f"Submitted update for server {server_id} with status {status}")
58+
59+
def main():
60+
parser = argparse.ArgumentParser(description="Server status checker")
61+
parser.add_argument("--collection_url", required=True, help="URL to fetch servers")
62+
parser.add_argument("--submission_url", required=True, help="URL to submit updates")
63+
parser.add_argument("--threads", type=int, default=10, help="Number of threads for checking servers")
64+
parser.add_argument("--timeout", type=int, default=5, help="Timeout for each server connection in seconds")
65+
args = parser.parse_args()
66+
67+
config = load_config(CONFIG_FILE)
68+
job_token = config["bnetdocs"]["server_update_job_token"]
69+
70+
servers = fetch_servers(args.collection_url)
71+
updates = {}
72+
73+
with ThreadPoolExecutor(max_workers=args.threads) as executor:
74+
futures = {executor.submit(check_server_status, server, updates, args.timeout): server for server in servers}
75+
for future in as_completed(futures):
76+
future.result() # Collect results
77+
78+
if updates:
79+
submit_updates(args.submission_url, job_token, updates)
80+
81+
if __name__ == "__main__":
82+
main()
83+

0 commit comments

Comments
 (0)