Skip to content

Commit

Permalink
Revert "Some improvement for Mellanox sniffer CLI (sonic-net#345)" (s…
Browse files Browse the repository at this point in the history
  • Loading branch information
qiluo-msft authored Oct 19, 2018
1 parent 57eeac1 commit 4562d57
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 55 deletions.
6 changes: 1 addition & 5 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import subprocess
import netaddr
import re

import sonic_platform
from swsssdk import ConfigDBConnector
from natsort import natsorted
from minigraph import parse_device_desc_xml
Expand Down Expand Up @@ -929,9 +927,7 @@ def asymmetric(ctx, status):
@config.group()
def platform():
"""Platform-related configuration tasks"""

if (sonic_platform.get_sonic_version_info()['asic_type'] == 'mellanox'):
platform.add_command(mlnx.mlnx)
platform.add_command(mlnx.mlnx)


#
Expand Down
126 changes: 97 additions & 29 deletions config/mlnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@

SNIFFER_SYSLOG_IDENTIFIER = "sniffer"

# Mellanox platform name
MLNX_PLATFORM_NAME = 'mellanox'

# sonic-cfggen -y /etc/sonic/sonic_version.yml -v asic_type
PLATFORM_ROOT_PATH = '/usr/share/sonic/device'
SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen'
SONIC_VERSION_PATH = '/etc/sonic/sonic_version.yml'
ASIC_TYPE_KEY = 'asic_type'

# SDK sniffer env variable
ENV_VARIABLE_SX_SNIFFER = 'SX_SNIFFER_ENABLE'
ENV_VARIABLE_SX_SNIFFER_TARGET = 'SX_SNIFFER_TARGET'
Expand Down Expand Up @@ -85,6 +94,28 @@ def run_command(command, display_cmd=False, ignore_error=False):
sys.exit(proc.returncode)


# Get asic type with command "sonic-cfggen -y /etc/sonic/sonic_version.yml -v asic_type"
def get_asic_type():
try:
proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-y', SONIC_VERSION_PATH, '-v', ASIC_TYPE_KEY],
stdout=subprocess.PIPE,
shell=False,
stderr=subprocess.STDOUT)
stdout = proc.communicate()[0]
proc.wait()
asic_type = stdout.rstrip('\n')
except OSError, e:
raise OSError("Cannot detect platform asic type, %s" % str(e))

return asic_type


# verify if the platform is with Mellanox asic.
def verify_asic_type():
asic_type = get_asic_type()
return cmp(asic_type, MLNX_PLATFORM_NAME)


# generate sniffer target file name include a time stamp.
def sniffer_filename_generate(path, filename_prefix, filename_ext):
time_stamp = time.strftime("%Y%m%d%H%M%S")
Expand Down Expand Up @@ -129,7 +160,7 @@ def conf_file_copy(src, dest):


def conf_file_receive():
command = "docker exec {} bash -c 'touch {}'".format(CONTAINER_NAME, SNIFFER_CONF_FILE)
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)

Expand All @@ -145,21 +176,21 @@ def sniffer_env_variable_set(enable, env_variable_name, env_variable_string=""):
env_variable_exist_string = env_variable_read(env_variable_name)
if env_variable_exist_string:
if enable is True:
print "sniffer is already enabled, do nothing"
print "sniffer is already running, do nothing"
ignore = True
else:
env_variable_delete(env_variable_exist_string)
else:
if enable is True:
env_variable_write(env_variable_string)
else:
print "sniffer is already disabled, do nothing"
print "sniffer is already turned off, do nothing"
ignore = True

if not ignore:
config_file_send()

command = 'rm -rf {}'.format(TMP_SNIFFER_CONF_FILE)
command = 'rm -rf ' + TMP_SNIFFER_CONF_FILE
run_command(command)

return ignore
Expand All @@ -186,30 +217,32 @@ def _abort_if_false(ctx, param, value):
# 'mlnx' group
@click.group()
def mlnx():
""" Mellanox platform configuration tasks """
pass
"""Mellanox platform specific configuration tasks"""
# check the platform info, this command only work on Mellanox platform
err = verify_asic_type()
if err != 0:
print "This command only supported on Mellanox platform"
sys.exit(2)


