get available resource pools for a given host.

1,732 views
Skip to first unread message

Nick Wiesmueller

unread,
Feb 20, 2012, 5:10:30 PM2/20/12
to pysphere
What mor_type is expected here? I'm trying to use the MOR for a host.
I don't see "Host" in the mor_types datastructure. The idea is to get
the resource pool for a given host.
server.get_resource_pools('host-772')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\nick.wiesmueller\workspace\ICTest\trunk\lib
\pysphere-0.1.6-py2.7.egg\pysphere\vi_server.py", line 196, in
get_resource_pools
obj_type=MORTypes.ResourcePool)
File "C:\Users\nick.wiesmueller\workspace\ICTest\trunk\lib
\pysphere-0.1.6-py2.7.egg\pysphere\vi_server.py", line 486, in
_retrieve_properties_traversal
FaultTypes.PARAMETER_ERROR)
VIException: [Parameter Error]: from_node must be a MOR object or a
(<str> mor_id, <str> mor_type) tuple


Thanks,

Nick

Sebastián Tello

unread,
Feb 20, 2012, 6:35:36 PM2/20/12
to pysp...@googlegroups.com, Nick Wiesmueller
Hi Nick,

The MOR Type for a host is called "HostSystem".

So you should do:

server.get_resource_pools(('host-772', 'HostSystem'))

or

server.get_resource_pools(('host-772', MORTypes.HostSystem))

Saludos,

Sebastian.


> server.get_resource_pools('host-772')

Nick Wiesmueller

unread,
Feb 21, 2012, 12:16:24 PM2/21/12
to Sebastián Tello, pysp...@googlegroups.com
server.get_resource_pools(('host-772','HostSystem'))

yields this. 

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\nick.wiesmueller\workspace\ICTest\trunk\lib\pysphere-0.1.6-py2.7.egg\pysphere\vi_server.py", line 197, in get_resource_pools
    for oc in prop:
TypeError: 'NoneType' object is not iterable

server.get_resource_pools() with no arguments results in this:

{'resgroup-773': '/Resources/QAVMs-250', 'resgroup-771': '/Resources', 'resgroup-382': '/Resources', 'resgroup-180': '/Resources', 'resgroup-182': '/Resources/QAVMs-252', 'resgroup-384': '/Resources/QAVMs-251'}

I don't know the code well enough to go much further here. I do know that my HostSystem MOR is correct and there is one resource pool (Resources/QAVMS-250) on this host. 

Thanks, 

Nick

On Mon, Feb 20, 2012 at 5:35 PM, Sebastián Tello <arg...@gmail.com> wrote:
Hi Nick,

   The MOR Type for a host is called "HostSystem".

So you should do:

server.get_resource_pools(('host-772', 'HostSystem'))

or

server.get_resource_pools(('host-772', MORTypes.HostSystem))

Saludos,
asdf

Seba

unread,
Feb 22, 2012, 5:04:00 PM2/22/12
to Nick Wiesmueller, pysp...@googlegroups.com
Yes, I've got the same error.

The reason is I'm not traversing from hosts to resource pools. I think there's no property in vsphere linking a resourcepool to a host or visceversa, because a resourcepool is associated to a cluster (or compute resource) which can be composed by many hosts. Summarizing, I think a resource pool can't belong to a host.

In fact in the inventory view of the VIClient for a vCenter hosts and root resourcepools are in the same hierarchy level. In ESX/ESXi, as you have only one host, you can assume that all resourcepools belong to it.

Instead you may try to filter by cluster (in vCenter), in ESX/ESXi I think is senseless to filter.

clusters = server.get_clusters()
for mor in clusters.keys():
   server.get_resource_pools(mor)

or

server.get_resource_pools(('domain-c716', MORTypes.ClusterComputeResource))

or

server.get_resource_pools(('domain-c716', 'ClusterComputeResource'))


