-
Notifications
You must be signed in to change notification settings - Fork 661
/
main.py
438 lines (383 loc) · 14.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import click
import json
from swsscommon.swsscommon import ConfigDBConnector
from tabulate import tabulate
BUFFER_POOL_WATERMARK = "BUFFER_POOL_WATERMARK"
PORT_BUFFER_DROP = "PORT_BUFFER_DROP"
PG_DROP = "PG_DROP"
ACL = "ACL"
DISABLE = "disable"
ENABLE = "enable"
DEFLT_60_SEC= "default (60000)"
DEFLT_10_SEC= "default (10000)"
DEFLT_1_SEC = "default (1000)"
@click.group()
def cli():
""" SONiC Static Counter Poll configurations """
# Queue counter commands
@cli.group()
def queue():
""" Queue counter commands """
@queue.command()
@click.argument('poll_interval', type=click.IntRange(100, 30000))
def interval(poll_interval):
""" Set queue counter query interval """
configdb = ConfigDBConnector()
configdb.connect()
queue_info = {}
if poll_interval is not None:
queue_info['POLL_INTERVAL'] = poll_interval
configdb.mod_entry("FLEX_COUNTER_TABLE", "QUEUE", queue_info)
@queue.command()
def enable():
""" Enable queue counter query """
configdb = ConfigDBConnector()
configdb.connect()
queue_info = {}
queue_info['FLEX_COUNTER_STATUS'] = 'enable'
configdb.mod_entry("FLEX_COUNTER_TABLE", "QUEUE", queue_info)
@queue.command()
def disable():
""" Disable queue counter query """
configdb = ConfigDBConnector()
configdb.connect()
queue_info = {}
queue_info['FLEX_COUNTER_STATUS'] = 'disable'
configdb.mod_entry("FLEX_COUNTER_TABLE", "QUEUE", queue_info)
# Port counter commands
@cli.group()
def port():
""" Port counter commands """
@port.command()
@click.argument('poll_interval', type=click.IntRange(100, 30000))
def interval(poll_interval):
""" Set port counter query interval """
configdb = ConfigDBConnector()
configdb.connect()
port_info = {}
if poll_interval is not None:
port_info['POLL_INTERVAL'] = poll_interval
configdb.mod_entry("FLEX_COUNTER_TABLE", "PORT", port_info)
@port.command()
def enable():
""" Enable port counter query """
configdb = ConfigDBConnector()
configdb.connect()
port_info = {}
port_info['FLEX_COUNTER_STATUS'] = 'enable'
configdb.mod_entry("FLEX_COUNTER_TABLE", "PORT", port_info)
@port.command()
def disable():
""" Disable port counter query """
configdb = ConfigDBConnector()
configdb.connect()
port_info = {}
port_info['FLEX_COUNTER_STATUS'] = 'disable'
configdb.mod_entry("FLEX_COUNTER_TABLE", "PORT", port_info)
# Port buffer drop counter commands
@cli.group()
def port_buffer_drop():
""" Port buffer drop counter commands """
@port_buffer_drop.command()
@click.argument('poll_interval', type=click.IntRange(30000, 300000))
def interval(poll_interval):
"""
Set port_buffer_drop counter query interval
This counter group causes high CPU usage when polled,
hence the allowed interval is between 30s and 300s.
This is a short term solution and
should be changed once the performance is enhanced
"""
configdb = ConfigDBConnector()
configdb.connect()
port_info = {}
if poll_interval:
port_info['POLL_INTERVAL'] = poll_interval
configdb.mod_entry("FLEX_COUNTER_TABLE", PORT_BUFFER_DROP, port_info)
@port_buffer_drop.command()
def enable():
""" Enable port counter query """
configdb = ConfigDBConnector()
configdb.connect()
port_info = {}
port_info['FLEX_COUNTER_STATUS'] = ENABLE
configdb.mod_entry("FLEX_COUNTER_TABLE", PORT_BUFFER_DROP, port_info)
@port_buffer_drop.command()
def disable():
""" Disable port counter query """
configdb = ConfigDBConnector()
configdb.connect()
port_info = {}
port_info['FLEX_COUNTER_STATUS'] = DISABLE
configdb.mod_entry("FLEX_COUNTER_TABLE", PORT_BUFFER_DROP, port_info)
# Ingress PG drop packet stat
@cli.group()
@click.pass_context
def pg_drop(ctx):
""" Ingress PG drop counter commands """
ctx.obj = ConfigDBConnector()
ctx.obj.connect()
@pg_drop.command()
@click.argument('poll_interval', type=click.IntRange(1000, 30000))
@click.pass_context
def interval(ctx, poll_interval):
"""
Set pg_drop packets counter query interval
interval is between 1s and 30s.
"""
port_info = {}
port_info['POLL_INTERVAL'] = poll_interval
ctx.obj.mod_entry("FLEX_COUNTER_TABLE", PG_DROP, port_info)
@pg_drop.command()
@click.pass_context
def enable(ctx):
""" Enable pg_drop counter query """
port_info = {}
port_info['FLEX_COUNTER_STATUS'] = ENABLE
ctx.obj.mod_entry("FLEX_COUNTER_TABLE", PG_DROP, port_info)
@pg_drop.command()
@click.pass_context
def disable(ctx):
""" Disable pg_drop counter query """
port_info = {}
port_info['FLEX_COUNTER_STATUS'] = DISABLE
ctx.obj.mod_entry("FLEX_COUNTER_TABLE", PG_DROP, port_info)
# RIF counter commands
@cli.group()
def rif():
""" RIF counter commands """
@rif.command()
@click.argument('poll_interval')
def interval(poll_interval):
""" Set rif counter query interval """
configdb = ConfigDBConnector()
configdb.connect()
rif_info = {}
if poll_interval is not None:
rif_info['POLL_INTERVAL'] = poll_interval
configdb.mod_entry("FLEX_COUNTER_TABLE", "RIF", rif_info)
@rif.command()
def enable():
""" Enable rif counter query """
configdb = ConfigDBConnector()
configdb.connect()
rif_info = {}
rif_info['FLEX_COUNTER_STATUS'] = 'enable'
configdb.mod_entry("FLEX_COUNTER_TABLE", "RIF", rif_info)
@rif.command()
def disable():
""" Disable rif counter query """
configdb = ConfigDBConnector()
configdb.connect()
rif_info = {}
rif_info['FLEX_COUNTER_STATUS'] = 'disable'
configdb.mod_entry("FLEX_COUNTER_TABLE", "RIF", rif_info)
# Watermark counter commands
@cli.group()
def watermark():
""" Watermark counter commands """
@watermark.command()
@click.argument('poll_interval', type=click.IntRange(1000, 60000))
def interval(poll_interval):
""" Set watermark counter query interval for both queue and PG watermarks """
configdb = ConfigDBConnector()
configdb.connect()
queue_wm_info = {}
pg_wm_info = {}
buffer_pool_wm_info = {}
if poll_interval is not None:
queue_wm_info['POLL_INTERVAL'] = poll_interval
pg_wm_info['POLL_INTERVAL'] = poll_interval
buffer_pool_wm_info['POLL_INTERVAL'] = poll_interval
configdb.mod_entry("FLEX_COUNTER_TABLE", "QUEUE_WATERMARK", queue_wm_info)
configdb.mod_entry("FLEX_COUNTER_TABLE", "PG_WATERMARK", pg_wm_info)
configdb.mod_entry("FLEX_COUNTER_TABLE", BUFFER_POOL_WATERMARK, buffer_pool_wm_info)
@watermark.command()
def enable():
""" Enable watermark counter query """
configdb = ConfigDBConnector()
configdb.connect()
fc_info = {}
fc_info['FLEX_COUNTER_STATUS'] = 'enable'
configdb.mod_entry("FLEX_COUNTER_TABLE", "QUEUE_WATERMARK", fc_info)
configdb.mod_entry("FLEX_COUNTER_TABLE", "PG_WATERMARK", fc_info)
configdb.mod_entry("FLEX_COUNTER_TABLE", BUFFER_POOL_WATERMARK, fc_info)
@watermark.command()
def disable():
""" Disable watermark counter query """
configdb = ConfigDBConnector()
configdb.connect()
fc_info = {}
fc_info['FLEX_COUNTER_STATUS'] = 'disable'
configdb.mod_entry("FLEX_COUNTER_TABLE", "QUEUE_WATERMARK", fc_info)
configdb.mod_entry("FLEX_COUNTER_TABLE", "PG_WATERMARK", fc_info)
configdb.mod_entry("FLEX_COUNTER_TABLE", BUFFER_POOL_WATERMARK, fc_info)
# ACL counter commands
@cli.group()
@click.pass_context
def acl(ctx):
""" ACL counter commands """
ctx.obj = ConfigDBConnector()
ctx.obj.connect()
@acl.command()
@click.argument('poll_interval', type=click.IntRange(1000, 30000))
@click.pass_context
def interval(ctx, poll_interval):
"""
Set ACL counters query interval
interval is between 1s and 30s.
"""
fc_group_cfg = {}
fc_group_cfg['POLL_INTERVAL'] = poll_interval
ctx.obj.mod_entry("FLEX_COUNTER_TABLE", ACL, fc_group_cfg)
@acl.command()
@click.pass_context
def enable(ctx):
""" Enable ACL counter query """
fc_group_cfg = {}
fc_group_cfg['FLEX_COUNTER_STATUS'] = ENABLE
ctx.obj.mod_entry("FLEX_COUNTER_TABLE", ACL, fc_group_cfg)
@acl.command()
@click.pass_context
def disable(ctx):
""" Disable ACL counter query """
fc_group_cfg = {}
fc_group_cfg['FLEX_COUNTER_STATUS'] = DISABLE
ctx.obj.mod_entry("FLEX_COUNTER_TABLE", ACL, fc_group_cfg)
# Tunnel counter commands
@cli.group()
def tunnel():
""" Tunnel counter commands """
@tunnel.command()
@click.argument('poll_interval', type=click.IntRange(100, 30000))
def interval(poll_interval):
""" Set tunnel counter query interval """
configdb = ConfigDBConnector()
configdb.connect()
tunnel_info = {}
tunnel_info['POLL_INTERVAL'] = poll_interval
configdb.mod_entry("FLEX_COUNTER_TABLE", "TUNNEL", tunnel_info)
@tunnel.command()
def enable():
""" Enable tunnel counter query """
configdb = ConfigDBConnector()
configdb.connect()
tunnel_info = {}
tunnel_info['FLEX_COUNTER_STATUS'] = ENABLE
configdb.mod_entry("FLEX_COUNTER_TABLE", "TUNNEL", tunnel_info)
@tunnel.command()
def disable():
""" Disable tunnel counter query """
configdb = ConfigDBConnector()
configdb.connect()
tunnel_info = {}
tunnel_info['FLEX_COUNTER_STATUS'] = DISABLE
configdb.mod_entry("FLEX_COUNTER_TABLE", "TUNNEL", tunnel_info)
# Trap flow counter commands
@cli.group()
@click.pass_context
def flowcnt_trap(ctx):
""" Trap flow counter commands """
ctx.obj = ConfigDBConnector()
ctx.obj.connect()
@flowcnt_trap.command()
@click.argument('poll_interval', type=click.IntRange(1000, 30000))
@click.pass_context
def interval(ctx, poll_interval):
""" Set trap flow counter query interval """
fc_info = {}
fc_info['POLL_INTERVAL'] = poll_interval
ctx.obj.mod_entry("FLEX_COUNTER_TABLE", "FLOW_CNT_TRAP", fc_info)
@flowcnt_trap.command()
@click.pass_context
def enable(ctx):
""" Enable trap flow counter query """
fc_info = {}
fc_info['FLEX_COUNTER_STATUS'] = 'enable'
ctx.obj.mod_entry("FLEX_COUNTER_TABLE", "FLOW_CNT_TRAP", fc_info)
@flowcnt_trap.command()
@click.pass_context
def disable(ctx):
""" Disable trap flow counter query """
fc_info = {}
fc_info['FLEX_COUNTER_STATUS'] = 'disable'
ctx.obj.mod_entry("FLEX_COUNTER_TABLE", "FLOW_CNT_TRAP", fc_info)
@cli.command()
def show():
""" Show the counter configuration """
configdb = ConfigDBConnector()
configdb.connect()
queue_info = configdb.get_entry('FLEX_COUNTER_TABLE', 'QUEUE')
port_info = configdb.get_entry('FLEX_COUNTER_TABLE', 'PORT')
port_drop_info = configdb.get_entry('FLEX_COUNTER_TABLE', PORT_BUFFER_DROP)
rif_info = configdb.get_entry('FLEX_COUNTER_TABLE', 'RIF')
queue_wm_info = configdb.get_entry('FLEX_COUNTER_TABLE', 'QUEUE_WATERMARK')
pg_wm_info = configdb.get_entry('FLEX_COUNTER_TABLE', 'PG_WATERMARK')
pg_drop_info = configdb.get_entry('FLEX_COUNTER_TABLE', PG_DROP)
buffer_pool_wm_info = configdb.get_entry('FLEX_COUNTER_TABLE', BUFFER_POOL_WATERMARK)
acl_info = configdb.get_entry('FLEX_COUNTER_TABLE', ACL)
tunnel_info = configdb.get_entry('FLEX_COUNTER_TABLE', 'TUNNEL')
trap_info = configdb.get_entry('FLEX_COUNTER_TABLE', 'FLOW_CNT_TRAP')
header = ("Type", "Interval (in ms)", "Status")
data = []
if queue_info:
data.append(["QUEUE_STAT", queue_info.get("POLL_INTERVAL", DEFLT_10_SEC), queue_info.get("FLEX_COUNTER_STATUS", DISABLE)])
if port_info:
data.append(["PORT_STAT", port_info.get("POLL_INTERVAL", DEFLT_1_SEC), port_info.get("FLEX_COUNTER_STATUS", DISABLE)])
if port_drop_info:
data.append([PORT_BUFFER_DROP, port_drop_info.get("POLL_INTERVAL", DEFLT_60_SEC), port_drop_info.get("FLEX_COUNTER_STATUS", DISABLE)])
if rif_info:
data.append(["RIF_STAT", rif_info.get("POLL_INTERVAL", DEFLT_1_SEC), rif_info.get("FLEX_COUNTER_STATUS", DISABLE)])
if queue_wm_info:
data.append(["QUEUE_WATERMARK_STAT", queue_wm_info.get("POLL_INTERVAL", DEFLT_60_SEC), queue_wm_info.get("FLEX_COUNTER_STATUS", DISABLE)])
if pg_wm_info:
data.append(["PG_WATERMARK_STAT", pg_wm_info.get("POLL_INTERVAL", DEFLT_60_SEC), pg_wm_info.get("FLEX_COUNTER_STATUS", DISABLE)])
if pg_drop_info:
data.append(['PG_DROP_STAT', pg_drop_info.get("POLL_INTERVAL", DEFLT_10_SEC), pg_drop_info.get("FLEX_COUNTER_STATUS", DISABLE)])
if buffer_pool_wm_info:
data.append(["BUFFER_POOL_WATERMARK_STAT", buffer_pool_wm_info.get("POLL_INTERVAL", DEFLT_60_SEC), buffer_pool_wm_info.get("FLEX_COUNTER_STATUS", DISABLE)])
if acl_info:
data.append([ACL, pg_drop_info.get("POLL_INTERVAL", DEFLT_10_SEC), acl_info.get("FLEX_COUNTER_STATUS", DISABLE)])
if tunnel_info:
data.append(["TUNNEL_STAT", rif_info.get("POLL_INTERVAL", DEFLT_10_SEC), rif_info.get("FLEX_COUNTER_STATUS", DISABLE)])
if trap_info:
data.append(["FLOW_CNT_TRAP_STAT", trap_info.get("POLL_INTERVAL", DEFLT_10_SEC), trap_info.get("FLEX_COUNTER_STATUS", DISABLE)])
click.echo(tabulate(data, headers=header, tablefmt="simple", missingval=""))
def _update_config_db_flex_counter_table(status, filename):
""" Update counter configuration in config_db file """
with open(filename) as config_db_file:
config_db = json.load(config_db_file)
write_config_db = False
if "FLEX_COUNTER_TABLE" in config_db:
if status != "delay":
for counter, counter_config in config_db["FLEX_COUNTER_TABLE"].items():
if "FLEX_COUNTER_STATUS" in counter_config and \
counter_config["FLEX_COUNTER_STATUS"] is not status:
counter_config["FLEX_COUNTER_STATUS"] = status
write_config_db = True
elif status == "delay":
write_config_db = True
for key in config_db["FLEX_COUNTER_TABLE"].keys():
config_db["FLEX_COUNTER_TABLE"][key].update({"FLEX_COUNTER_DELAY_STATUS":"true"})
if write_config_db:
with open(filename, 'w') as config_db_file:
json.dump(config_db, config_db_file, indent=4)
# Working on Config DB
@cli.group()
def config_db():
""" Config DB counter commands """
@config_db.command()
@click.argument("filename", default="/etc/sonic/config_db.json", type=click.Path(exists=True))
def enable(filename):
""" Enable counter configuration in config_db file """
_update_config_db_flex_counter_table("enable", filename)
@config_db.command()
@click.argument("filename", default="/etc/sonic/config_db.json", type=click.Path(exists=True))
def disable(filename):
""" Disable counter configuration in config_db file """
_update_config_db_flex_counter_table("disable", filename)
@config_db.command()
@click.argument("filename", default="/etc/sonic/config_db.json", type=click.Path(exists=True))
def delay(filename):
""" Delay counters in config_db file """
_update_config_db_flex_counter_table("delay", filename)