2015-07-21 66 views
2

我正在使用yavijava,並且需要遍歷vCenter並構建所有主機和虛擬機的列表。對於每個主機和虛擬機,我需要找回一些屬性,如名稱,RAM/CPU等我當前的代碼看起來像這樣的量:遍歷vCenter中心和檢索屬性的高效方式

ManagedEntity[] hosts = new InventoryNavigator.searchManagedEntities("VirtualMachine"); 
for(int i=0;i<hosts.length;i++) { 
    String name = hosts[i].getName(); 
    String xxx = hosts[i].XXXXX; 
..... 

等了虛擬機了。

我的問題是,考慮到存在大量對象並且每個調用(例如getName)向vSphere發送新請求的事實,是否有更有效的方法來執行此操作?

回答

3

您將需要手動構建屬性收集器,並根據您要創建庫存系統來關聯對象的庫存。我在github上有一個樣本集羣:https://github.com/yavijava/yavijava_cluster_prop_example

RetrieveOptions options = new RetrieveOptions(); 
    options.setMaxObjects(100); 
    String[] vmProps = new String[2]; 
    vmProps[0] = "name"; 
    vmProps[1] = "runtime.host"; 
    PropertySpec vmSpec = new PropertySpec(); 
    vmSpec.setAll(false); 
    vmSpec.setType("VirtualMachine"); 
    vmSpec.setPathSet(vmProps); 

    String[] hostProps = new String[4]; 
    hostProps[0] = "name"; 
    hostProps[1] = "summary.hardware.numCpuCores"; 
    hostProps[2] = "summary.hardware.cpuModel"; 
    hostProps[3] = "summary.hardware.memorySize"; 
    PropertySpec hostSpec = new PropertySpec(); 
    hostSpec.setAll(false); 
    hostSpec.setType("HostSystem"); 
    hostSpec.setPathSet(hostProps); 

    String[] clusterProps = new String[2]; 
    clusterProps[0] = "name"; 
    clusterProps[1] = "parent"; 
    PropertySpec clusterSpec = new PropertySpec(); 
    clusterSpec.setAll(false); 
    clusterSpec.setType("ClusterComputeResource"); 
    clusterSpec.setPathSet(clusterProps); 

    ObjectSpec oSpec = new ObjectSpec(); 
    oSpec.setObj(clusterMe.getMOR()); 
    oSpec.setSelectSet(com.vmware.vim25.mo.util.PropertyCollectorUtil.buildFullTraversalV4()); 
    PropertyFilterSpec[] pfSpec = new PropertyFilterSpec[1]; 
    pfSpec[0] = new PropertyFilterSpec(); 

    ObjectSpec[] oo = new ObjectSpec[1]; 
    oo[0] = oSpec; 

    pfSpec[0].setObjectSet(oo); 
    PropertySpec[] pp = new PropertySpec[3]; 
    pp[0] = vmSpec; 
    pp[1] = hostSpec; 
    pp[2] = clusterSpec; 

    pfSpec[0].setPropSet(pp); 
    RetrieveResult ret = serviceInstance.getPropertyCollector().retrievePropertiesEx(pfSpec, options); 

    for (ObjectContent aRet : ret.getObjects()) { 
     if(aRet.getObj().type.equalsIgnoreCase("ClusterComputeResource")) { 
      printInfo(aRet); 
     } 
     if(aRet.getObj().type.equalsIgnoreCase("HostSystem")) { 
      System.out.println("Host Info: "); 
      printInfo(aRet); 
      System.out.println("#######################"); 
     } 
     if(aRet.getObj().type.equalsIgnoreCase("VirtualMachine")) { 
      System.out.println("VirtualMachine: "); 
      printInfo(aRet); 
      System.out.println("#######################################"); 
     } 
    } 
} 

private static void printInfo(ObjectContent objectContent) { 
    // This is super generic here... To actually relate the objects so you 
    // know which HostSystem a VirtualMachine lives on you need to implement 
    // some kind of inventory system and use the MOR from the HostSystem 
    // and the MOR from the vm.runtime.host 
    for(DynamicProperty props: objectContent.getPropSet()) { 
     System.out.println(props.val); 
    } 
} 
+0

謝謝邁克爾。這正是我所期待的。我無法找到有關某個對象可用屬性的任何文檔。例如,summary.hardware.memorySize,如何獲取第一張網卡的屬性? – darkstar

+0

這些屬性都列在給定對象的SOAP文檔中。例如:http://www.yavijava.com/docs/vim.VirtualMachine.html這些收藏家可以變得非常先進。 –

+0

噢,是的,我想知道如何將它轉換爲虛擬機上的多個網卡的點符號。 – darkstar