Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[config] Add snmp-community command to set SNMP community string #687

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
49 changes: 49 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import netaddr
import re
import syslog
import yaml
import netifaces


import sonic_device_util
samaity marked this conversation as resolved.
Show resolved Hide resolved
import ipaddress
from swsssdk import ConfigDBConnector
Expand All @@ -23,8 +25,10 @@

SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen'
SYSLOG_IDENTIFIER = "config"
SNMP_COMMUNITY_FILE = '/etc/sonic/snmp.yml'
VLAN_SUB_INTERFACE_SEPARATOR = '.'


# ========================== Syslog wrappers ==========================
samaity marked this conversation as resolved.
Show resolved Hide resolved

def log_debug(msg):
Expand Down Expand Up @@ -728,6 +732,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='<community_name>', required=True)
def snmp_community(community_name):
"""Add 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():
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
samaity marked this conversation as resolved.
Show resolved Hide resolved
if 'snmp_rocommunities' not in snmp_community_dict.keys():
snmp_community_dict['snmp_rocommunities'] = community_name
samaity marked this conversation as resolved.
Show resolved Hide resolved
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:
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 ...')
#
Expand Down
21 changes: 21 additions & 0 deletions doc/Command-Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
* [Startup & Running Configuration](#startup--running-configuration)
* [Startup Configuration](#startup-configuration)
* [Running Configuration](#running-configuration)
* [SNMP Community Configuration](#snmp-community-configuration)
* [snmp-community config command](#snmp-community-config-commands)
* [Syslog](#syslog)
* [Syslog config commands](#syslog-config-commands)
* [System State](#system-state)
Expand Down Expand Up @@ -106,6 +108,7 @@
| v1 | Mar-23-2019 | Initial version of CLI Guide with minimal command set |

## Introduction

SONiC is an open source network operating system based on Linux that runs on switches from multiple vendors and ASICs. SONiC offers a full-suite of network functionality, like BGP and RDMA, that has been production-hardened in the data centers of some of the largest cloud-service providers. It offers teams the flexibility to create the network solutions they need while leveraging the collective strength of a large ecosystem and community.

SONiC software shall be loaded in these [supported devices](https://github.com/Azure/SONiC/wiki/Supported-Devices-and-Platforms) and this CLI guide shall be used to configure the devices as well as to display the configuration, state and status.
Expand Down Expand Up @@ -248,6 +251,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 Add SNMP community string
tacacs TACACS+ server configuration
vlan VLAN-related configuration tasks
warm_restart warm_restart-related configuration tasks
Expand Down Expand Up @@ -3538,6 +3542,23 @@ This command displays the running configuration of the snmp module.

Go Back To [Beginning of the document](#) or [Beginning of this section](#Startup--Running-Configuration)

## SNMP Community Configuration

### snmp-community config command

This command is used to add SNMP community string to snmp_rocommunities.

- Usage:
config snmp-community <community_name>

- 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)

## Syslog

Expand Down