Bug 1661207 - Api fails to retrieve host nics statistics
Summary: Api fails to retrieve host nics statistics
Keywords:
Status: CLOSED CURRENTRELEASE
Alias: None
Product: ovirt-engine-sdk-python
Classification: oVirt
Component: General
Version: 4.2.8
Hardware: All
OS: All
unspecified
medium vote
Target Milestone: ovirt-4.3.0
: ---
Assignee: Ondra Machacek
QA Contact: Ravi Nori
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2018-12-20 12:12 UTC by muchorrollodejavi
Modified: 2019-02-13 07:43 UTC (History)
6 users (show)

Fixed In Version: 4.3.0
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
Last Closed: 2019-02-13 07:43:29 UTC
oVirt Team: Infra
rule-engine: ovirt-4.3+


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
oVirt gerrit 96577 0 master MERGED Change HostNic statistics and labels to Link 2019-01-07 07:55:24 UTC
oVirt gerrit 96948 0 master MERGED restapi: Update to model 4.3.21 2019-01-17 11:20:24 UTC

Description muchorrollodejavi 2018-12-20 12:12:46 UTC
Description of problem:

The python 2.7 api lacks some code in the HostNicReader class that cause a crash when you try to do a follow_link(nic.statistics)



Version-Release number of selected component (if applicable):

ovirt-engine-sdk-python 4.2.9
oVirt Engine Version: 4.1.9.1-1.el7.centos



How reproducible:

Always, I'm using this python 2.7 code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging

import ovirtsdk4 as sdk 
import ovirtsdk4.types as types

# Create the connection to the server:
connection = sdk.Connection(
    url='https://******************/ovirt-engine/api',
    username='*************',
    password='***************',
    debug=True,
    log=logging.getLogger(),
)

# Get the reference to the "hosts" service:
hosts_service = connection.system_service().hosts_service()

# Use the "list" method of the "hosts" service to list all the hosts of the system:
hosts = hosts_service.list()

for host in hosts:
    memory_used = 0 
    memory_total = 0 
    cpu_used = 0 
    print("%s: Status %s"  % (host.name, host.status))
    #print statistic information like ram and cpu
    statistics = connection.follow_link(host.statistics)
    for statistic in statistics:
        if(statistic.name == "memory.used"):
            memory_used = statistic.values[0].datum
        elif(statistic.name == "memory.total"):
            memory_total = statistic.values[0].datum
        elif(statistic.name == "cpu.current.idle"):
            cpu_used = 100 - statistic.values[0].datum
    if (memory_used is not None and memory_total is not None):
        print("    Ram:%.0f" %
              (float(memory_used)/float(memory_total)*100))+r'%'
        print("    CPU: %.0f" % cpu_used)+r'%'

    nics = connection.follow_link(host.nics)
    print("    Network interfaces:")
    for nic in nics:
        if nic.speed is not None:
            speed_mb = (int(nic.speed) /1000 /1000) #convert from bits to Mbits

            statistics = connection.follow_link(nic.statistics)
            tx_use = 0 
            rx_use = 0 
            for statistic in statistics:
                if(statistic.name == "data.current.tx"):
                    tx_use = statistic.values[0].datum
                    tx_use = tx_use/nic.speed*100
                elif(statistic.name == "data.current.rx"):
                    rx_use = statistic.values[0].datum
                    rx_use = rx_use/nic.speed*100


            print("        "+str(nic.name)+": Status: "+str(nic.status)+", Speed:"+str(nic.speed/1000/1000)+" Mb, Tx "+str(tx_use)+"%,Rx "+str(rx_use)+"%")
        elif (nic.status == types.NicStatus.DOWN):
            print("        %s: Status: %s" % (nic.name,nic.status))

connection.close()



Steps to Reproduce:
1. get a host from host service using the python 2.7 api
2. list all nics and iterate
3. for each nic, try to get the statistics using follow_link(nic.statistics)



Actual results:

It crash when try to follow the link, due to the HostNicReader ignore the link tags and lack a _process_link method



Expected results:

It should return a statistics object with the nic statistics information



Additional info:

I've fixed it in my local instalation modifying the HostNicReader class in readers.py, first i have added around the line : 

elif tag == 'link':
                links.append((reader.get_attribute('rel'), reader.get_attribute('href')))
                reader.next_element()

also this class needs a _process_link method, I just copied it from HostReader class, and by now i only have implemented statistics links, but it lacks the other links due that i don't know how many types of links can have this class, around the line :

@staticmethod
    def _process_link(link, obj): 
        rel = link[0]
        href = link[1]
        if href and rel:
            if rel == "statistics":
                if obj.statistics is not None: 
                    obj.statistics.href = href
                else:
                    obj.statistics = List(href)

I have read in the github repo that this code is generated from the api model, could the api model be wrong or using a old version? I have instaled it from pypi

Comment 1 Ondra Machacek 2019-01-02 13:11:01 UTC
This is indeed a bug in API model. I've sent a fix. It will be part of another Python SDK release.

Comment 2 Sandro Bonazzola 2019-01-22 12:33:11 UTC
Martin, I see you moved this bug to modified. Can you please cross-check? Referenced patches in merged status are not related to this bugzilla component.

Comment 3 Martin Perina 2019-01-22 15:30:06 UTC
(In reply to Sandro Bonazzola from comment #2)
> Martin, I see you moved this bug to modified. Can you please cross-check?
> Referenced patches in merged status are not related to this bugzilla
> component.

SDKs are generated from ovirt-engine-api-model used by ovirt-engine, am I right Ondro?

Comment 4 Ondra Machacek 2019-01-22 16:58:15 UTC
Correct, it refers model patches, which were used to rebuild SDKs.

Comment 5 Sandro Bonazzola 2019-01-24 13:51:04 UTC
Ok, thanks, moving to QE accordingly

Comment 6 Ravi Nori 2019-01-29 16:52:43 UTC
Verified on oVirt 4.3.0 Fourth Release Candidate

Comment 7 muchorrollodejavi 2019-01-30 13:32:16 UTC
I have tested and its solved, thanks.

Comment 8 Sandro Bonazzola 2019-02-13 07:43:29 UTC
This bugzilla is included in oVirt 4.3.0 release, published on February 4th 2019.

Since the problem described in this bug report should be
resolved in oVirt 4.3.0 release, it has been closed with a resolution of CURRENT RELEASE.

If the solution does not work for you, please open a new bug report.


Note You need to log in before you can comment on or make changes to this bug.