Skip to content

Commit

Permalink
Fix to use IPv6 linklocal address as snmp agent address (#3215)
Browse files Browse the repository at this point in the history
What I did
If link local IPv6 address is added as SNMP agent address, it will fail.
This PR requires changes in snmpd.conf.j2 template here sonic-net/sonic-buildimage#18350

How I did it
Append scope id to ipv6 link local IP address.

How to verify it
Able to configure link local ipv6 address as snmp agent address
sudo config snmpagentaddress add fe80::a%eth0
  • Loading branch information
SuvarnaMeenakshi committed Aug 5, 2024
1 parent f50587a commit 018eb73
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
7 changes: 5 additions & 2 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3498,7 +3498,10 @@ def add_snmp_agent_address(ctx, agentip, port, vrf):
"""Add the SNMP agent listening IP:Port%Vrf configuration"""

#Construct SNMP_AGENT_ADDRESS_CONFIG table key in the format ip|<port>|<vrf>
if not clicommon.is_ipaddress(agentip):
# Link local IP address should be provided along with zone id
# <link_local_ip>%<zone_id> for ex fe80::1%eth0
agent_ip_addr = agentip.split('%')[0]
if not clicommon.is_ipaddress(agent_ip_addr):
click.echo("Invalid IP address")
return False
config_db = ctx.obj['db']
Expand All @@ -3508,7 +3511,7 @@ def add_snmp_agent_address(ctx, agentip, port, vrf):
click.echo("ManagementVRF is Enabled. Provide vrf.")
return False
found = 0
ip = ipaddress.ip_address(agentip)
ip = ipaddress.ip_address(agent_ip_addr)
for intf in netifaces.interfaces():
ipaddresses = netifaces.ifaddresses(intf)
if ip_family[ip.version] in ipaddresses:
Expand Down
28 changes: 28 additions & 0 deletions tests/config_snmp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,34 @@ def test_config_snmp_community_add_new_community_with_invalid_type_yang_validati
assert result.exit_code != 0
assert 'SNMP community configuration failed' in result.output

@patch('netifaces.interfaces', mock.Mock(return_value=['eth0']))
@patch('netifaces.ifaddresses', mock.Mock(return_value={2:
[{'addr': '10.1.0.32', 'netmask': '255.255.255.0',
'broadcast': '10.1.0.255'}],
10: [{'addr': 'fe80::1%eth0', 'netmask': 'ffff:ffff:ffff:ffff::/64'}]}))
@patch('os.system', mock.Mock(return_value=0))
def test_config_snmpagentaddress_add_linklocal(self):
db = Db()
obj = {'db': db.cfgdb}
runner = CliRunner()
runner.invoke(config.config.commands["snmpagentaddress"].commands["add"], ["fe80::1%eth0"], obj=obj)
assert ('fe80::1%eth0', '', '') in db.cfgdb.get_keys('SNMP_AGENT_ADDRESS_CONFIG')
assert db.cfgdb.get_entry("SNMP_AGENT_ADDRESS_CONFIG", "fe80::1%eth0||") == {}

@patch('netifaces.interfaces', mock.Mock(return_value=['eth0']))
@patch('netifaces.ifaddresses', mock.Mock(return_value={2:
[{'addr': '10.1.0.32', 'netmask': '255.255.255.0',
'broadcast': '10.1.0.255'}],
10: [{'addr': 'fe80::1', 'netmask': 'ffff:ffff:ffff:ffff::/64'}]}))
@patch('os.system', mock.Mock(return_value=0))
def test_config_snmpagentaddress_add_ipv4(self):
db = Db()
obj = {'db': db.cfgdb}
runner = CliRunner()
runner.invoke(config.config.commands["snmpagentaddress"].commands["add"], ["10.1.0.32"], obj=obj)
assert ('10.1.0.32', '', '') in db.cfgdb.get_keys('SNMP_AGENT_ADDRESS_CONFIG')
assert db.cfgdb.get_entry("SNMP_AGENT_ADDRESS_CONFIG", "10.1.0.32||") == {}

@classmethod
def teardown_class(cls):
print("TEARDOWN")
Expand Down

0 comments on commit 018eb73

Please sign in to comment.