简体   繁体   English

使用VI Java在Vmware中获取PoweredOn VM计数

[英]Get PoweredOn VM count in Vmware using VI Java

How to get the PoweredOn VM count in Vmware using the VI Java SDK? 如何使用VI Java SDK获取Vmware中的PoweredOn VM计数? Is there a way to filter the VMs based on the power state? 有没有一种方法可以根据电源状态过滤VM?

The following code gives the VMs reference objects. 以下代码提供了VM参考对象。

 ManagedEntity[] vms = new InventoryNavigator(clusterEntity).searchManagedEntities("VirtualMachine");

To filter the PoweredOn state VMs, we have to loop through the managed entities and retrieve the VM object, which is a time-consuming operation. 要筛选PoweredOn状态的VM,我们必须遍历托管实体并检索VM对象,这是一项耗时的操作。 Hence is there a way to get the count information alone? 因此,有没有办法单独获取计数信息?

One way of doing this is via the PropertyCollector 's RetrievePropertiesEx method. 一种方法是通过PropertyCollectorRetrievePropertiesEx方法。

Note that the PropertyCollector is a fairly complex beast - very briefly you need to provide it with a recursion strategy (eg recurse into child folders) as well as the properties you wish to extract. 请注意, PropertyCollector是一个相当复杂的野兽-简要地说,您需要为其提供递归策略(例如,递归到子文件夹中)以及要提取的属性。 If you've never worked with the PropertyCollector before then you should look up a sample program online and modify it. 如果您以前从未使用过PropertyCollector ,则应该在线查找示例程序并进行修改。

The nice thing about the PropertyCollector is that it is a single call that can retrieve a specific piece of information for a large number of entities. 关于PropertyCollector是,它是单个调用,可以为大量实体检索特定的信息。 So it's quite efficient in that respect. 因此在这方面非常有效。 You can also get the count pretty easily. 您还可以轻松获得计数。

As you noted, the alternative is to query for each VM one by one, performing multiple queries and receiving much more data you have to process. 如您所述,另一种方法是逐一查询每个VM,执行多个查询并接收要处理的更多数据。 Depending on the number of VMs you have/expect to have, and your performance requirements, you can select the best approach for you. 根据您拥有/期望拥有的VM数量以及您的性能要求,可以选择最适合您的方法。

If you're on vSphere 6.5, then consider using the REST API. 如果您使用的是vSphere 6.5,请考虑使用REST API。 It has a list operation for VMs that accepts query parameters, and the power state is among the few properties that are exposed via that API. 它具有用于VM的list操作,该操作接受查询参数,并且电源状态是通过该API公开的少数属性之一。 So you can get a list of powered on VMs by using the URL GET https://{server}/rest/vcenter/vm?filter.power_states.1=POWERED_ON . 因此,您可以使用URL GET https://{server}/rest/vcenter/vm?filter.power_states.1=POWERED_ON来获取已启动的VM的列表。 Note that this is limited to 1000 VMs. 请注意,这仅限于1000个VM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM