Ask Your Question
1

Problems accessing os-simple-tenant-usage with Python API

asked 2015-02-11 21:53:28 -0500

amitabh sinha gravatar image

updated 2015-02-12 08:38:00 -0500

larsks gravatar image

Python api for usage reports using os-simple-tenant-usage gives the output as <computeusage> . Please help me rectify this error.

import novaclient.v1_1.client as novaclient
import datetime
from novaclient.v1_1.client import usage
from credentials import get_nova_creds
creds = get_nova_creds()
nc = novaclient.Client(**creds)
now = datetime.datetime.now()
later = now + datetime.timedelta(seconds=30)
tenant_id = "70ec7525-be04-4c73-9257-e1e8d60b132a"
usage_report = nc.usage.get(tenant_id, now, later)
print usage_report

Gives the output:

<ComputeUsage>
edit retag flag offensive close merge delete

Comments

Taking a minute to read the instructions for formatting code goes a long way to demonstrating that you are able to follow basic documentation. It also makes your question much easier to read.

larsks gravatar imagelarsks ( 2015-02-12 08:38:47 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-02-12 09:23:57 -0500

larsks gravatar image

updated 2015-02-13 10:43:25 -0500

The usage.get(...) call returns a ComputeUsage object. You can see the available methods and attributes on that object like this:

>>> usage_report = nc.usage.get(tenant_id, now, later)
>>> dir(usage_report)

Which gets you:

['HUMAN_ID', 'NAME_ATTR', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattr__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_add_details', '_info', '_init_completion_cache', '_loaded', 'get', 'human_id', 'is_loaded', 'manager', 'server_usages', 'set_loaded', 'start', 'stop', 'tenant_id', 'to_dict', 'total_hours', 'total_local_gb_usage', 'total_memory_mb_usage', 'total_vcpus_usage']

Which means you can useful information doing something like:

>>> usage_report.total_hours
43.58250000000001

Or extract everything as a dictionary, which will looking something like this:

>>> pprint.pprint(usage_report.to_dict())
{u'server_usages': [{u'ended_at': u'2015-02-11T04:57:21.000000',
                     u'flavor': u'm1.small',
                     u'hours': 28.955833333333334,
                     u'instance_id': u'00d9e81b-5878-4fb4-b05e-b0cea16fe9eb',
                     u'local_gb': 20,
                     u'memory_mb': 2048,
                     u'name': u'test1',
                     u'started_at': u'2015-02-09T21:59:44.000000',
                     u'state': u'terminated',
                     u'tenant_id': u'95d9bbd9b446438a89a353d8adb60704',
                     u'uptime': 111457,
                     u'vcpus': 1},
                    {u'ended_at': u'2015-02-11T18:08:35.000000',
                     u'flavor': u'm1.medium',
                     u'hours': 2.9794444444444443,
                     u'instance_id': u'fa18f51f-4e03-40d2-af8a-7e46159f18ec',
                     u'local_gb': 40,
                     u'memory_mb': 4096,
                     u'name': u'devstack-nd',
                     u'started_at': u'2015-02-11T15:09:49.000000',
                     u'state': u'terminated',
                     u'tenant_id': u'95d9bbd9b446438a89a353d8adb60704',
                     u'uptime': 10726,
                     u'vcpus': 2},
                    {u'ended_at': u'2015-02-11T21:07:13.000000',
                     u'flavor': u'm1.medium',
                     u'hours': 2.955277777777778,
                     u'instance_id': u'45c7e90b-3af2-4177-a0b0-459e14b63c0f',
                     u'local_gb': 40,
                     u'memory_mb': 4096,
                     u'name': u'devstack-nd',
                     u'started_at': u'2015-02-11T18:09:54.000000',
                     u'state': u'terminated',
                     u'tenant_id': u'95d9bbd9b446438a89a353d8adb60704',
                     u'uptime': 10639,
                     u'vcpus': 2},
                    {u'ended_at': u'2015-02-12T03:33:57.000000',
                     u'flavor': u'm1.medium',
                     u'hours': 5.8475,
                     u'instance_id': u'5adbc459-4533-455f-b851-1f6ae7cee9a0',
                     u'local_gb': 40,
                     u'memory_mb': 4096,
                     u'name': u'devstack',
                     u'started_at': u'2015-02-11T21:43:06.000000',
                     u'state': u'terminated',
                     u'tenant_id': u'95d9bbd9b446438a89a353d8adb60704',
                     u'uptime': 21051,
                     u'vcpus': 2},
                    {u'ended_at': u'2015-02-12T03:33:57.000000',
                     u'flavor': u'm1.small',
                     u'hours': 1.4552777777777777,
                     u'instance_id': u'3cb010c8-45a3-458c-8181-455b2d969509',
                     u'local_gb': 20,
                     u'memory_mb': 2048,
                     u'name': u'coreos',
                     u'started_at': u'2015-02-12T02:06:38.000000',
                     u'state': u'terminated',
                     u'tenant_id': u'95d9bbd9b446438a89a353d8adb60704',
                     u'uptime': 5239,
                     u'vcpus': 1},
                    {u'ended_at': u'2015-02-12T04:31:18.000000',
                     u'flavor': u'm1.medium',
                     u'hours': 0.9466666666666667,
                     u'instance_id': u'e87682c7-0549-4b1e-ba12-fbe958a340b3',
                     u'local_gb': 40,
                     u'memory_mb': 4096,
                     u'name': u'devstack',
                     u'started_at': u'2015-02-12T03:34:30.000000',
                     u'state': u'terminated',
                     u'tenant_id': u'95d9bbd9b446438a89a353d8adb60704',
                     u'uptime': 3408,
                     u'vcpus': 2},
                    {u'ended_at': u'2015-02-12T04:31:21.000000',
                     u'flavor': u'm1.small',
                     u'hours': 0.4425,
                     u'instance_id': u'76ee0564-0718-4c8a-afd2-0c7766b8dfeb',
                     u'local_gb': 20,
                     u'memory_mb': 2048,
                     u'name': u'atomic',
                     u'started_at': u'2015-02-12T04:04:48.000000',
                     u'state': u'terminated',
                     u'tenant_id': u'95d9bbd9b446438a89a353d8adb60704',
                     u'uptime': 1593,
                     u'vcpus': 1}],
 u'start': u'2015-02-10T00:00:00.000000',
 u'stop': u'2015-02-12T10:17:19.948670',
 u'tenant_id': u'95d9bbd9b446438a89a353d8adb60704',
 u'total_hours': 43.58250000000001,
 u'total_local_gb_usage': 1126.2277777777776,
 u'total_memory_mb_usage': 115325.72444444445,
 u'total_vcpus_usage': 56.31138888888889}

Update

This works for me ... (more)

edit flag offensive delete link more

Comments

I'm using Devstack-Icehouse. I don't find the 'server_usages', 'total_hours', 'total_local_gb_usage', 'total_memory_mb_usage', 'total_vcpus_usage'. Do I have to enable it? How?

amitabh sinha gravatar imageamitabh sinha ( 2015-02-12 23:25:52 -0500 )edit

Maybe this is a client version issue? Running against an Icehouse cloud I see the same result as against my Juno cloud. My local nova client reports version 2.20.0 (in response to nova --version). I am bringing up a devstack/icehouse environment to test right now.

larsks gravatar imagelarsks ( 2015-02-13 09:45:15 -0500 )edit

See my update: this works just fine for me on devstack/icehouse.

larsks gravatar imagelarsks ( 2015-02-13 10:43:44 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Get to know Ask OpenStack

Resources for moderators

Question Tools

2 followers

Stats

Asked: 2015-02-11 21:53:28 -0500

Seen: 628 times

Last updated: Feb 13 '15