From 88041b404f4f7ce43694f56f9d0c6b07baa79403 Mon Sep 17 00:00:00 2001 From: Baobhan Sith <80159437+Heavrnl@users.noreply.github.com> Date: Tue, 6 May 2025 22:17:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=BB=9F=E4=B8=80=E5=A4=84=E7=90=86=20B?= =?UTF-8?q?usyBox=20=E7=8E=AF=E5=A2=83=E4=B8=8B=20free=20=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E7=9A=84=E5=86=85=E5=AD=98=E5=8D=95=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to #16 --- .../src/services/status-monitor.service.ts | 57 ++++++++++++++----- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/packages/backend/src/services/status-monitor.service.ts b/packages/backend/src/services/status-monitor.service.ts index b98ebf6..7fd02fe 100644 --- a/packages/backend/src/services/status-monitor.service.ts +++ b/packages/backend/src/services/status-monitor.service.ts @@ -155,32 +155,61 @@ export class StatusMonitorService { try { - const freeOutput = await this.executeSshCommand(sshClient, 'free -m'); + let freeCommand = 'free -m'; + let isBusyBox = false; + try { + const busyboxCheck = await this.executeSshCommand(sshClient, 'busybox --help'); + if (busyboxCheck.includes('BusyBox')) { + freeCommand = 'free'; + isBusyBox = true; + } + } catch (err) { + // 如果检查失败,默认使用 free -m + } + const freeOutput = await this.executeSshCommand(sshClient, freeCommand); const lines = freeOutput.split('\n'); const memLine = lines.find(line => line.startsWith('Mem:')); const swapLine = lines.find(line => line.startsWith('Swap:')); if (memLine) { const parts = memLine.split(/\s+/); - if (parts.length >= 4) { - const total = parseInt(parts[1], 10); - const used = parseInt(parts[2], 10); - if (!isNaN(total) && !isNaN(used)) { - status.memTotal = total; status.memUsed = used; - status.memPercent = total > 0 ? parseFloat(((used / total) * 100).toFixed(1)) : 0; + if (parts.length >= 3) { + let totalVal = parseInt(parts[1], 10); + let usedVal = parseInt(parts[2], 10); + + if (isBusyBox) { + if (!isNaN(totalVal)) totalVal = Math.round(totalVal / 1024); + if (!isNaN(usedVal)) usedVal = Math.round(usedVal / 1024); + } + + if (!isNaN(totalVal) && !isNaN(usedVal)) { + status.memTotal = totalVal; + status.memUsed = usedVal; + status.memPercent = totalVal > 0 ? parseFloat(((usedVal / totalVal) * 100).toFixed(1)) : 0; } } } if (swapLine) { const parts = swapLine.split(/\s+/); - if (parts.length >= 4) { - const total = parseInt(parts[1], 10); - const used = parseInt(parts[2], 10); - if (!isNaN(total) && !isNaN(used)) { - status.swapTotal = total; status.swapUsed = used; - status.swapPercent = total > 0 ? parseFloat(((used / total) * 100).toFixed(1)) : 0; + if (parts.length >= 3) { + let totalVal = parseInt(parts[1], 10); + let usedVal = parseInt(parts[2], 10); + + if (isBusyBox) { + if (!isNaN(totalVal)) totalVal = Math.round(totalVal / 1024); + if (!isNaN(usedVal)) usedVal = Math.round(usedVal / 1024); + } + + if (!isNaN(totalVal) && !isNaN(usedVal)) { + status.swapTotal = totalVal; + status.swapUsed = usedVal; + status.swapPercent = totalVal > 0 ? parseFloat(((usedVal / totalVal) * 100).toFixed(1)) : 0; } } - } else { status.swapTotal = 0; status.swapUsed = 0; status.swapPercent = 0; } + } else { + status.swapTotal = 0; + status.swapUsed = 0; + status.swapPercent = 0; + } } catch (err) { /* 静默处理 */ }