wenlele
2 years ago
5 changed files with 100 additions and 3 deletions
@ -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 |
|||
} |
@ -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)) |
|||
|
|||
}; |
@ -1,8 +1,25 @@ |
|||
import React, { useEffect, useState } from 'react' |
|||
import { connect } from 'react-redux'; |
|||
import './style.less' |
|||
|
|||
function homePage (props) { |
|||
return <>nothing</> |
|||
function homePage(props) { |
|||
const { clientHeight } = props; |
|||
return <div style={{ width: '100%', height: clientHeight, overflow: 'hidden' }}> |
|||
1 |
|||
</div> |
|||
} |
|||
|
|||
export default homePage |
|||
function mapStateToProps(state) { |
|||
const { |
|||
auth, global |
|||
} = state; |
|||
return { |
|||
clientHeight: global.clientHeight, |
|||
actions: global.actions, |
|||
|
|||
}; |
|||
} |
|||
|
|||
export default connect(mapStateToProps)(homePage); |
|||
|
|||
|
|||
|
Loading…
Reference in new issue