diff --git a/config/main.py b/config/main.py index 5fda67f5f1..a6f0e8df2d 100755 --- a/config/main.py +++ b/config/main.py @@ -978,6 +978,104 @@ def vrf_del (ctx, vrfname): else: click.echo("Deletion of data vrf={} is not yet supported".format(vrfname)) +@config.group() +@click.pass_context +def snmpagentaddress(ctx): + """SNMP agent listening IP address, port, vrf configuration""" + config_db = ConfigDBConnector() + config_db.connect() + ctx.obj = {'db': config_db} + pass + +@snmpagentaddress.command('add') +@click.argument('agentip', metavar='', required=True) +@click.option('-p', '--port', help="SNMP AGENT LISTENING PORT") +@click.option('-v', '--vrf', help="VRF Name mgmt/DataVrfName/None") +@click.pass_context +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|| + key = agentip+'|' + if port: + key = key+port + key = key+'|' + if vrf: + key = key+vrf + config_db = ctx.obj['db'] + config_db.set_entry('SNMP_AGENT_ADDRESS_CONFIG', key, {}) + + #Restarting the SNMP service will regenerate snmpd.conf and rerun snmpd + cmd="systemctl restart snmp" + os.system (cmd) + +@snmpagentaddress.command('del') +@click.argument('agentip', metavar='', required=True) +@click.option('-p', '--port', help="SNMP AGENT LISTENING PORT") +@click.option('-v', '--vrf', help="VRF Name mgmt/DataVrfName/None") +@click.pass_context +def del_snmp_agent_address(ctx, agentip, port, vrf): + """Delete the SNMP agent listening IP:Port%Vrf configuration""" + + key = agentip+'|' + if port: + key = key+port + key = key+'|' + if vrf: + key = key+vrf + config_db = ctx.obj['db'] + config_db.set_entry('SNMP_AGENT_ADDRESS_CONFIG', key, None) + cmd="systemctl restart snmp" + os.system (cmd) + +@config.group() +@click.pass_context +def snmptrap(ctx): + """SNMP Trap server configuration to send traps""" + config_db = ConfigDBConnector() + config_db.connect() + ctx.obj = {'db': config_db} + pass + +@snmptrap.command('modify') +@click.argument('ver', metavar='', type=click.Choice(['1', '2', '3']), required=True) +@click.argument('serverip', metavar='', required=True) +@click.option('-p', '--port', help="SNMP Trap Server port, default 162", default="162") +@click.option('-v', '--vrf', help="VRF Name mgmt/DataVrfName/None", default="None") +@click.option('-c', '--comm', help="Community", default="public") +@click.pass_context +def modify_snmptrap_server(ctx, ver, serverip, port, vrf, comm): + """Modify the SNMP Trap server configuration""" + + #SNMP_TRAP_CONFIG for each SNMP version + config_db = ctx.obj['db'] + if ver == "1": + #By default, v1TrapDest value in snmp.yml is "NotConfigured". Modify it. + config_db.mod_entry('SNMP_TRAP_CONFIG',"v1TrapDest",{"DestIp": serverip, "DestPort": port, "vrf": vrf, "Community": comm}) + elif ver == "2": + config_db.mod_entry('SNMP_TRAP_CONFIG',"v2TrapDest",{"DestIp": serverip, "DestPort": port, "vrf": vrf, "Community": comm}) + else: + config_db.mod_entry('SNMP_TRAP_CONFIG',"v3TrapDest",{"DestIp": serverip, "DestPort": port, "vrf": vrf, "Community": comm}) + + cmd="systemctl restart snmp" + os.system (cmd) + +@snmptrap.command('del') +@click.argument('ver', metavar='', type=click.Choice(['1', '2', '3']), required=True) +@click.pass_context +def delete_snmptrap_server(ctx, ver): + """Delete the SNMP Trap server configuration""" + + config_db = ctx.obj['db'] + if ver == "1": + config_db.mod_entry('SNMP_TRAP_CONFIG',"v1TrapDest",None) + elif ver == "2": + config_db.mod_entry('SNMP_TRAP_CONFIG',"v2TrapDest",None) + else: + config_db.mod_entry('SNMP_TRAP_CONFIG',"v3TrapDest",None) + cmd="systemctl restart snmp" + os.system (cmd) + @vlan.group('dhcp_relay') @click.pass_context def vlan_dhcp_relay(ctx): diff --git a/show/main.py b/show/main.py index e94cae45ae..9aa119c0eb 100755 --- a/show/main.py +++ b/show/main.py @@ -503,6 +503,49 @@ def address (): click.echo("Management IP address = {0}".format(key[1])) click.echo("Management Network Default Gateway = {0}".format(mgmt_ip_data[key]['gwaddr'])) +# +# 'snmpagentaddress' group ("show snmpagentaddress ...") +# + +@cli.group('snmpagentaddress', invoke_without_command=True) +@click.pass_context +def snmpagentaddress (ctx): + """Show SNMP agent listening IP address configuration""" + config_db = ConfigDBConnector() + config_db.connect() + agenttable = config_db.get_table('SNMP_AGENT_ADDRESS_CONFIG') + + header = ['ListenIP', 'ListenPort', 'ListenVrf'] + body = [] + for agent in agenttable.keys(): + body.append([agent[0], agent[1], agent[2]]) + click.echo(tabulate(body, header)) + +# +# 'snmptrap' group ("show snmptrap ...") +# + +@cli.group('snmptrap', invoke_without_command=True) +@click.pass_context +def snmptrap (ctx): + """Show SNMP agent Trap server configuration""" + config_db = ConfigDBConnector() + config_db.connect() + traptable = config_db.get_table('SNMP_TRAP_CONFIG') + + header = ['Version', 'TrapReceiverIP', 'Port', 'VRF', 'Community'] + body = [] + for row in traptable.keys(): + if row == "v1TrapDest": + ver=1 + elif row == "v2TrapDest": + ver=2 + else: + ver=3 + body.append([ver, traptable[row]['DestIp'], traptable[row]['DestPort'], traptable[row]['vrf'], traptable[row]['Community']]) + click.echo(tabulate(body, header)) + + # # 'interfaces' group ("show interfaces ...") #