Get Docker Container CPU Usage as Percentage

前端 未结 3 1676
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 17:10

Docker provides an interactive stats command, docker stats [cid] which gives up to date information on the CPU usage, like so:

CONTAINER      CPU         


        
3条回答
  •  佛祖请我去吃肉
    2020-12-14 17:20

    After we consume the remote api we get these fields: precpu_stats/cpu_stats

    Then, basically here is the code: (javascript example)

    var res <---- remote api response
    
    var cpuDelta = res.cpu_stats.cpu_usage.total_usage -  res.precpu_stats.cpu_usage.total_usage;
    var systemDelta = res.cpu_stats.system_cpu_usage - res.precpu_stats.system_cpu_usage;
    var RESULT_CPU_USAGE = cpuDelta / systemDelta * 100;
    

    Just to clarify the RESULT_CPU_USAGE... it's the amount of resource consumed from your physical hardware, so supposing you are getting RESULT_CPU_USAGE as 50%, it means that 50% of all your PC power is being used by container X

提交回复
热议问题