Is there any ways to get drive information of each vmware in Vmware ESX via Vsphere SDK with Java?

1.3k views Asked by At

In my vmware esx server, I have four vmware such as Window 7, Window XP, Window Server 2008 and Window Server 2012. And drive information for each vmware:

  • Window 7 -> drive C - Used 2GB, Free 2GB, Total 4GB

  • Window XP -> drive C - Used 2GB, Free 2GB, Total 4GB

  • Window Server 2008 -> drive C - Used 2GB, Free 2GB, Total 4GB -> drive D - Used 2GB, Free 2GB, Total 4GB

  • Window Server 2012 -> drive C - Used 2GB, Free 2GB, Total 4GB -> drive D - Used 2GB, Free 2GB, Total 4GB

My question is:

How do I get drive detail information (for example, Drive C, used space and free space of Window 7) by vsphere sdk? And also drive information of another 3 vmware.

2

There are 2 answers

0
Tun Lin Aung On BEST ANSWER

Answer:

GuestInfo info = vm.getGuest(); //(vm means VirtualMachine object.)
GuestDiskInfo[] dInfos = info.getDisk();
if (dInfos != null) {
  System.out.println("Disk Info");
  for (GuestDiskInfo dInfo : dInfos) {
    System.out.println(" Capacity(GB):"+dInfo.getCapacity()/1024/1024/1024);
    System.out.println(" Free(GB):"+dInfo.getFreeSpace()/1024/1024/1024);
    System.out.println(" Disk Path:"+dInfo.getDiskPath());
    System.out.println("============");
  }
}

**Important: Firstly need to install vmware tools on each guest os machine. From this question, guest os are Window 7, Window Xp, Window Server 2008 and Window server 2012.

0
Rishi Anand On

From 'vm' object you can get all disk attached to virtual machine by given below code.

Below image is displaying the information inside virtualDisk object.

enter image description here

VirtualMachine vm = getVM(); // get the vm object
VirtualMachineConfigInfo vmConfig = vm.getConfig();
VirtualDevice[] vds = vmConfig.getHardware().getDevice();
for(VirtualDevice vd : vds){
    if(vd instanceof VirtualDisk){
        System.out.println("CapacityInKB : " + ((VirtualDisk) vd).getCapacityInKB());
        System.out.println("Name :"+((VirtualDisk) vd).getDeviceInfo().getLabel());
        //vd object contains many more information
    }
}