node获取服务器信息
简单写一篇关于node服务端获取状态信息的文章吧
其实也没什么好说的也有现成的文档
就是嫌弃麻烦直接封装了一下成品代码如下自取即可
node os模块参考文档
这个文档是内置的不需要npm insatll / yarn install
废话少说直接上代码
// const os = require("os");
// console.log("系统架构==>", os.arch());
// console.log('错误码、进程信号等的常用操作系统特定常量==>',os.constants);
// console.log("系统架构==>", os.cpus());
/**
* user <number> CPU 在用户模式下花费的毫秒数。
* nice <number> CPU 在良好模式下花费的毫秒数。
* sys <number> CPU 在系统模式下花费的毫秒数。
* idle <number> CPU 在空闲模式下花费的毫秒数。
* irq <number> CPU 在中断请求模式下花费的毫秒数。
*/
// console.log("系统空闲内存字节单位==>", os.freemem());
// console.log("系统空闲内存kb单位==>", os.freemem() / 1024);
// console.log("系统空闲内存mb单位==>", os.freemem() / 1024 / 1024);
// console.log("系统空闲内存Gb单位==>", os.freemem() / 1024 / 1024 / 1024);
// console.log("返回当前用户的主目录的字符串路径==>", os.homedir());
// console.log("以字符串形式返回操作系统的主机名==>", os.hostname());
// console.log("返回包含 1、5 和 15 分钟平均负载的数组。==>", os.loadavg());
// console.log("返回包含已分配网络地址的网络接口的对象。==>",os.networkInterfaces());
/**
* address <string> 分配的 IPv4 或 IPv6 地址
* netmask <string> IPv4 或 IPv6 网络掩码
* family <string> IPv4 或 IPv6
* mac <string> 网络接口的 MAC 地址
* internal <boolean> true 如果网络接口是环回接口或不可远程访问的类似接口; 否则 false
* scopeid <number> 数字的 IPv6 范围 ID(仅在 family 为 IPv6 时指定)
* cidr <string> 使用 CIDR 表示法的路由前缀分配的 IPv4 或 IPv6 地址。 如果 netmask 无效,则此属性设置为 null。
*/
// 返回标识为其编译 Node.js 二进制文件的操作系统平台的字符串。 该值在编译时设置。 可能的值为 'aix'、'darwin'、'freebsd'、'linux'、'openbsd'、'sunos'、以及 'win32'。
// console.log("二进制文件的操作系统平台的字符串==>", os.platform());
// console.log("以字符串形式返回操作系统==>", os.release());
// console.log("以整数形式返回系统内存总量(以字节为单位)。==>", os.totalmem());
// console.log("返回的操作系统名称==>", os.type());
// 系统信息
// 注意这是一个异步方法!
async function getSystemInfo() {
const osInfo = require("os");
let info = {
// CPU
cpu: await osInfo.cpus(),
// 内存 返回单位为GB
freemem: Number(await osInfo.freemem()).toFixed(2) / 1024 / 1024 / 1024,
// HostName
HostName: await osInfo.hostname(),
// 系统状态
loadavg: await osInfo.loadavg(),
// 网络对象
network: osInfo.networkInterfaces(),
// 操作系统
OsType: osInfo.type(),
// 主目录路径
platform: osInfo.platform(),
};
return info;
}