@@ -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+
20942097def _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