Skip to content

Commit

Permalink
[config] Add new command to show current sdk sniffer status (sonic-ne…
Browse files Browse the repository at this point in the history
…t#311)

Besides enable/disable operation, implements a new command to show the
status of sniffer.
Additionally, improve the implemention of the enable/disable operation
to be more robust and extensible.

Signed-off-by: Kevin Wang <kevinw@mellanox.com>
  • Loading branch information
kevinwangsk authored and lguohan committed Sep 9, 2018
1 parent 8819b4f commit 98add9e
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 79 deletions.
188 changes: 109 additions & 79 deletions config/mlnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,12 @@

# Supervisor config file path
TMP_SNIFFER_CONF_FILE = '/tmp/tmp.conf'
CONTAINER_NAME = 'syncd'
SNIFFER_CONF_FILE = '/etc/supervisor/conf.d/mlnx_sniffer.conf'

SNIFFER_CONF_FILE_IN_CONTAINER = CONTAINER_NAME + ':' + SNIFFER_CONF_FILE
# Command to restart swss service
COMMAND_RESTART_SWSS = 'systemctl restart swss.service'

# global variable SDK_SNIFFER_TARGET_FILE_NAME
SDK_SNIFFER_TARGET_FILE_NAME = ''


# ========================== Syslog wrappers ==========================
def log_info(msg, syslog_identifier, also_print_to_console=False):
syslog.openlog(syslog_identifier)
Expand Down Expand Up @@ -81,15 +78,20 @@ def log_error(msg, syslog_identifier, also_print_to_console=False):


# run command
def run_command(command, pager=False):
if pager is True:
# click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green'))
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
click.echo_via_pager(p.stdout.read())
else:
# click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green'))
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
click.echo(p.stdout.read())
def run_command(command, display_cmd=False, ignore_error=False):
"""Run bash command and print output to stdout
"""
if display_cmd == True:
click.echo(click.style("Running command: ", fg='cyan') + click.style(command, fg='green'))

proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
(out, err) = proc.communicate()

if len(out) > 0:
click.echo(out)

if proc.returncode != 0 and not ignore_error:
sys.exit(proc.returncode)


# Get asic type with command "sonic-cfggen -y /etc/sonic/sonic_version.yml -v asic_type"
Expand All @@ -115,68 +117,84 @@ def verify_asic_type():


# generate sniffer target file name include a time stamp.
def generate_file_name(prm=False, sdk=False):
def sniffer_filename_generate(path, filename_prefix, filename_ext):
time_stamp = time.strftime("%Y%m%d%H%M%S")
if sdk is True:
file_name = SDK_SNIFFER_FILENAME_PREFIX + time_stamp + SDK_SNIFFER_FILENAME_EXT
global SDK_SNIFFER_TARGET_FILE_NAME
SDK_SNIFFER_TARGET_FILE_NAME = file_name
elif prm is True:
# place holders for 'sniff prm enable/disable' and 'sniffer all enable/disable'
pass
else:
file_name = ''

return file_name
filename = path + filename_prefix + time_stamp + filename_ext
return filename


# generate supervisor conf file for sniffer
def generate_conf_file(sdk=False, prm=False, all=False):
if sdk is True:
target_filename = generate_file_name(sdk=True)
tart_fullpath = SDK_SNIFFER_TARGET_PATH + target_filename
conf_file = open(TMP_SNIFFER_CONF_FILE, 'w')
# write environment variable in local tmp file for sniffer
def env_variable_write(env_variable_string):
conf_file = open(TMP_SNIFFER_CONF_FILE, 'a')
if os.path.getsize(TMP_SNIFFER_CONF_FILE) == 0:
conf_file.write('[program:syncd]\n')
env_str = 'environment=' + ENV_VARIABLE_SX_SNIFFER + '="1",' + ENV_VARIABLE_SX_SNIFFER_TARGET + '="' + tart_fullpath + '"\n'
conf_file.write(env_str)
conf_file.close()
elif prm is True:
# place holder for prm sniffer
pass
conf_file.write(env_variable_string)
conf_file.close()

elif all is True:
# place holder for all sniffer
pass

def env_variable_read(env_variable_name):
conf_file = open(TMP_SNIFFER_CONF_FILE, 'r')
for env_variable_string in conf_file:
if env_variable_string.find(env_variable_name) >= 0:
break
else:
pass
env_variable_string = ''
conf_file.close()
return env_variable_string

# set supervisor conf file for sniffer enable
def set_conf_for_sniffer_enable(prm=False, sdk=False, all=False):
if sdk is True:
generate_conf_file(sdk=True)
command = 'docker cp ' + TMP_SNIFFER_CONF_FILE + ' ' + 'syncd:' + SNIFFER_CONF_FILE
run_command(command)

command = 'rm -rf ' + TMP_SNIFFER_CONF_FILE
run_command(command)
def env_variable_delete(delete_line):
conf_file = open(TMP_SNIFFER_CONF_FILE, 'r+')
all_lines = conf_file.readlines()
conf_file.seek(0)
for line in all_lines:
if line != delete_line:
conf_file.write(line)
conf_file.truncate()
conf_file.close()

elif prm is True:
# place holder for prm sniffer
pass

elif all is True:
# place holder for all sniffer
pass
def conf_file_copy(src, dest):
command = 'docker cp ' + src + ' ' + dest
run_command(command)


def conf_file_receive():
command = 'docker exec -ti ' + CONTAINER_NAME + ' bash -c "touch ' + SNIFFER_CONF_FILE + '"'
run_command(command)
conf_file_copy(SNIFFER_CONF_FILE_IN_CONTAINER, TMP_SNIFFER_CONF_FILE)


def config_file_send():
conf_file_copy(TMP_SNIFFER_CONF_FILE, SNIFFER_CONF_FILE_IN_CONTAINER)


# set supervisor conf file for sniffer enable
def sniffer_env_variable_set(enable, env_variable_name, env_variable_string=""):
ignore = False
conf_file_receive()
env_variable_exist_string = env_variable_read(env_variable_name)
if env_variable_exist_string:
if enable is True:
print "sniffer is already running, do nothing"
ignore = True
else:
env_variable_delete(env_variable_exist_string)
else:
pass
if enable is True:
env_variable_write(env_variable_string)
else:
print "sniffer is already turned off, do nothing"
ignore = True

if not ignore:
config_file_send()

# remove the sniffer supervisor conf file from syncd container
def rm_conf_for_sniffer_disable():
command = 'docker exec syncd rm -rf ' + SNIFFER_CONF_FILE
command = 'rm -rf ' + TMP_SNIFFER_CONF_FILE
run_command(command)

return ignore


# restart the swss service with command 'service swss restart'
def restart_swss():
Expand Down Expand Up @@ -225,36 +243,48 @@ def sdk():
@sdk.command('enable')
@click.option('-y', '--yes', is_flag=True, callback=_abort_if_false, expose_value=False,
prompt='To enable SDK sniffer swss service will be restarted, continue?')
def enable_sdk_sniffer():
def sdk_sniffer_enable():
"""Enable SDK Sniffer"""
print "Enabling SDK sniffer"

set_conf_for_sniffer_enable(sdk=True)

err = restart_swss()
if err is not 0:
return

full_file_path = SDK_SNIFFER_TARGET_PATH + SDK_SNIFFER_TARGET_FILE_NAME

print 'sdk sniffer recording to file %s.' % full_file_path
sdk_sniffer_filename = sniffer_filename_generate(SDK_SNIFFER_TARGET_PATH,
SDK_SNIFFER_FILENAME_PREFIX,
SDK_SNIFFER_FILENAME_EXT)
sdk_sniffer_env_variable_dict = {ENV_VARIABLE_SX_SNIFFER: "1" + ",",
ENV_VARIABLE_SX_SNIFFER_TARGET: sdk_sniffer_filename}
sdk_sniffer_env_variable_string = "environment="

for env_variable_name, env_variable_value in sdk_sniffer_env_variable_dict.items():
sdk_sniffer_env_variable_string += (env_variable_name + "=" + env_variable_value)

sdk_sniffer_env_variable_string += "\n"

ignore = sniffer_env_variable_set(enable=True, env_variable_name=ENV_VARIABLE_SX_SNIFFER,
env_variable_string=sdk_sniffer_env_variable_string)
if not ignore:
err = restart_swss()
if err is not 0:
return
print 'SDK sniffer is enabled, recording file is %s.' % sdk_sniffer_filename
else:
pass


# 'sniffer sdk disable' command
@sdk.command('disable')
@click.option('-y', '--yes', is_flag=True, callback=_abort_if_false, expose_value=False,
prompt='To disable SDK sniffer swss service will be restarted, continue?')
def disable_sdk_sniffer():
def sdk_sniffer_disable():
"""Disable SDK Sniffer"""
print "Disabling SDK sniffer"

rm_conf_for_sniffer_disable()

err = restart_swss()
if err is not 0:
return

print "SDK sniffer disabled"
ignore = sniffer_env_variable_set(enable=False, env_variable_name=ENV_VARIABLE_SX_SNIFFER)
if not ignore:
err = restart_swss()
if err is not 0:
return
print "SDK sniffer is disabled"
else:
pass


# place holders for 'sniff prm enable/disable' and 'sniffer all enable/disable'
Expand Down
4 changes: 4 additions & 0 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from tabulate import tabulate
from swsssdk import ConfigDBConnector

import mlnx

try:
# noinspection PyPep8Naming
import ConfigParser as configparser
Expand Down Expand Up @@ -630,6 +632,8 @@ def platform():
"""Show platform-specific hardware info"""
pass

platform.add_command(mlnx.mlnx)

# 'summary' subcommand ("show platform summary")
@platform.command()
def summary():
Expand Down
78 changes: 78 additions & 0 deletions show/mlnx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env python
#
# main.py
#
# Specific command-line utility for Mellanox platform
#

try:
import sys
import subprocess
import click
except ImportError as e:
raise ImportError("%s - required module not found" % str(e))

ENV_VARIABLE_SX_SNIFFER = 'SX_SNIFFER_ENABLE'
CONTAINER_NAME = 'syncd'
SNIFFER_CONF_FILE = '/etc/supervisor/conf.d/mlnx_sniffer.conf'
SNIFFER_CONF_FILE_IN_CONTAINER = CONTAINER_NAME + ':' + SNIFFER_CONF_FILE
TMP_SNIFFER_CONF_FILE = '/tmp/tmp.conf'


# run command
def run_command(command, display_cmd=False):
if display_cmd:
click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green'))

proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)

