mirror of
https://github.com/HackPlan/RootPanel.git
synced 2026-04-24 11:45:40 +08:00
some test of linux
This commit is contained in:
@@ -62,7 +62,7 @@ exports.getPasswdMap = (callback) ->
|
||||
, callback
|
||||
|
||||
exports.getMemoryInfo = (callback) ->
|
||||
cache.try 'linux.getProcessList', (SETEX) ->
|
||||
cache.try 'linux.getMemoryInfo', (SETEX) ->
|
||||
fs.readFile '/proc/meminfo', (err, content) ->
|
||||
logger.error err if err
|
||||
mapping = {}
|
||||
@@ -73,12 +73,13 @@ exports.getMemoryInfo = (callback) ->
|
||||
mapping[key.trim()] = parseInt (parseInt(value.trim().match(/\d+/)) / 1024).toFixed()
|
||||
|
||||
used = mapping['MemTotal'] - mapping['MemFree'] - mapping['Buffers'] - mapping['Cached']
|
||||
used_per = (used / mapping['MemTotal'] * 100).toFixed()
|
||||
cached_per = (mapping['Cached'] / mapping['MemTotal'] * 100).toFixed()
|
||||
buffers_per = (mapping['Buffers'] / mapping['MemTotal'] * 100).toFixed()
|
||||
used_per = parseInt (used / mapping['MemTotal'] * 100).toFixed()
|
||||
cached_per = parseInt (mapping['Cached'] / mapping['MemTotal'] * 100).toFixed()
|
||||
buffers_per = parseInt (mapping['Buffers'] / mapping['MemTotal'] * 100).toFixed()
|
||||
free_per = 100 - used_per - cached_per - buffers_per
|
||||
|
||||
swap_free_per = (mapping['SwapFree'] / mapping['SwapTotal'] * 100).toFixed()
|
||||
swap_free_per = parseInt (mapping['SwapFree'] / mapping['SwapTotal'] * 100).toFixed()
|
||||
swap_free_per = 100 if _.isNaN swap_free_per
|
||||
swap_used_per = 100 - swap_free_per
|
||||
|
||||
SETEX
|
||||
@@ -96,8 +97,8 @@ exports.getMemoryInfo = (callback) ->
|
||||
buffers_per: buffers_per
|
||||
free_per: free_per
|
||||
|
||||
swap_used_per: swap_used_per
|
||||
swap_free_per: swap_free_per
|
||||
swap_used_per: swap_used_per ? 0
|
||||
swap_free_per: swap_free_per ? 0
|
||||
, 3
|
||||
|
||||
, callback
|
||||
@@ -108,32 +109,32 @@ exports.getProcessList = (callback) ->
|
||||
child_process.exec "sudo ps awufxn", (err, stdout) ->
|
||||
logger.error err if err
|
||||
|
||||
result = _.map stdout.split('\n')[1 ... -1], (item) ->
|
||||
plist = _.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
|
||||
|
||||
return {
|
||||
user: do ->
|
||||
if passwd_map[result[1]]
|
||||
return passwd_map[result[1]]
|
||||
else
|
||||
return result[1]
|
||||
user: do ->
|
||||
if passwd_map[result[1]]
|
||||
return passwd_map[result[1]]
|
||||
else
|
||||
return result[1]
|
||||
|
||||
time: do ->
|
||||
[minute, second] = result[10].split ':'
|
||||
return parseInt(minute) * 60 + parseInt(second)
|
||||
time: do ->
|
||||
[minute, second] = result[10].split ':'
|
||||
return parseInt(minute) * 60 + parseInt(second)
|
||||
|
||||
pid: parseInt result[2]
|
||||
cpu_per: parseInt result[3]
|
||||
mem_per: parseInt result[4]
|
||||
vsz: parseInt result[5]
|
||||
rss: parseInt result[6]
|
||||
tty: result[7]
|
||||
stat: result[8]
|
||||
start: result[9]
|
||||
command: result[11]
|
||||
pid: parseInt result[2]
|
||||
cpu_per: parseInt result[3]
|
||||
mem_per: parseInt result[4]
|
||||
vsz: parseInt result[5]
|
||||
rss: parseInt result[6]
|
||||
tty: result[7]
|
||||
stat: result[8]
|
||||
start: result[9]
|
||||
command: result[11]
|
||||
}
|
||||
|
||||
SETEX result, 5
|
||||
SETEX plist, 5
|
||||
|
||||
, callback
|
||||
|
||||
@@ -228,7 +229,7 @@ exports.getStorageInfo = (callback) ->
|
||||
, callback
|
||||
|
||||
exports.getResourceUsageByAccounts = (callback) ->
|
||||
cache.try 'linux.getStorageInfo', (SETEX) ->
|
||||
cache.try 'linux.getResourceUsageByAccounts', (SETEX) ->
|
||||
async.parallel
|
||||
storage_quota: wrapAsync exports.getStorageQuota
|
||||
process_list: wrapAsync exports.getProcessList
|
||||
|
||||
@@ -25,13 +25,57 @@ describe 'plugin/linux', ->
|
||||
it 'pending'
|
||||
|
||||
describe 'getPasswdMap', ->
|
||||
it 'pending'
|
||||
before (done) ->
|
||||
cache.delete 'linux.getPasswdMap', done
|
||||
|
||||
it 'should success', (done) ->
|
||||
linux.getPasswdMap (passwd_map) ->
|
||||
passwd_map.should.be.a 'object'
|
||||
|
||||
for k, v of passwd_map
|
||||
parseInt(k).toString().should.be.equal k
|
||||
v.should.be.a 'string'
|
||||
|
||||
done()
|
||||
|
||||
describe 'getMemoryInfo', ->
|
||||
it 'pending'
|
||||
before (done) ->
|
||||
cache.delete 'linux.getMemoryInfo', done
|
||||
|
||||
it 'should success', (done) ->
|
||||
linux.getMemoryInfo (memory_info) ->
|
||||
for field in [
|
||||
'used', 'cached', 'buffers', 'free', 'total', 'swap_used', 'swap_free'
|
||||
'swap_total', 'used_per', 'cached_per', 'buffers_per', 'free_per'
|
||||
'swap_used_per', 'swap_free_per'
|
||||
]
|
||||
expect(isNaN(memory_info[field])).to.be.false
|
||||
memory_info[field].should.be.a 'number'
|
||||
|
||||
done()
|
||||
|
||||
describe 'getProcessList', ->
|
||||
it 'pending'
|
||||
before (done) ->
|
||||
cache.delete 'linux.getProcessList', done
|
||||
|
||||
it 'should success', (done) ->
|
||||
linux.getProcessList (plist) ->
|
||||
plist.should.be.a 'array'
|
||||
|
||||
for p in plist
|
||||
p.user.should.be.a 'string'
|
||||
p.time.should.be.a 'number'
|
||||
p.pid.should.be.a 'number'
|
||||
p.cpu_per.should.be.a 'number'
|
||||
p.mem_per.should.be.a 'number'
|
||||
p.vsz.should.be.a 'number'
|
||||
p.rss.should.be.a 'number'
|
||||
p.tty.should.be.a 'string'
|
||||
p.stat.should.be.a 'string'
|
||||
p.start.should.be.a 'string'
|
||||
p.command.should.be.a 'string'
|
||||
|
||||
done()
|
||||
|
||||
describe 'getStorageQuota', ->
|
||||
it 'pending'
|
||||
|
||||
Reference in New Issue
Block a user