Files
2025-12-03 23:08:39 +08:00

122 lines
3.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const os = require('os');
const http = require('http');
// 获取系统信息
const getSystemInfo = () => {
try {
const totalMem = (os.totalmem() / (1024 ** 3)).toFixed(2) + ' GB';
const freeMem = (os.freemem() / (1024 ** 3)).toFixed(2) + ' GB';
const usedMem = ((os.totalmem() - os.freemem()) / (1024 ** 3)).toFixed(2) + ' GB';
const memUsage = (((os.totalmem() - os.freemem()) / os.totalmem()) * 100).toFixed(2) + '%';
const cpus = os.cpus();
const cpuModel = cpus[0]?.model || 'Unknown';
const cpuCores = cpus.length;
return {
hostname: os.hostname(),
platform: os.platform(),
arch: os.arch(),
uptime: (os.uptime() / 3600).toFixed(2) + ' hours',
// 内存信息:使用前面计算好的变量
totalMem, // 总内存大小
usedMem, // 已使用内存
freeMem, // 空闲内存
memUsage, // 内存使用率
// CPU信息使用前面计算好的变量
cpuModel, // CPU型号
cpuCores // CPU核心数
};
} catch (error) {
console.error('获取系统信息失败:', error.message);
return {};
}
};
// 创建HTTP服务器
const server = http.createServer((req, res) => {
if (req.url === '/') {
const systemInfo = getSystemInfo();
const html = `
<!DOCTYPE html>
<html>
<head>
<title>系统信息监控</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 40px;
background: #f5f5f5;
}
.container {
max-width: 800px;
margin: 0 auto;
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
text-align: center;
border-bottom: 2px solid #eee;
padding-bottom: 10px;
}
.info-item {
margin: 15px 0;
padding: 10px;
background: #f9f9f9;
border-radius: 5px;
}
.label {
font-weight: bold;
color: #555;
}
.memory { border-left: 4px solid #4CAF50; }
.cpu { border-left: 4px solid #2196F3; }
.system { border-left: 4px solid #FF9800; }
</style>
</head>
<body>
<div class="container">
<h1>🖥️ 系统信息监控</h1>
<div class="info-item system">
<span class="label">主机名:</span> ${systemInfo.hostname || 'N/A'}<br>
<span class="label">平台:</span> ${systemInfo.platform || 'N/A'}<br>
<span class="label">架构:</span> ${systemInfo.arch || 'N/A'}<br>
<span class="label">运行时间:</span> ${systemInfo.uptime || 'N/A'}
</div>
<div class="info-item memory">
<span class="label">总内存:</span> ${systemInfo.totalMem || 'N/A'}<br>
<span class="label">已用内存:</span> ${systemInfo.usedMem || 'N/A'}<br>
<span class="label">空闲内存:</span> ${systemInfo.freeMem || 'N/A'}<br>
<span class="label">内存使用率:</span> ${systemInfo.memUsage || 'N/A'}
</div>
<div class="info-item cpu">
<span class="label">CPU型号:</span> ${systemInfo.cpuModel || 'N/A'}<br>
<span class="label">CPU核心数:</span> ${systemInfo.cpuCores || 'N/A'}
</div>
</div>
</body>
</html>
`;
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end(html);
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('页面未找到');
}
});
// 启动服务器
const PORT = 3000;
server.listen(PORT, () => {
console.log(`🚀 服务器运行在 http://localhost:${PORT}`);
});