如何通过API的方式从cAdvisor获取容器的CPU、内存、磁盘、网络等信息?


如何通过API的方式从cAdvisor获取容器的CPU、内存、磁盘、网络等信息?
已邀请:

junjun16818 - 菜bi

赞同来自: justasabc


官方API文档
https://github.com/google/cadv ... pi.md

例如:
Container Name Resource Name
/ /api/v1.0/containers/
/foo /api/v1.0/containers/foo
/docker/2c4dee605d22 /api/v1.0/containers/docker/2c4dee605d22

进入cAdvisor Web页面 查看源码 找到 container.js 这里有如何计算返回的数据

例如container.js里面的 drawCpuTotalUsage ,drawMemoryUsage,drawNetworkBytes 等方法
把它们修改一下就可以了

下面是用nodejs写的 仅作参考

<pre>
'use strict';

var _ = require('underscore')
, config = require('../../conf/config.js')
, httpTools = require('../../lib/http.js')
, monitor = require('../../lib/monitor.js')

function MonitorManager(){}

MonitorManager.prototype.getResourceUsage = function(container, done)
{
var self = this

this.getMachineInfo(container, function(err, machine)
{
if( err )
return done(err)

self.getContainerInfo(container, function(err, info)
{
if( err )
return done(err)

var data = self.getOverallUsage(machine, info)

if( data )
done(null, data)
else
done('error')
})
})
}

MonitorManager.prototype.getContainerInfo = function(container, done)
{
var data = {
port: config.cadvisors_port
, hostname: container.server
, method: 'POST'
, path: '/api/v1.0/containers/docker/' + container._id
}

httpTools.request(data, { num_stats: 2, num_samples: 0 }, function(err, data){
done(err, data)
})
}

MonitorManager.prototype.getMachineInfo = function(container, done)
{
var data = {
port: config.cadvisors_port
, hostname: container.server
, path: '/api/v1.0/machine'
}

console.log(data)

httpTools.request(data, {}, function(err, data){
done(err, data)
})
}

MonitorManager.prototype.getInterval = function(current, previous)
{
var cur = new Date(current)
, prev = new Date(previous)

return (cur.getTime() - prev.getTime()) * 1000000
}

MonitorManager.prototype.getOverallUsage = function(machineInfo, containerInfo)
{

var cur = containerInfo.stats
, data = {}

data.cpuUse = this.getCpuUse(machineInfo, containerInfo)
data.memoryUse = this.getMemoryUse(machineInfo, containerInfo)

data.broadband = this.getBroadbandUse(machineInfo, containerInfo)

return data
}

MonitorManager.prototype.getBroadbandUse = function(machineInfo, stats)
{
var cur = stats.stats
, prev = stats.stats

var rx_bytes = cur.network.rx_bytes - prev.network.rx_bytes
, tx_bytes = cur.network.tx_bytes - prev.network.tx_bytes

if( tx_bytes < 1024000 ){
tx_bytes = (tx_bytes/1024).toFixed(2) + 'KB'
}
else{
tx_bytes = (tx_bytes/1024/1024).toFixed(2) + 'MB'
}

if( rx_bytes < 1024000 ){
rx_bytes = (rx_bytes/1024).toFixed(2) + 'KB'
}
else{
rx_bytes = (rx_bytes/1024/1024).toFixed(2) + 'MB'
}

return { rx_bytes: rx_bytes, tx_bytes: tx_bytes }
}

MonitorManager.prototype.getMemoryUse = function(machineInfo, stats)
{
var cur = stats.stats
, memoryUsage = 0
if( stats.spec.has_memory)

{
var limit = stats.spec.memory.limit

if( limit > machineInfo.memory_capacity ) {
limit = machineInfo.memory_capacity
}

memoryUsage = Math.round(cur.memory.usage || 0);
}

return Math.round(memoryUsage / 1024 / 1024)
}

MonitorManager.prototype.hasResource = function(stats, resource) {
return stats.stats.length > 0 && stats.stats;
}

MonitorManager.prototype.getCpuUse = function(machineInfo, stats)
{
if (stats.spec.has_cpu && !this.hasResource(stats, "cpu")) {
return
}

var data = <span>
, cur = stats.stats
, prev = stats.stats
, intervalInNs = this.getInterval(cur.timestamp, prev.timestamp)

for (var j = 0; j < machineInfo.num_cores; j++) {

data.push(((cur.cpu.usage.per_cpu_usage - prev.cpu.usage.per_cpu_usage) / intervalInNs * 100).toFixed(2));
}

return data
}

module.exports = new MonitorManager()

</pre>

william - cSphere CEO

赞同来自:


为何不直接从docker stats接口获取?

weekface

赞同来自:


cAdvisor提供了一个golang客户端:https://github.com/google/cadv ... lient

要回复问题请先登录注册