forked from HomeAssistant/fn_nas
Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
0f691e956f | ||
![]() |
25348fff9b | ||
![]() |
30b1b7d271 |
@@ -313,11 +313,34 @@ class FlynasCoordinator(DataUpdateCoordinator):
|
||||
if connection_id is not None:
|
||||
await self.release_ssh_connection(connection_id)
|
||||
|
||||
async def ping_system(self) -> bool:
|
||||
"""轻量级系统状态检测"""
|
||||
# 对于本地主机直接返回True
|
||||
if self.host in ['localhost', '127.0.0.1']:
|
||||
return True
|
||||
|
||||
try:
|
||||
# 使用异步ping检测,减少超时时间
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
'ping', '-c', '1', '-W', '1', self.host,
|
||||
stdout=asyncio.subprocess.DEVNULL,
|
||||
stderr=asyncio.subprocess.DEVNULL
|
||||
)
|
||||
await asyncio.wait_for(proc.wait(), timeout=2) # 总超时时间2秒
|
||||
return proc.returncode == 0
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
async def _monitor_system_status(self):
|
||||
"""系统离线时轮询检测状态"""
|
||||
self._debug_log(f"启动系统状态监控,每{self._retry_interval}秒检测一次")
|
||||
|
||||
# 使用指数退避策略,避免频繁检测
|
||||
check_interval = self._retry_interval
|
||||
max_interval = 300 # 最大5分钟检测一次
|
||||
|
||||
while True:
|
||||
await asyncio.sleep(self._retry_interval)
|
||||
await asyncio.sleep(check_interval)
|
||||
|
||||
if await self.ping_system():
|
||||
self._info_log("检测到系统已开机,触发重新加载")
|
||||
@@ -326,24 +349,10 @@ class FlynasCoordinator(DataUpdateCoordinator):
|
||||
self.hass.config_entries.async_reload(self.config_entry.entry_id)
|
||||
)
|
||||
break
|
||||
|
||||
async def ping_system(self) -> bool:
|
||||
"""轻量级系统状态检测"""
|
||||
# 对于本地主机直接返回True
|
||||
if self.host in ['localhost', '127.0.0.1']:
|
||||
return True
|
||||
|
||||
try:
|
||||
# 使用异步ping检测
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
'ping', '-c', '1', '-W', '1', self.host,
|
||||
stdout=asyncio.subprocess.DEVNULL,
|
||||
stderr=asyncio.subprocess.DEVNULL
|
||||
)
|
||||
await proc.wait()
|
||||
return proc.returncode == 0
|
||||
except Exception:
|
||||
return False
|
||||
else:
|
||||
# 系统仍然离线,增加检测间隔(指数退避)
|
||||
check_interval = min(check_interval * 1.5, max_interval)
|
||||
self._debug_log(f"系统仍离线,下次检测间隔: {check_interval}秒")
|
||||
|
||||
async def _async_update_data(self):
|
||||
"""数据更新入口,优化命令执行频率"""
|
||||
@@ -421,6 +430,14 @@ class FlynasCoordinator(DataUpdateCoordinator):
|
||||
|
||||
return self.get_default_data()
|
||||
|
||||
async def shutdown_system(self):
|
||||
"""关闭系统 - 委托给SystemManager"""
|
||||
return await self.system_manager.shutdown_system()
|
||||
|
||||
async def reboot_system(self):
|
||||
"""重启系统 - 委托给SystemManager"""
|
||||
return await self.system_manager.reboot_system()
|
||||
|
||||
class UPSDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
def __init__(self, hass: HomeAssistant, config, main_coordinator):
|
||||
self.config = config
|
||||
|
@@ -14,6 +14,7 @@ class DiskManager:
|
||||
self.disk_full_info_cache = {} # 缓存磁盘完整信息
|
||||
self.first_run = True # 首次运行标志
|
||||
self.initial_detection_done = False # 首次完整检测完成标志
|
||||
self.disk_io_stats_cache = {} # 缓存磁盘I/O统计信息
|
||||
|
||||
def extract_value(self, text: str, patterns, default="未知", format_func=None):
|
||||
if not text:
|
||||
@@ -38,10 +39,9 @@ class DiskManager:
|
||||
async def check_disk_active(self, device: str, window: int = 30) -> bool:
|
||||
"""检查硬盘在指定时间窗口内是否有活动"""
|
||||
try:
|
||||
# 正确的路径是 /sys/block/{device}/stat
|
||||
stat_path = f"/sys/block/{device}/stat"
|
||||
|
||||
# 读取统计文件
|
||||
# 读取当前统计文件
|
||||
stat_output = await self.coordinator.run_command(f"cat {stat_path} 2>/dev/null")
|
||||
if not stat_output:
|
||||
self.logger.debug(f"无法读取 {stat_path},默认返回活跃状态")
|
||||
@@ -53,52 +53,148 @@ class DiskManager:
|
||||
self.logger.debug(f"无效的统计信息格式:{stat_output}")
|
||||
return True
|
||||
|
||||
# 关键字段:当前正在进行的I/O操作数量(第9个字段,索引8)
|
||||
in_flight = int(stats[8])
|
||||
try:
|
||||
# /sys/block/{device}/stat 字段说明:
|
||||
# 0: read I/Os requests 读请求次数
|
||||
# 1: read I/Os merged 读请求合并次数
|
||||
# 2: read sectors 读扇区数
|
||||
# 3: read ticks 读操作耗时(ms)
|
||||
# 4: write I/Os requests 写请求次数
|
||||
# 5: write I/Os merged 写请求合并次数
|
||||
# 6: write sectors 写扇区数
|
||||
# 7: write ticks 写操作耗时(ms)
|
||||
# 8: in_flight 当前进行中的I/O请求数
|
||||
# 9: io_ticks I/O活动时间(ms)
|
||||
# 10: time_in_queue 队列中的总时间(ms)
|
||||
|
||||
# 如果当前有I/O操作,直接返回活跃状态
|
||||
if in_flight > 0:
|
||||
current_stats = {
|
||||
'read_ios': int(stats[0]),
|
||||
'write_ios': int(stats[4]),
|
||||
'in_flight': int(stats[8]),
|
||||
'io_ticks': int(stats[9])
|
||||
}
|
||||
|
||||
# 如果当前有正在进行的I/O操作,直接返回活跃状态
|
||||
if current_stats['in_flight'] > 0:
|
||||
self.logger.debug(f"磁盘 {device} 有正在进行的I/O操作: {current_stats['in_flight']}")
|
||||
self.disk_io_stats_cache[device] = current_stats
|
||||
return True
|
||||
|
||||
# 检查I/O操作时间(第10个字段,索引9) - io_ticks(单位毫秒)
|
||||
io_ticks = int(stats[9])
|
||||
# 检查是否有缓存的统计信息
|
||||
cached_stats = self.disk_io_stats_cache.get(device)
|
||||
|
||||
# 如果设备在窗口时间内有I/O活动,返回活跃状态
|
||||
if io_ticks > window * 1000:
|
||||
if cached_stats:
|
||||
# 比较I/O请求次数的变化
|
||||
read_ios_diff = current_stats['read_ios'] - cached_stats['read_ios']
|
||||
write_ios_diff = current_stats['write_ios'] - cached_stats['write_ios']
|
||||
io_ticks_diff = current_stats['io_ticks'] - cached_stats['io_ticks']
|
||||
|
||||
self.logger.debug(f"磁盘 {device} I/O变化: 读={read_ios_diff}, 写={write_ios_diff}, 活动时间={io_ticks_diff}ms")
|
||||
|
||||
# 如果在检测窗口内有I/O活动,认为磁盘活跃
|
||||
if read_ios_diff > 0 or write_ios_diff > 0 or io_ticks_diff > 100: # 100ms内的活动
|
||||
self.logger.debug(f"磁盘 {device} 在窗口期内有I/O活动")
|
||||
self.disk_io_stats_cache[device] = current_stats
|
||||
return True
|
||||
|
||||
# 所有检查都通过,返回非活跃状态
|
||||
# 检查io_ticks是否表明最近有活动
|
||||
# io_ticks是累积值,如果在合理范围内增长,说明有轻微活动
|
||||
if io_ticks_diff > 0 and io_ticks_diff < window * 1000: # 在窗口时间内的轻微活动
|
||||
self.logger.debug(f"磁盘 {device} 有轻微I/O活动")
|
||||
self.disk_io_stats_cache[device] = current_stats
|
||||
return True
|
||||
else:
|
||||
# 首次检测,保存当前状态并认为活跃
|
||||
self.logger.debug(f"磁盘 {device} 首次检测,保存统计信息")
|
||||
self.disk_io_stats_cache[device] = current_stats
|
||||
return True
|
||||
|
||||
# 更新缓存
|
||||
self.disk_io_stats_cache[device] = current_stats
|
||||
|
||||
# 检查硬盘电源状态
|
||||
power_state = await self.get_disk_power_state(device)
|
||||
if power_state in ["standby", "sleep", "idle"]:
|
||||
self.logger.debug(f"磁盘 {device} 处于省电状态: {power_state}")
|
||||
return False
|
||||
|
||||
# 所有检查都通过,返回非活跃状态
|
||||
self.logger.debug(f"磁盘 {device} 判定为非活跃状态")
|
||||
return False
|
||||
|
||||
except (ValueError, IndexError) as e:
|
||||
self.logger.debug(f"解析统计信息失败: {e}")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"检测硬盘活动状态失败: {str(e)}", exc_info=True)
|
||||
self.logger.error(f"检测硬盘活动状态失败: {str(e)}")
|
||||
return True # 出错时默认执行检测
|
||||
|
||||
async def get_disk_activity(self, device: str) -> str:
|
||||
"""获取硬盘活动状态(活动中/空闲中/休眠中)"""
|
||||
async def get_disk_power_state(self, device: str) -> str:
|
||||
"""获取硬盘电源状态"""
|
||||
try:
|
||||
# 检查硬盘是否处于休眠状态
|
||||
# 检查 SCSI 设备状态
|
||||
state_path = f"/sys/block/{device}/device/state"
|
||||
state_output = await self.coordinator.run_command(f"cat {state_path} 2>/dev/null || echo 'unknown'")
|
||||
state = state_output.strip().lower()
|
||||
|
||||
if state in ["standby", "sleep"]:
|
||||
if state in ["running", "active"]:
|
||||
return "active"
|
||||
elif state in ["standby", "sleep"]:
|
||||
return state
|
||||
|
||||
# 对于某些设备,尝试通过hdparm检查状态(非侵入性)
|
||||
hdparm_output = await self.coordinator.run_command(f"hdparm -C /dev/{device} 2>/dev/null || echo 'unknown'")
|
||||
if "standby" in hdparm_output.lower():
|
||||
return "standby"
|
||||
elif "sleeping" in hdparm_output.lower():
|
||||
return "sleep"
|
||||
elif "active/idle" in hdparm_output.lower():
|
||||
return "active"
|
||||
|
||||
return "unknown"
|
||||
|
||||
except Exception as e:
|
||||
self.logger.debug(f"获取磁盘 {device} 电源状态失败: {e}")
|
||||
return "unknown"
|
||||
|
||||
async def get_disk_activity(self, device: str) -> str:
|
||||
"""获取硬盘活动状态(活动中/空闲中/休眠中)"""
|
||||
try:
|
||||
# 先检查电源状态
|
||||
power_state = await self.get_disk_power_state(device)
|
||||
if power_state in ["standby", "sleep"]:
|
||||
return "休眠中"
|
||||
|
||||
# 检查最近一分钟内的硬盘活动
|
||||
# 检查最近的I/O活动
|
||||
stat_path = f"/sys/block/{device}/stat"
|
||||
stat_output = await self.coordinator.run_command(f"cat {stat_path}")
|
||||
stat_output = await self.coordinator.run_command(f"cat {stat_path} 2>/dev/null")
|
||||
|
||||
if stat_output:
|
||||
stats = stat_output.split()
|
||||
|
||||
if len(stats) >= 11:
|
||||
# 第9个字段是最近完成的读操作数
|
||||
# 第10个字段是最近完成的写操作数
|
||||
recent_reads = int(stats[8])
|
||||
recent_writes = int(stats[9])
|
||||
try:
|
||||
in_flight = int(stats[8]) # 当前进行中的I/O
|
||||
|
||||
if recent_reads > 0 or recent_writes > 0:
|
||||
# 如果有正在进行的I/O,返回活动中
|
||||
if in_flight > 0:
|
||||
return "活动中"
|
||||
|
||||
# 检查缓存的统计信息来判断近期活动
|
||||
cached_stats = self.disk_io_stats_cache.get(device)
|
||||
if cached_stats:
|
||||
current_read_ios = int(stats[0])
|
||||
current_write_ios = int(stats[4])
|
||||
|
||||
read_diff = current_read_ios - cached_stats.get('read_ios', 0)
|
||||
write_diff = current_write_ios - cached_stats.get('write_ios', 0)
|
||||
|
||||
if read_diff > 0 or write_diff > 0:
|
||||
return "活动中"
|
||||
|
||||
except (ValueError, IndexError):
|
||||
pass
|
||||
|
||||
return "空闲中"
|
||||
|
||||
except Exception as e:
|
||||
|
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"domain": "fn_nas",
|
||||
"name": "飞牛NAS",
|
||||
"version": "1.3.6",
|
||||
"version": "1.3.7",
|
||||
"documentation": "https://github.com/anxms/fn_nas",
|
||||
"dependencies": [],
|
||||
"codeowners": ["@anxms"],
|
||||
|
@@ -293,32 +293,163 @@ class SystemManager:
|
||||
return {}
|
||||
|
||||
async def get_vol_usage(self) -> dict:
|
||||
"""获取 /vol* 开头的存储卷使用信息"""
|
||||
"""获取 /vol* 开头的存储卷使用信息,避免唤醒休眠磁盘"""
|
||||
try:
|
||||
# 优先使用字节单位
|
||||
df_output = await self.coordinator.run_command("df -B 1 /vol* 2>/dev/null")
|
||||
if df_output:
|
||||
return self.parse_df_bytes(df_output)
|
||||
# 首先尝试智能检测活跃卷
|
||||
active_vols = await self.check_active_volumes()
|
||||
|
||||
df_output = await self.coordinator.run_command("df -h /vol*")
|
||||
if active_vols:
|
||||
# 只查询活跃的卷,避免使用通配符可能唤醒所有磁盘
|
||||
vol_list = " ".join(active_vols)
|
||||
df_output = await self.coordinator.run_command(f"df -B 1 {vol_list} 2>/dev/null")
|
||||
if df_output:
|
||||
result = self.parse_df_bytes(df_output)
|
||||
if result: # 确保有数据返回
|
||||
return result
|
||||
|
||||
df_output = await self.coordinator.run_command(f"df -h {vol_list} 2>/dev/null")
|
||||
if df_output:
|
||||
result = self.parse_df_human_readable(df_output)
|
||||
if result: # 确保有数据返回
|
||||
return result
|
||||
|
||||
# 如果智能检测失败,回退到传统方法(仅在必要时)
|
||||
self._debug_log("智能卷检测无结果,回退到传统检测方法")
|
||||
|
||||
# 优先使用字节单位,但添加错误处理
|
||||
df_output = await self.coordinator.run_command("df -B 1 /vol* 2>/dev/null || true")
|
||||
if df_output and "No such file or directory" not in df_output:
|
||||
result = self.parse_df_bytes(df_output)
|
||||
if result:
|
||||
return result
|
||||
|
||||
df_output = await self.coordinator.run_command("df -h /vol* 2>/dev/null || true")
|
||||
if df_output and "No such file or directory" not in df_output:
|
||||
result = self.parse_df_human_readable(df_output)
|
||||
if result:
|
||||
return result
|
||||
|
||||
# 最后的回退:尝试检测任何挂载的卷
|
||||
mount_output = await self.coordinator.run_command("mount | grep '/vol' || true")
|
||||
if mount_output:
|
||||
vol_points = []
|
||||
for line in mount_output.splitlines():
|
||||
parts = line.split()
|
||||
for part in parts:
|
||||
if part.startswith('/vol') and part not in vol_points:
|
||||
vol_points.append(part)
|
||||
|
||||
if vol_points:
|
||||
self._debug_log(f"从mount输出检测到卷: {vol_points}")
|
||||
vol_list = " ".join(vol_points)
|
||||
df_output = await self.coordinator.run_command(f"df -h {vol_list} 2>/dev/null || true")
|
||||
if df_output:
|
||||
return self.parse_df_human_readable(df_output)
|
||||
|
||||
return {}
|
||||
except Exception as e:
|
||||
self.logger.error("获取存储卷信息失败: %s", str(e))
|
||||
self._debug_log("所有存储卷检测方法都失败,返回空字典")
|
||||
return {}
|
||||
|
||||
except Exception as e:
|
||||
self._error_log(f"获取存储卷信息失败: {str(e)}")
|
||||
return {}
|
||||
|
||||
async def check_active_volumes(self) -> list:
|
||||
"""检查当前活跃的存储卷,避免唤醒休眠磁盘"""
|
||||
try:
|
||||
# 获取所有挂载点,这个操作不会访问磁盘内容
|
||||
mount_output = await self.coordinator.run_command("mount | grep '/vol' 2>/dev/null || true")
|
||||
if not mount_output:
|
||||
self._debug_log("未找到任何/vol挂载点")
|
||||
return []
|
||||
|
||||
active_vols = []
|
||||
|
||||
for line in mount_output.splitlines():
|
||||
if '/vol' in line:
|
||||
# 提取挂载点
|
||||
parts = line.split()
|
||||
mount_point = None
|
||||
|
||||
# 查找挂载点(通常在 'on' 关键词之后)
|
||||
try:
|
||||
on_index = parts.index('on')
|
||||
if on_index + 1 < len(parts):
|
||||
candidate = parts[on_index + 1]
|
||||
# 严格检查是否以/vol开头
|
||||
if candidate.startswith('/vol'):
|
||||
mount_point = candidate
|
||||
except ValueError:
|
||||
# 如果没有 'on' 关键词,查找以/vol开头的部分
|
||||
for part in parts:
|
||||
if part.startswith('/vol'):
|
||||
mount_point = part
|
||||
break
|
||||
|
||||
# 过滤挂载点:只保留根级别的/vol*挂载点
|
||||
if mount_point and self.is_root_vol_mount(mount_point):
|
||||
# 检查这个卷对应的磁盘是否活跃
|
||||
is_active = await self.is_volume_disk_active(mount_point)
|
||||
if is_active:
|
||||
active_vols.append(mount_point)
|
||||
self._debug_log(f"添加活跃卷: {mount_point}")
|
||||
else:
|
||||
# 即使磁盘不活跃,也添加到列表中,但标记为可能休眠
|
||||
# 这样可以保证有基本的存储信息
|
||||
active_vols.append(mount_point)
|
||||
self._debug_log(f"卷 {mount_point} 对应磁盘可能休眠,但仍包含在检测中")
|
||||
else:
|
||||
self._debug_log(f"跳过非根级别vol挂载点: {mount_point}")
|
||||
|
||||
# 去重并排序
|
||||
active_vols = sorted(list(set(active_vols)))
|
||||
self._debug_log(f"最终检测到的根级别/vol存储卷: {active_vols}")
|
||||
return active_vols
|
||||
|
||||
except Exception as e:
|
||||
self._debug_log(f"检查活跃存储卷失败: {e}")
|
||||
return []
|
||||
|
||||
def is_root_vol_mount(self, mount_point: str) -> bool:
|
||||
"""检查是否为根级别的/vol挂载点"""
|
||||
if not mount_point or not mount_point.startswith('/vol'):
|
||||
return False
|
||||
|
||||
# 移除开头的/vol部分进行分析
|
||||
remainder = mount_point[4:] # 去掉'/vol'
|
||||
|
||||
# 如果remainder为空,说明是/vol,这是根级别
|
||||
if not remainder:
|
||||
return True
|
||||
|
||||
# 如果remainder只是数字(如/vol1, /vol2),这是根级别
|
||||
if remainder.isdigit():
|
||||
return True
|
||||
|
||||
# 如果remainder是单个字母或字母数字组合且没有斜杠,也认为是根级别
|
||||
# 例如:/vola, /volb, /vol1a 等
|
||||
if '/' not in remainder and len(remainder) <= 3:
|
||||
return True
|
||||
|
||||
# 其他情况都认为是子目录,如:
|
||||
# /vol1/docker/overlay2/...
|
||||
# /vol1/data/...
|
||||
# /vol1/config/...
|
||||
self._debug_log(f"检测到子目录挂载点: {mount_point}")
|
||||
return False
|
||||
|
||||
def parse_df_bytes(self, df_output: str) -> dict:
|
||||
"""解析df命令的字节输出"""
|
||||
volumes = {}
|
||||
for line in df_output.splitlines()[1:]:
|
||||
try:
|
||||
for line in df_output.splitlines()[1:]: # 跳过标题行
|
||||
parts = line.split()
|
||||
if len(parts) < 6:
|
||||
continue
|
||||
|
||||
mount_point = parts[-1]
|
||||
# 只处理 /vol 开头的挂载点
|
||||
if not mount_point.startswith("/vol"):
|
||||
# 严格检查只处理根级别的 /vol 挂载点
|
||||
if not self.is_root_vol_mount(mount_point):
|
||||
self._debug_log(f"跳过非根级别vol挂载点: {mount_point}")
|
||||
continue
|
||||
|
||||
try:
|
||||
@@ -341,21 +472,28 @@ class SystemManager:
|
||||
"available": bytes_to_human(avail_bytes),
|
||||
"use_percent": use_percent
|
||||
}
|
||||
self._debug_log(f"添加根级别/vol存储卷信息: {mount_point}")
|
||||
except (ValueError, IndexError) as e:
|
||||
self.logger.debug("解析存储卷行失败: %s - %s", line, str(e))
|
||||
self._debug_log(f"解析存储卷行失败: {line} - {str(e)}")
|
||||
continue
|
||||
except Exception as e:
|
||||
self._error_log(f"解析df字节输出失败: {e}")
|
||||
|
||||
return volumes
|
||||
|
||||
def parse_df_human_readable(self, df_output: str) -> dict:
|
||||
"""解析df命令输出"""
|
||||
volumes = {}
|
||||
for line in df_output.splitlines()[1:]:
|
||||
try:
|
||||
for line in df_output.splitlines()[1:]: # 跳过标题行
|
||||
parts = line.split()
|
||||
if len(parts) < 6:
|
||||
continue
|
||||
|
||||
mount_point = parts[-1]
|
||||
if not mount_point.startswith("/vol"):
|
||||
# 严格检查只处理根级别的 /vol 挂载点
|
||||
if not self.is_root_vol_mount(mount_point):
|
||||
self._debug_log(f"跳过非根级别vol挂载点: {mount_point}")
|
||||
continue
|
||||
|
||||
try:
|
||||
@@ -371,9 +509,12 @@ class SystemManager:
|
||||
"available": avail,
|
||||
"use_percent": use_percent
|
||||
}
|
||||
self._debug_log(f"添加根级别/vol存储卷信息: {mount_point}")
|
||||
except (ValueError, IndexError) as e:
|
||||
self.logger.debug("解析存储卷行失败: %s - %s", line, str(e))
|
||||
self._debug_log(f"解析存储卷行失败: {line} - {str(e)}")
|
||||
continue
|
||||
except Exception as e:
|
||||
self._error_log(f"解析df输出失败: {e}")
|
||||
|
||||
return volumes
|
||||
|
||||
|
Reference in New Issue
Block a user