Q1: Answer
هذا الالتزام موجود في:
42
q1-deploy-monitor/health-check.py
Normal file
42
q1-deploy-monitor/health-check.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
import time
|
||||
import json
|
||||
from datetime import datetime, timezone
|
||||
|
||||
API_URL = "http://localhost:3000"
|
||||
METRICS_FILE = "metrics.json"
|
||||
|
||||
print(f"Starting monitoring for {API_URL}... (Press Ctrl+C to stop)")
|
||||
|
||||
while True:
|
||||
metrics = {
|
||||
"status": "Down",
|
||||
"latency_ms": 0,
|
||||
"total_requests": 0,
|
||||
"timestamp": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||
}
|
||||
|
||||
try:
|
||||
start_time = time.time()
|
||||
health_req = urllib.request.urlopen(f"{API_URL}/health", timeout=5)
|
||||
latency = time.time() - start_time
|
||||
|
||||
if health_req.getcode() == 200:
|
||||
metrics["status"] = "Healthy"
|
||||
metrics["latency_ms"] = int(latency * 1000)
|
||||
|
||||
root_req = urllib.request.urlopen(f"{API_URL}/", timeout=5)
|
||||
root_data = json.loads(root_req.read().decode())
|
||||
metrics["total_requests"] = root_data.get("total_requests", 0)
|
||||
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
with open(METRICS_FILE, "w") as f:
|
||||
json.dump(metrics, f, indent=4)
|
||||
|
||||
current_time = datetime.now().strftime("%H:%M:%S")
|
||||
print(f"[{current_time}] Status: {metrics['status']} | Latency: {metrics['latency_ms']}ms | Requests: {metrics['total_requests']}")
|
||||
|
||||
time.sleep(30)
|
||||
المرجع في مشكلة جديدة
حظر مستخدم