user3322141
user3322141

Reputation: 158

How to get information of all esx hosts these are connected to vCenter server using vSphere API and Java?

I am collecting information of esx systems using vSphere API. By using this API, I am able to collect information of esx hosts by giving IP.

I am using java and vsphere API and java. Here is code:

try {
ServiceInstance si = new ServiceInstance(new URL(url), user, pass,true);
System.out.println("host :"+host+"---"+si.getAboutInfo().getFullName());

System.out.println(" Version is .. " +si.getAboutInfo().version);
System.out.println(" os type is .. " +si.getAboutInfo().osType);
System.out.println("Vendor is .. " + si.getAboutInfo().vendor);
System.out.println("name is" + si.getAboutInfo().name);

ManagedEntity[] managedEntities = new InventoryNavigator(
si.getRootFolder()).searchManagedEntities("VirtualMachine");
ManagedEntity[] hostmanagedEntities = new InventoryNavigator(
si.getRootFolder()).searchManagedEntities("HostSystem");

for (ManagedEntity hostmanagedEntity : hostmanagedEntities) {
HostSystem hostsys = (HostSystem) hostmanagedEntity;

String ESXhostname = hostsys.getName();

HostListSummary hls = hostsys.getSummary();
HostHardwareSummary hosthwi = hls.getHardware();
HostListSummaryQuickStats hqs = hls.getQuickStats();
Datastore[] HDS = hostsys.getDatastores();
StringBuilder DS = new StringBuilder();
for (int i=0;i <HDS.length;i++){
DatastoreSummary dsm =HDS[i].getSummary();

DS.append(dsm.name+":"+dsm.capacity+":"+dsm.freeSpace+"-");
}

int MEM=hqs.overallMemoryUsage;
int UPT=hqs.getUptime();
Integer CPU=hqs.getOverallCpuUsage();   

String esxkey = "ESXRealInfo";
String esxvalue = "ESXhostname-" + ESXhostname
+ ";CPU Usage-" + CPU + ";MEM Usage-"
+ MEM + ";UPTIME-" + UPT+"; Datastores -"+DS;
}


}
 catch (Exception e){ e.printStackTrace();}

By using this code and manipulating managedentities object I can extract information of each host and vm working on each host.

Now instead of collecting information from different hosts, I want to collect information from vcenter server. vCenter server also contains all details about hosts and their VMs.

I want to get this information by using vCenter server instead of visiting each host. Is it possible to get this information? how?

Upvotes: 1

Views: 8903

Answers (2)

Jeet Singh Parmar
Jeet Singh Parmar

Reputation: 727

package com.vcenter;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import com.vmware.vim25.VirtualMachineQuickStats;
import com.vmware.vim25.VirtualMachineSummary;
import com.vmware.vim25.mo.ClusterComputeResource;
import com.vmware.vim25.mo.Folder;
import com.vmware.vim25.mo.HostSystem;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ManagedEntity;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.VirtualMachine;

public class VCenterUtils {
 private ServiceInstance serviceInstance = null;

 public VCenterUtils(String url, String userName, String password) {
  try {
   serviceInstance = new ServiceInstance(new URL(url), userName, password, true);
  } catch (RemoteException e) {
   e.printStackTrace();
  } catch (MalformedURLException e) {
   e.printStackTrace();
  }
 }

 private ManagedEntity[] getClusters() {
  Folder rootFolder = null;
  ManagedEntity[] entities = null;
  rootFolder = serviceInstance.getRootFolder();
  try {
   entities = new InventoryNavigator(rootFolder).searchManagedEntities("ClusterComputeResource");
  } catch (RemoteException e) {
   e.printStackTrace();
  }
  return entities;
 }

