如何使用HTTP API在Docker中计算容器的CPU使用率

时间:2019-11-28 06:26:43

标签: docker

我知道可以通过docker stats获得容器的CPU利用率:

#docker stats
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT    MEM %               NET I/O             BLOCK I/O           PIDS
05076af468cd        mystifying_kepler   0.02%               10.5MiB / 5.712GiB   0.18%               656B / 0B           0B / 0B             1


我想通过HTTP api:http api获取此数据。 我从此http api获得的数据是:

{
    "read": "2019-11-26T22:18:33.027963669Z",
    "preread": "2019-11-26T22:18:32.013978454Z",
    "pids_stats": {
        "current": 1
    },
    "blkio_stats": {
        "io_service_bytes_recursive": [],
        "io_serviced_recursive": [],
        "io_queue_recursive": [],
        "io_service_time_recursive": [],
        "io_wait_time_recursive": [],
        "io_merged_recursive": [],
        "io_time_recursive": [],
        "sectors_recursive": []
    },
    "num_procs": 0,
    "storage_stats": {},
    "cpu_stats": {
        "cpu_usage": {
            "total_usage": 361652820,
            "percpu_usage": [361652820],
            "usage_in_kernelmode": 50000000,
            "usage_in_usermode": 100000000
        },
        "system_cpu_usage": 144599100000000,
        "online_cpus": 1,
        "throttling_data": {
            "periods": 0,
            "throttled_periods": 0,
            "throttled_time": 0
        }
    },
    "precpu_stats": {
        "cpu_usage": {
            "total_usage": 361488978,
            "percpu_usage": [361488978],
            "usage_in_kernelmode": 50000000,
            "usage_in_usermode": 100000000
        },
        "system_cpu_usage": 144598090000000,
        "online_cpus": 1,
        "throttling_data": {
            "periods": 0,
            "throttled_periods": 0,
            "throttled_time": 0
        }
    },
    "memory_stats": {
        "usage": 11005952,
        "max_usage": 11108352,
        "stats": {
            "active_anon": 11005952,
            "active_file": 0,
            "cache": 0,
            "dirty": 0,
            "hierarchical_memory_limit": 9223372036854771712,
            "hierarchical_memsw_limit": 9223372036854771712,
            "inactive_anon": 0,
            "inactive_file": 0,
            "mapped_file": 0,
            "pgfault": 8151,
            "pgmajfault": 0,
            "pgpgin": 4137,
            "pgpgout": 1450,
            "rss": 11005952,
            "rss_huge": 0,
            "total_active_anon": 11005952,
            "total_active_file": 0,
            "total_cache": 0,
            "total_dirty": 0,
            "total_inactive_anon": 0,
            "total_inactive_file": 0,
            "total_mapped_file": 0,
            "total_pgfault": 8151,
            "total_pgmajfault": 0,
            "total_pgpgin": 4137,
            "total_pgpgout": 1450,
            "total_rss": 11005952,
            "total_rss_huge": 0,
            "total_unevictable": 0,
            "total_writeback": 0,
            "unevictable": 0,
            "writeback": 0
        },
        "limit": 6133108736
    },
    "name": "/mystifying_kepler",
    "id": "05076af468cdeb3d15d147a25e8ccee5f4d029ffcba1d60f14f84e2c9e25d6a9",
    "networks": {
        "eth0": {
            "rx_bytes": 656,
            "rx_packets": 8,
            "rx_errors": 0,
            "rx_dropped": 0,
            "tx_bytes": 0,
            "tx_packets": 0,
            "tx_errors": 0,
            "tx_dropped": 0
        }
    }
}

我能够从数据中计算内存的利用率,但是我没有找到获取CPU利用率的方法。

还有主意吗?

1 个答案:

答案 0 :(得分:0)

您现在可能已经解决了这个问题,但是对于下一个人来说……此示例在Python中,但是如果您要进行API调用,则数据字段和数学是相同的。

API返回累积值,因此您需要多个样本-使用样本之间的差进行数学运算以获得该时间段的利用率。此示例使用流模式,该模式每秒推送一次更新。

  # These initial values will seed the "last" cycle's saved values
containerCPU = 0
systemCPU = 0

container = client.containers.get(containerID)

  #This function is blocking; the loop will proceed when there's a new update to iterate
for stats in container.stats(decode=True):

    #Save the values from the last sample
  lastContainerCPU = containerCPU
  lastSystemCPU = systemCPU

    #Get the container's usage, the total system capacity, and the number of CPUs
    #The math returns a Linux-style %util, where 100.0 = 1 CPU core fully used
  containerCPU = stats.get('cpu_stats',{}).get('cpu_usage',{}).get('total_usage')
  systemCPU    = stats.get('cpu_stats',{}).get('system_cpu_usage')
  numCPU   = len(stats.get('cpu_stats',{}).get('cpu_usage',{}).get('percpu_usage',0))

    # Skip the first sample (result will be wrong because the saved values are 0)
  if lastContainerCPU and lastSystemCPU:
    cpuUtil = (containerCPU - lastContainerCPU) / (systemCPU - lastSystemCPU)
    cpuUtil = cpuUtil * numCPU * 100
    print(cpuUtil)