start to work on RRD

This commit is contained in:
Olivier Lambert
2015-03-22 23:55:14 +01:00
committed by Fabrice Marsaud
parent 71194d5b4e
commit 178a429f26
3 changed files with 65 additions and 0 deletions

View File

@@ -1,10 +1,14 @@
$debug = (require 'debug') 'xo:api:vm'
$findWhere = require 'lodash.find'
$result = require 'lodash.result'
$forEach = require 'lodash.foreach'
$isArray = require 'lodash.isarray'
$findIndex = require 'lodash.findindex'
$request = require 'request'
{$coroutine, $wait} = require '../fibers-utils'
{formatXml: $js2xml} = require '../utils'
{parseXml} = require '../utils'
$isVMRunning = do ->
runningStates = {
@@ -1004,3 +1008,60 @@ detachPci.resolve = {
}
detachPci.permission = 'admin'
exports.detachPci = detachPci
stats = $coroutine ({vm}) ->
xapi = @getXAPI vm
host = @getObject vm.$container
do (type = host.type) =>
if type is 'pool'
host = @getObject host.master, 'host'
else unless type is 'host'
throw new Error "unexpected type: got #{type} instead of host"
url = $request {
method: 'get'
rejectUnauthorized: false
url: 'https://'+host.address+'/vm_rrd?session_id='+xapi.sessionId+'&uuid='+vm.UUID
}, (error, response, body) ->
if !error and response.statusCode == 200
json = parseXml(body)
# Find index of needed objects for getting their values after
# CPU: TODO fetch every CPUs!
cpuIndex = $findIndex(json.rrd.ds, 'name': 'cpu0')
memoryFreeIndex = $findIndex(json.rrd.ds, 'name': 'memory_internal_free')
memoryIndex = $findIndex(json.rrd.ds, 'name': 'memory')
memoryFree = []
memoryUsed = []
memory = []
cpu = []
date = [] #TODO
# TODO: fetch other info: network, IOPS etc.
$forEach json.rrd.rra[0].database.row, (n, key) ->
# WARNING! memoryFree is in Kb not in b, memory is in b
memoryFree.push(n.v[memoryFreeIndex]*1024)
memoryUsed.push(Math.round(parseInt(n.v[memoryIndex])-(n.v[memoryFreeIndex]*1024)))
memory.push(parseInt(n.v[memoryIndex]))
cpu.push(n.v[cpuIndex]*100)
date.push(key)
return
vm.stats.memoryFree = memoryFree
vm.stats.memoryUsed = memoryUsed
vm.stats.memory = memory
vm.stats.date = date
vm.stats.cpu = cpu
return true
stats.params = {
id: { type: 'string' }
}
stats.resolve = {
vm: ['id', ['VM', 'VM-snapshot']],
}
stats.permission = 'admin'
exports.stats = stats;

View File

@@ -539,6 +539,9 @@ module.exports = ->
$VBDs: -> @genval.VBDs
VIFs: -> @genval.VIFs
stats: -> {}
}
@rule VM: VMdef
@rule 'VM-controller': VMdef

View File

@@ -10,6 +10,7 @@ $isString = require 'lodash.isstring'
$pluck = require 'lodash.pluck'
$Promise = require 'bluebird'
$proxyRequest = require 'proxy-http-request'
$httpRequest = require 'request'
{createClient: $createRedisClient} = require 'then-redis'
{
hash: $hash