Skip to content

updated jinja template for snmp contact python2 vs python3 issue#9949

Merged
tsvanduyn merged 1 commit intosonic-net:masterfrom
tsvanduyn:snmp_bugfix
Feb 10, 2022
Merged

updated jinja template for snmp contact python2 vs python3 issue#9949
tsvanduyn merged 1 commit intosonic-net:masterfrom
tsvanduyn:snmp_bugfix

Conversation

@tsvanduyn
Copy link
Contributor

Why I did it

The SNMP contact was not generating correctly when a docker container was restarted.

How I did it

Root Cause:
This was an incompatibility between python2 vs python3.

Type "help", "copyright", "credits" or "license" for more information.
>>> from swsssdk import ConfigDBConnector, SonicV2Connector, SonicDBConfig
>>> config_db = ConfigDBConnector()
>>> config_db.connect()
>>> SNMP = config_db.get_table('SNMP')
>>> SNMP['CONTACT'].keys()[0]
'SN:MT2115X12126;Asset:12518181;PIN:mls-dla_tor_leaf;'
>>>


Python3
>>> config_db = ConfigDBConnector()
>>> config_db.connect()
>>> SNMP = config_db.get_table('SNMP')
>>> SNMP 
{'LOCATION': {'Location': 'public'}, 'CONTACT': {'SN:MT2115X12126;Asset:12518181;PIN:mls-dla_tor_leaf;': '[email protected]'}}
>>> 
>>> 
>>> 
>>> SNMP['CONTACT']
{'SN:MT2115X12126;Asset:12518181;PIN:mls-dla_tor_leaf;': '[email protected]'}
>>> SNMP['CONTACT'].keys()
dict_keys(['SN:MT2115X12126;Asset:12518181;PIN:mls-dla_tor_leaf;'])
>>> SNMP['CONTACT'].keys()[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'dict_keys' object is not subscriptable


So I did some tests and this is how it needs to be fixed to work in python3.
{% if SNMP is defined and SNMP.CONTACT is defined %}
sysContact     {{ SNMP.CONTACT.keys() | first }} {{ SNMP.CONTACT.values() | first }}
{% else %}
sysContact     Azure Cloud Switch vteam [[email protected]](mailto:[email protected])
{% endif %}

How to verify it

I tested this in a lab device and it all worked as expected.
Once the Jinja2 template was updated.

Before

root@str-s6100-acs-1:/usr/share/sonic/templates# cat snmpd.conf.j2 | grep -a10 -i contact
#

#  Note that setting these values here, results in the corresponding MIB objects being 'read-only'
#  See snmpd.conf(5) for more details

{% if SNMP is defined and SNMP.LOCATION is defined %}
sysLocation    {{ SNMP.LOCATION.Location }}
{% else %}
sysLocation    public
{% endif %}
{% if SNMP is defined and SNMP.CONTACT is defined %}
sysContact     {{ SNMP.CONTACT.keys()[0] }} {{ SNMP.CONTACT.values()[0] }}
{% else %}
sysContact     Azure Cloud Switch vteam <[email protected]>
{% endif %}

                                                 # Application + End-to-End layers
sysServices    72


#
#  Process Monitoring
#
# todo: should we enable snmp based monitoring of sswsyncd and other processes?
root@str-s6100-acs-1:/usr/share/sonic/templates# 

Before test:

admin@str-s6100-acs-1:~$ show run snmp contact
Contact                                              Contact Email
---------------------------------------------------  -----------------------
SN:MT2115X12126;Asset:12518181;PIN:mls-dla_tor_leaf  [email protected]
admin@str-s6100-acs-1:~$ 


trvanduy@nettools4-eastus2:~$ snmpget -v 2c -c Test1 str-s6100-acs-1 sysContact.0
SNMPv2-MIB::sysContact.0 = STRING: root

After

root@str-s6100-acs-1:/usr/share/sonic/templates# cat snmpd.conf.j2 | grep -a10 -i contact
#

#  Note that setting these values here, results in the corresponding MIB objects being 'read-only'
#  See snmpd.conf(5) for more details

{% if SNMP is defined and SNMP.LOCATION is defined %}
sysLocation    {{ SNMP.LOCATION.Location }}
{% else %}
sysLocation    public
{% endif %}
{% if SNMP is defined and SNMP.CONTACT is defined %}
sysContact     {{ SNMP.CONTACT.keys() | first }} {{ SNMP.CONTACT.values() | first }}
{% else %}
sysContact     Azure Cloud Switch vteam <[email protected]>
{% endif %}

                                                 # Application + End-to-End layers
sysServices    72


#
#  Process Monitoring
#
# todo: should we enable snmp based monitoring of sswsyncd and other processes?
root@str-s6100-acs-1:/usr/share/sonic/templates# 

After test:

admin@str-s6100-acs-1:~$ show run snmp contact
Contact                                              Contact Email
---------------------------------------------------  -----------------------
SN:MT2115X12126;Asset:12518181;PIN:mls-dla_tor_leaf  [email protected]
admin@str-s6100-acs-1:~$ 


trvanduy@nettools4-eastus2:~$ snmpget -v 2c -c Test1 str-s6100-acs-1 sysContact.0
SNMPv2-MIB::sysContact.0 = STRING: SN:MT2115X12126;Asset:12518181;PIN:mls-dla_tor_leaf [email protected]

If there is no contact after the fix

admin@str-s6100-acs-1:~$ show run snmp contact
Contact    Contact Email
---------  ---------------
admin@str-s6100-acs-1:~$ 

trvanduy@nettools4-eastus2:~$ snmpget -v 2c -c Test1 str-s6100-acs-1 sysContact.0
SNMPv2-MIB::sysContact.0 = STRING: Azure Cloud Switch vteam <[email protected]>
trvanduy@nettools4-eastus2:~$ 

Which release branch to backport (provide reason below if selected)

  • 201811
  • 201911
  • 202006
  • 202012
  • 202106

We should backport to these branches that have the feature enabled to use the new snmp commands.

Description for the changelog

A picture of a cute animal (not mandatory but encouraged)

@tsvanduyn tsvanduyn requested a review from qiluo-msft as a code owner February 9, 2022 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants