diff --git a/hwmonDaemon.py b/hwmonDaemon.py index d09f56b..5a3fec9 100644 --- a/hwmonDaemon.py +++ b/hwmonDaemon.py @@ -4128,10 +4128,7 @@ class SystemHealthMonitor: Reading the database avoids needing the hostd API password on every node. Returns None if the usage cannot be read. """ - query = ( - "SELECT COALESCE(SUM(used_sectors), 0), COALESCE(SUM(total_sectors), 0) " - f"FROM storage_volumes WHERE disk_path LIKE '{managed['mountpoint']}/%';" - ) + query = "SELECT disk_path, used_sectors, total_sectors FROM storage_volumes;" try: result = subprocess.run( ['pct', 'exec', vmid, '--', 'sqlite3', f"file:{managed['db_path']}?mode=ro", query], @@ -4143,7 +4140,12 @@ class SystemHealthMonitor: if result.returncode != 0: logger.warning(f"Could not read hostd usage for LXC {vmid}: {result.stderr.strip()}") return None - used_sectors, total_sectors = (int(value) for value in result.stdout.strip().split('|')) + used_sectors = total_sectors = 0 + for row in result.stdout.splitlines(): + disk_path, used, total = row.rsplit('|', 2) + if disk_path.startswith(f"{managed['mountpoint']}/"): + used_sectors += int(used) + total_sectors += int(total) except (subprocess.SubprocessError, OSError, ValueError) as e: logger.warning(f"Could not read hostd usage for LXC {vmid}: {e}") return None diff --git a/tests/test_hwmon.py b/tests/test_hwmon.py index bb9e46e..22dd2f8 100644 --- a/tests/test_hwmon.py +++ b/tests/test_hwmon.py @@ -615,7 +615,7 @@ GIB = 1024 ** 3 SIA_DF = "mp0 storj-pool:vm-141-disk-0 15.5T 14.8T 700.0G 95.5 /mnt/sia\n" -def _run_lxc_check(monitor, df_rows, hostd_stdout='1|2', hostd_returncode=0, hostd_error=None): +def _run_lxc_check(monitor, df_rows, hostd_stdout='/mnt/sia/d.dat|1|2\n', hostd_returncode=0, hostd_error=None): """Run _check_lxc_storage against canned pct output; return (health, pct exec calls).""" exec_calls = [] @@ -639,7 +639,7 @@ def _run_lxc_check(monitor, df_rows, hostd_stdout='1|2', hostd_returncode=0, hos class TestManagedVolumes: def test_full_mount_with_low_hostd_usage_cuts_no_ticket(self, monitor): """The mount reads 95.5% full but hostd only uses 3.4 of 15 TiB.""" - health, calls = _run_lxc_check(monitor, SIA_DF, hostd_stdout='895045|3932160\n') + health, calls = _run_lxc_check(monitor, SIA_DF, hostd_stdout='/mnt/sia/hostd-data/data.dat|895045|3932160\n') assert health['status'] == 'OK' assert health['issues'] == [] fs = health['containers'][0]['filesystems'][0] @@ -648,26 +648,27 @@ class TestManagedVolumes: assert 'mode=ro' in calls[0][5] def test_warns_when_hostd_usage_crosses_warning(self, monitor): - health, _ = _run_lxc_check(monitor, SIA_DF, hostd_stdout='3342336|3932160\n') + health, _ = _run_lxc_check(monitor, SIA_DF, hostd_stdout='/mnt/sia/hostd-data/data.dat|3342336|3932160\n') assert health['status'] == 'WARNING' assert health['issues'] == ['LXC 141 high storage usage: 85.0% on /mnt/sia'] def test_critical_when_hostd_usage_crosses_critical(self, monitor): - health, _ = _run_lxc_check(monitor, SIA_DF, hostd_stdout='3800000|3932160\n') + health, _ = _run_lxc_check(monitor, SIA_DF, hostd_stdout='/mnt/sia/hostd-data/data.dat|3800000|3932160\n') assert health['status'] == 'CRITICAL' assert health['issues'] == ['LXC 141 critical storage usage: 96.6% on /mnt/sia'] def test_critical_when_filesystem_is_nearly_out_of_space(self, monitor): """A real out-of-space risk must alert even with low hostd usage.""" df = "mp0 storj-pool:vm-141-disk-0 15.5T 15.4T 10.0G 99.9 /mnt/sia\n" - health, _ = _run_lxc_check(monitor, df, hostd_stdout='895045|3932160\n') + health, _ = _run_lxc_check(monitor, df, hostd_stdout='/mnt/sia/hostd-data/data.dat|895045|3932160\n') assert health['status'] == 'CRITICAL' assert health['issues'] == ['LXC 141 critical storage usage: filesystem nearly out of space on /mnt/sia'] @pytest.mark.parametrize('kwargs', [ {'hostd_returncode': 1, 'hostd_stdout': ''}, {'hostd_stdout': 'garbage'}, - {'hostd_stdout': '0|0\n'}, + {'hostd_stdout': ''}, + {'hostd_stdout': '/mnt/other/data.dat|3800000|3932160\n'}, {'hostd_error': subprocess.TimeoutExpired('pct', 30)}, ]) def test_unreadable_hostd_usage_does_not_fall_back_to_df(self, monitor, kwargs): @@ -681,6 +682,14 @@ class TestManagedVolumes: assert health['status'] == 'CRITICAL' assert len(health['issues']) == 1 + def test_sums_only_volumes_on_this_mount(self, monitor): + stdout = ('/mnt/sia/a.dat|100|1000\n' + '/mnt/sia/b.dat|800|1000\n' + '/mnt/sia-other/c.dat|1000|1000\n') + health, _ = _run_lxc_check(monitor, SIA_DF, hostd_stdout=stdout) + assert health['containers'][0]['filesystems'][0]['usage_percent'] == pytest.approx(45.0) + assert health['status'] == 'OK' + def test_other_mounts_still_use_df(self, monitor): """The container's rootfs, and other containers, keep normal df alerting.""" df = "rootfs sia-nvme:vm-141-disk-0 125.9G 115.0G 5.0G 91.3 /\n"