Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion app/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3586,7 +3586,7 @@ <h2 class="mod-title">${def.title}</h2>
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()) : '<span class="faint">—</span>';
const flag = v => (+v > 0 ? 'warn' : true);
let rows = [['整体评估', dd.health ? esc(/PASS/i.test(dd.health) ? '通过' : dd.health) : '<span class="faint">未读取到</span>',
Expand Down
52 changes: 52 additions & 0 deletions tests/test_unit_smart_sas.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

纯 fixture(不需要真实 SAS 盘)。测试通过 mock run_cmd 喂入 smartctl 文本。
"""
import os
import tempfile
import time
import unittest
from unittest import mock
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion ui-src/45-sheets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()) : '<span class="faint">—</span>';
const flag = v => (+v > 0 ? 'warn' : true);
let rows = [['整体评估', dd.health ? esc(/PASS/i.test(dd.health) ? '通过' : dd.health) : '<span class="faint">未读取到</span>',
Expand Down
Loading