From 9d8ab9d555ce42c3e08a78f249f403fd7208843d Mon Sep 17 00:00:00 2001 From: Sangita Maity Date: Mon, 30 Sep 2019 22:40:37 +0000 Subject: [PATCH 1/6] [config] Add snmp-community command to set SNMP community string Signed-off-by: Sangita Maity --- config/main.py | 47 ++++++++++++++++++++++++++++++++++++++++ doc/Command-Reference.md | 19 ++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/config/main.py b/config/main.py index 1b95c60b4a..20799781e2 100755 --- a/config/main.py +++ b/config/main.py @@ -8,6 +8,7 @@ import netaddr import re import syslog +import yaml import sonic_device_util import ipaddress @@ -22,6 +23,7 @@ SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen' SYSLOG_IDENTIFIER = "config" +SNMP_COMMUNITY_FILE = '/etc/sonic/snmp.yml' # ========================== Syslog wrappers ========================== @@ -675,6 +677,51 @@ def reload(): else: click.secho('Buffer definition template not found at {}'.format(buffer_template_file), fg='yellow') +# +# 'snmp-community' command +# +@config.command('snmp-community') +@click.argument('community-name', metavar='', required=True) +def snmp_community(community_name): + """Set SNMP community string""" + + """Serialization of a Python dict from a SNMP YAML File """ + with open(SNMP_COMMUNITY_FILE, 'r') as stream: + try: + snmp_community_dict = yaml.safe_load(stream) + except yaml.YAMLError as error: + click.echo("SNMP config file not loaded correctly with error {}".format(error)) + raise + + if 'snmp_rocommunity' in snmp_community_dict.keys(): + community_exist_name = snmp_community_dict['snmp_rocommunity'] + del snmp_community_dict['snmp_rocommunity'] + snmp_community_dict['snmp_rocommunities'] = [community_exist_name] + snmp_community_dict['snmp_rocommunities'].append(community_name) + + elif 'snmp_rocommunities' in snmp_community_dict.keys(): + snmp_community_dict['snmp_rocommunities'].append(community_name) + + else: + snmp_community_dict['snmp_rocommunity'] = community_name + + """Serialization of a Python dict into a SNMP YAML File""" + with open(SNMP_COMMUNITY_FILE, 'w') as yaml_file: + try: + yaml.safe_dump(snmp_community_dict, yaml_file, default_flow_style=False) + except yaml.YAMLError as error: + click.echo("SNMP config data not dumped properly with error {}",format(error)) + raise + + """Restart SNMP Service""" + try: + click.echo("Restarting SNMP service...") + command = 'service snmp restart' + run_command(command, display_cmd=False) + except SystemExit as error: + click.echo("Restart service SNMP failed with error {}".format(error)) + raise + # # 'warm_restart' group ('config warm_restart ...') # diff --git a/doc/Command-Reference.md b/doc/Command-Reference.md index 314717abbb..e7f04b0350 100644 --- a/doc/Command-Reference.md +++ b/doc/Command-Reference.md @@ -67,6 +67,8 @@ Table of Contents * [Startup & Running Configuration](#startup--running-configuration) * [Startup Configuration command](#startup-configuration-command) * [Running Configuration command](#running-configuration-command) + * [SNMP Community Configuration](#snmp-community-configuration) + * [snmp-community config command](#snmp-community-config-commands) * [System State](#system-state) * [Processes show commands](#processes-show-commands) * [Services & memory show commands](#services--memory-show-commands) @@ -232,6 +234,7 @@ This command lists all the possible configuration commands at the top level. qos reload Clear current configuration and import a... save Export current config DB to a file on disk. + snmp-community Set SNMP community string tacacs TACACS+ server configuration vlan VLAN-related configuration tasks warm_restart warm_restart-related configuration tasks @@ -3129,7 +3132,23 @@ This command displays the running configuration of the snmp module. Go Back To [Beginning of the document](#SONiC-COMMAND-LINE-INTERFACE-GUIDE) or [Beginning of this section](#Startup--Running-Configuration) +# SNMP Community Configuration +## snmp-community config command + + This command is used to set SNMP community string or add more string to snmp_rocommunities. + + - Usage: + config snmp-community + + - Example: + + ``` + admin@sonic:~$ sudo config snmp-community new_public + Restarting SNMP service... + ``` + +Go Back To [Beginning of the document](#SONiC-COMMAND-LINE-INTERFACE-GUIDE) or [Beginning of this section](#snmp-community-configuration) # System State From 3041d763a7b5be244387f081cf6aa1ec5192d958 Mon Sep 17 00:00:00 2001 From: Sangita Maity Date: Thu, 24 Oct 2019 17:28:32 +0000 Subject: [PATCH 2/6] Change the logic to keep both snmp_rocommunity and snmp_rocommunities key --- config/main.py | 12 ++++-------- doc/Command-Reference.md | 4 ++-- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/config/main.py b/config/main.py index fa8ddecf76..fc75675080 100755 --- a/config/main.py +++ b/config/main.py @@ -716,7 +716,7 @@ def reload(): @config.command('snmp-community') @click.argument('community-name', metavar='', required=True) def snmp_community(community_name): - """Set SNMP community string""" + """Add SNMP community string""" """Serialization of a Python dict from a SNMP YAML File """ with open(SNMP_COMMUNITY_FILE, 'r') as stream: @@ -727,14 +727,10 @@ def snmp_community(community_name): raise if 'snmp_rocommunity' in snmp_community_dict.keys(): - community_exist_name = snmp_community_dict['snmp_rocommunity'] - del snmp_community_dict['snmp_rocommunity'] - snmp_community_dict['snmp_rocommunities'] = [community_exist_name] + existing_community_name = snmp_community_dict['snmp_rocommunity'] + if 'snmp_rocommunities' not in snmp_community_dict.keys(): + snmp_community_dict['snmp_rocommunities'] = [existing_community_name] snmp_community_dict['snmp_rocommunities'].append(community_name) - - elif 'snmp_rocommunities' in snmp_community_dict.keys(): - snmp_community_dict['snmp_rocommunities'].append(community_name) - else: snmp_community_dict['snmp_rocommunity'] = community_name diff --git a/doc/Command-Reference.md b/doc/Command-Reference.md index e7f04b0350..cdf8f2a3e3 100644 --- a/doc/Command-Reference.md +++ b/doc/Command-Reference.md @@ -234,7 +234,7 @@ This command lists all the possible configuration commands at the top level. qos reload Clear current configuration and import a... save Export current config DB to a file on disk. - snmp-community Set SNMP community string + snmp-community Add SNMP community string tacacs TACACS+ server configuration vlan VLAN-related configuration tasks warm_restart warm_restart-related configuration tasks @@ -3136,7 +3136,7 @@ Go Back To [Beginning of the document](#SONiC-COMMAND-LINE-INTERFACE-GUIDE) or [ ## snmp-community config command - This command is used to set SNMP community string or add more string to snmp_rocommunities. + This command is used to add SNMP community string to snmp_rocommunities. - Usage: config snmp-community From e9b059934ae7b254f5df82fca74926abb0df7944 Mon Sep 17 00:00:00 2001 From: Sangita Maity Date: Wed, 30 Oct 2019 19:37:39 +0000 Subject: [PATCH 3/6] Minor Update Signed-off-by: Sangita Maity --- config/main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/config/main.py b/config/main.py index 4aa7465881..43b5951de8 100755 --- a/config/main.py +++ b/config/main.py @@ -752,6 +752,7 @@ def snmp_community(community_name): snmp_community_dict['snmp_rocommunities'].append(community_name) else: snmp_community_dict['snmp_rocommunity'] = community_name + snmp_community_dict['snmp_rocommunities'] = community_name """Serialization of a Python dict into a SNMP YAML File""" with open(SNMP_COMMUNITY_FILE, 'w') as yaml_file: From 931d9929bc2431366b45704f3251a6b12dfa29ad Mon Sep 17 00:00:00 2001 From: Sangita Maity Date: Mon, 4 Nov 2019 18:06:59 +0000 Subject: [PATCH 4/6] Minor change Signed-off-by: Sangita Maity --- config/main.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/config/main.py b/config/main.py index 43b5951de8..696c70f27e 100755 --- a/config/main.py +++ b/config/main.py @@ -746,13 +746,16 @@ def snmp_community(community_name): raise if 'snmp_rocommunity' in snmp_community_dict.keys(): - existing_community_name = snmp_community_dict['snmp_rocommunity'] if 'snmp_rocommunities' not in snmp_community_dict.keys(): + existing_community_name = snmp_community_dict['snmp_rocommunity'] snmp_community_dict['snmp_rocommunities'] = [existing_community_name] snmp_community_dict['snmp_rocommunities'].append(community_name) else: snmp_community_dict['snmp_rocommunity'] = community_name - snmp_community_dict['snmp_rocommunities'] = community_name + if 'snmp_rocommunities' not in snmp_community_dict.keys(): + snmp_community_dict['snmp_rocommunities'] = community_name + else: + snmp_community_dict['snmp_rocommunities'].append(community_name) """Serialization of a Python dict into a SNMP YAML File""" with open(SNMP_COMMUNITY_FILE, 'w') as yaml_file: From a18cc1e75cf0b9c9ad04ba905c0f9ad48a141e04 Mon Sep 17 00:00:00 2001 From: Sangita Maity Date: Tue, 12 Nov 2019 19:32:51 +0000 Subject: [PATCH 5/6] convert string to list while adding new community string --- config/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/main.py b/config/main.py index ed697aa736..7aa64a7685 100755 --- a/config/main.py +++ b/config/main.py @@ -756,7 +756,7 @@ def snmp_community(community_name): else: snmp_community_dict['snmp_rocommunity'] = community_name if 'snmp_rocommunities' not in snmp_community_dict.keys(): - snmp_community_dict['snmp_rocommunities'] = community_name + snmp_community_dict['snmp_rocommunities'] = [community_name] else: snmp_community_dict['snmp_rocommunities'].append(community_name) From 0f3ec95fafa1e9d707ef7288fd0296e1ee00e4b2 Mon Sep 17 00:00:00 2001 From: Sangita Maity Date: Thu, 5 Dec 2019 19:06:42 +0000 Subject: [PATCH 6/6] Removed extra space Signed-off-by: Sangita Maity --- config/main.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/config/main.py b/config/main.py index cb504faefb..ecc606454e 100755 --- a/config/main.py +++ b/config/main.py @@ -12,7 +12,6 @@ import time import netifaces - import sonic_device_util import ipaddress from swsssdk import ConfigDBConnector @@ -29,7 +28,6 @@ SNMP_COMMUNITY_FILE = '/etc/sonic/snmp.yml' VLAN_SUB_INTERFACE_SEPARATOR = '.' - # ========================== Syslog wrappers ========================== def log_debug(msg):