gpt4 book ai didi

java - 遍历 vcenter list 和检索属性的有效方法

转载 作者:行者123 更新时间:2023-11-30 08:10:42 34 4
gpt4 key购买 nike

我正在使用 yavijava,需要遍历 vCenter 并在其上构建所有主机和 VM 的列表。对于每个主机和虚拟机,我需要检索一些属性,例如名称、RAM/CPU 数量等。我当前的代码如下所示:

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 发送新请求这一事实,是否有更有效的方法来执行此操作?

最佳答案

您需要手动构建一个属性收集器,并根据库存创建一个库存系统来关联对象。我在 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);
}
}

关于java - 遍历 vcenter list 和检索属性的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31546443/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com