diff --git a/app/server.py b/app/server.py index be64841..875f853 100644 --- a/app/server.py +++ b/app/server.py @@ -7763,9 +7763,14 @@ def read_temps(with_disks=True): temps["disks"].append({"name": blk, "temp": cached}) continue temp = None - hwdir = os.path.join("/sys/block", blk, "device", "hwmon") + devdir = os.path.join("/sys/block", blk, "device") + hwdir = os.path.join(devdir, "hwmon") + if not os.path.isdir(hwdir): + hwdir = devdir # NVMe 的 hwmonN 直接挂在控制器目录下 if os.path.isdir(hwdir): for hw in sorted(os.listdir(hwdir)): + if not hw.startswith("hwmon"): + continue tfile = os.path.join(hwdir, hw, "temp1_input") if os.path.isfile(tfile): t = read_int_file(tfile) @@ -7777,8 +7782,10 @@ def read_temps(with_disks=True): tnow = time.time() if tnow - _DISK_TEMP_TS.get(blk, 0.0) <= _DISK_TEMP_TTL: temp = _DISK_TEMP_CACHE.get(blk) - else: - temp = smartctl_temp(blk) + if temp: + temps["disks"].append({"name": blk, "temp": temp}) + continue # 复用缓存不推进实际读取时间,否则持续采集会让缓存永不过期 + temp = smartctl_temp(blk) if temp: _DISK_TEMP_CACHE[blk] = temp _DISK_TEMP_TS[blk] = time.time() diff --git a/app/www/index.html b/app/www/index.html index 24afa48..12f88e5 100644 --- a/app/www/index.html +++ b/app/www/index.html @@ -3586,7 +3586,7 @@

${def.title}

const dd = disksOf(S.o).find(z => z.name === a); if (!dd) return; const slp = isSleep(dd), sd = smartDetailOf(a), sas = !!(sd && sd.scsi); const val = k => (sd && sd[k] != null ? sd[k] : dd[k]); -const temp = (sd && sd.temp_c != null) ? sd.temp_c : dd.temp; +const temp = ok(dd.temp) ? dd.temp : (sd && sd.temp_c); // 与告警同源,SMART 缓存仅作缺失时兜底 const num = v => ok(v) ? esc(Number(v).toLocaleString()) : '—'; const flag = v => (+v > 0 ? 'warn' : true); let rows = [['整体评估', dd.health ? esc(/PASS/i.test(dd.health) ? '通过' : dd.health) : '未读取到', diff --git a/tests/test_unit_smart_sas.py b/tests/test_unit_smart_sas.py index 49480bb..d8a48ac 100644 --- a/tests/test_unit_smart_sas.py +++ b/tests/test_unit_smart_sas.py @@ -7,6 +7,8 @@ 纯 fixture(不需要真实 SAS 盘)。测试通过 mock run_cmd 喂入 smartctl 文本。 """ +import os +import tempfile import time import unittest from unittest import mock @@ -273,6 +275,56 @@ def test_standby_returns_none(self): def test_unknown_format_returns_none(self): self.assertIsNone(self._temp("no temperature here")) + def test_temperature_cache_expires_during_regular_collection(self): + with mock.patch.dict(srv._DISK_TEMP_CACHE, {}, clear=True), \ + mock.patch.dict(srv._DISK_TEMP_TS, {}, clear=True), \ + mock.patch.object(srv.os.path, "isdir", return_value=False), \ + mock.patch.object(srv.os, "listdir", return_value=["nvme0n1"]), \ + mock.patch.object(srv, "disk_power_states", return_value={"nvme0n1": "active/idle"}), \ + mock.patch.object(srv.time, "time") as clock, \ + mock.patch.object(srv, "run_cmd") as cmd: + for elapsed in range(0, int(srv._DISK_TEMP_TTL) + 21, 10): + clock.return_value = 1000.0 + elapsed + cmd.return_value = ("Temperature: %d Celsius\n" % (56 if elapsed == 0 else 55), 0) + expected = 56 if elapsed <= srv._DISK_TEMP_TTL else 55 + self.assertEqual(srv.read_temps()["disks"], [{"name": "nvme0n1", "temp": expected}]) + self.assertEqual(cmd.call_count, 2) + + def test_hwmon_layouts_update_without_smartctl_and_skip_standby(self): + listdir, isdir, isfile = os.listdir, os.path.isdir, os.path.isfile + read_int = srv.read_int_file + states = {"sda": "active/idle", "nvme0n1": "active/idle"} + with tempfile.TemporaryDirectory() as root: + paths = { + "sda": root + "/sys/block/sda/device/hwmon/hwmon2/temp1_input", + "nvme0n1": root + "/sys/block/nvme0n1/device/hwmon3/temp1_input", + } + for path in paths.values(): + os.makedirs(os.path.dirname(path)) + with open(path, "w") as f: + f.write("56000") + with mock.patch.dict(srv._DISK_TEMP_CACHE, {}, clear=True), \ + mock.patch.dict(srv._DISK_TEMP_TS, {}, clear=True), \ + mock.patch.object(srv.os, "listdir", side_effect=lambda p: listdir(root + p)), \ + mock.patch.object(srv.os.path, "isdir", side_effect=lambda p: isdir(root + p)), \ + mock.patch.object(srv.os.path, "isfile", side_effect=lambda p: isfile(root + p)), \ + mock.patch.object(srv, "read_int_file", side_effect=lambda p: read_int(root + p)) as reads, \ + mock.patch.object(srv, "disk_power_states", return_value=states), \ + mock.patch.object(srv, "smartctl_temp", return_value=None) as smart: + self.assertEqual({d["name"]: d["temp"] for d in srv.read_temps()["disks"]}, + {"sda": 56.0, "nvme0n1": 56.0}) + for path in paths.values(): + with open(path, "w") as f: + f.write("55000") + self.assertEqual({d["name"]: d["temp"] for d in srv.read_temps()["disks"]}, + {"sda": 55.0, "nvme0n1": 55.0}) + states.update({"sda": "standby", "nvme0n1": "standby"}) + reads.reset_mock() + self.assertEqual({d["name"]: d["temp"] for d in srv.read_temps()["disks"]}, + {"sda": 55.0, "nvme0n1": 55.0}) + reads.assert_not_called() + smart.assert_not_called() + if __name__ == "__main__": unittest.main() diff --git a/ui-src/45-sheets.js b/ui-src/45-sheets.js index 41c7166..0b08b7a 100644 --- a/ui-src/45-sheets.js +++ b/ui-src/45-sheets.js @@ -186,7 +186,7 @@ const SHEETS = { const dd = disksOf(S.o).find(z => z.name === a); if (!dd) return; const slp = isSleep(dd), sd = smartDetailOf(a), sas = !!(sd && sd.scsi); const val = k => (sd && sd[k] != null ? sd[k] : dd[k]); - const temp = (sd && sd.temp_c != null) ? sd.temp_c : dd.temp; + const temp = ok(dd.temp) ? dd.temp : (sd && sd.temp_c); // 与告警同源,SMART 缓存仅作缺失时兜底 const num = v => ok(v) ? esc(Number(v).toLocaleString()) : '—'; const flag = v => (+v > 0 ? 'warn' : true); let rows = [['整体评估', dd.health ? esc(/PASS/i.test(dd.health) ? '通过' : dd.health) : '未读取到',