while True:
output = proc.stdout.readline()
if output == "" and proc.poll() is not None:
break
if output:
click.echo(output.rstrip('\n'))

rc = proc.poll()
if rc != 0:
sys.exit(rc)


# 'mlnx' group
@click.group()
def mlnx():
"""Mellanox platform specific configuration tasks"""
pass


# get current status of sniffer from conf file
def sniffer_status_get(env_variable_name):
enabled = False

command = 'docker exec -ti ' + CONTAINER_NAME + ' bash -c "touch ' + SNIFFER_CONF_FILE + '"'
run_command(command)
command = 'docker cp ' + SNIFFER_CONF_FILE_IN_CONTAINER + ' ' + TMP_SNIFFER_CONF_FILE
run_command(command)
conf_file = open(TMP_SNIFFER_CONF_FILE, 'r')
for env_variable_string in conf_file:
if env_variable_string.find(env_variable_name) >= 0:
enabled = True
break
conf_file.close()
command = 'rm -rf ' + TMP_SNIFFER_CONF_FILE
run_command(command)

return enabled


@mlnx.command('sniffer')
def sniffer_status():
""" Sniffer running status """
components = ['sdk']
env_variable_strings = [ENV_VARIABLE_SX_SNIFFER]
for index in range(len(components)):
enabled = sniffer_status_get(env_variable_strings[index])
if enabled is True:
print components[index] + " sniffer is RUNNING"
else:
print components[index] + " sniffer is OFF"

0 comments on commit 98add9e

Please sign in to comment.