Anyway, I should catch that exception and raise a more specific exception. I'll create an issue ticket for that.


Regards,


Seba


2012/2/21 Nick Wiesmueller

Andrea Colapicchioni

unread,
Feb 23, 2012, 5:19:01 AM2/23/12
to pysp...@googlegroups.com, Nick Wiesmueller
Dear Seba, 

thank you for your great job.

I am trying to gather usage statistics for a number of vSphere 4.1 hosts I manage. I have the free version, so I have no vCenter.

s = VIServer()
s.connect("myserver", "mysuser", "mypassword")
h = s.get_hosts()
s.get_resource_pools()   # this is what I obtain: {'ha-root-pool': '/Resources', 'pool1': '/Resources/CSNDC', 'pool0': '/Resources/COMMON', 'pool2': '/Resources/IMDATE'}
s.get_resource_pools(('ha-root-pool',  'ClusterComputeResource'))

I have this:

Traceback (most recent call last):
  File "C:\Users\Andrea\Documents\Development\VMWare Py Utilities\testVM.py", line 11, in <module>
    s.get_resource_pools(('ha-root-pool',  'ClusterComputeResource'))
  File "C:\Python26\lib\site-packages\pysphere-0.1.6-py2.6.egg\pysphere\vi_server.py", line 196, in get_resource_pools
    obj_type=MORTypes.ResourcePool)
  File "C:\Python26\lib\site-packages\pysphere-0.1.6-py2.6.egg\pysphere\vi_server.py", line 624, in _retrieve_properties_traversal
    return request_call(request)
  File "C:\Python26\lib\site-packages\pysphere-0.1.6-py2.6.egg\pysphere\vi_server.py", line 640, in call_retrieve_properties_ex
    request)._returnval
  File "C:\Python26\lib\site-packages\pysphere-0.1.6-py2.6.egg\pysphere\resources\VimService_services.py", line 118, in RetrievePropertiesEx
    response = self.binding.Receive(RetrievePropertiesExResponseMsg.typecode)
  File "C:\Python26\lib\site-packages\pysphere-0.1.6-py2.6.egg\pysphere\ZSI\client.py", line 544, in Receive
    self.ReceiveSOAP(**kw)
  File "C:\Python26\lib\site-packages\pysphere-0.1.6-py2.6.egg\pysphere\ZSI\client.py", line 427, in ReceiveSOAP
    if not self.IsSOAP():
  File "C:\Python26\lib\site-packages\pysphere-0.1.6-py2.6.egg\pysphere\ZSI\client.py", line 419, in IsSOAP
    self.ReceiveRaw()
  File "C:\Python26\lib\site-packages\pysphere-0.1.6-py2.6.egg\pysphere\ZSI\client.py", line 386, in ReceiveRaw
    response = self.local.h.getresponse()
  File "C:\Python26\lib\httplib.py", line 990, in getresponse
    response.begin()
  File "C:\Python26\lib\httplib.py", line 391, in begin
    version, status, reason = self._read_status()
  File "C:\Python26\lib\httplib.py", line 355, in _read_status
    raise BadStatusLine(line)
httplib.BadStatusLine

This interrupts the connection to all connected VSphere client. I suspect this is a bug in the VMWare API's..

I found no other way to query statistics for the VM the various server are hosting. I am trying to obtain the host-cpu and gust mem usage (the same I can see in the vSphere client tab).

Regards, 
Andrea

Nick Wiesmueller

unread,
Feb 23, 2012, 10:56:35 AM2/23/12
to Seba, pysp...@googlegroups.com
Thanks Seba,

Unfortunately we don't have any clusters defined in our environment (and creating one is not an option), is there a way to get to ComputeResources  MOR without using a cluster MOR? It seems like there must be. 

Thanks again,

Nick

Seba

