23
23
24
24
SNIFFER_SYSLOG_IDENTIFIER = "sniffer"
25
25
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
+
26
35
# SDK sniffer env variable
27
36
ENV_VARIABLE_SX_SNIFFER = 'SX_SNIFFER_ENABLE'
28
37
ENV_VARIABLE_SX_SNIFFER_TARGET = 'SX_SNIFFER_TARGET'
@@ -85,6 +94,28 @@ def run_command(command, display_cmd=False, ignore_error=False):
85
94
sys .exit (proc .returncode )
86
95
87
96
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
+
88
119
# generate sniffer target file name include a time stamp.
89
120
def sniffer_filename_generate (path , filename_prefix , filename_ext ):
90
121
time_stamp = time .strftime ("%Y%m%d%H%M%S" )
@@ -129,7 +160,7 @@ def conf_file_copy(src, dest):
129
160
130
161
131
162
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 + '"'
133
164
run_command (command )
134
165
conf_file_copy (SNIFFER_CONF_FILE_IN_CONTAINER , TMP_SNIFFER_CONF_FILE )
135
166
@@ -145,21 +176,21 @@ def sniffer_env_variable_set(enable, env_variable_name, env_variable_string=""):
145
176
env_variable_exist_string = env_variable_read (env_variable_name )
146
177
if env_variable_exist_string :
147
178
if enable is True :
148
- print "sniffer is already enabled , do nothing"
179
+ print "sniffer is already running , do nothing"
149
180
ignore = True
150
181
else :
151
182
env_variable_delete (env_variable_exist_string )
152
183
else :
153
184
if enable is True :
154
185
env_variable_write (env_variable_string )
155
186
else :
156
- print "sniffer is already disabled , do nothing"
187
+ print "sniffer is already turned off , do nothing"
157
188
ignore = True
158
189
159
190
if not ignore :
160
191
config_file_send ()
161
192
162
- command = 'rm -rf {}' . format ( TMP_SNIFFER_CONF_FILE )
193
+ command = 'rm -rf ' + TMP_SNIFFER_CONF_FILE
163
194
run_command (command )
164
195
165
196
return ignore
@@ -186,30 +217,32 @@ def _abort_if_false(ctx, param, value):
186
217
# 'mlnx' group
187
218
@click .group ()
188
219
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 )
191
226
192
227
193
228
# 'sniffer' group
194
229
@mlnx .group ()
195
230
def sniffer ():
196
- """ Utility for managing Mellanox SDK/PRM sniffer """
231
+ """sniffer - Utility for managing Mellanox SDK/PRM sniffer"""
197
232
pass
198
233
199
234
200
235
# '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 ():
206
238
"""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
211
240
212
241
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?' )
213
246
def sdk_sniffer_enable ():
214
247
"""Enable SDK Sniffer"""
215
248
print "Enabling SDK sniffer"
@@ -231,11 +264,15 @@ def sdk_sniffer_enable():
231
264
err = restart_swss ()
232
265
if err is not 0 :
233
266
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
235
268
else :
236
269
pass
237
270
238
271
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?' )
239
276
def sdk_sniffer_disable ():
240
277
"""Disable SDK Sniffer"""
241
278
print "Disabling SDK sniffer"
@@ -245,23 +282,54 @@ def sdk_sniffer_disable():
245
282
err = restart_swss ()
246
283
if err is not 0 :
247
284
return
248
- print "Disabled SDK sniffer"
285
+ print "SDK sniffer is disabled "
249
286
else :
250
287
pass
251
288
252
289
253
290
# 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
264
296
265
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
326
+
327
+ @cli.group()
328
+ def status():
329
+ """Sniffer running status - Command Line to show sniffer running status"""
330
+ pass
331
+
332
+ '''
333
+
266
334
if __name__ == '__main__' :
267
- sniffer ()
335
+ mlnx ()
0 commit comments