|
@@ -1159,15 +1159,13 @@ def dtu_publish_status(force=False):
|
|
|
screen_connected = screen_display.get_status().get('connected', False)
|
|
screen_connected = screen_display.get_status().get('connected', False)
|
|
|
|
|
|
|
|
# CPU/内存使用率 (Linux 才有意义)
|
|
# CPU/内存使用率 (Linux 才有意义)
|
|
|
- cpu_usage = None
|
|
|
|
|
- memory_usage = None
|
|
|
|
|
|
|
+ cpu_usage = _get_cpu_usage()
|
|
|
|
|
+ memory_usage = _get_memory_usage()
|
|
|
dtu_temperature = None
|
|
dtu_temperature = None
|
|
|
try:
|
|
try:
|
|
|
- import psutil
|
|
|
|
|
- cpu_usage = psutil.cpu_percent(interval=None)
|
|
|
|
|
- memory_usage = psutil.virtual_memory().percent
|
|
|
|
|
# 主板温度: 优先 psutil.sensors_temperatures
|
|
# 主板温度: 优先 psutil.sensors_temperatures
|
|
|
try:
|
|
try:
|
|
|
|
|
+ import psutil
|
|
|
temps = psutil.sensors_temperatures(fahrenheit=False) or {}
|
|
temps = psutil.sensors_temperatures(fahrenheit=False) or {}
|
|
|
for chip_name, entries in temps.items():
|
|
for chip_name, entries in temps.items():
|
|
|
for entry in entries:
|
|
for entry in entries:
|
|
@@ -3935,19 +3933,63 @@ def manual_dtu_register():
|
|
|
return jsonify({'success': False, 'message': str(e)}), 500
|
|
return jsonify({'success': False, 'message': str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+_cpu_last_times = None
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
def _get_cpu_usage():
|
|
def _get_cpu_usage():
|
|
|
|
|
+ """CPU 使用率 (psutil 优先,无 psutil 时回退读 /proc/stat 计算瞬时差值)"""
|
|
|
|
|
+ global _cpu_last_times
|
|
|
try:
|
|
try:
|
|
|
import psutil
|
|
import psutil
|
|
|
return psutil.cpu_percent(interval=None)
|
|
return psutil.cpu_percent(interval=None)
|
|
|
except Exception:
|
|
except Exception:
|
|
|
|
|
+ pass
|
|
|
|
|
+ try:
|
|
|
|
|
+ with open('/proc/stat') as f:
|
|
|
|
|
+ fields = f.readline().split()
|
|
|
|
|
+ if len(fields) < 5 or not fields[0].startswith('cpu'):
|
|
|
|
|
+ return None
|
|
|
|
|
+ vals = [int(x) for x in fields[1:]]
|
|
|
|
|
+ idle = vals[3] + (vals[4] if len(vals) > 4 else 0)
|
|
|
|
|
+ total = sum(vals)
|
|
|
|
|
+ if _cpu_last_times:
|
|
|
|
|
+ dtotal = total - _cpu_last_times[0]
|
|
|
|
|
+ didle = idle - _cpu_last_times[1]
|
|
|
|
|
+ if dtotal > 0:
|
|
|
|
|
+ pct = round((1.0 - didle / dtotal) * 100, 1)
|
|
|
|
|
+ _cpu_last_times = (total, idle)
|
|
|
|
|
+ return pct
|
|
|
|
|
+ _cpu_last_times = (total, idle)
|
|
|
|
|
+ return None
|
|
|
|
|
+ except Exception:
|
|
|
return None
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_memory_usage():
|
|
def _get_memory_usage():
|
|
|
|
|
+ """内存使用率 (psutil 优先,无 psutil 时回退读 /proc/meminfo)"""
|
|
|
try:
|
|
try:
|
|
|
import psutil
|
|
import psutil
|
|
|
return psutil.virtual_memory().percent
|
|
return psutil.virtual_memory().percent
|
|
|
except Exception:
|
|
except Exception:
|
|
|
|
|
+ pass
|
|
|
|
|
+ try:
|
|
|
|
|
+ meminfo = {}
|
|
|
|
|
+ with open('/proc/meminfo') as f:
|
|
|
|
|
+ for line in f:
|
|
|
|
|
+ parts = line.split(':')
|
|
|
|
|
+ if len(parts) == 2:
|
|
|
|
|
+ try:
|
|
|
|
|
+ meminfo[parts[0].strip()] = int(parts[1].split()[0])
|
|
|
|
|
+ except (ValueError, IndexError):
|
|
|
|
|
+ continue
|
|
|
|
|
+ total = meminfo.get('MemTotal', 0)
|
|
|
|
|
+ available = meminfo.get('MemAvailable')
|
|
|
|
|
+ if available is None:
|
|
|
|
|
+ available = meminfo.get('MemFree', 0)
|
|
|
|
|
+ if total > 0:
|
|
|
|
|
+ return round((1.0 - available / total) * 100, 1)
|
|
|
|
|
+ return None
|
|
|
|
|
+ except Exception:
|
|
|
return None
|
|
return None
|
|
|
|
|
|
|
|
|
|
|