systeminformation 终极使用指南:轻松获取系统信息的 Node.js 神器
想要快速获取系统硬件和操作系统信息吗?systeminformation 是一个功能强大的 Node.js 库,提供了 50 多个函数来帮助你轻松获取详尽的系统信息。无论你是系统管理员、开发人员还是运维工程师,这个库都能为你提供准确可靠的系统数据。
项目亮点速览
- 零依赖设计:无需安装额外的 npm 包,直接使用
- 跨平台支持:支持 Linux、macOS、Windows、FreeBSD、OpenBSD、NetBSD、SunOS 和 Android
- 全面覆盖:从 CPU、内存到网络、Docker,应有尽有
- 简单易用:几行代码即可获取所需信息
- 高性能:异步设计,不阻塞事件循环
极速安装体验
安装 systeminformation 非常简单,只需要一条命令:
npm install systeminformation --save
如果你只是想快速体验一下,甚至不需要安装:
npx systeminformation info
核心功能演示
获取 CPU 信息
const si = require('systeminformation');
// Promise 风格
si.cpu()
.then(data => {
console.log('CPU 制造商:', data.manufacturer);
console.log('CPU 品牌:', data.brand);
console.log('CPU 频率:', data.speed);
console.log('CPU 核心数:', data.cores);
})
.catch(error => console.error(error));
获取系统内存信息
async function getMemoryInfo() {
try {
const data = await si.mem();
console.log('总内存:', (data.total / 1024 / 1024 / 1024).toFixed(2), 'GB');
console.log('可用内存:', (data.available / 1024 / 1024 / 1024).toFixed(2), 'GB');
console.log('内存使用率:', ((data.used / data.total) * 100).toFixed(2), '%');
} catch (error) {
console.error('获取内存信息失败:', error);
}
}
getMemoryInfo();
获取磁盘空间信息
si.fsSize()
.then(data => {
data.forEach(fs => {
console.log(`文件系统 ${fs.mount}:`);
console.log(`- 总空间:${(fs.size / 1024 / 1024 / 1024).toFixed(2)} GB`);
console.log(`- 已使用:${(fs.used / 1024 / 1024 / 1024).toFixed(2)} GB`);
console.log(`- 使用率:${fs.use}%`);
});
})
.catch(error => console.error(error));
实际应用场景
系统监控仪表板
你可以使用 systeminformation 构建实时系统监控仪表板:
const si = require('systeminformation');
async function getSystemStats() {
const [cpu, memory, fsSize] = await Promise.all([
si.cpu(),
si.mem(),
si.fsSize()
]);
return {
cpuUsage: cpu,
memoryUsage: memory,
diskUsage: fsSize
};
}
// 定时获取系统状态
setInterval(async () => {
const stats = await getSystemStats();
console.log('当前系统状态:', stats);
}, 5000);
自动化运维脚本
在自动化运维中,systeminformation 可以帮助你检测系统状态:
async function checkSystemHealth() {
const currentLoad = await si.currentLoad();
const mem = await si.mem();
// 设置告警阈值
if (currentLoad.currentLoad > 80) {
console.warn('警告:CPU 使用率过高!');
}
if (mem.used / mem.total > 0.85) {
console.warn('警告:内存使用率过高!');
}
}
生态系统集成
与 Prometheus 集成
你可以将 systeminformation 收集的数据导出为 Prometheus 指标:
const si = require('systeminformation');
const client = require('prom-client');
const cpuUsageGauge = new client.Gauge({
name: 'cpu_usage_percent',
help: 'CPU 使用率百分比'
});
async function updateMetrics() {
const load = await si.currentLoad();
cpuUsageGauge.set(load.currentLoad);
}
进阶使用技巧
批量获取系统信息
使用 getAll() 函数一次性获取所有系统信息:
si.getAll()
.then(data => {
console.log('完整系统信息:', data);
})
.catch(error => console.error(error));
自定义数据收集
你可以创建自定义的数据收集函数:
async function collectCustomMetrics() {
const [cpuTemp, diskIO, networkStats] = await Promise.all([
si.cpuTemperature(),
si.disksIO(),
si.networkStats()
]);
return {
cpuTemperature: cpuTemp.main,
diskRead: diskIO.rIO,
diskWrite: diskIO.wIO,
networkRx: networkStats.rx_sec,
networkTx: networkStats.tx_sec
};
}
错误处理最佳实践
async function safeGetSystemInfo() {
try {
const data = await si.system();
return data;
} catch (error) {
console.error('获取系统信息失败:', error);
return null;
}
}
总结
systeminformation 是一个功能强大且易于使用的 Node.js 库,它为系统监控和运维自动化提供了完整的解决方案。无论你是初学者还是经验丰富的开发者,都能快速上手并发挥其价值。
通过这个库,你可以:
- 轻松获取全面的系统硬件信息
- 构建实时监控系统
- 自动化运维流程
- 集成到现有的 DevOps 工具链中
现在就开始使用 systeminformation,让你的系统监控工作变得更加简单高效!
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考




