libvirt.ListAllDomains返回的domains结构体中包含每个虚拟机的信息,包括CPU占用率和内存信息。要获取每个docker的CPU占用率,可以使用以下代码:

import libvirt

conn = libvirt.open()
if conn == None:
    print('Failed to open connection to the hypervisor')
    exit(1)

domains = conn.listAllDomains()

for domain in domains:
    stats = domain.getCPUStats(True)
    print("Domain name: " + domain.name())
    print("CPU usage: " + str(stats[0]['cpu_time'] / 1000000000.0) + " seconds")

要获取每个docker使用的内存和目前已经使用的内存,可以使用以下代码:

import libvirt

conn = libvirt.open()
if conn == None:
    print('Failed to open connection to the hypervisor')
    exit(1)

domains = conn.listAllDomains()

for domain in domains:
    mem_stats = domain.memoryStats()
    print("Domain name: " + domain.name())
    print("Memory usage: " + str(mem_stats['actual'] / 1024) + " MB")
    print("Memory usage (current): " + str(mem_stats['rss'] / 1024) + " MB")

其中,mem_stats['actual']表示当前分配给虚拟机的内存大小,mem_stats['rss']表示虚拟机当前使用的内存大小。两者都以KB为单位

libvirtListAllDomains中返回的domains结构体如何获取每个docker的cpu占用率和所用内存和目前已经使用的内存

原文地址: https://www.cveoy.top/t/topic/hfLL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录