Spaces:
BG5
/
Running

BG5 commited on
Commit
58f7470
·
verified ·
1 Parent(s): 7d165bf

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +82 -1
api.py CHANGED
@@ -23,7 +23,87 @@ PORT_RANGE = (9300, 9700)
23
  CHROME_PATH = "google-chrome" # Linux下可用命令,或自定义绝对路径
24
  PROFILE_BASE = "/tmp/profiles"
25
 
 
 
 
 
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  def get_system_usage():
28
  cpu = subprocess.check_output("top -bn1 | grep 'Cpu(s)'", shell=True).decode().strip()
29
  mem = subprocess.check_output("free -m | grep Mem", shell=True).decode().strip()
@@ -56,8 +136,9 @@ def get_system_usage():
56
  "Free": f"{mem_free}MB",
57
  "Available": f"{mem_available}MB"
58
  }
 
59
  logger.info(f"CPU: {cpu_info}, Memory: {mem_info}")
60
- info = {"CPU": cpu_info, "Memory": mem_info}
61
  return info
62
 
63
 
 
23
  CHROME_PATH = "google-chrome" # Linux下可用命令,或自定义绝对路径
24
  PROFILE_BASE = "/tmp/profiles"
25
 
26
+ import re
27
+ import platform
28
+ import psutil
29
+ import datetime
30
 
31
+ def get_system_info():
32
+ # CPU型号
33
+ try:
34
+ with open("/proc/cpuinfo") as f:
35
+ cpuinfo = f.read()
36
+ model_match = re.search(r"model name\s*:\s*(.+)", cpuinfo)
37
+ cpu_model = model_match.group(1) if model_match else "Unknown"
38
+ except Exception:
39
+ cpu_model = "Unknown"
40
+
41
+ # GPU型号
42
+ try:
43
+ gpu_info = subprocess.check_output("nvidia-smi --query-gpu=name --format=csv,noheader", shell=True).decode().strip()
44
+ gpu_model = gpu_info.split('\n')[0] if gpu_info else "Unknown"
45
+ except Exception:
46
+ try:
47
+ lspci_info = subprocess.check_output("lspci | grep VGA", shell=True).decode().strip()
48
+ gpu_model = lspci_info.split(":")[-1].strip() if lspci_info else "Unknown"
49
+ except Exception:
50
+ gpu_model = "Unknown"
51
+
52
+ # 操作系统信息
53
+ os_name = platform.system()
54
+ os_version = platform.version()
55
+ os_release = platform.release()
56
+
57
+ # 主机名
58
+ hostname = socket.gethostname()
59
+
60
+ # 启动时间
61
+ boot_time = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
62
+
63
+ # 运行时长
64
+ uptime_seconds = int(time.time() - psutil.boot_time())
65
+ uptime = str(datetime.timedelta(seconds=uptime_seconds))
66
+
67
+ # 物理内存
68
+ mem = psutil.virtual_memory()
69
+ mem_total = f"{int(mem.total / 1024 / 1024)}MB"
70
+
71
+ # CPU核心数
72
+ cpu_count_logical = psutil.cpu_count()
73
+ cpu_count_physical = psutil.cpu_count(logical=False)
74
+
75
+ # 磁盘
76
+ disk = psutil.disk_usage('/')
77
+ disk_total = f"{int(disk.total / 1024 / 1024 / 1024)}GB"
78
+ disk_used = f"{int(disk.used / 1024 / 1024 / 1024)}GB"
79
+
80
+ # 当前用户
81
+ try:
82
+ user = os.getlogin()
83
+ except Exception:
84
+ user = "Unknown"
85
+
86
+ # IP地址
87
+ try:
88
+ ip = socket.gethostbyname(hostname)
89
+ except Exception:
90
+ ip = "Unknown"
91
+
92
+ return {
93
+ "cpu_model": cpu_model,
94
+ "gpu_model": gpu_model,
95
+ "os": f"{os_name} {os_release} ({os_version})",
96
+ "hostname": hostname,
97
+ "boot_time": boot_time,
98
+ "uptime": uptime,
99
+ "mem_total": mem_total,
100
+ "cpu_count_logical": cpu_count_logical,
101
+ "cpu_count_physical": cpu_count_physical,
102
+ "disk_total": disk_total,
103
+ "disk_used": disk_used,
104
+ "user": user,
105
+ "ip": ip
106
+ }
107
  def get_system_usage():
108
  cpu = subprocess.check_output("top -bn1 | grep 'Cpu(s)'", shell=True).decode().strip()
109
  mem = subprocess.check_output("free -m | grep Mem", shell=True).decode().strip()
 
136
  "Free": f"{mem_free}MB",
137
  "Available": f"{mem_available}MB"
138
  }
139
+ sys_info = get_system_info()
140
  logger.info(f"CPU: {cpu_info}, Memory: {mem_info}")
141
+ info = {"CPU": cpu_info, "Memory": mem_info, "System": sys_info}
142
  return info
143
 
144