feat(*): 代码仓库初始化
This commit is contained in:
133
Server/tests/test1/common.js
Normal file
133
Server/tests/test1/common.js
Normal file
@@ -0,0 +1,133 @@
|
||||
const os = require("os");
|
||||
const path = require("path");
|
||||
|
||||
// 格式化字节数为人类可读的单位
|
||||
function formatBytes(bytes) {
|
||||
const size = {
|
||||
GB: 1024 * 1024 * 1024,
|
||||
MB: 1024 * 1024,
|
||||
KB: 1024,
|
||||
B: 1,
|
||||
};
|
||||
for (const key in size) {
|
||||
if (bytes >= size[key]) return `${bytes / size[key]} ${key}`;
|
||||
}
|
||||
}
|
||||
|
||||
// 工具函数:统一处理路径分隔符为正斜杠
|
||||
const formatPath = (pathStr) => {
|
||||
return pathStr.replace(/\\/g, "/"); // 单个反斜杠替换为正斜杠
|
||||
};
|
||||
|
||||
// 1. 操作系统基本信息
|
||||
const getSystemInfo = () => {
|
||||
return {
|
||||
osType: os.type(),
|
||||
osPlatform: os.platform(),
|
||||
osVersion: os.release(),
|
||||
osName: os.version(),
|
||||
arch: os.arch(),
|
||||
hostname: os.hostname(),
|
||||
systemStartTime: new Date(os.uptime() * 1000).toLocaleDateString(),
|
||||
systemRunningTime: `${Math.floor(os.uptime() / 3600)}小时 ${
|
||||
Math.floor(os.uptime() % 3600) / 60
|
||||
}分钟`,
|
||||
};
|
||||
};
|
||||
|
||||
// 2. CPU信息
|
||||
const getCPUInfo = () => {
|
||||
const cpus = os.cpus();
|
||||
return {
|
||||
number: cpus.length,
|
||||
model: cpus[0].model,
|
||||
speed: cpus[0].speed + "MHz",
|
||||
};
|
||||
};
|
||||
|
||||
// 3. 内存信息
|
||||
const getMemoryInfo = () => {
|
||||
return {
|
||||
total: formatBytes(os.totalmem()),
|
||||
freeMemory: formatBytes(os.freemem()),
|
||||
useMemory: formatBytes(os.totalmem() - os.freemem()),
|
||||
useRate: `${Math.round((1 - os.freemem() / os.totalmem()) * 100)}%`,
|
||||
};
|
||||
};
|
||||
|
||||
// 4. 用户信息
|
||||
const getUserInfo = () => {
|
||||
const userInfo = os.userInfo();
|
||||
return {
|
||||
userName: userInfo.username,
|
||||
uid: userInfo.uid,
|
||||
gid: userInfo.gid,
|
||||
homeDir: formatPath(userInfo.homedir),
|
||||
shell: userInfo.shell || "unknown",
|
||||
};
|
||||
};
|
||||
|
||||
// 5. 目录信息
|
||||
const getDirInfo = () => {
|
||||
return {
|
||||
homeDir: formatPath(os.homedir()),
|
||||
tmpDir: formatPath(os.tmpdir()),
|
||||
};
|
||||
};
|
||||
|
||||
// 6. 负载信息 (仅 Unix 系统有效)
|
||||
const getDutyInfo = () => {
|
||||
const loadavg = os.loadavg();
|
||||
return {
|
||||
min1: loadavg[0].toFixed(2),
|
||||
min5: loadavg[1].toFixed(2),
|
||||
min15: loadavg[2].toFixed(2),
|
||||
};
|
||||
};
|
||||
|
||||
// 7. 网络接口信息
|
||||
const getNetworkInterfaceInfo = () => {
|
||||
const networkInterfaces = os.networkInterfaces();
|
||||
const data = [];
|
||||
for (const [interfaceName, interfaceInfo] of Object.entries(
|
||||
networkInterfaces
|
||||
)) {
|
||||
let item;
|
||||
interfaceInfo.forEach((info) => {
|
||||
item = {
|
||||
name: interfaceName,
|
||||
family: info.family,
|
||||
ip: info.address,
|
||||
mac: info.mac,
|
||||
localAddress: info.internal ? true : false,
|
||||
netmask: info.netmask,
|
||||
gateway: info.gateway,
|
||||
};
|
||||
});
|
||||
data.push(item);
|
||||
}
|
||||
return data;
|
||||
};
|
||||
|
||||
const getAll = () => {
|
||||
return {
|
||||
SystemInfo: getSystemInfo(),
|
||||
CPUInfo: getCPUInfo(),
|
||||
MemoryInfo: getMemoryInfo(),
|
||||
UserInfo: getUserInfo(),
|
||||
DirInfo: getDirInfo(),
|
||||
DutyInfo: getDutyInfo(),
|
||||
NetworkInterfaceInfo: getNetworkInterfaceInfo(),
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getSystemInfo,
|
||||
getCPUInfo,
|
||||
getMemoryInfo,
|
||||
getUserInfo,
|
||||
getDirInfo,
|
||||
getDutyInfo,
|
||||
getNetworkInterfaceInfo,
|
||||
getAll,
|
||||
};
|
||||
Reference in New Issue
Block a user