You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.8 KiB
67 lines
1.8 KiB
2 years ago
|
'use strict';
|
||
|
const moment = require('moment');
|
||
|
const diskinfo = require('diskinfo');
|
||
|
const os = require('os-utils');
|
||
|
|
||
|
function getNodeResources(opts) {
|
||
|
return async function (ctx, next) {
|
||
|
let errMsg = { message: '获取节点资源失败' }
|
||
|
try {
|
||
|
//获取系统内存情况
|
||
|
let freeMem = os.freemem()
|
||
|
let totalMem = os.totalmem()
|
||
|
let memory = Math.round(((totalMem - freeMem) / totalMem) * 1000) / 10
|
||
|
//cpu利用率
|
||
|
let cpuUsage = await getCPUUsage()
|
||
|
|
||
|
//硬盘占用率数据
|
||
|
let disk = await getDiskInfo();
|
||
|
ctx.status = 200;
|
||
|
ctx.body = { disk, memory, cpu: Math.round(cpuUsage * 1000) / 10 };
|
||
|
|
||
|
} catch (error) {
|
||
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
||
|
ctx.status = 400;
|
||
|
ctx.body = errMsg
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取硬盘信息
|
||
|
*/
|
||
|
async function getDiskInfo() {
|
||
|
return new Promise(function (resolve, reject) {
|
||
|
diskinfo.getDrives((err, aDrives) => {
|
||
|
let map = new Map()
|
||
|
//遍历所有磁盘信息
|
||
|
let total = 0, used = 0;
|
||
|
for (var i = 0; i < aDrives.length; i++) {
|
||
|
if (!map.has(aDrives[i].mounted)) {
|
||
|
map.set(aDrives[i].mounted, true)
|
||
|
total += aDrives[i].blocks; //总量
|
||
|
used += aDrives[i].used; //已使用
|
||
|
}
|
||
|
}
|
||
|
let rate = Math.round(used / total * 1000) / 10
|
||
|
resolve(rate);
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取系统cpu利用率
|
||
|
*/
|
||
|
async function getCPUUsage() {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
os.cpuUsage(function (v) {
|
||
|
resolve(v)
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
|
||
|
module.exports = {
|
||
|
getNodeResources
|
||
|
}
|