[PR #6920] [MERGED] fix(monitoring): use container sources for LXC CPU, swap and uptime #6523

Closed
opened 2026-11-02 04:09:22 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6920
Author: @Vaso73
Created: 10/24/2026
Status: ✅ Merged
Merged: 10/24/2026
Merged by: @MHSanaei

Base: main ← Head: fix/lxc-container-metrics


📝 Commits (10+)

  • 52a2c2a fix(monitoring): use container sources for LXC CPU, swap and uptime
  • 85c1634 docs(monitoring): add synthetic LXC metrics screenshots
  • 7ca4dcf style(monitoring): format LXC metric regression fixtures
  • 5867aac fix(monitoring): retain metric availability in nodes and bots
  • aec5e11 fix(monitoring): wait for status before seeding overview history
  • 497c225 Merge remote-tracking branch 'origin/main' into pr/6920
  • 85cf483 chore(monitoring): drop the unreferenced screenshots and README section
  • b2be24d test(monitoring): fold the metric availability tests into the existing files
  • 536e1be fix(nodes): show an unprobed node's uptime as a dash again
  • b044f19 style(monitoring): drop the blank lines closing the host status blocks

📊 Changes

34 files changed (+1706 additions, -127 deletions)

View changed files

📝 docs/public/openapi.json (+34 -0)
📝 frontend/public/openapi.json (+34 -0)
📝 frontend/src/generated/examples.ts (+9 -0)
📝 frontend/src/generated/schemas.ts (+19 -0)
📝 frontend/src/generated/types.ts (+3 -0)
📝 frontend/src/generated/zod.ts (+3 -0)
📝 frontend/src/models/status.ts (+11 -1)
➕ frontend/src/pages/index/ContainerMetrics.stories.tsx (+67 -0)
📝 frontend/src/pages/index/IndexPage.tsx (+7 -1)
📝 frontend/src/pages/index/SystemStrip.tsx (+6 -2)
📝 frontend/src/pages/index/VitalTile.tsx (+7 -5)
📝 frontend/src/pages/index/useOverviewHistory.ts (+35 -7)
📝 frontend/src/pages/nodes/NodeList.tsx (+17 -6)
📝 frontend/src/schemas/node.ts (+2 -0)
📝 frontend/src/schemas/status.ts (+2 -0)
➕ frontend/src/test/container-history.test.tsx (+186 -0)
➕ frontend/src/test/container-metrics.test.ts (+39 -0)
➕ frontend/src/test/node-metrics-availability.test.tsx (+73 -0)
📝 internal/database/model/model.go (+5 -3)
➕ internal/util/sys/lxc_metrics.go (+10 -0)

...and 14 more files

📄 Description

Summary

Use verified container sources for CPU, swap and uptime on Linux LXC installations with LXCFS. Show unavailable samples as — and keep corrected CPU/swap history separate from earlier host values. Preserve unavailable-metric flags in node heartbeats, API responses and bot reports, and wait for the first valid status before loading Overview history.

Why

The existing dashboard can combine container RAM with host swap, host uptime and physical CPU topology. A synthetic container with 512 MiB of swap and 52,624 seconds of uptime reproduces the discrepancy against the current upstream because the swap and uptime helpers bypass LXCFS.

Swap and uptime now come from their verified LXCFS mounts. CPU uses the whole-container cgroup v2 usage delta, normalized by the visible LXCFS CPU count and capped by any readable CPU quota. The cgroup mount root and container init membership must be verified before using that counter. First samples, counter/capacity resets and unverifiable sources are unavailable rather than measured zeroes.

Optional resourceScope and unavailableMetrics response fields apply to CPU, swap and uptime only. Existing fields remain compatible. Native and non-Linux paths and all RAM/load/disk/network implementations are retained. This targets LXCFS with a unified cgroup v2 root; general Docker, cgroup v1 and limits hidden outside the namespace are outside its scope.

Node probes retain unavailableMetrics through heartbeat storage and the node read API. Unavailable CPU samples are excluded from node history; the node list, mobile details and Telegram/Discord uptime reports display —. Measured zero and older node responses without availability metadata retain their existing meaning.

Overview history is loaded after the first valid status establishes its resource scope. Temporary status errors do not prepend the same history again, and cancelled responses from an earlier scope are ignored.

Type of change

  • Bug fix

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Statistics / traffic counters

How was this tested?

  • Reproduced the upstream swap/uptime defect before applying the fix with synthetic proc/cgroup fixtures.
  • go test -shuffle=on -count=1 -p 2 ./... passed.
  • go test -race -shuffle=on -count=1 ./internal/util/sys ./internal/web/service passed. The node/Telegram/Discord follow-up also passed race tests across all three affected service packages; a local Go build passed.
  • Frontend typecheck, lint, formatting, all 1,991 unit/component/Storybook tests, frontend build and Storybook build passed.
  • Chromium exercised the built Overview page in five scenarios: measured values, unavailable CPU/swap/uptime, zero swap/uptime, a legacy native response, and a 390px mobile viewport. Those five scenarios had no JavaScript errors or horizontal overflow. Three additional Chromium scenarios verified desktop/mobile node availability versus measured zero and delayed first-status loading without duplicate history; all eight passed without JavaScript errors.
  • Regressions cover quota normalization, CPU baseline/reset/recovery, unverified cgroup roots, malformed sources, native compatibility, unchanged unrelated metrics, API metadata, and history isolation without false zeroes.
  • Follow-up regressions reproduced both review findings before the fixes. They cover node probe/API/persistence and zero-value recovery, migration of an older SQLite node row, both Discord uptime outputs, Telegram uptime, delayed first status, temporary status errors, empty initial history and cancelled scope changes.
  • A clean local merge with upstream de21c128 passed the affected node/Telegram/Discord package tests, UI regressions, frontend static checks and generated-contract consistency.
  • Tests used private network namespaces with loopback only and disposable data. This PR preparation did not contact a production instance. Windows/macOS runtime behavior was not tested.

Screenshots / recordings

All images show the built Overview page with synthetic API fixtures.

Measured container values

Synthetic measured CPU, swap and container uptime

Unavailable CPU, swap and uptime

Synthetic unavailable container metrics

Mobile layout at 390 px

Synthetic mobile Overview

Breaking changes

No breaking API or configuration changes. The normal database migration adds a nullable nodes.unavailable_metrics column so availability survives refreshes and restarts. Legacy rows and older node responses without metadata remain compatible, and existing numeric response fields keep their types. Existing host history is retained, with separate CPU/swap series used for the corrected container samples.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build and the test suite pass locally.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I updated the README for the new monitoring behavior and optional API metadata.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6920 **Author:** [@Vaso73](https://github.com/Vaso73) **Created:** 10/24/2026 **Status:** ✅ Merged **Merged:** 10/24/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/lxc-container-metrics` --- ### 📝 Commits (10+) - [`52a2c2a`](https://github.com/MHSanaei/3x-ui/commit/52a2c2add9b54b0b0739609bbf78ab54b630fd7b) fix(monitoring): use container sources for LXC CPU, swap and uptime - [`85c1634`](https://github.com/MHSanaei/3x-ui/commit/85c1634acc536f51018aa68ca571aa45e1784cfc) docs(monitoring): add synthetic LXC metrics screenshots - [`7ca4dcf`](https://github.com/MHSanaei/3x-ui/commit/7ca4dcfea4c6ad8104ea38839224d929d3f073a2) style(monitoring): format LXC metric regression fixtures - [`5867aac`](https://github.com/MHSanaei/3x-ui/commit/5867aac7a60c175bef7aa44c53d3a0df334bc002) fix(monitoring): retain metric availability in nodes and bots - [`aec5e11`](https://github.com/MHSanaei/3x-ui/commit/aec5e110b515294497093b06d7aaed78f5f750d0) fix(monitoring): wait for status before seeding overview history - [`497c225`](https://github.com/MHSanaei/3x-ui/commit/497c225f2560b5d103c257cf35b3f76aef24524b) Merge remote-tracking branch 'origin/main' into pr/6920 - [`85cf483`](https://github.com/MHSanaei/3x-ui/commit/85cf483010904457f2a425673635b80295bec486) chore(monitoring): drop the unreferenced screenshots and README section - [`b2be24d`](https://github.com/MHSanaei/3x-ui/commit/b2be24d3d6a687428883b4d8fcce08fcd1a6a338) test(monitoring): fold the metric availability tests into the existing files - [`536e1be`](https://github.com/MHSanaei/3x-ui/commit/536e1be06647b42afc6c78ea0882d3e938dd0bd9) fix(nodes): show an unprobed node's uptime as a dash again - [`b044f19`](https://github.com/MHSanaei/3x-ui/commit/b044f19cccd631846f661d4f973adb1deae55491) style(monitoring): drop the blank lines closing the host status blocks ### 📊 Changes **34 files changed** (+1706 additions, -127 deletions) <details> <summary>View changed files</summary> 📝 `docs/public/openapi.json` (+34 -0) 📝 `frontend/public/openapi.json` (+34 -0) 📝 `frontend/src/generated/examples.ts` (+9 -0) 📝 `frontend/src/generated/schemas.ts` (+19 -0) 📝 `frontend/src/generated/types.ts` (+3 -0) 📝 `frontend/src/generated/zod.ts` (+3 -0) 📝 `frontend/src/models/status.ts` (+11 -1) ➕ `frontend/src/pages/index/ContainerMetrics.stories.tsx` (+67 -0) 📝 `frontend/src/pages/index/IndexPage.tsx` (+7 -1) 📝 `frontend/src/pages/index/SystemStrip.tsx` (+6 -2) 📝 `frontend/src/pages/index/VitalTile.tsx` (+7 -5) 📝 `frontend/src/pages/index/useOverviewHistory.ts` (+35 -7) 📝 `frontend/src/pages/nodes/NodeList.tsx` (+17 -6) 📝 `frontend/src/schemas/node.ts` (+2 -0) 📝 `frontend/src/schemas/status.ts` (+2 -0) ➕ `frontend/src/test/container-history.test.tsx` (+186 -0) ➕ `frontend/src/test/container-metrics.test.ts` (+39 -0) ➕ `frontend/src/test/node-metrics-availability.test.tsx` (+73 -0) 📝 `internal/database/model/model.go` (+5 -3) ➕ `internal/util/sys/lxc_metrics.go` (+10 -0) _...and 14 more files_ </details> ### 📄 Description ## Summary Use verified container sources for CPU, swap and uptime on Linux LXC installations with LXCFS. Show unavailable samples as `—` and keep corrected CPU/swap history separate from earlier host values. Preserve unavailable-metric flags in node heartbeats, API responses and bot reports, and wait for the first valid status before loading Overview history. ## Why The existing dashboard can combine container RAM with host swap, host uptime and physical CPU topology. A synthetic container with 512 MiB of swap and 52,624 seconds of uptime reproduces the discrepancy against the current upstream because the swap and uptime helpers bypass LXCFS. Swap and uptime now come from their verified LXCFS mounts. CPU uses the whole-container cgroup v2 usage delta, normalized by the visible LXCFS CPU count and capped by any readable CPU quota. The cgroup mount root and container init membership must be verified before using that counter. First samples, counter/capacity resets and unverifiable sources are unavailable rather than measured zeroes. Optional `resourceScope` and `unavailableMetrics` response fields apply to CPU, swap and uptime only. Existing fields remain compatible. Native and non-Linux paths and all RAM/load/disk/network implementations are retained. This targets LXCFS with a unified cgroup v2 root; general Docker, cgroup v1 and limits hidden outside the namespace are outside its scope. Node probes retain `unavailableMetrics` through heartbeat storage and the node read API. Unavailable CPU samples are excluded from node history; the node list, mobile details and Telegram/Discord uptime reports display `—`. Measured zero and older node responses without availability metadata retain their existing meaning. Overview history is loaded after the first valid status establishes its resource scope. Temporary status errors do not prepend the same history again, and cancelled responses from an earlier scope are ignored. ## Type of change - [x] Bug fix ## Areas affected - [x] Frontend (UI / panel pages) - [x] Backend (API endpoints, login, settings) - [x] Statistics / traffic counters ## How was this tested? - Reproduced the upstream swap/uptime defect before applying the fix with synthetic proc/cgroup fixtures. - `go test -shuffle=on -count=1 -p 2 ./...` passed. - `go test -race -shuffle=on -count=1 ./internal/util/sys ./internal/web/service` passed. The node/Telegram/Discord follow-up also passed race tests across all three affected service packages; a local Go build passed. - Frontend typecheck, lint, formatting, all 1,991 unit/component/Storybook tests, frontend build and Storybook build passed. - Chromium exercised the built Overview page in five scenarios: measured values, unavailable CPU/swap/uptime, zero swap/uptime, a legacy native response, and a 390px mobile viewport. Those five scenarios had no JavaScript errors or horizontal overflow. Three additional Chromium scenarios verified desktop/mobile node availability versus measured zero and delayed first-status loading without duplicate history; all eight passed without JavaScript errors. - Regressions cover quota normalization, CPU baseline/reset/recovery, unverified cgroup roots, malformed sources, native compatibility, unchanged unrelated metrics, API metadata, and history isolation without false zeroes. - Follow-up regressions reproduced both review findings before the fixes. They cover node probe/API/persistence and zero-value recovery, migration of an older SQLite node row, both Discord uptime outputs, Telegram uptime, delayed first status, temporary status errors, empty initial history and cancelled scope changes. - A clean local merge with upstream `de21c128` passed the affected node/Telegram/Discord package tests, UI regressions, frontend static checks and generated-contract consistency. - Tests used private network namespaces with loopback only and disposable data. This PR preparation did not contact a production instance. Windows/macOS runtime behavior was not tested. ## Screenshots / recordings All images show the built Overview page with synthetic API fixtures. **Measured container values** ![Synthetic measured CPU, swap and container uptime](https://raw.githubusercontent.com/Vaso73/3x-ui/85c1634acc536f51018aa68ca571aa45e1784cfc/media/lxc-container-metrics-measured.png) **Unavailable CPU, swap and uptime** ![Synthetic unavailable container metrics](https://raw.githubusercontent.com/Vaso73/3x-ui/85c1634acc536f51018aa68ca571aa45e1784cfc/media/lxc-container-metrics-unavailable.png) <details> <summary>Mobile layout at 390 px</summary> ![Synthetic mobile Overview](https://raw.githubusercontent.com/Vaso73/3x-ui/85c1634acc536f51018aa68ca571aa45e1784cfc/media/lxc-container-metrics-mobile.png) </details> ## Breaking changes No breaking API or configuration changes. The normal database migration adds a nullable `nodes.unavailable_metrics` column so availability survives refreshes and restarts. Legacy rows and older node responses without metadata remain compatible, and existing numeric response fields keep their types. Existing host history is retained, with separate CPU/swap series used for the corrected container samples. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build` and the test suite pass locally. - [x] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [x] I updated the README for the new monitoring behavior and optional API metadata. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:09:22 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6523
No description provided.