Skip to content

Commit e842517

Browse files
osfricklerberendt
authored andcommitted
sonic: Add SNMP config
Signed-off-by: Dr. Jens Harbott <harbott@osism.tech>
1 parent 29a1825 commit e842517

2 files changed

Lines changed: 92 additions & 12 deletions

File tree

files/sonic/config_db.json

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,14 +413,29 @@
413413
"default|connected|bgp|ipv4": {},
414414
"default|connected|bgp|ipv6": {}
415415
},
416-
"SNMP": {
417-
"LOCATION": {
418-
"Location": "public"
416+
"SNMP_SERVER": {
417+
"SYSTEM": {}
418+
},
419+
"SNMP_SERVER_GROUP": {
420+
"monitoring": {}
421+
},
422+
"SNMP_SERVER_GROUP_ACCESS": {
423+
"monitoring|Default|usm|auth-no-priv": {
424+
"contextMatch": "exact",
425+
"notifyView": "None",
426+
"readView": "rview",
427+
"writeView": "None"
419428
}
420429
},
421-
"SNMP_COMMUNITY": {
422-
"public": {
423-
"TYPE": "RO"
430+
"SNMP_SERVER_GROUP_MEMBER": {},
431+
"SNMP_SERVER_GROUP_PARAMS": {},
432+
"SNMP_SERVER_GROUP_TARGET": {},
433+
"SNMP_SERVER_GROUP_USER": {},
434+
"SNMP_SERVER_VIEW": {
435+
"rview": {
436+
"include": [
437+
".1"
438+
]
424439
}
425440
},
426441
"STATIC_ROUTE": {},

osism/tasks/conductor/sonic/config_generator.py

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,6 @@ def generate_sonic_config(device, hwsku, device_as_mapping=None, config_version=
253253
# Add log-server configuration
254254
_add_log_server_configuration(config, device)
255255

256-
# Add SNMP configuration
257-
_add_snmp_configuration(config, device)
258-
259256
# Add management interface configuration
260257
if oob_ip_result:
261258
oob_ip, prefix_len = oob_ip_result
@@ -264,6 +261,11 @@ def generate_sonic_config(device, hwsku, device_as_mapping=None, config_version=
264261
metalbox_ip = _get_metalbox_ip_for_device(device)
265262
config["STATIC_ROUTE"] = {}
266263
config["STATIC_ROUTE"]["mgmt|0.0.0.0/0"] = {"nexthop": metalbox_ip}
264+
else:
265+
oob_ip = None
266+
267+
# Add SNMP configuration
268+
_add_snmp_configuration(config, device, oob_ip)
267269

268270
# Add breakout configuration
269271
if breakout_info["breakout_cfgs"]:
@@ -2091,6 +2093,7 @@ def _add_portchannel_configuration(config, portchannel_info):
20912093
f"Added port channel {pc_name} with {len(pc_data['members'])} members"
20922094
)
20932095

2096+
20942097
def _add_log_server_configuration(config, device):
20952098
"""Add SYSLOG_SERVER configuration to device config.
20962099
@@ -2111,6 +2114,68 @@ def _add_log_server_configuration(config, device):
21112114
config["SYSLOG_SERVER"][host]["severity"] = severity
21122115
config["SYSLOG_SERVER"][host]["vrf_name"] = vrf
21132116

2114-
logger.debug(
2115-
f"Added syslog_server {host}"
2116-
)
2117+
logger.debug(f"Added syslog_server {host}")
2118+
2119+
2120+
def _add_snmp_configuration(config, device, oob_ip):
2121+
"""Add Snmp configuration to device config.
2122+
2123+
The configuration is taken from multiple _segment_snmp_server_* variables
2124+
in the config_context of the device.
2125+
"""
2126+
2127+
location = device.config_context.get("_segment_snmp_server_location", "Data Center")
2128+
contact = device.config_context.get(
2129+
"_segment_snmp_server_contact", "info@example.com"
2130+
)
2131+
config["SNMP_SERVER"] = {"SYSTEM": {"sysContact": contact, "sysLocation": location}}
2132+
2133+
traps = device.config_context.get("_segment_snmp_server_traps", True)
2134+
if traps:
2135+
config["SNMP_SERVER"]["SYSTEM"]["traps"] = "enable"
2136+
2137+
if oob_ip:
2138+
config["SNMP_AGENT_ADDRESS_CONFIG"] = {
2139+
f"{oob_ip}|161|mgmt": {"name": "agentEntry1"}
2140+
}
2141+
2142+
username = device.config_context.get("_segment_snmp_server_username", None)
2143+
if username:
2144+
userauthpass = device.config_context.get(
2145+
"_segment_snmp_server_userauthpass", "OBFUSCATEDSECRET1"
2146+
)
2147+
userprivpass = device.config_context.get(
2148+
"_segment_snmp_server_userprivpass", "OBFUSCATEDSECRET2"
2149+
)
2150+
config["SNMP_SERVER_GROUP_MEMBER"] = {}
2151+
config["SNMP_SERVER_USER"] = {}
2152+
config["SNMP_SERVER_GROUP_MEMBER"][f"monitoring|{username}"] = {
2153+
"securityModel": ["usm"]
2154+
}
2155+
config["SNMP_SERVER_USER"][f"{username}"] = {
2156+
"shaKey": userauthpass,
2157+
"aesKey": userprivpass,
2158+
}
2159+
logger.debug(f"Added snmp_server_user {username}")
2160+
2161+
hosts = device.config_context.get("_segment_snmp_server_hosts", [])
2162+
if hosts:
2163+
config["SNMP_SERVER_PARAMS"] = {}
2164+
config["SNMP_SERVER_TARGET"] = {}
2165+
counter = 1
2166+
for host in hosts:
2167+
config["SNMP_SERVER_PARAMS"][f"targetEntry{counter}"] = {
2168+
"security-level": "auth-priv",
2169+
"user": username,
2170+
}
2171+
config["SNMP_SERVER_TARGET"][f"targetEntry{counter}"] = {
2172+
"ip": host,
2173+
"port": "162",
2174+
"retries": "3",
2175+
"tag": ["trapNotify", "mgmt"],
2176+
"targetParams": f"targetEntry{counter}",
2177+
"timeout": "1500",
2178+
}
2179+
counter += 1
2180+
2181+
logger.debug(f"Added snmp_server_target {host}")

0 commit comments

Comments
 (0)