dnaeon / py-vpoller

Distributed vSphere API Proxy
Other
84 stars 31 forks source link

A JSON error when use resource.pool.get #213

Closed swordcrash closed 7 years ago

swordcrash commented 7 years ago

when use API request: msg={"method":"resource.pool.get","hostname":"10.112.10.26","name":"Resources","properties":['name','runtime.overallStatus']} result: {"msg": "Successfully retrieved object properties", "result": [{"name": "Resources", "runtime.overallStatus": "green"}], "success": 0} everything is ok request: arg:{"method":"resource.pool.get","hostname":"10.112.10.26","name":"Resources","properties":['name','runtime.overallStatus','runtime.cpu']} result:No handlers could be found for logger "root"

Traceback (most recent call last): File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap self.run() File "build/bdist.linux-x86_64/egg/vpoller/worker.py", line 382, in run self.wait_for_tasks() File "build/bdist.linux-x86_64/egg/vpoller/worker.py", line 533, in wait_for_tasks data = json.dumps(result, ensure_ascii=False) File "/usr/lib/python2.7/json/init.py", line 238, in dumps **kw).encode(obj) File "/usr/lib/python2.7/json/encoder.py", line 200, in encode chunks = self.iterencode(o, _one_shot=True) File "/usr/lib/python2.7/json/encoder.py", line 263, in iterencode return _iterencode(o, 0) File "/usr/lib/python2.7/json/encoder.py", line 177, in default raise TypeError(repr(o) + " is not JSON serializable") TypeError: (vim.ResourcePool.ResourceUsage) { dynamicType = , dynamicProperty = (vmodl.DynamicProperty) [], reservationUsed = 93662L, reservationUsedForVm = 0L, unreservedForPool = 374650L, unreservedForVm = 374650L, overallUsage = 17201L, maxUsage = 468312L } is not JSON serializable

any idea about the problem?

dnaeon commented 7 years ago

Can you please format the code to make it easier to read?

Looks like you are requesting a property that is not a scalar value, so the result is that vPoller is not able to serialize it to JSON.

It looks like you are requesting the ResourceUsage property, correct?

swordcrash commented 7 years ago

Yes, my request method is "resource.pool.get". When add properties "name"&"runtime.overallStatus", everything is correct, but when add properties "runtime.cpu" or "runtime.memory" the error comes.

vpoller-client --method resource.pool.get -V 10.110.10.5 --name Resources --properties runtime.overallStatus

{"msg": "Successfully retrieved object properties", "result": [{"name": "Resources", "runtime.overallStatus": "green"}], "success": 0}

vpoller-client --method resource.pool.get -V 10.110.10.5 --name Resources --properties runtime.cpu

[2017-05-18 09:34:26,971 - WARNING/MainProcess] Did not receive response, retrying... [2017-05-18 09:34:36,982 - WARNING/MainProcess] Did not receive response, retrying... [2017-05-18 09:34:46,989 - WARNING/MainProcess] Did not receive response, retrying... [2017-05-18 09:34:46,991 - ERROR/MainProcess] Did not receive response, aborting... {"msg": "Did not receive response, aborting...", "success": 1}

the message on the backend: "TypeError: (vim.ResourcePool.ResourceUsage) { dynamicType = , dynamicProperty = (vmodl.DynamicProperty) [], reservationUsed = 93662L, reservationUsedForVm = 0L, unreservedForPool = 374650L, unreservedForVm = 374650L, overallUsage = 17201L, maxUsage = 468312L } is not JSON serializable"

then I check the process by "ps -elf": "[vpoller-worker] " before the request, it should be: /usr/bin/python /usr/local/bin/vpoller-worker start

the reference I used(task.py): ` """
Get properties of a single vim.ResourcePool managed object

Example client message would be:

{
    "method":     "resource.pool.get",
    "hostname":   "vc01.example.org",
    "name":       "MyResourcePool",
    "properties": [
        "name",
        "runtime.cpu",
        "runtime.memory",
        "runtime.overallStatus"
    ]
}

Returns:
    The managed object properties in JSON format

"""`

I'll do more debug today, thanks for your reply.

dnaeon commented 7 years ago

Hey @swordcrash

You are requesting the whole data structure this way, you need to request the individual properties from the runtime.memory and runtime.cpu.

So, instead of requesting runtime.memory for example, you would request runtime.memory.overallUsage, runtime.memory.maxUsage for example (or any of the other properties).

Check out the properties you can request here for runtime.memory and runtime.cpu here.

Example command would look like this:

$ vpoller-client --method resource.pool.get \
                         -V 10.110.10.5 \
                         --name Resources \
                         --properties runtime.overallStatus,runtime.memory.overallUsage,runtime.memory.maxUsage,runtime.cpu.overallUsage
swordcrash commented 7 years ago

Hi @dnaeon It works, thanks for your help!