user3322141 user3322141 - 1 month ago 14
Java Question

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?

Answer

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..