pyvmomi获取vCenter服务器的总cpu,内存统计信息

时间:2018-10-23 06:05:22

标签: python api vmware vcenter pyvmomi

我能够通过api获取主机,数据中心等不同对象的CPU内存。但是我需要整个vcenter的总CPU和内存统计信息,如下图所示使用pyvmomi。任何帮助,将不胜感激。 enter image description here

1 个答案:

答案 0 :(得分:1)

使用SmartConnect连接到vCentre后,您需要遍历并获取主机

主机硬件信息位于Git (git-credential-winstore)

一旦有了主机,便可以遍历它们并获取硬件详细信息。它们为原始格式,因此您必须进行转换。我已经粘贴了我使用的convertMemory函数

hosts = content.viewManager.CreateContainerView(content.rootFolder,[vim.HostSystem],True)
for host in hosts.view:
    # Print the hosts cpu details
    print(host.hardware.cpuInfo)
    # convert CPU to total hz to ghz times numCpuCores
    print("CPU:", round(((host.hardware.cpuInfo.hz/1e+9)*host.hardware.cpuInfo.numCpuCores),0),"GHz")
    #covert the raw bytes to readable size via convertMemory
    print("Memory:", convertMemory(host.hardware.memorySize))

convertMemory函数只是将数字转换为可读存储器

def convertMemory(sizeBytes):
    name = ("B", "KB", "MB", "GB", "TB", "PB")
    base = int(floor(log(sizeBytes, 1024)))
    power = pow(1024,base)
    size = round(sizeBytes/power,2)
    return "{}{}".format(floor(size),name[base])