feat: 综合平台services多服务支持+SW网络优先缓存修复+适配层/定时任务完善
This commit is contained in:
+10
-4
@@ -34,15 +34,21 @@ def collect_metrics() -> dict:
|
||||
|
||||
|
||||
def _get_ip() -> str:
|
||||
"""获取本机出口 IP(不实际发包)"""
|
||||
"""获取本机出口 IP(UDP 不实际发包;设超时避免无网环境阻塞)"""
|
||||
s = None
|
||||
try:
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
s.settimeout(2) # 无网络环境下 connect 可能阻塞,加超时保护
|
||||
s.connect(("8.8.8.8", 80))
|
||||
ip = s.getsockname()[0]
|
||||
s.close()
|
||||
return ip
|
||||
return s.getsockname()[0]
|
||||
except Exception:
|
||||
return ""
|
||||
finally:
|
||||
if s is not None:
|
||||
try:
|
||||
s.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def collect_server_info() -> dict:
|
||||
|
||||
+20
-10
@@ -4,10 +4,12 @@
|
||||
VPS_MANAGER_URL 中心地址,如 http://100.89.0.11:8000
|
||||
VPS_ASSET_ID 本机对应的资产 ID
|
||||
VPS_AGENT_KEY Agent Key(中心配置了 AGENT_KEY 时必填)
|
||||
VPS_AGENT_RETRY 上报失败重试次数(默认 2,指数退避)
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
import httpx
|
||||
|
||||
@@ -18,6 +20,7 @@ def main() -> int:
|
||||
url = os.environ.get("VPS_MANAGER_URL", "").rstrip("/")
|
||||
asset_id = os.environ.get("VPS_ASSET_ID", "")
|
||||
key = os.environ.get("VPS_AGENT_KEY", "")
|
||||
max_retry = int(os.environ.get("VPS_AGENT_RETRY", "2"))
|
||||
|
||||
if not url or not asset_id:
|
||||
print("错误:需设置 VPS_MANAGER_URL 和 VPS_ASSET_ID")
|
||||
@@ -31,16 +34,23 @@ def main() -> int:
|
||||
}
|
||||
headers = {"X-Agent-Key": key} if key else {}
|
||||
|
||||
try:
|
||||
resp = httpx.post(
|
||||
f"{url}/api/agent/report", json=payload, headers=headers, timeout=30
|
||||
)
|
||||
print(f"上报完成 HTTP {resp.status_code}: {resp.text}")
|
||||
resp.raise_for_status()
|
||||
return 0
|
||||
except Exception as e: # noqa: BLE001
|
||||
print(f"上报失败: {e}")
|
||||
return 1
|
||||
for attempt in range(max_retry + 1):
|
||||
try:
|
||||
resp = httpx.post(
|
||||
f"{url}/api/agent/report", json=payload, headers=headers, timeout=30
|
||||
)
|
||||
print(f"上报完成 HTTP {resp.status_code}: {resp.text}")
|
||||
resp.raise_for_status()
|
||||
return 0
|
||||
except Exception as e: # noqa: BLE001
|
||||
if attempt < max_retry:
|
||||
wait = 2 ** attempt
|
||||
print(f"上报失败({attempt + 1}/{max_retry + 1}),{wait}s 后重试: {e}")
|
||||
time.sleep(wait)
|
||||
else:
|
||||
print(f"上报失败(已重试 {max_retry} 次): {e}")
|
||||
return 1
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user