Skip to content

Commit 09977fc

Browse files
kevinwangsklguohan
authored andcommitted
Some improvement for Mellanox sniffer CLI (sonic-net#345)
* Hide Mellanox sniffer CLI on other platforms * Set enable/disable to be the argument of sniffer command rather than the sub-command Signed-off-by: Kevin Wang <[email protected]>
1 parent b1156be commit 09977fc

File tree

4 files changed

+55
-122
lines changed

4 files changed

+55
-122
lines changed

config/main.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import subprocess
88
import netaddr
99
import re
10+
11+
import sonic_platform
1012
from swsssdk import ConfigDBConnector
1113
from natsort import natsorted
1214
from minigraph import parse_device_desc_xml
@@ -926,7 +928,9 @@ def asymmetric(status, interface):
926928
@cli.group()
927929
def platform():
928930
"""Platform-related configuration tasks"""
929-
platform.add_command(mlnx.mlnx)
931+
932+
if (sonic_platform.get_sonic_version_info()['asic_type'] == 'mellanox'):
933+
platform.add_command(mlnx.mlnx)
930934

931935

932936
#

config/mlnx.py

+29-97
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@
2323

2424
SNIFFER_SYSLOG_IDENTIFIER = "sniffer"
2525

26-
# Mellanox platform name
27-
MLNX_PLATFORM_NAME = 'mellanox'
28-
29-
# sonic-cfggen -y /etc/sonic/sonic_version.yml -v asic_type
30-
PLATFORM_ROOT_PATH = '/usr/share/sonic/device'
31-
SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen'
32-
SONIC_VERSION_PATH = '/etc/sonic/sonic_version.yml'
33-
ASIC_TYPE_KEY = 'asic_type'
34-
3526
# SDK sniffer env variable
3627
ENV_VARIABLE_SX_SNIFFER = 'SX_SNIFFER_ENABLE'
3728
ENV_VARIABLE_SX_SNIFFER_TARGET = 'SX_SNIFFER_TARGET'
@@ -94,28 +85,6 @@ def run_command(command, display_cmd=False, ignore_error=False):
9485
sys.exit(proc.returncode)
9586

9687

97-
# Get asic type with command "sonic-cfggen -y /etc/sonic/sonic_version.yml -v asic_type"
98-
def get_asic_type():
99-
try:
100-
proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-y', SONIC_VERSION_PATH, '-v', ASIC_TYPE_KEY],
101-
stdout=subprocess.PIPE,
102-
shell=False,
103-
stderr=subprocess.STDOUT)
104-
stdout = proc.communicate()[0]
105-
proc.wait()
106-
asic_type = stdout.rstrip('\n')
107-
except OSError, e:
108-
raise OSError("Cannot detect platform asic type, %s" % str(e))
109-
110-
return asic_type
111-
112-
113-
# verify if the platform is with Mellanox asic.
114-
def verify_asic_type():
115-
asic_type = get_asic_type()
116-
return cmp(asic_type, MLNX_PLATFORM_NAME)
117-
118-
11988
# generate sniffer target file name include a time stamp.
12089
def sniffer_filename_generate(path, filename_prefix, filename_ext):
12190
time_stamp = time.strftime("%Y%m%d%H%M%S")
@@ -160,7 +129,7 @@ def conf_file_copy(src, dest):
160129

161130

162131
def conf_file_receive():
163-
command = 'docker exec -ti ' + CONTAINER_NAME + ' bash -c "touch ' + SNIFFER_CONF_FILE + '"'
132+
command = "docker exec {} bash -c 'touch {}'".format(CONTAINER_NAME, SNIFFER_CONF_FILE)
164133
run_command(command)
165134
conf_file_copy(SNIFFER_CONF_FILE_IN_CONTAINER, TMP_SNIFFER_CONF_FILE)
166135

@@ -176,21 +145,21 @@ def sniffer_env_variable_set(enable, env_variable_name, env_variable_string=""):
176145
env_variable_exist_string = env_variable_read(env_variable_name)
177146
if env_variable_exist_string:
178147
if enable is True:
179-
print "sniffer is already running, do nothing"
148+
print "sniffer is already enabled, do nothing"
180149
ignore = True
181150
else:
182151
env_variable_delete(env_variable_exist_string)
183152
else:
184153
if enable is True:
185154
env_variable_write(env_variable_string)
186155
else:
187-
print "sniffer is already turned off, do nothing"
156+
print "sniffer is already disabled, do nothing"
188157
ignore = True
189158

