如何从HostSystem对象确定vCenter Server?

时间:2013-10-21 16:03:36

标签: vmware vsphere esx

我正在查询ESX主机,其中一些主机由vCenter服务器管理,而另一些则不是。我想找出管理该主机的vCenter服务器的名称,如果有的话。

我正在使用Python psphere模块,但我想要的只是我应该查看的对象和属性的类型。这是我的代码中的相关摘录:

from psphere.client import Client
import psphere.managedobjects

items = []

cl = Client( hostname, userid, password )
dcs = psphere.managedobjects.Datacenter.all( cl )

我通过检查数据中心列表来识别ESX主机与vCenter服务器:

if len( dcs ) == 1 and dcs[0].name == 'ha-datacenter':
    hosts = psphere.managedobjects.HostSystem.all( cl )

通常,上面的主机将是一个元素的列表:ESX主机。我想知道如何找出该主机是否有管理ESX服务器。 vSphere客户端会这样做,所以必须有办法。

else:   #  This is a vCenter server, so we can drill down to find the ESX servers
    for dc in dcs:
        items.extend( getEntities( dc, [hostname] ) )

getEntities()是我自己的功能,用于收集有关主机和vCenter服务器的详细信息。

2 个答案:

答案 0 :(得分:1)

我在HostSystem对象中找到了以下属性:.summary.managementServerIp。并非所有HostSystem个对象都具有此属性,因此我按如下方式检查它:

host = ... a HostSystem object, acquired as described in the question ...
if 'managementServerIp' in host.summary:
    ... do something with the management server IP address ...

答案 1 :(得分:1)

并不总是填充该数据字段(managementServerIp)。获取vcenter信息的更好属性是来自get-vmhost cmdlet的ServiceUrl(.extensiondata.Client.ServiceUrl)。此外,无需在IP上进行反向查找以获取名称。

只需解析URL即可获取vCenter Server的名称。这个正则表达式(使用-replace)完成了这项工作:

'http[s|]://([^:]*):.*$', '$1'