# 'sniffer' group
@mlnx.group()
def sniffer():
""" Utility for managing Mellanox SDK/PRM sniffer """
"""sniffer - Utility for managing Mellanox SDK/PRM sniffer"""
pass


# 'sdk' subgroup
@sniffer.command()
@click.option('-y', '--yes', is_flag=True, callback=_abort_if_false, expose_value=False,
prompt='To change SDK sniffer status, swss service will be restarted, continue?')
@click.argument('option', type=click.Choice(["enable", "disable"]))
def sdk(option):
@sniffer.group()
def sdk():
"""SDK Sniffer - Command Line to enable/disable SDK sniffer"""
if option == 'enable':
sdk_sniffer_enable()
elif option == 'disable':
sdk_sniffer_disable()
pass


# 'sniffer sdk enable' command
@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 sdk_sniffer_enable():
"""Enable SDK Sniffer"""
print "Enabling SDK sniffer"
Expand All @@ -231,11 +264,15 @@ def sdk_sniffer_enable():
err = restart_swss()
if err is not 0:
return
print 'Enabled SDK sniffer, recording file is %s' % sdk_sniffer_filename
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 sdk_sniffer_disable():
"""Disable SDK Sniffer"""
print "Disabling SDK sniffer"
Expand All @@ -245,23 +282,54 @@ def sdk_sniffer_disable():
err = restart_swss()
if err is not 0:
return
print "Disabled SDK sniffer"
print "SDK sniffer is disabled"
else:
pass


# place holders for 'sniff prm enable/disable' and 'sniffer all enable/disable'
# @sniffer.command()
# @click.argument('option', type=click.Choice(["enable", "disable"]))
# def prf():
# pass
#
#
# @sniffer.command()
# @click.argument('option', type=click.Choice(["enable", "disable"]))
# def all():
# pass
'''
@cli.group()
def prm():
"""PRM Sniffer - Command Line to enable/disable PRM sniffer"""
pass
@prm.command('enable')
def enable_prm_sniffer():
"""Enable SDK sniffer"""
pass
@prm.command('disable')
def disable_prm_sniffer():
"""Disable PRM sniffer"""
pass
@cli.group()
def all():
"""ALL SNIFFERS - Command line to enable/disable PRM and SDK sniffer"""
pass
@all.command('enable')
def enable_all_sniffer():
"""Enable PRM and SDK sniffers"""
pass
@all.command('disable')
def disable_all_sniffer():
"""Disable PRM and SDK sniffers"""
pass
@cli.group()
def status():
"""Sniffer running status - Command Line to show sniffer running status"""
pass
'''

if __name__ == '__main__':
sniffer()
mlnx()
3 changes: 1 addition & 2 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,8 +874,7 @@ def platform():
"""Show platform-specific hardware info"""
pass

if (sonic_platform.get_sonic_version_info()['asic_type'] == 'mellanox'):
platform.add_command(mlnx.mlnx)
platform.add_command(mlnx.mlnx)

# 'summary' subcommand ("show platform summary")
@platform.command()
Expand Down
42 changes: 23 additions & 19 deletions show/mlnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,55 +20,59 @@


# run command
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'))
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)
(out, err) = proc.communicate()

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

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


# 'mlnx' group
@click.group()
def mlnx():
""" Show Mellanox platform information """
"""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 {} bash -c 'touch {}'".format(CONTAINER_NAME, SNIFFER_CONF_FILE)

command = 'docker exec -ti ' + CONTAINER_NAME + ' bash -c "touch ' + SNIFFER_CONF_FILE + '"'
run_command(command)
command = 'docker cp {} {}'.format(SNIFFER_CONF_FILE_IN_CONTAINER, TMP_SNIFFER_CONF_FILE)
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 {}'.format(TMP_SNIFFER_CONF_FILE)
command = 'rm -rf ' + TMP_SNIFFER_CONF_FILE
run_command(command)

return enabled


@mlnx.command()
def sniffer():
""" Show sniffer status """
@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 enabled"
print components[index] + " sniffer is RUNNING"
else:
print components[index] + " sniffer is disabled"
print components[index] + " sniffer is OFF"

0 comments on commit 4562d57

Please sign in to comment.