2

Code:

import docker
cli = docker.from_env()
print(cli.containers.get('container1').stats(stream=False)['precpu_stats'])

Output:

{'cpu_usage': {'total_usage': 6121320874, 'percpu_usage': [4662552384, 1458768490], 'usage_in_kernelmode': 970000000, 'usage_in_usermode': 4940000000}, 'system_cpu_usage': 24545140000000, 'online_cpus': 2, 'throttling_data': {'periods': 0, 'throttled_periods': 0, 'throttled_time': 0}}

How Can I get CPU usage percent from these informations? I used docker stats command on terminal and I got 0.11% CPU usage. But I can't get that 0.11% from this information.

2 Answers2

5

Finally I got The answer and it's way is a little long...

import docker
cli = docker.from_env()
print(cli.containers.get('container').stats(stream=False)['cpu_stats'])
print('---------------------------------------------')
print(cli.containers.get('container').stats(stream=False)['precpu_stats'])

Output is:

{
'cpu_usage': 
    {
     'total_usage': 25382985593,
     'percpu_usage': [17829217240, 7553768353],
     'usage_in_kernelmode': 3280000000,
     'usage_in_usermode': 21040000000
    },
'system_cpu_usage': 75406420000000,
'online_cpus': 2,
'throttling_data': 
    {
     'periods': 0,
     'throttled_periods': 0,
     'throttled_time': 0
    }
}
---------------------------------------------
{
'cpu_usage': 
    {
     'total_usage': 25382168431,
     'percpu_usage': [17828400078, 7553768353],
     'usage_in_kernelmode': 3280000000,
     'usage_in_usermode': 21040000000
    },
'system_cpu_usage': 75400410000000,
'online_cpus': 2,
'throttling_data': 
    {
        'periods': 0,
        'throttled_periods': 0,
        'throttled_time': 0
    }
}

And now for getting percentage we have to do these:

import docker
client = docker.from_env()
stats = client.containers.get('container').stats(stream=False)
UsageDelta = stats['cpu_stats']['cpu_usage']['total_usage'] - stats['precpu_stats']['cpu_usage']['total_usage']
# from informations : UsageDelta = 25382985593 - 25382168431

SystemDelta = stats['cpu_stats']['cpu_usage']['system_cpu_usage'] - stats['precpu_stats']['cpu_usage']['system_cpu_usage']
# from informations : SystemDelta = 75406420000000 - 75400410000000

len_cpu = len(stats['cpu_stats']['cpu_usage']['percpu_usage'])
# from my informations : len_cpu = 2


percentage = (UsageDelta / SystemDelta) * len_cpu * 100
# this is a little big because the result is : 0.02719341098169717

percent = round(percentage, 2)
# now The output is 0.02 and thats the answer.
  • 1
    As of the 15/12/2021 (docker-py 5.0.3), you get system_cpu_usage by doing stats["cpu_stats"]["system_cpu_usage"] and stats["precpu_stats"]["system_cpu_usage"] – David Bensoussan Dec 15 '21 at 13:27
1

https://gist.github.com/mdaum/a80913e9c828d4bad3714a709f49586e This typescript implementation will poll the container stats endpoint of your local docker dameon, and compute the utilization each time. You will see that the computations are lining up one-for-one with reported utilization from your dashboard.

Run Example

Mdaum
  • 21
  • 2