[Users] Rest-api to fetch the hosts details ( active vm's , CPU , Physical memory etc.)

Michael Pasternak mpastern at redhat.com
Tue Feb 19 10:26:39 UTC 2013


Hi Romil,

On 02/19/2013 11:22 AM, Romil Gupta wrote:
> Hi Michael ,
> 
> 
> Thanks for ur continuous help. I have one more query here ....
> 
> I want to fetch  the host elements like ..
> Type 
> address 
> status
> No. of CPUs
> cpu->type
> cpu -> name->topology->cores->sockets 
> memory 
> summary 
> version
> 
> Is is the correct way of doing to get these details?
> 
> hosts=api.hosts.list()
> for host in hosts:
>     type=api.hosts.get(host.id <http://host.id>).type
>     address=api.hosts.get(host.id <http://host.id>).address

no, please use getter methods instead as they're encapsulating attributes that may be
called differently in python,

for instance 'type' attribute in python renamed to 'type_' cause 'type' is preserved name.

you can see all available get_X() methods by running code bellow:

#> print [token for token in type(host.superclass).__dict__.keys() if token.startswith('get_')]

> 
> I got some exception :
>  address=api.hosts.get(host.id <http://host.id>).get_address()
> AttributeError: 'NoneType' object has no attribute 'get_address'

this can only happen if your host lookup has failed.

> 
> 
> Please suggest me the correct way of doing it ....if have referred 
> https://access.redhat.com/knowledge/docs/en-US/Red_Hat_Enterprise_Virtualization/3.1/html/Developer_Guide/chap-REST_API_Guide-Hosts.html
> 
> Regards,
> Romil
> 
> 
> 
> 
> 
> 
> On Wed, Jan 30, 2013 at 6:50 PM, Michael Pasternak <mpastern at redhat.com <mailto:mpastern at redhat.com>> wrote:
> 
>     On 01/30/2013 03:09 PM, Romil Gupta wrote:
>     > thanks for all your guidance , now I m able to fetch the details of a host using
>     > the below script :
>     >
>     > hosts=api.hosts.list()
>     > for host in hosts:
>     >     print "host name--> %s  id--->> %s \n"%(host.name <http://host.name> <http://host.name> , host.id <http://host.id> <http://host.id>)
>     >     clusterid=api.hosts.get(host.name <http://host.name> <http://host.name>).cluster.id <http://cluster.id> <http://cluster.id>
>     >     print clusterid
>     >
>     >     hostname=api.hosts.get(host.name <http://host.name> <http://host.name>)
>     >     statistic=hostname.statistics.list()
>     >     i=0
>     >     while i < 14:
>     >         print statistic[i].name
>     >         print statistic[i].description
>     >         print statistic[i].unit
>     >         print statistic[i].values.value[0].datum
>     >         i=i+1;
>     >
>     >
>     > summary=api.get_summary()
>     > print summary
>     >
>     > How I can print the summary , its only return the Object??
> 
>     this is summary object structure:
> 
>         <summary>
>             <vms>
>                 <total></total>
>                 <active></active>
>             </vms>
>             <hosts>
>                 <total></total>
>                 <active></active>
>             </hosts>
>             <users>
>                 <total></total>
>                 <active></active>
>             </users>
>             <storage_domains>
>                 <total></total>
>                 <active></active>
>             </storage_domains>
>         </summary>
> 
>     you can access properties directly, like this:
>     summary.hosts.active
> 
>     >
>     > Thanks,
>     > Romil
>     >
>     >
>     > On Wed, Jan 30, 2013 at 4:52 PM, Michael Pasternak <mpastern at redhat.com <mailto:mpastern at redhat.com> <mailto:mpastern at redhat.com <mailto:mpastern at redhat.com>>> wrote:
>     >
>     >
>     >     Romil,
>     >
>     >     On 01/30/2013 12:18 PM, Romil Gupta wrote:
>     >     > Hi,
>     >     >
>     >     > Is this is a right way to get it ??
>     >     >
>     >     >     statistics=params.Host(host.name <http://host.name> <http://host.name> <http://host.name>).get_statistic()
>     >
>     >     1. first you need to fetch the host to see it's statistics (by doing params.Host(...) you creating
>     >        host parameters holder which is needed for adding new host to the system)
>     >
>     >     2. get_x() getters used to access object attributes, while collections are exposed as properties, do
>     >
>     >     1. myhost = api.hosts.get(name="xxx")
>     >     2. myhost.statistics.list()
>     >     3. loop over returned collection of statistics to find what you're looking for
>     >
>     >     - note, statistic objects are complex types, you can look for data at:
>     >
>     >     statistics[i].unit // the unit of the holder data
>     >     statistics[i].values.value[0].datum // actual data
>     >
>     >     >     print statistics
>     >     >
>     >     >      summary=params.Host(host.name <http://host.name> <http://host.name> <http://host.name>).get_summary()
>     >
>     >     summary() is an api method, do:
>     >
>     >     1. api = API(url='', username='', password='')
>     >     2. api.get_summary()
>     >
>     >
>     >     >      print summary
>     >     >
>     >     >
>     >     > Output is : none
>     >     >
>     >     > Thanks
>     >     > Romil
>     >     >
>     >     >
>     >     > On Wed, Jan 30, 2013 at 2:04 PM, Michael Pasternak <mpastern at redhat.com <mailto:mpastern at redhat.com> <mailto:mpastern at redhat.com <mailto:mpastern at redhat.com>>
>     <mailto:mpastern at redhat.com <mailto:mpastern at redhat.com> <mailto:mpastern at redhat.com <mailto:mpastern at redhat.com>>>> wrote:
>     >     >
>     >     >
>     >     >     Hi Romil,
>     >     >
>     >     >     On 01/30/2013 10:17 AM, Romil Gupta wrote:
>     >     >     > Hi all ,
>     >     >     >
>     >     >     > how I can get the hosts details like Active VM's ,
>     >     >
>     >     >     host doesn't have running vms attribute, instead you
>     >     >     can see in the guest on which host it's running,
>     >     >
>     >     >     general system summary you can see at api.get_summary()
>     >     >
>     >     >     Number of CPU's , CPU name , CPU type ,
>     >     >
>     >     >     these are host attributes
>     >     >
>     >     >     Physical Memory (used , free ) , swap size and other parameters
>     >     >
>     >     >     these are host.statistics attributes
>     >     >
>     >     >     > using ovirt-engine-sdk-3.2.0.5-1.
>     >     >     >
>     >     >     >
>     >     >     >
>     >     >     > Regards,
>     >     >     > Romil
>     >     >     >
>     >     >     > --
>     >     >     > I don't wish to be everything to everyone, but I would like to be something to someone.
>     >     >
>     >     >
>     >     >     --
>     >     >
>     >     >     Michael Pasternak
>     >     >     RedHat, ENG-Virtualization R&D
>     >     >
>     >     >
>     >     >
>     >     >
>     >     > --
>     >     > I don't wish to be everything to everyone, but I would like to be something to someone.
>     >
>     >
>     >     --
>     >
>     >     Michael Pasternak
>     >     RedHat, ENG-Virtualization R&D
>     >
>     >
>     >
>     >
>     > --
>     > I don't wish to be everything to everyone, but I would like to be something to someone.
> 
> 
>     --
> 
>     Michael Pasternak
>     RedHat, ENG-Virtualization R&D
> 
> 
> 
> 
> -- 
> I don't wish to be everything to everyone, but I would like to be something to someone.


-- 

Michael Pasternak
RedHat, ENG-Virtualization R&D



More information about the Users mailing list