diff --git a/api/app/lib/controllers/homepage/index.js b/api/app/lib/controllers/homepage/index.js new file mode 100644 index 0000000..1c0d886 --- /dev/null +++ b/api/app/lib/controllers/homepage/index.js @@ -0,0 +1,66 @@ +'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 +} diff --git a/api/app/lib/routes/homepage/index.js b/api/app/lib/routes/homepage/index.js new file mode 100644 index 0000000..eafcb5d --- /dev/null +++ b/api/app/lib/routes/homepage/index.js @@ -0,0 +1,10 @@ +'use strict'; + +const backups = require('../../controllers/homepage/index'); + +module.exports = function (app, router, opts, AuthCode) { + + app.fs.api.logAttr['GET/homepage/node/resources'] = { content: '获取节点资源信息', visible: true }; + router.get('/homepage/node/resources', backups.getNodeResources(opts)) + +}; diff --git a/api/package.json b/api/package.json index cd41ba8..486827d 100644 --- a/api/package.json +++ b/api/package.json @@ -20,6 +20,7 @@ "better-xlsx": "^0.7.6", "clickhouse": "^2.6.0", "crypto-js": "^4.0.0", + "diskinfo": "0.0.3", "file-saver": "^2.0.2", "fs-web-server-scaffold": "^2.0.2", "ioredis": "^5.0.4", @@ -30,6 +31,7 @@ "mqtt": "^4.3.7", "node-schedule": "^2.1.0", "nodemailer": "^6.7.7", + "os-utils": "0.0.14", "path": "^0.12.7", "path-to-regexp": "^3.0.0", "pg": "^7.9.0", diff --git a/web/client/src/sections/homePage/containers/homePage.js b/web/client/src/sections/homePage/containers/homePage.js index b4db901..ebc7883 100644 --- a/web/client/src/sections/homePage/containers/homePage.js +++ b/web/client/src/sections/homePage/containers/homePage.js @@ -1,8 +1,11 @@ import React, { useEffect, useState } from 'react' import './style.less' -function homePage (props) { - return <>nothing> +function homePage(props) { + + return