Convert snmp.yml to configdb (#6205)
This PR is in preparation to move from snmp.yml to configdb. This will more closely align with other commands in sonic and use configdb as the source of truth for snmp configuration. Note: This is the first of 2 PR's to enable this. This PR will not change any functionality but will allow the snmp.yml file info to be put into the configdb. Created a script that takes the snmp.yml variables and converts them to the configdb format. Added file to dockerfile.j2 so that file is copied in the container. Updated start.sh file to automatically run the python conversion script each time the docker container is restarted.
This commit is contained in:
parent
9432bb82ec
commit
6efc0a885f
@ -68,6 +68,7 @@ RUN apt-get -y purge \
|
|||||||
rm -rf /debs /python-wheels ~/.cache
|
rm -rf /debs /python-wheels ~/.cache
|
||||||
|
|
||||||
COPY ["start.sh", "/usr/bin/"]
|
COPY ["start.sh", "/usr/bin/"]
|
||||||
|
COPY ["snmp_yml_to_configdb.py", "/usr/bin/"]
|
||||||
COPY ["supervisord.conf", "/etc/supervisor/conf.d/"]
|
COPY ["supervisord.conf", "/etc/supervisor/conf.d/"]
|
||||||
COPY ["*.j2", "/usr/share/sonic/templates/"]
|
COPY ["*.j2", "/usr/share/sonic/templates/"]
|
||||||
COPY ["files/supervisor-proc-exit-listener", "/usr/bin"]
|
COPY ["files/supervisor-proc-exit-listener", "/usr/bin"]
|
||||||
|
56
dockers/docker-snmp/snmp_yml_to_configdb.py
Executable file
56
dockers/docker-snmp/snmp_yml_to_configdb.py
Executable file
@ -0,0 +1,56 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
from sonic_py_common.logger import Logger
|
||||||
|
from swsssdk import ConfigDBConnector
|
||||||
|
|
||||||
|
db = ConfigDBConnector()
|
||||||
|
db.connect()
|
||||||
|
|
||||||
|
|
||||||
|
SYSLOG_IDENTIFIER = 'snmp_yml_to_configdb.py'
|
||||||
|
logger = Logger(SYSLOG_IDENTIFIER)
|
||||||
|
logger.set_min_log_priority_info()
|
||||||
|
|
||||||
|
snmp_comm_config_db = db.get_table('SNMP_COMMUNITY')
|
||||||
|
snmp_config_db_communities = snmp_comm_config_db.keys()
|
||||||
|
snmp_general_config_db = db.get_table('SNMP')
|
||||||
|
snmp_general_keys = snmp_general_config_db.keys()
|
||||||
|
|
||||||
|
full_snmp_comm_list = ['snmp_rocommunity', 'snmp_rocommunities', 'snmp_rwcommunity', 'snmp_rwcommunities']
|
||||||
|
|
||||||
|
if not os.path.exists('/etc/sonic/snmp.yml'):
|
||||||
|
logger.log_info('/etc/sonic/snmp.yml does not exist')
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
with open('/etc/sonic/snmp.yml', 'r') as yaml_file:
|
||||||
|
yaml_snmp_info = yaml.load(yaml_file, Loader=yaml.FullLoader)
|
||||||
|
|
||||||
|
for comm_type in full_snmp_comm_list:
|
||||||
|
if comm_type in yaml_snmp_info.keys():
|
||||||
|
if comm_type.startswith('snmp_rocommunities'):
|
||||||
|
for community in yaml_snmp_info[comm_type]:
|
||||||
|
if community not in snmp_config_db_communities:
|
||||||
|
db.set_entry('SNMP_COMMUNITY', community, {"TYPE": "RO"})
|
||||||
|
elif comm_type.startswith('snmp_rocommunity'):
|
||||||
|
community = yaml_snmp_info['snmp_rocommunity']
|
||||||
|
if community not in snmp_config_db_communities:
|
||||||
|
db.set_entry('SNMP_COMMUNITY', community, {"TYPE": "RO"})
|
||||||
|
elif comm_type.startswith('snmp_rwcommunities'):
|
||||||
|
for community in yaml_snmp_info[comm_type]:
|
||||||
|
if community not in snmp_config_db_communities:
|
||||||
|
db.set_entry('SNMP_COMMUNITY', community, {"TYPE": "RW"})
|
||||||
|
elif comm_type.startswith('snmp_rwcommunity'):
|
||||||
|
community = yaml_snmp_info['snmp_rwcommunity']
|
||||||
|
if community not in snmp_config_db_communities:
|
||||||
|
db.set_entry('SNMP_COMMUNITY', community, {"TYPE": "RW"})
|
||||||
|
|
||||||
|
if yaml_snmp_info.get('snmp_location'):
|
||||||
|
if 'LOCATION' not in snmp_general_keys:
|
||||||
|
db.set_entry('SNMP', 'LOCATION', {'Location': yaml_snmp_info['snmp_location']})
|
||||||
|
else:
|
||||||
|
logger.log_info('snmp_location does not exist in snmp.yml file')
|
||||||
|
sys.exit(1)
|
@ -13,6 +13,9 @@ fi
|
|||||||
|
|
||||||
mkdir -p /etc/ssw /etc/snmp
|
mkdir -p /etc/ssw /etc/snmp
|
||||||
|
|
||||||
|
# Parse snmp.yml and insert the data in Config DB
|
||||||
|
/usr/bin/snmp_yml_to_configdb.py
|
||||||
|
|
||||||
SONIC_CFGGEN_ARGS=" \
|
SONIC_CFGGEN_ARGS=" \
|
||||||
-d \
|
-d \
|
||||||
-y /etc/sonic/sonic_version.yml \
|
-y /etc/sonic/sonic_version.yml \
|
||||||
|
Loading…
Reference in New Issue
Block a user