Skip to content

Commit 4562d57

Browse files
authored
Revert "Some improvement for Mellanox sniffer CLI (sonic-net#345)" (sonic-net#349)
1 parent 57eeac1 commit 4562d57

File tree

4 files changed

+122
-55
lines changed

4 files changed

+122
-55
lines changed

config/main.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import subprocess
88
import netaddr
99
import re
10-
11-
import sonic_platform
1210
from swsssdk import ConfigDBConnector
1311
from natsort import natsorted
1412
from minigraph import parse_device_desc_xml
@@ -929,9 +927,7 @@ def asymmetric(ctx, status):
929927
@config.group()
930928
def platform():
931929
"""Platform-related configuration tasks"""
932-
933-
if (sonic_platform.get_sonic_version_info()['asic_type'] == 'mellanox'):
934-
platform.add_command(mlnx.mlnx)
930+
platform.add_command(mlnx.mlnx)
935931

936932

937933
#

config/mlnx.py

Lines changed: 97 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
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+
2635
# SDK sniffer env variable
2736
ENV_VARIABLE_SX_SNIFFER = 'SX_SNIFFER_ENABLE'
2837
ENV_VARIABLE_SX_SNIFFER_TARGET = 'SX_SNIFFER_TARGET'
@@ -85,6 +94,28 @@ def run_command(command, display_cmd=False, ignore_error=False):
8594
sys.exit(proc.returncode)
8695

8796

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+
88119
# generate sniffer target file name include a time stamp.
89120
def sniffer_filename_generate(path, filename_prefix, filename_ext):
90121
time_stamp = time.strftime("%Y%m%d%H%M%S")
@@ -129,7 +160,7 @@ def conf_file_copy(src, dest):
129160

130161

131162
def conf_file_receive():
132-
command = "docker exec {} bash -c 'touch {}'".format(CONTAINER_NAME, SNIFFER_CONF_FILE)
163+
command = 'docker exec -ti ' + CONTAINER_NAME + ' bash -c "touch ' + SNIFFER_CONF_FILE + '"'
133164
run_command(command)
134165
conf_file_copy(SNIFFER_CONF_FILE_IN_CONTAINER, TMP_SNIFFER_CONF_FILE)
135166

@@ -145,21 +176,21 @@ def sniffer_env_variable_set(enable, env_variable_name, env_variable_string=""):
145176
env_variable_exist_string = env_variable_read(env_variable_name)
146177
if env_variable_exist_string:
147178
if enable is True:
148-
print "sniffer is already enabled, do nothing"
179+
print "sniffer is already running, do nothing"
149180
ignore = True
150181
else:
151182
env_variable_delete(env_variable_exist_string)
152183
else:
153184
if enable is True:
154185
env_variable_write(env_variable_string)
155186
else:
156-
print "sniffer is already disabled, do nothing"
187+
print "sniffer is already turned off, do nothing"
157188
ignore = True
158189

159190
if not ignore:
160191
config_file_send()
161192

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

165196
return ignore
@@ -186,30 +217,32 @@ def _abort_if_false(ctx, param, value):
186217
# 'mlnx' group
187218
@click.group()
188219
def mlnx():
189-
""" Mellanox platform configuration tasks """
190-
pass
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)
191226

192227

193228
# 'sniffer' group
194229
@mlnx.group()
195230
def sniffer():
196-
""" Utility for managing Mellanox SDK/PRM sniffer """
231+
"""sniffer - Utility for managing Mellanox SDK/PRM sniffer"""
197232
pass
198233

199234

200235
# 'sdk' subgroup
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):
236+
@sniffer.group()
237+
def sdk():
206238
"""SDK Sniffer - Command Line to enable/disable SDK sniffer"""
207-
if option == 'enable':
208-
sdk_sniffer_enable()
209-
elif option == 'disable':
210-
sdk_sniffer_disable()
239+
pass
211240

212241

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?')
213246
def sdk_sniffer_enable():
214247
"""Enable SDK Sniffer"""
215248
print "Enabling SDK sniffer"
@@ -231,11 +264,15 @@ def sdk_sniffer_enable():
231264
err = restart_swss()
232265
if err is not 0:
233266
return
234-
print 'Enabled SDK sniffer, recording file is %s' % sdk_sniffer_filename
267+
print 'SDK sniffer is enabled, recording file is %s.' % sdk_sniffer_filename
235268
else:
236269
pass
237270

238271

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?')
239276
def sdk_sniffer_disable():
240277
"""Disable SDK Sniffer"""
241278
print "Disabling SDK sniffer"
@@ -245,23 +282,54 @@ def sdk_sniffer_disable():
245282
err = restart_swss()
246283
if err is not 0:
247284
return
248-
print "Disabled SDK sniffer"
285+
print "SDK sniffer is disabled"
249286
else:
250287
pass
251288

252289

253290
# place holders for 'sniff prm enable/disable' and 'sniffer all enable/disable'
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
291+
'''
292+
@cli.group()
293+
def prm():
294+
"""PRM Sniffer - Command Line to enable/disable PRM sniffer"""
295+
pass
264296
265297
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
326+
327+
@cli.group()
328+
def status():
329+
"""Sniffer running status - Command Line to show sniffer running status"""
330+
pass
331+
332+
'''
333+
266334
if __name__ == '__main__':
267-
sniffer()
335+
mlnx()

show/main.py

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

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

880879
# 'summary' subcommand ("show platform summary")
881880
@platform.command()

show/mlnx.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,55 +20,59 @@
2020

2121

2222
# run command
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'))
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'))
2826

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

32-
if len(out) > 0:
33-
click.echo(out)
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'))
3435

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

3840

3941
# 'mlnx' group
4042
@click.group()
4143
def mlnx():
42-
""" Show Mellanox platform information """
44+
"""Mellanox platform specific configuration tasks"""
4345
pass
4446

4547

4648
# get current status of sniffer from conf file
4749
def sniffer_status_get(env_variable_name):
4850
enabled = False
49-
command = "docker exec {} bash -c 'touch {}'".format(CONTAINER_NAME, SNIFFER_CONF_FILE)
51+
52+
command = 'docker exec -ti ' + CONTAINER_NAME + ' bash -c "touch ' + SNIFFER_CONF_FILE + '"'
5053
run_command(command)
51-
command = 'docker cp {} {}'.format(SNIFFER_CONF_FILE_IN_CONTAINER, TMP_SNIFFER_CONF_FILE)
54+
command = 'docker cp ' + SNIFFER_CONF_FILE_IN_CONTAINER + ' ' + TMP_SNIFFER_CONF_FILE
5255
run_command(command)
5356
conf_file = open(TMP_SNIFFER_CONF_FILE, 'r')
5457
for env_variable_string in conf_file:
5558
if env_variable_string.find(env_variable_name) >= 0:
5659
enabled = True
5760
break
5861
conf_file.close()
59-
command = 'rm -rf {}'.format(TMP_SNIFFER_CONF_FILE)
62+
command = 'rm -rf ' + TMP_SNIFFER_CONF_FILE
6063
run_command(command)
64+
6165
return enabled
6266

6367

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

0 commit comments

Comments
 (0)