[PATCH] Adding 'link_detected' to netinfo.py

'Link_detected' returns 'yes' if the network card is attached to a cable (or other media), 'no' if it's disconnected and 'n/a' if the link state can't be determined (if the network interface is down). This is an information that might be useful for some plug-ins - Ginger, for instance - to present to the user. Example of the updated netinfo.get_interface_info call: $ sudo PYTHONPATH=src python Python 2.7.5 (default, Sep 25 2014, 13:52:19) [GCC 4.8.3 20140624 (Red Hat 4.8.3-1)] on linux2 Type "help", "copyright", "credits" or "license" for more information.
from kimchi import netinfo netinfo.get_interface_info('em1') {'status': 'active', 'name': 'em1', 'ipaddr': '', 'netmask': '', 'link_detected': 'no', 'type': 'nic'} ---- with em1 interface down ----- netinfo.get_interface_info('em1') {'status': 'inactive', 'name': 'em1', 'ipaddr': '', 'netmask': '', 'link_detected': 'n/a', 'type': 'nic'}
Daniel Henrique Barboza (1): netinfo.py: adding 'link_detected' to get_interface_info src/kimchi/netinfo.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) -- 1.8.3.1

The 'link_detected' tells the caller if the network interface is attached to a network cable (or other media). This information is only attainable if the network interface is up, thus querying 'link_detected' will return 'n/a' in the interface is down. Signed-off-by: Daniel Henrique Barboza <danielhb@linux.vnet.ibm.com> --- src/kimchi/netinfo.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/kimchi/netinfo.py b/src/kimchi/netinfo.py index bb50479..991e91f 100644 --- a/src/kimchi/netinfo.py +++ b/src/kimchi/netinfo.py @@ -101,16 +101,21 @@ def is_bondlave(nic): def operstate(dev): - # try to read interface status + link_status = link_detected(dev) + return "down" if link_status == "n/a" else "up" + + +def link_detected(dev): + # try to read interface carrier (link) status try: - open(NET_STATE % dev).readline().strip() + carrier = open(NET_STATE % dev).readline().strip() # when IOError is raised, interface is down except IOError: - return "down" + return "n/a" # if value is 1, interface up with cable connected # 0 corresponds to interface up with cable disconnected - return "up" + return "yes" if carrier == '1' else "no" def get_vlan_device(vlan): @@ -196,8 +201,12 @@ def get_interface_info(iface): except IOError: pass + iface_link_detected = link_detected(iface) + iface_status = 'active' if iface_link_detected != "n/a" else "inactive" + return {'name': iface, 'type': get_interface_type(iface), - 'status': 'active' if operstate(iface) == 'up' else 'inactive', + 'status': iface_status, + 'link_detected': iface_link_detected, 'ipaddr': ipaddr, 'netmask': netmask} -- 1.8.3.1

On 11/04/2014 11:25 AM, Daniel Henrique Barboza wrote:
The 'link_detected' tells the caller if the network interface is attached to a network cable (or other media). This information is only attainable if the network interface is up, thus querying 'link_detected' will return 'n/a' in the interface is down.
Signed-off-by: Daniel Henrique Barboza <danielhb@linux.vnet.ibm.com> --- src/kimchi/netinfo.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
diff --git a/src/kimchi/netinfo.py b/src/kimchi/netinfo.py index bb50479..991e91f 100644 --- a/src/kimchi/netinfo.py +++ b/src/kimchi/netinfo.py @@ -101,16 +101,21 @@ def is_bondlave(nic):
def operstate(dev): - # try to read interface status + link_status = link_detected(dev) + return "down" if link_status == "n/a" else "up" + + +def link_detected(dev): + # try to read interface carrier (link) status try: - open(NET_STATE % dev).readline().strip() + carrier = open(NET_STATE % dev).readline().strip() # when IOError is raised, interface is down except IOError: - return "down" + return "n/a"
# if value is 1, interface up with cable connected # 0 corresponds to interface up with cable disconnected - return "up" + return "yes" if carrier == '1' else "no"
Those values "yes" and "no" are being ignored when link_detected() is used. Is that correct? I mean, if "no" the final result should be "down", right? And I don't think it is happening here.
def get_vlan_device(vlan): @@ -196,8 +201,12 @@ def get_interface_info(iface): except IOError: pass
+ iface_link_detected = link_detected(iface) + iface_status = 'active' if iface_link_detected != "n/a" else "inactive" + return {'name': iface, 'type': get_interface_type(iface), - 'status': 'active' if operstate(iface) == 'up' else 'inactive', + 'status': iface_status, + 'link_detected': iface_link_detected, 'ipaddr': ipaddr, 'netmask': netmask}

Reviewed-by: Crístian Viana <vianac@linux.vnet.ibm.com> On 04-11-2014 11:25, Daniel Henrique Barboza wrote:
'Link_detected' returns 'yes' if the network card is attached to a cable (or other media), 'no' if it's disconnected and 'n/a' if the link state can't be determined (if the network interface is down).
This is an information that might be useful for some plug-ins - Ginger, for instance - to present to the user.
Example of the updated netinfo.get_interface_info call:
$ sudo PYTHONPATH=src python Python 2.7.5 (default, Sep 25 2014, 13:52:19) [GCC 4.8.3 20140624 (Red Hat 4.8.3-1)] on linux2 Type "help", "copyright", "credits" or "license" for more information.
from kimchi import netinfo netinfo.get_interface_info('em1') {'status': 'active', 'name': 'em1', 'ipaddr': '', 'netmask': '', 'link_detected': 'no', 'type': 'nic'} ---- with em1 interface down ----- netinfo.get_interface_info('em1') {'status': 'inactive', 'name': 'em1', 'ipaddr': '', 'netmask': '', 'link_detected': 'n/a', 'type': 'nic'} Daniel Henrique Barboza (1): netinfo.py: adding 'link_detected' to get_interface_info
src/kimchi/netinfo.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
participants (3)
-
Aline Manera
-
Crístian Viana
-
Daniel Henrique Barboza