From 9178faaf0acdeeeb40e4770919382651a64b0abe Mon Sep 17 00:00:00 2001 From: jysperm Date: Wed, 23 Jul 2014 01:03:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E9=A6=96=E9=A1=B5=E4=B8=8A=E5=AE=9E?= =?UTF-8?q?=E6=97=B6=20CPU=20=E5=92=8C=E5=86=85=E5=AD=98=E5=8D=A0=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/static/style/panel.less | 13 ++++++++++++- plugin/linux/monitor.coffee | 8 ++++++-- plugin/linux/service.coffee | 7 ++++++- plugin/linux/view/widget.jade | 12 ++++++++---- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/core/static/style/panel.less b/core/static/style/panel.less index 92a0fe5..1d187e3 100644 --- a/core/static/style/panel.less +++ b/core/static/style/panel.less @@ -45,4 +45,15 @@ td { font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 16px; } -} \ No newline at end of file +} + +.progress { + position: relative; +} + +.progress span { + position: absolute; + display: block; + width: 100%; + color: black; +} diff --git a/plugin/linux/monitor.coffee b/plugin/linux/monitor.coffee index 2f055ea..07ad0ad 100644 --- a/plugin/linux/monitor.coffee +++ b/plugin/linux/monitor.coffee @@ -12,6 +12,8 @@ ITEM_IN_RESOURCES_LIST = 3600 * 1000 / config.plugins.linux.monitor_cycle last_plist = [] passwd_cache = {} +exports.resources_usage = {} + exports.run = -> exports.monitoring() setInterval exports.monitoring, config.plugins.linux.monitor_cycle @@ -107,7 +109,9 @@ exports.monitoring = -> addAccountUsage account_name, 'memory', memory_usage for account_name, usage of account_usage - usage.memory = usage.memory / (resources_usage_list.length * config.plugins.linux.monitor_cycle * 1000) + usage.memory = usage.memory / resources_usage_list.length / config.plugins.linux.monitor_cycle * 1000 + + exports.resources_usage = account_usage app.redis.setex REDIS_OVERVIEW, 60, JSON.stringify(account_usage), -> async.each _.keys(account_usage), (account_name, callback) -> @@ -147,7 +151,7 @@ exports.monitoringCpu = (plist, callback) -> for item in exist_process last_process = findLastProcess item - addTime item.user, last_process.time - item.time + addTime item.user, item.time - last_process.time for item in new_process addTime item.user, item.time diff --git a/plugin/linux/service.coffee b/plugin/linux/service.coffee index 0ce866f..2f3cd8f 100644 --- a/plugin/linux/service.coffee +++ b/plugin/linux/service.coffee @@ -1,5 +1,6 @@ jade = require 'jade' path = require 'path' +monitor = require './monitor' module.exports = enable: (account, callback) -> @@ -9,7 +10,11 @@ module.exports = callback() widget: (account, callback) -> - jade.renderFile path.join(__dirname, 'view/widget.jade'), {}, (err, html) -> + jade.renderFile path.join(__dirname, 'view/widget.jade'), + account: account + resources_usage: monitor.resources_usage[account.username] ? {cpu: 0, memory: 0} + , (err, html) -> + throw err if err callback html preview: (callback) -> diff --git a/plugin/linux/view/widget.jade b/plugin/linux/view/widget.jade index 18d763d..b7e5035 100644 --- a/plugin/linux/view/widget.jade +++ b/plugin/linux/view/widget.jade @@ -5,19 +5,23 @@ table.table.table-hover td(style='width: 200px;') 一小时 CPU 时间 td .progress - .progress-bar.progress-bar-success(role='progressbar', aria-valuenow='20', aria-valuemin='0', aria-valuemax='100', style='width: 20%;') 58 / 144 s + .progress-bar.progress-bar-success(role='progressbar', aria-valuenow='#{(resources_usage.cpu / account.attribute.resources_limit.cpu * 100).toFixed()}', aria-valuemin='0', aria-valuemax='100', style='width: #{(resources_usage.cpu / account.attribute.resources_limit.cpu * 100).toFixed()}%;') + span #{resources_usage.cpu.toFixed(1)} / #{account.attribute.resources_limit.cpu} s tr td 一小时内存占用 td .progress - .progress-bar.progress-bar-success(role='progressbar', aria-valuenow='85', aria-valuemin='0', aria-valuemax='100', style='width: 85%;') 23 / 27 M + .progress-bar.progress-bar-success(role='progressbar', aria-valuenow='#{(resources_usage.memory / account.attribute.resources_limit.memory * 100).toFixed()}', aria-valuemin='0', aria-valuemax='100', style='min-width: 30px; width: #{(resources_usage.memory / account.attribute.resources_limit.memory * 100).toFixed()}%;') + span #{resources_usage.memory.toFixed(1)} / #{account.attribute.resources_limit.memory} M tr td 储存空间 td .progress - .progress-bar.progress-bar-success(role='progressbar', aria-valuenow='10', aria-valuemin='0', aria-valuemax='100', style='width: 10%;') 52 / 520 M + .progress-bar.progress-bar-success(role='progressbar', aria-valuenow='0', aria-valuemin='0', aria-valuemax='100', style='width: 0%;') + span 0 / #{account.attribute.resources_limit.storage} M tr td 月流量 td .progress - .progress-bar.progress-bar-success(role='progressbar', aria-valuenow='10', aria-valuemin='0', aria-valuemax='100', style='width: 10%;') 4 / 39G + .progress-bar.progress-bar-success(role='progressbar', aria-valuenow='0', aria-valuemin='0', aria-valuemax='100', style='width: 0%;') + span 0 / #{account.attribute.resources_limit.transfer} G