190159
if not ignore:
191160
config_file_send()
192161

193-
command = 'rm -rf ' + TMP_SNIFFER_CONF_FILE
162+
command = 'rm -rf {}'.format(TMP_SNIFFER_CONF_FILE)
194163
run_command(command)
195164

196165
return ignore
@@ -217,32 +186,30 @@ def _abort_if_false(ctx, param, value):
217186
# 'mlnx' group
218187
@click.group()
219188
def mlnx():
220-
"""Mellanox platform specific configuration tasks"""
221-
# check the platform info, this command only work on Mellanox platform
222-
err = verify_asic_type()
223-
if err != 0:
224-
print "This command only supported on Mellanox platform"
225-
sys.exit(2)
189+
""" Mellanox platform configuration tasks """
190+
pass
226191

227192

228193
# 'sniffer' group
229194
@mlnx.group()
230195
def sniffer():
231-
"""sniffer - Utility for managing Mellanox SDK/PRM sniffer"""
196+
""" Utility for managing Mellanox SDK/PRM sniffer """
232197
pass
233198

234199

235200
# 'sdk' subgroup
236-
@sniffer.group()
237-
def sdk():
201+
@sniffer.command()
202+
@click.option('-y', '--yes', is_flag=True, callback=_abort_if_false, expose_value=False,
203+
prompt='To change SDK sniffer status, swss service will be restarted, continue?')
204+
@click.argument('option', type=click.Choice(["enable", "disable"]))
205+
def sdk(option):
238206
"""SDK Sniffer - Command Line to enable/disable SDK sniffer"""
239-
pass
207+
if option == 'enable':
208+
sdk_sniffer_enable()
209+
elif option == 'disable':
210+
sdk_sniffer_disable()
240211

241212

242-
# 'sniffer sdk enable' command
243-
@sdk.command('enable')
244-
@click.option('-y', '--yes', is_flag=True, callback=_abort_if_false, expose_value=False,
245-
prompt='To enable SDK sniffer swss service will be restarted, continue?')
246213
def sdk_sniffer_enable():
247214
"""Enable SDK Sniffer"""
248215
print "Enabling SDK sniffer"
@@ -264,15 +231,11 @@ def sdk_sniffer_enable():
264231
err = restart_swss()
265232
if err is not 0:
266233
return
267-
print 'SDK sniffer is enabled, recording file is %s.' % sdk_sniffer_filename
234+
print 'Enabled SDK sniffer, recording file is %s' % sdk_sniffer_filename
268235
else:
269236
pass
270237

271238

272-
# 'sniffer sdk disable' command
273-
@sdk.command('disable')
274-
@click.option('-y', '--yes', is_flag=True, callback=_abort_if_false, expose_value=False,
275-
prompt='To disable SDK sniffer swss service will be restarted, continue?')
276239
def sdk_sniffer_disable():
277240
"""Disable SDK Sniffer"""
278241
print "Disabling SDK sniffer"
@@ -282,54 +245,23 @@ def sdk_sniffer_disable():
282245
err = restart_swss()
283246
if err is not 0:
284247
return
285-
print "SDK sniffer is disabled"
248+
print "Disabled SDK sniffer"
286249
else:
287250
pass
288251

289252

290253
# place holders for 'sniff prm enable/disable' and 'sniffer all enable/disable'
291-
'''
292-
@cli.group()
293-
def prm():
294-
"""PRM Sniffer - Command Line to enable/disable PRM sniffer"""
295-
pass
296-
297-
298-
@prm.command('enable')
299-
def enable_prm_sniffer():
300-
"""Enable SDK sniffer"""
301-
pass
302-
303-
304-
@prm.command('disable')
305-
def disable_prm_sniffer():
306-
"""Disable PRM sniffer"""
307-
pass
308-
309-
310-
@cli.group()
311-
def all():
312-
"""ALL SNIFFERS - Command line to enable/disable PRM and SDK sniffer"""
313-
pass
314-
315-
316-
@all.command('enable')
317-
def enable_all_sniffer():
318-
"""Enable PRM and SDK sniffers"""
319-
pass
320-
321-
322-
@all.command('disable')
323-
def disable_all_sniffer():
324-
"""Disable PRM and SDK sniffers"""
325-
pass
254+
# @sniffer.command()
255+
# @click.argument('option', type=click.Choice(["enable", "disable"]))
256+
# def prf():
257+
# pass
258+
#
259+
#
260+
# @sniffer.command()
261+
# @click.argument('option', type=click.Choice(["enable", "disable"]))
262+
# def all():
263+
# pass
326264

327-
@cli.group()
328-
def status():
329-
"""Sniffer running status - Command Line to show sniffer running status"""
330-
pass
331-
332-
'''
333265