 public List findVirtualMachines() {
  List machineDTOs = new ArrayList();
  ManagedEntity[] entities = getClusters();
  ClusterComputeResource cluster = null;
  if (entities.length > 0) {
   for (ManagedEntity entity : entities) {
    cluster = (ClusterComputeResource) entity;
    HostSystem[] hosts = cluster.getHosts();
    for(HostSystem host : hosts) {
     VirtualMachine[] vms;
     try {
      vms = host.getVms();
      for(VirtualMachine vm : vms) { 
       VirtualMachineDTO machineDTO = new VirtualMachineDTO();
       machineDTO.setName(vm.getName());
       machineDTO.setStatus(vm.getOverallStatus().name());
       machineDTO.setState(vm.getSummary().getRuntime().getPowerState().name());

       VirtualMachineSummary summary = vm.getSummary();
       Double comittedSpace = Double.valueOf(summary.getStorage().getUncommitted())/1024/1024/1024;
       Double uncomittedSpace = Double.valueOf(summary.getStorage().getUncommitted())/1024/1024/1024;
       //calculating Provisioned Space of a virtual machine
       Double provisionedSpace = comittedSpace + uncomittedSpace;
       machineDTO.setProvisionedSpace(provisionedSpace);
       machineDTO.setUsedSpace(comittedSpace);

       VirtualMachineQuickStats virtualMachineQuickStats =summary.getQuickStats();
       machineDTO.setHostCpu(virtualMachineQuickStats.getOverallCpuUsage());
       machineDTO.setHostMemory(virtualMachineQuickStats.getHostMemoryUsage()); 
       machineDTOs.add(machineDTO);
      }
     } catch (RemoteException e) {
      e.printStackTrace();
     }
    }
   }
  }
  return machineDTOs;
 }
}

If still have some doubt then click below like to see live demo

How to get information of all esx hosts these are connected to vCenter server using vSphere API and Java?

Upvotes: 0

Vishwas
Vishwas

Reputation: 7067

You are on right track. You just need to iterate hostsystems object.

Here is a code:

String url = "https://" + host + "/sdk/vimService";
    System.out.println("In main function");
ServiceInstance si = null;
ManagedEntity[] managedEntities = null;
ManagedEntity[] hostmanagedEntities = null;
try {
si = new ServiceInstance(new URL(url), user, pass,true);

hostmanagedEntities = new InventoryNavigator(
si.getRootFolder()).searchManagedEntities("HostSystem");

//process each host and call getVMDetails function to get details of all host       
for (ManagedEntity managedEntity : hostmanagedEntities) {
HostSystem hostsystemobj = (HostSystem) managedEntity;
System.out.println("Host: '" + hostsystemobj.getName() + "'");
hostname = hostsystemobj.getName();
String ESXhostname = hostsystemobj.getName();
HostHardwareInfo hw = hostsystemobj.getHardware();
String ESXhostmodel = hw.getSystemInfo().getModel();
String Vendor = hw.getSystemInfo().getVendor();
//print all above variables. 
long ESXhostmem = hw.getMemorySize();

short ESXhostcores = hw.getCpuInfo().getNumCpuCores();
long ESXMHZ = hw.getCpuInfo().getHz();

  //call function that collects vms data for specific host
getVMDetailsInv(si,hostsystemobj.getName());

}
}
catch (InvalidProperty e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (RuntimeFault e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (RemoteException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

private void getVMDetailsInv(ServiceInstance si,String name) {

HostSystem myhost;
System.out.println("host name from details function  is .. " + name);
try {
myhost = (HostSystem) new        InventoryNavigator(si.getRootFolder()).searchManagedEntity("HostSystem",name);
System.out.println("myhost is .. " + myhost);
ManagedEntity[] hostSpecificEntities = new InventoryNavigator(myhost).searchManagedEntities("VirtualMachine");

for (int i = 0; i < hostSpecificEntities.length; i++) {
    VirtualMachine vm = (VirtualMachine) hostSpecificEntities[i];
    String macAddress="";
    for(VirtualDevice vd:vm.getConfig().getHardware().getDevice()){
    try {
    VirtualEthernetCard vEth=(VirtualEthernetCard)vd;
     macAddress=vEth.macAddress;
    }
    catch(Exception e){}
    }
System.out.println("Name : "+vm.getName());
String vmVersion = vm.getConfig().version;
System.out.println("vm wayer version is ..from inventory.. "+ vm.getConfig().version);
System.out.println("geust os uuid "+vm.getSummary().getConfig().uuid);
System.out.println("geust mac Address of guest  "+macAddress);
System.out.println("geust id is "+vm.getSummary().getGuest().guestId);
System.out.println("host id is "+vm.getSummary().getGuest().getIpAddress());    
    }
    } catch (RuntimeFault e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (RemoteException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}

This might be helpful to you..

Upvotes: 3

Related Questions