napalm-automation / napalm

Network Automation and Programmability Abstraction Layer with Multivendor support
Apache License 2.0
2.21k stars 548 forks source link

KeyError in get_environment() on Juniper MX10k3 with Junos 19.2R1.8 #1247

Closed steffann closed 4 years ago

steffann commented 4 years ago

Description of Issue/Question

Calling get_environment() on our Juniper MX10003 throws a KeyError.

The error has been traced back to pem_name = pem[0].replace("PEM", "Power Supply"). I assume this was done because Junos returned "Power Suppply" in junos_environment_table and "PEM" in junos_pem_table. However, my box returns "PEM" in both, so replacing it causes the lookup error later.

Suggested fix is to also replace "PEM" with "Power Supply" on the name from the environment. This makes the lookup work and keeps the output consistent by always using the "Power Supply" naming:

for sensor_object, object_data in environment.items():
    ...
    if structured_object_data["class"] == "Power":
        sensor_object = sensor_object.replace("PEM", "Power Supply")
        ....

Did you follow the steps from https://github.com/napalm-automation/napalm#faq

(Place an x between the square brackets where applicable)

Setup

napalm version

(Paste verbatim output from pip freeze | grep napalm between quotes below)

napalm==3.0.1

Network operating system version

(Paste verbatim output from show version - or equivalent - between quotes below)

Model: mx10003
Junos: 19.2R1.8

Steps to Reproduce the Issue

In [1]: import napalm                                                                                                                                                                                                           
In [2]: driver = napalm.get_network_driver('junos')                                                                                                                                                                             
In [3]: rtr = driver('10.x.y.z', 'sander', 'wouldntyouliketoknow')                                                                                                                                                                      
In [4]: rtr.open()
In [5]: rtr.get_environment()                                                                                                                                                                                                   

Error Traceback

(Paste the complete traceback of the exception between quotes below)

/opt/netbox-dev/venv/lib/python3.6/site-packages/napalm/junos/junos.py in get_environment(self)
    527                     environment_data["power"][pem_name]["capacity"] = pem_table[
    528                         pem_name
--> 529                     ]["capacity"]
    530                 if pem_table[pem_name]["output"] is not None:
    531                     environment_data["power"][pem_name]["output"] = pem_table[pem_name][

KeyError: 'Power Supply 0'
mirceaulinic commented 4 years ago

Hi @steffann. Thanks for reporting. I think the fix you suggested sounds good.

steffann commented 4 years ago

PR incoming!