334266
if __name__ == '__main__':
335-
mlnx()
267+
sniffer()

show/main.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,8 @@ def platform():
874874
"""Show platform-specific hardware info"""
875875
pass
876876

877-
platform.add_command(mlnx.mlnx)
877+
if (sonic_platform.get_sonic_version_info()['asic_type'] == 'mellanox'):
878+
platform.add_command(mlnx.mlnx)
878879

879880
# 'summary' subcommand ("show platform summary")
880881
@platform.command()

show/mlnx.py

+19-23
Original file line numberDiff line numberDiff line change
@@ -20,59 +20,55 @@
2020

2121

2222
# run command
23-
def run_command(command, display_cmd=False):
24-
if display_cmd:
25-
click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green'))
23+
def run_command(command, display_cmd=False, ignore_error=False):
24+
"""Run bash command and print output to stdout
25+
"""
26+
if display_cmd == True:
27+
click.echo(click.style("Running command: ", fg='cyan') + click.style(command, fg='green'))
2628

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

29-
while True:
30-
output = proc.stdout.readline()
31-
if output == "" and proc.poll() is not None:
32-
break
33-
if output:
34-
click.echo(output.rstrip('\n'))
32+
if len(out) > 0:
33+
click.echo(out)
3534

36-
rc = proc.poll()
37-
if rc != 0:
38-
sys.exit(rc)
35+
if proc.returncode != 0 and not ignore_error:
36+
sys.exit(proc.returncode)
3937

4038

4139
# 'mlnx' group
4240
@click.group()
4341
def mlnx():
44-
"""Mellanox platform specific configuration tasks"""
42+
""" Show Mellanox platform information """
4543
pass
4644

4745

4846
# get current status of sniffer from conf file
4947
def sniffer_status_get(env_variable_name):
5048
enabled = False
51-
52-
command = 'docker exec -ti ' + CONTAINER_NAME + ' bash -c "touch ' + SNIFFER_CONF_FILE + '"'
49+
command = "docker exec {} bash -c 'touch {}'".format(CONTAINER_NAME, SNIFFER_CONF_FILE)
5350
run_command(command)
54-
command = 'docker cp ' + SNIFFER_CONF_FILE_IN_CONTAINER + ' ' + TMP_SNIFFER_CONF_FILE
51+
command = 'docker cp {} {}'.format(SNIFFER_CONF_FILE_IN_CONTAINER, TMP_SNIFFER_CONF_FILE)
5552
run_command(command)
5653
conf_file = open(TMP_SNIFFER_CONF_FILE, 'r')
5754
for env_variable_string in conf_file:
5855
if env_variable_string.find(env_variable_name) >= 0:
5956
enabled = True
6057
break
6158
conf_file.close()
62-
command = 'rm -rf ' + TMP_SNIFFER_CONF_FILE
59+
command = 'rm -rf {}'.format(TMP_SNIFFER_CONF_FILE)
6360
run_command(command)
64-
6561
return enabled
6662

6763

68-
@mlnx.command('sniffer')
69-
def sniffer_status():
70-
""" Sniffer running status """
64+
@mlnx.command()
65+
def sniffer():
66+
""" Show sniffer status """
7167
components = ['sdk']
7268
env_variable_strings = [ENV_VARIABLE_SX_SNIFFER]
7369
for index in range(len(components)):
7470
enabled = sniffer_status_get(env_variable_strings[index])
7571
if enabled is True:
76-
print components[index] + " sniffer is RUNNING"
72+
print components[index] + " sniffer is enabled"
7773
else:
78-
print components[index] + " sniffer is OFF"
74+
print components[index] + " sniffer is disabled"

0 commit comments

Comments
 (0)