unread,
Feb 24, 2012, 2:37:33 PM2/24/12
to pysp...@googlegroups.com, Andrea Colapicchioni
Hi Andrea,

   Nice crash!, yep it seams to be a bug in 4.1, I also got the crash in a ESX 4.1 server but it doesn't crash in my ESXi 5.0 server.

   It seams the issue is triggered when sending a MOR with the wrong type. To set the correct type, instead of:

s.get_resource_pools(('ha-root-pool',  'ClusterComputeResource'))

  You should do:

s.get_resource_pools(('ha-root-pool',  'ResourcePool'))

I'll look into the server's log and probably attach a debugger to it to find out if this bug is exploitable (perhaps it may lead to remote code execution and let an attacker gain control of the server).

I'll also see if I can achieve a similar crash without any user credentials to turn that into a denial of service attack.


To get a host overall cpu and memory usage you can do this:

from pysphere import VIServer, VIProperty

s = VIServer()
s.connect("server", "user", "password")


for mor, name in s.get_hosts().items():
    prop = VIProperty(s, mor)
    print "Stats for", name
    print "  overall processor usage:", prop.summary.quickStats.overallCpuUsage
    print "  overall memory usage:", prop.summary.quickStats.overallMemoryUsage

    #the info in prop is cached when constructed, to flush the cache
    #run prop._flush_cache()

s.disconnect()


That might be pretty slow, as VIProperty will get all the properties from the host object (which are a lot), let me know if you are interested in a more performant method (e.g. to continuously monitor these values) so I can provide you with a much more faster solution.


Regards,

Seba

2012/2/23 Andrea Colapicchioni 

Seba

unread,
Feb 29, 2012, 4:53:42 PM2/29/12
to pysp...@googlegroups.com, Nick Wiesmueller
Hi Nick,

  Sorry I forgot to answer this question of yours.

  Under the current trunk version (revision 62) you can do this:

>>> compute_resources = s._get_managed_objects(MORTypes.ComputeResource)

or

>>> compute_resources = s._get_managed_objects("ComputeResource")

That will return a dictionary where the keys are the MORs you're looking for and the values the name of the object.

If you're under the 0.1.6 release you can do this to get the same result:

>>> content = s._retrieve_properties_traversal(property_names=['name'], obj_type="ComputeResource")
>>> compute_resources = dict([(o.Obj, o.PropSet[0].Val) for o in content])

Then you can use those keys in the returned dictionary to filter the resourcepools:

s.get_resource_pools(compute_resources .keys()[0])


Regards,


Sebastian.

2012/2/23 Nick Wiesmueller
Thanks Seba,

Unfortunately we don't have any clusters defined in our environment (and creating one is not an option), is there a way to get to ComputeResources  MOR without using a cluster MOR? It seems like there must be. 

Thanks again,

Nick



2012/2/21 Nick Wiesmueller

Mohammed Salih

unread,
May 17, 2013, 2:51:01 AM5/17/13
to pysp...@googlegroups.com, Andrea Colapicchioni
Hi Seba,

That might be pretty slow, as VIProperty will get all the properties from the host object (which are a lot), let me know if you are interested in a more performant method (e.g. to continuously monitor these values) so I can provide you with a much more faster solution.

As you said, what would be the best solution to get these stats in a performant way ?

Thanks & Regards
M Salih 

Raj Dudhare

unread,
Mar 2, 2017, 4:15:25 AM3/2/17
to pysphere, nick.wie...@singlewire.com
I don't think following way is correct but hope somehow this could help you. and this is again very slow which I tried out:

server = VIServer()

server.connect(host,user,password)

def resourcepool_by_host(server,host):
 rp_mor = []
 for k, v in server.get_resource_pools().items():
        rp_mor.append(k)
 dx = []
 for rp in rp_mor:
        r = VIProperty(server,rp)
        if r.owner.host[0].name == host:
                dx.append(r.summary.name)
 return dx


host = "1.1.1.1"

print resourcepool_by_host(server,host)
Reply all
Reply to author
Forward
0 new messages