问题
I've recently started using pyvmomi to get a detailed inventory of vmware servers prior to migrating instances into AWS.
In the vcenter web interface, or vsphere client, I can inspect an instance and see its disks, and it'll tell me the disk size (provisioned), and how much of it is in use (used storage).
From the samples github repo (https://github.com/vmware/pyvmomi-community-samples) I could quickly learn how to get information on the instances, so getting the disk size is trivial (there's even a question in SO that shows an easy way to get the drives - How to get sizes of VMWare VM disks using PyVMomi), but I can't figure out how to get the actual used storage the web/client can show.
So, how do I get the used space for a given instance disks?
回答1:
For getting the freespace from the VM via PyVMomi first you have to check if the VMware tools for VM's is installed on your system or not. For checking if its installed, check from your VM's guest information from its summary page (via MOB) if it shows:
toolsStatus - VirtualMachineToolsStatus - "toolsNotInstalled": This means that you have to install the VMware tools to your respective VM, you can refer following links to install: a)https://my.vmware.com/web/vmware/details?productId=491&downloadGroup=VMTOOLS1000 or, b)https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1018377
toolsStatus - VirtualMachineToolsStatus - "toolsOk": This means that your VM already has VMware tools installed, and you can get the diskPath, capacity and freeSpace properties values from vim.vm.GuestInfo.DiskInfo. (If you install VMware tools manually as mentioned above, following should be true)
Once, the above environment is set, you can get the respective information from your VM via following code:
service_instance = None
vcenter_host = "HOSTNAME"
vcenter_port = NUMERIC_PORT
vcenter_username = "USERNAME"
vcenter_password = "PASSWORD"
vmName = "VM_NAME";
try:
#For trying to connect to VM
service_instance = connect.SmartConnect(host=vcenter_host, user=vcenter_username, pwd=vcenter_password, port=vcenter_port, sslContext=context)
atexit.register(connect.Disconnect, service_instance)
content = service_instance.RetrieveContent()
container = content.rootFolder # starting point to look into
viewType = [vim.VirtualMachine] # object types to look for
recursive = True # whether we should look into it recursively
containerView = content.viewManager.CreateContainerView(
container, viewType, recursive)
#getting all the VM's from the connection
children = containerView.view
#going 1 by 1 to every VM
for child in children:
vm = child.summary.config.name
#check for the VM
if(vm == vmName):
vmSummary = child.summary
#get the diskInfo of the selected VM
info = vmSummary.vm.guest.disk
#check for the freeSpace property of each disk
for each in info:
#To get the freeSPace in GB's
diskFreeSpace = each.freeSpace/1024/1024/1024
Hope it resolves your issue.
回答2:
If you are using above vSphere API 4.0, please have a look this vmware documentation
here is my vm configuration
json dump
vm = <find your vm>
jsonify(vm.summary.storage)
# dump output
(vim.vm.Summary.StorageSummary) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
committed = 8591326612, # 8GB (first commmited hdd)
uncommitted = 54964532611, # 20GB + 30GB (two added hdd)
unshared = 8589934592,
timestamp = 2018-07-03T07:23:26.715559Z }
python code
...
vm = <find your vm>
#disk size in byte
disk_size = vm.summary.storage.committed + vm.summary.storage.uncommitted
来源:https://stackoverflow.com/questions/38666195/getting-an-instances-actual-used-allocated-disk-space-in-vmware-with-pyvmomi