Reputation: 34
I am able to get cpu memory of different objects like hosts, datacenter through the api. But i need the total CPU, memory statistics of the whole vcenter as shown in the pic below using pyvmomi. Any help would be appreciated.
Upvotes: 0
Views: 3641
Reputation: 184
You need to loop through and get the hosts after connecting to vCentre with SmartConnect
The host hardware information is located here
Once you have the hosts you loop through them and get the hardware details. They are in raw format so you have to convert them. I've pasted the convertMemory function I use
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))
The convertMemory function just converts the number to readable memory
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])
Upvotes: 2