Skip to content

Commit 0911cef

Browse files
committed
Optimize the server information retrieval
1 parent 9b3425d commit 0911cef

File tree

1 file changed

+65
-45
lines changed

1 file changed

+65
-45
lines changed

backend/utils/server_info.py

Lines changed: 65 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3+
from __future__ import annotations
4+
35
import os
46
import platform
57
import socket
68
import sys
79

810
from datetime import datetime, timedelta
911
from datetime import timezone as tz
12+
from typing import Dict, List
1013

1114
import psutil
1215

@@ -61,94 +64,111 @@ def fmt_timedelta(td: timedelta) -> str:
6164
:param td: 时间差对象
6265
:return:
6366
"""
64-
total_seconds = round(td.total_seconds())
65-
return ServerInfo.fmt_seconds(total_seconds)
67+
return ServerInfo.fmt_seconds(round(td.total_seconds()))
6668

6769
@staticmethod
6870
def get_cpu_info() -> dict[str, float | int]:
6971
"""获取 CPU 信息"""
70-
cpu_info = {'usage': round(psutil.cpu_percent(percpu=False), 2)} # %
72+
cpu_info = {
73+
'usage': round(psutil.cpu_percent(interval=0.1), 2), # %
74+
'logical_num': psutil.cpu_count(logical=True) or 0,
75+
'physical_num': psutil.cpu_count(logical=False) or 0,
76+
'max_freq': 0.0,
77+
'min_freq': 0.0,
78+
'current_freq': 0.0,
79+
}
7180

7281
try:
73-
# CPU 频率信息,最大、最小和当前频率
74-
cpu_freq = psutil.cpu_freq()
75-
cpu_info.update({
76-
'max_freq': round(cpu_freq.max, 2), # MHz
77-
'min_freq': round(cpu_freq.min, 2), # MHz
78-
'current_freq': round(cpu_freq.current, 2), # MHz
79-
})
82+
if hasattr(psutil, 'cpu_freq'):
83+
cpu_freq = psutil.cpu_freq()
84+
if cpu_freq: # Some systems return None
85+
cpu_info.update({
86+
'max_freq': round(cpu_freq.max, 2),
87+
'min_freq': round(cpu_freq.min, 2),
88+
'current_freq': round(cpu_freq.current, 2),
89+
})
8090
except Exception:
81-
cpu_info.update({'max_freq': 0, 'min_freq': 0, 'current_freq': 0})
91+
pass
8292

83-
# CPU 逻辑核心数,物理核心数
84-
cpu_info.update({
85-
'logical_num': psutil.cpu_count(logical=True),
86-
'physical_num': psutil.cpu_count(logical=False),
87-
})
8893
return cpu_info
8994

9095
@staticmethod
9196
def get_mem_info() -> dict[str, float]:
9297
"""获取内存信息"""
9398
mem = psutil.virtual_memory()
99+
gb_factor = 1024**3
94100
return {
95-
'total': round(mem.total / 1024 / 1024 / 1024, 2), # GB
96-
'used': round(mem.used / 1024 / 1024 / 1024, 2), # GB
97-
'free': round(mem.available / 1024 / 1024 / 1024, 2), # GB
98-
'usage': round(mem.percent, 2), # %
101+
'total': round(mem.total / gb_factor, 2),
102+
'used': round(mem.used / gb_factor, 2),
103+
'free': round(mem.available / gb_factor, 2),
104+
'usage': round(mem.percent, 2),
99105
}
100106

101107
@staticmethod
102-
def get_sys_info() -> dict[str, str]:
108+
def get_sys_info() -> Dict[str, str]:
103109
"""获取服务器信息"""
110+
hostname = socket.gethostname()
111+
ip = '127.0.0.1'
112+
104113
try:
105-
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sk:
106-
sk.connect(('8.8.8.8', 80))
107-
ip = sk.getsockname()[0]
108-
except socket.gaierror:
109-
ip = '127.0.0.1'
114+
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
115+
s.settimeout(0.5)
116+
s.connect(('8.8.8.8', 80))
117+
ip = s.getsockname()[0]
118+
except (socket.gaierror, socket.timeout, OSError):
119+
pass
110120

111121
return {
112-
'name': socket.gethostname(),
122+
'name': hostname,
113123
'ip': ip,
114124
'os': platform.system(),
115125
'arch': platform.machine(),
116126
}
117127

118128
@staticmethod
119-
def get_disk_info() -> list[dict[str, str]]:
129+
def get_disk_info() -> List[Dict[str, str]]:
120130
"""获取磁盘信息"""
121131
disk_info = []
122-
for disk in psutil.disk_partitions():
123-
usage = psutil.disk_usage(disk.mountpoint)
124-
disk_info.append({
125-
'dir': disk.mountpoint,
126-
'type': disk.fstype,
127-
'device': disk.device,
128-
'total': ServerInfo.format_bytes(usage.total),
129-
'free': ServerInfo.format_bytes(usage.free),
130-
'used': ServerInfo.format_bytes(usage.used),
131-
'usage': f'{round(usage.percent, 2)} %',
132-
})
132+
for partition in psutil.disk_partitions(all=False):
133+
try:
134+
usage = psutil.disk_usage(partition.mountpoint)
135+
disk_info.append({
136+
'dir': partition.mountpoint,
137+
'type': partition.fstype,
138+
'device': partition.device,
139+
'total': ServerInfo.format_bytes(usage.total),
140+
'free': ServerInfo.format_bytes(usage.free),
141+
'used': ServerInfo.format_bytes(usage.used),
142+
'usage': f'{usage.percent:.2f}%',
143+
})
144+
except (PermissionError, psutil.AccessDenied):
145+
continue
133146
return disk_info
134147

135148
@staticmethod
136149
def get_service_info() -> dict[str, str | datetime]:
137150
"""获取服务信息"""
138151
process = psutil.Process(os.getpid())
139152
mem_info = process.memory_info()
140-
start_time = timezone.f_datetime(datetime.utcfromtimestamp(process.create_time()).replace(tzinfo=tz.utc))
153+
154+
try:
155+
create_time = datetime.fromtimestamp(process.create_time(), tz=tz.utc)
156+
start_time = timezone.f_datetime(create_time)
157+
except (psutil.NoSuchProcess, OSError):
158+
start_time = timezone.now()
159+
160+
elapsed = ServerInfo.fmt_timedelta(timezone.now() - start_time)
141161

142162
return {
143163
'name': 'Python3',
144164
'version': platform.python_version(),
145165
'home': sys.executable,
146-
'cpu_usage': f'{round(process.cpu_percent(interval=1), 2)} %',
147-
'mem_vms': ServerInfo.format_bytes(mem_info.vms), # 虚拟内存, 即当前进程申请的虚拟内存
148-
'mem_rss': ServerInfo.format_bytes(mem_info.rss), # 常驻内存, 即当前进程实际使用的物理内存
149-
'mem_free': ServerInfo.format_bytes(mem_info.vms - mem_info.rss), # 空闲内存
166+
'cpu_usage': f'{process.cpu_percent(interval=0.1):.2f}%',
167+
'mem_vms': ServerInfo.format_bytes(mem_info.vms),
168+
'mem_rss': ServerInfo.format_bytes(mem_info.rss),
169+
'mem_free': ServerInfo.format_bytes(mem_info.vms - mem_info.rss),
150170
'startup': start_time,
151-
'elapsed': ServerInfo.fmt_timedelta(timezone.now() - start_time),
171+
'elapsed': elapsed,
152172
}
153173

154174

0 commit comments

Comments
 (0)