my-openclaw / app.py
wbteve's picture
Update app.py
c9841fd verified
Raw
History Blame Contribute Delete
1.87 kB
from fastapi import FastAPI, Request
from datetime import datetime
from zoneinfo import ZoneInfo
import socket
import urllib.request
import psutil
app = FastAPI()
@app.get("/")
async def system_info(request: Request):
# 使用 Asia/Shanghai 时区
current_time = datetime.now(ZoneInfo("Asia/Shanghai")).strftime("%Y-%m-%d %H:%M:%S")
# 获取客户端 IP 地址
client_host = request.client.host
# 获取服务器端内网 IP 地址
server_host = socket.gethostbyname(socket.gethostname())
# 获取服务器端公网 IP 地址
try:
public_ip = urllib.request.urlopen("https://api.ipify.org").read().decode("utf-8")
except Exception:
public_ip = "Unavailable"
# 获取系统资源信息
memory = psutil.virtual_memory()
disk = psutil.disk_usage('/')
cpu_percent = psutil.cpu_percent(interval=1)
cpu_times = psutil.cpu_times_percent(interval=1)
# CPU 温度(不同平台支持情况不同)
try:
temps = psutil.sensors_temperatures()
cpu_temp = temps.get("coretemp", [{}])[0].current if "coretemp" in temps else "Unavailable"
except Exception:
cpu_temp = "Unavailable"
return {
"message": "Hello World!",
"timestamp": current_time,
"client_ip": client_host,
"server_ip": server_host,
"public_ip": public_ip,
"status": "success",
"system": {
"memory_total_MB": round(memory.total / 1024 / 1024, 2),
"memory_available_MB": round(memory.available / 1024 / 1024, 2),
"disk_total_GB": round(disk.total / 1024 / 1024 / 1024, 2),
"disk_free_GB": round(disk.free / 1024 / 1024 / 1024, 2),
"cpu_percent": cpu_percent,
"cpu_times_percent": cpu_times._asdict(),
"cpu_temperature": cpu_temp
}
}