This commit is contained in:
root
2026-02-10 02:54:13 +08:00
parent 4a59323398
commit 64bfd85368
22 changed files with 2572 additions and 422 deletions

View File

@@ -5,10 +5,38 @@ import subprocess
import logging
import re
import json
import sys
from typing import Dict, List, Optional, Tuple
from dataclasses import dataclass
from enum import Enum
# Python 3.6 兼容性: dataclasses 是 3.7+ 的特性
if sys.version_info >= (3, 7):
from dataclasses import dataclass
else:
# Python 3.6 回退: 使用普通类
def dataclass(cls):
"""简化的 dataclass 装饰器兼容层"""
# 自动添加 __init__
annotations = getattr(cls, '__annotations__', {})
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
# 设置默认值
for key in annotations:
if not hasattr(self, key):
setattr(self, key, None)
cls.__init__ = __init__
# 添加 __repr__
def __repr__(self):
fields = ', '.join(f'{k}={getattr(self, k, None)!r}' for k in annotations)
return f"{cls.__name__}({fields})"
cls.__repr__ = __repr__
return cls
logger = logging.getLogger(__name__)
@@ -77,8 +105,9 @@ class SmartMonitor:
try:
result = subprocess.run(
["which", "smartctl"],
capture_output=True,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding='utf-8',
check=False
)
return result.returncode == 0
@@ -99,8 +128,9 @@ class SmartMonitor:
# 获取基本信息
result = subprocess.run(
["sudo", "smartctl", "-a", device_path],
capture_output=True,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding='utf-8',
check=False,
timeout=30
)
@@ -232,8 +262,9 @@ class SmartMonitor:
# 获取所有块设备
result = subprocess.run(
["lsblk", "-d", "-n", "-o", "NAME,TYPE,ROTA"],
capture_output=True,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding='utf-8',
check=True
)
@@ -270,7 +301,7 @@ class SmartMonitor:
score = 70
# 根据错误数量调整
error_count = len(smart_info.errors)
error_count = len(smart_info.errors) if smart_info.errors else 0
score -= error_count * 10
# 确保分数在合理范围内