2014-04-02 73 views
0

我正在使用vSphere API收集esx系統的信息。通過使用此API,我可以通過提供IP來收集esx主機的信息。如何獲取使用vSphere API和Java連接到vCenter Server的所有esx主機的信息?

我正在使用java和vsphere API和java。 這裏是代碼:

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();} 

通過使用該代碼和操作對象managedentities我可以提取每個主機的信息和VM每個主機上工作。

現在,我不想收集來自不同主機的信息,而是想從vcenter服務器收集信息。 vCenter Server還包含有關主機及其VM的所有詳細信息。

我想通過使用vCenter Server而不是訪問每臺主機來獲取此信息。是否有可能獲得這些信息?怎麼樣?

回答

3

您正在正確的軌道上。你只需要迭代主機系統對象。

下面是一個代碼:

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(); 
} 

} 

這可能對你有幫助..

0
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; 
} 
} 

如果仍然有一些疑問,然後點擊下方喜歡看現場演示

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

相關問題