MCPcopy
hub / github.com/dqzboy/Docker-Proxy / getSystemInfo

Function getSystemInfo

hubcmdui/server-utils.js:37–102  ·  view source on GitHub ↗

* 获取系统信息 * @returns {Promise } 系统信息对象

()

Source from the content-addressed store, hash-verified

35 * @returns {Promise<object>} 系统信息对象
36 */
37async function getSystemInfo() {
38 const platform = os.platform();
39 let memoryInfo = {};
40 let cpuInfo = {};
41 let diskInfo = {};
42
43 try {
44 // 内存信息 - 使用OS模块,适用于所有平台
45 const totalMem = os.totalmem();
46 const freeMem = os.freemem();
47 const usedMem = totalMem - freeMem;
48 const memPercent = Math.round((usedMem / totalMem) * 100);
49
50 memoryInfo = {
51 total: formatBytes(totalMem),
52 free: formatBytes(freeMem),
53 used: formatBytes(usedMem),
54 percent: memPercent
55 };
56
57 // CPU信息 - 使用不同平台的方法
58 await getCpuInfo(platform).then(info => {
59 cpuInfo = info;
60 }).catch(err => {
61 logger.warn('获取CPU信息失败:', err);
62 // 降级方案:使用OS模块
63 const cpuLoad = os.loadavg();
64 const cpuCores = os.cpus().length;
65
66 cpuInfo = {
67 model: os.cpus()[0].model,
68 cores: cpuCores,
69 load1: cpuLoad[0].toFixed(2),
70 load5: cpuLoad[1].toFixed(2),
71 load15: cpuLoad[2].toFixed(2),
72 percent: Math.round((cpuLoad[0] / cpuCores) * 100)
73 };
74 });
75
76 // 磁盘信息 - 根据平台调用不同方法
77 await getDiskInfo(platform).then(info => {
78 diskInfo = info;
79 }).catch(err => {
80 logger.warn('获取磁盘信息失败:', err);
81 diskInfo = {
82 filesystem: 'unknown',
83 size: 'unknown',
84 used: 'unknown',
85 available: 'unknown',
86 percent: '0%'
87 };
88 });
89
90 return {
91 platform,
92 hostname: os.hostname(),
93 memory: memoryInfo,
94 cpu: cpuInfo,

Callers 2

getSystemStatsFunction · 0.85
system.jsFile · 0.85

Calls 4

getCpuInfoFunction · 0.85
getDiskInfoFunction · 0.85
formatBytesFunction · 0.70
formatUptimeFunction · 0.70

Tested by

no test coverage detected