如何使用pyVmomi从ESXi服务器及vCenter提取主机CPU/RAM/存储详情?
Hey there! Let's tackle your two pyVmomi tasks step by step—first pulling hardware details directly from an ESXi host, then fetching the same info for hosts managed by vCenter. I'll include working code snippets with explanations so you can adapt them easily.
1. Fetch CPU, RAM, and Storage Details Directly from ESXi
First, you'll need to connect directly to your ESXi host. Note that ESXi might require enabling direct API access (check your host's security settings), and you can bypass certificate checks temporarily for testing (but don't do this in production!).
Here's a complete example:
import ssl from pyVim.connect import SmartConnect, Disconnect import vim def get_esxi_details(host_ip, username, password): # Bypass certificate verification (for testing only!) ssl_context = ssl._create_unverified_context() try: # Connect to ESXi host si = SmartConnect( host=host_ip, user=username, pwd=password, sslContext=ssl_context ) if not si: print("Could not connect to the ESXi host.") return # Get the ESXi host object (only one host when connecting directly) content = si.RetrieveContent() host = content.viewManager.CreateContainerView(content.rootFolder, [vim.HostSystem], True).view[0] # Extract CPU details cpu_info = host.hardware.cpuInfo cpu_details = { "total_cores": cpu_info.numCpuCores, "total_threads": cpu_info.numCpuThreads, "cpu_model": cpu_info.model, "mhz_per_core": cpu_info.hz / 1000000 # Convert Hz to MHz } # Extract RAM details ram_size_bytes = host.hardware.memorySize ram_details = { "total_ram_gb": round(ram_size_bytes / (1024**3), 2), "available_ram_gb": round(host.hardware.memoryAvailable / (1024**3), 2) } # Extract Storage details storage_system = host.configManager.storageSystem datastores = storage_system.datastore storage_details = [] for ds in datastores: ds_info = { "datastore_name": ds.name, "total_capacity_gb": round(ds.summary.capacity / (1024**3), 2), "free_space_gb": round(ds.summary.freeSpace / (1024**3), 2), "datastore_type": ds.summary.type } storage_details.append(ds_info) # Combine all details esxi_details = { "host_name": host.name, "cpu": cpu_details, "ram": ram_details, "storage": storage_details } return esxi_details finally: if si: Disconnect(si) # Usage example if __name__ == "__main__": details = get_esxi_details("your-esxi-ip", "root", "your-esxi-password") if details: print("ESXi Host Details:") print(details)
Quick Notes:
- The
vim.HostSystemobject holds all core hardware info—since we're connecting directly to ESXi, there's only one host in the view. - For storage, we use the host's
storageSystemto pull datastore stats like capacity and free space. - Remember to swap out the placeholder credentials with your actual ESXi info.
2. Fetch Host Details from vCenter
When connecting to vCenter, you'll likely have multiple hosts under its management. The logic is similar, but we'll iterate through all hosts in the vCenter inventory.
Here's the code:
import ssl from pyVim.connect import SmartConnect, Disconnect import vim def get_vcenter_host_details(vcenter_ip, username, password): ssl_context = ssl._create_unverified_context() try: # Connect to vCenter si = SmartConnect( host=vcenter_ip, user=username, pwd=password, sslContext=ssl_context ) if not si: print("Could not connect to vCenter.") return content = si.RetrieveContent() # Get all hosts in vCenter's inventory host_view = content.viewManager.CreateContainerView(content.rootFolder, [vim.HostSystem], True) hosts = host_view.view host_view.Destroy() # Clean up the view after fetching data all_host_details = [] for host in hosts: # CPU details (same as direct ESXi query) cpu_info = host.hardware.cpuInfo cpu_details = { "total_cores": cpu_info.numCpuCores, "total_threads": cpu_info.numCpuThreads, "cpu_model": cpu_info.model, "mhz_per_core": cpu_info.hz / 1000000 } # RAM details ram_size_bytes = host.hardware.memorySize ram_details = { "total_ram_gb": round(ram_size_bytes / (1024**3), 2), "available_ram_gb": round(host.hardware.memoryAvailable / (1024**3), 2) } # Storage details storage_system = host.configManager.storageSystem datastores = storage_system.datastore storage_details = [] for ds in datastores: ds_info = { "datastore_name": ds.name, "total_capacity_gb": round(ds.summary.capacity / (1024**3), 2), "free_space_gb": round(ds.summary.freeSpace / (1024**3), 2), "datastore_type": ds.summary.type } storage_details.append(ds_info) # Combine host details (added management IP for clarity) host_detail = { "host_name": host.name, "management_ip": host.config.network.vnic[0].spec.ip.ipAddress, "cpu": cpu_details, "ram": ram_details, "storage": storage_details } all_host_details.append(host_detail) return all_host_details finally: if si: Disconnect(si) # Usage example if __name__ == "__main__": host_details = get_vcenter_host_details("your-vcenter-ip", "admin@vsphere.local", "your-vcenter-password") if host_details: print("All Hosts from vCenter:") for hd in host_details: print("\n--- Host:", hd["host_name"], "---") print(hd)
Quick Notes:
CreateContainerViewlets us fetch everyvim.HostSystemobject managed by the vCenter—perfect for bulk queries.- We added the host's management IP by pulling the first vNIC's address; adjust this if your hosts use multiple management interfaces.
- In production, replace the unverified SSL context with a proper certificate setup to avoid security risks.
内容的提问来源于stack exchange,提问作者Udit

