From 20d540b05ab2dbd93e6450efa79744d3ae851135 Mon Sep 17 00:00:00 2001 From: jysperm Date: Wed, 15 Oct 2014 19:33:40 +0800 Subject: [PATCH] fixbugs in linux plugin --- core/view/panel.jade | 6 +++--- plugin/linux/index.coffee | 14 +++++++++---- plugin/linux/linux.coffee | 16 +++++++++++---- plugin/linux/locale/zh_CN.json | 10 +++++++-- plugin/linux/monitor.coffee | 3 ++- plugin/linux/view/widget.jade | 37 ++++++++++++++++++---------------- sample/rpvhost.config.coffee | 9 +++++++-- 7 files changed, 62 insertions(+), 33 deletions(-) diff --git a/core/view/panel.jade b/core/view/panel.jade index 5ea47b2..8f5f9d0 100644 --- a/core/view/panel.jade +++ b/core/view/panel.jade @@ -26,9 +26,9 @@ block main td= t(plan.t_description) td if plan.is_enable - button.action-leave-plan.btn.btn-danger.btn-sm= t('plan.join') + button.action-leave-plan.btn.btn-danger.btn-sm= t('plan.leave') else - button.action-join-plan.btn.btn-success.btn-sm= t('plan.leave') + button.action-join-plan.btn.btn-success.btn-sm= t('plan.join') if selectHook('view.panel.switch_buttons').length #service-switch.row @@ -40,7 +40,7 @@ block main button(data-name=hook.name).btn.btn-success #{t('common.enable')} #{t('plugins.' + hook.name + '.name')} for widget_html in widgets_html - != widget_html + .row!= widget_html prepend sidebar .row diff --git a/plugin/linux/index.coffee b/plugin/linux/index.coffee index 5af017a..d38cec1 100644 --- a/plugin/linux/index.coffee +++ b/plugin/linux/index.coffee @@ -29,10 +29,16 @@ exports.registerHook 'view.panel.styles', exports.registerHook 'view.panel.widgets', generator: (req, callback) -> - linux.getResourceUsageByAccount (resources_usage) -> - console.log resources_usage + linux.getResourceUsageByAccount req.account, (resources_usage) -> + resources_usage ?= + username: req.account.username + cpu: 0 + memory: 0 + storage: 0 + process: 0 + exports.render 'widget', req, - resources_usage: resources_usage[req.account.username] + usage: resources_usage , (html) -> callback html @@ -43,7 +49,7 @@ exports.registerHook 'account.resources_limit_changed', exports.registerServiceHook 'enable', action: (req, callback) -> linux.createUser req.account, -> - linux.setResourceLimit account, callback + linux.setResourceLimit req.account, callback exports.registerServiceHook 'disable', action: (req, callback) -> diff --git a/plugin/linux/linux.coffee b/plugin/linux/linux.coffee index d8f7d25..33db0ba 100644 --- a/plugin/linux/linux.coffee +++ b/plugin/linux/linux.coffee @@ -55,6 +55,7 @@ exports.getPasswdMap = (callback) -> is_json: true , (callback) -> fs.readFile '/etc/passwd', (err, content) -> + console.error err if err result = {} for line in _.compact(content.toString().split '\n') @@ -70,6 +71,7 @@ exports.getMemoryInfo = (callback) -> is_json: true , (callback) -> fs.readFile '/proc/meminfo', (err, content) -> + console.error err if err mapping = {} for line in content.toString().split('\n') @@ -113,6 +115,8 @@ exports.getProcessList = (callback) -> , (callback) -> exports.getPasswdMap (passwd_map) -> child_process.exec "sudo ps awufxn", (err, stdout) -> + console.error err if err + callback _.map stdout.split('\n')[1 ... -1], (item) -> result = /^\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$/.exec item @@ -146,6 +150,7 @@ exports.getStorageQuota = (callback) -> is_json: true , (callback) -> child_process.exec "sudo repquota -a", (err, stdout) -> + console.error err if err lines = _.filter stdout.split('\n')[5...-1], (i) -> i lines = _.map lines, (line) -> @@ -157,7 +162,7 @@ exports.getStorageQuota = (callback) -> return { username: username - size_used: parseInt size_used + size_used: parseFloat (parseInt(size_used) / 1024 / 1024).toFixed(1) inode_used: parseInt inode_used } @@ -192,7 +197,7 @@ exports.getSystemInfo = (callback) -> hostname: os.hostname() cpu: os.cpus()[0]['model'] uptime: os.uptime() - loadavg: _.map os.loadavg(), (i) -> i.toFixed(2) + loadavg: _.map os.loadavg(), (i) -> parseFloat(i.toFixed(2)) time: new Date() , callback @@ -203,6 +208,7 @@ exports.getStorageInfo = (callback) -> is_json: true , (callback) -> child_process.exec "df -h", (err, stdout) -> + console.error err if err disks = {} for line in stdout.split('\n') @@ -244,6 +250,7 @@ exports.getResourceUsageByAccounts = (callback) -> process_list: wrapAsync exports.getProcessList , (err, result) -> + console.error err if err resources_usage_by_accounts = [] for username, usage of monitor.resources_usage @@ -258,6 +265,7 @@ exports.getResourceUsageByAccounts = (callback) -> , callback -exports.getResourceUsageByAccount = (callback) -> +exports.getResourceUsageByAccount = (account, callback) -> exports.getResourceUsageByAccounts (resources_usage_by_accounts) -> - callback _.indexBy resources_usage_by_accounts, 'username' + callback _.findWhere resources_usage_by_accounts, + username: account.username diff --git a/plugin/linux/locale/zh_CN.json b/plugin/linux/locale/zh_CN.json index 1b1f746..6293d6a 100644 --- a/plugin/linux/locale/zh_CN.json +++ b/plugin/linux/locale/zh_CN.json @@ -1,5 +1,11 @@ { - "name": "Linux", + "": "Linux", "server_monitor": "服务器状态", - "description": "Linux 是 RP 主机的基础服务,负责进行资源限制" + "description": "Linux 是 RP 主机的基础服务,负责进行资源限制", + "widget": { + "hour_cpu": "一小时 CPU 时间", + "hour_memory": "一小时内存占用", + "storage": "储存空间", + "month_transfer": "月流量" + } } diff --git a/plugin/linux/monitor.coffee b/plugin/linux/monitor.coffee index cd72832..06a9f6a 100644 --- a/plugin/linux/monitor.coffee +++ b/plugin/linux/monitor.coffee @@ -59,7 +59,8 @@ exports.monitoring = (callback) -> IncreaseAccountUsage account_name, 'memory', memory_usage for username, usage of resources_usage - usage.memory = usage.memory / recent_resources_usage.length / config.plugins.linux.monitor_cycle * 1000 + base = recent_resources_usage.length / config.plugins.linux.monitor_cycle * 1000 + usage.memory = parseFloat (usage.memory / base).toFixed(1) async.each _.keys(resources_usage), (username, callback) -> mAccount.search username, (err, account) -> diff --git a/plugin/linux/view/widget.jade b/plugin/linux/view/widget.jade index 93539ee..5b678fb 100644 --- a/plugin/linux/view/widget.jade +++ b/plugin/linux/view/widget.jade @@ -1,27 +1,30 @@ -header Linux +- limit = account.resources_limit + +mixin displayProgressBar(now, limit, unit) + - per = parseInt((now / limit * 100).toFixed()) + - color = per < 85 ? 'success' : 'danger' + + .progress + .progress-bar(class="progress-bar-#{color}", role='progressbar', aria-valuenow='#{per}', + aria-valuemin='0', aria-valuemax='100', style='width: #{per}%;') + span #{now} / #{limit} #{unit} + +header= t('') table.table.table-hover tbody tr - td(style='width: 200px;') 一小时 CPU 时间 + td(style='width: 200px;')= t('widget.hour_cpu') td - .progress - .progress-bar.progress-bar-success(role='progressbar', aria-valuenow='#{resources_usage.cpu.now_per}', aria-valuemin='0', aria-valuemax='100', style='width: #{resources_usage.cpu.now_per}%;') - span #{resources_usage.cpu.now} / #{resources_usage.cpu.limit} s + mixin displayProgressBar(usage.cpu, limit.cpu, 's') tr - td 一小时内存占用 + td= t('widget.hour_memory') td - .progress - .progress-bar.progress-bar-success(role='progressbar', aria-valuenow='#{(resources_usage.memory.now_per}', aria-valuemin='0', aria-valuemax='100', style='width: #{resources_usage.memory.now_per}%;') - span #{resources_usage.memory.now} / #{resources_usage.memory.limit} M + mixin displayProgressBar(usage.memory, limit.memory, 'M') tr - td 储存空间 + td= t('widget.storage') td - .progress - .progress-bar(class='progress-bar-#{storage_usage.color}',role='progressbar', aria-valuenow='#{storage_usage.now_per}', aria-valuemin='0', aria-valuemax='100', style='width: #{storage_usage.now_per}%;') - span #{storage_usage.now} / #{storage_usage.limit} M + mixin displayProgressBar(usage.storage, limit.storage, 'M') tr - td 月流量 + td= t('widget.month_transfer') td - .progress - .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 + mixin displayProgressBar(0, limit.transfer, 'G') diff --git a/sample/rpvhost.config.coffee b/sample/rpvhost.config.coffee index 6e65fc1..fd64c00 100644 --- a/sample/rpvhost.config.coffee +++ b/sample/rpvhost.config.coffee @@ -35,8 +35,13 @@ module.exports = unit: 24 * 3600 * 1000 price: 10 / 30 - services: [] - resources: {} + services: ['ssh', 'linux'] + + resources: + cpu: 144 + storage: 520 + transfer: 39 + memory: 27 mongodb: user: 'rpadmin'