diff --git a/scripts/watermarkstat b/scripts/watermarkstat index 70ea853bc4..2a296c1e95 100755 --- a/scripts/watermarkstat +++ b/scripts/watermarkstat @@ -81,13 +81,13 @@ class WatermarkstatWrapper(object): self.db = None @multi_asic_util.run_on_multi_asic - def run(self, clear, persistent, wm_type): + def run(self, clear, persistent, wm_type, json_output): watermarkstat = Watermarkstat(self.db, self.multi_asic.current_namespace) if clear: watermarkstat.send_clear_notification(("PERSISTENT" if persistent else "USER", wm_type.upper())) else: table_prefix = PERSISTENT_TABLE_PREFIX if persistent else USER_TABLE_PREFIX - watermarkstat.print_all_stat(table_prefix, wm_type) + watermarkstat.print_all_stat(table_prefix, wm_type, json_output) class Watermarkstat(object): @@ -283,8 +283,9 @@ class Watermarkstat(object): fields[pos] = str(int(counter_data)) return fields - def print_all_stat(self, table_prefix, key): + def print_all_stat(self, table_prefix, key, json_output): table = [] + json_result = [] type = self.watermark_types[key] if key in ['buffer_pool', 'headroom_pool']: self.header_list = type['header'] @@ -298,6 +299,7 @@ class Watermarkstat(object): if data is None: data = STATUS_NA table.append((buf_pool, data)) + json_result.append({buf_pool:data}) else: self.build_header(type, key) # Get stat for each port @@ -309,10 +311,14 @@ class Watermarkstat(object): row_data.append(port) row_data.extend(data) table.append(tuple(row_data)) - + json_result.append(dict(zip(self.header_list, [port]+data))) + namespace_str = f" (Namespace {self.namespace})" if multi_asic.is_multi_asic() else '' print(type["message"] + namespace_str) - print(tabulate(table, self.header_list, tablefmt='simple', stralign='right')) + if json_output: + print(json.dumps(json_result, indent=4)) + else: + print(tabulate(table, self.header_list, tablefmt='simple', stralign='right')) def send_clear_notification(self, data): msg = json.dumps(data, separators=(',', ':')) @@ -324,8 +330,9 @@ class Watermarkstat(object): @click.option('-p', '--persistent', is_flag=True, help='Do the operations on the persistent watermark') @click.option('-t', '--type', 'wm_type', type=click.Choice(['pg_headroom', 'pg_shared', 'q_shared_uni', 'q_shared_multi', 'buffer_pool', 'headroom_pool', 'q_shared_all']), help='The type of watermark', required=True) @click.option('-n', '--namespace', type=click.Choice(multi_asic.get_namespace_list()), help='Namespace name or skip for all', default=None) +@click.option('--json','-j','json_output', is_flag=True, default=False, show_default=True, help="Display output in JSON format") @click.version_option(version='1.0') -def main(clear, persistent, wm_type, namespace): +def main(clear, persistent, wm_type, namespace, json_output=False): """ Display the watermark counters @@ -347,7 +354,7 @@ def main(clear, persistent, wm_type, namespace): """ namespace_context = WatermarkstatWrapper(namespace) - namespace_context.run(clear, persistent, wm_type) + namespace_context.run(clear, persistent, wm_type, json_output) sys.exit(0) if __name__ == "__main__": diff --git a/show/main.py b/show/main.py index 9c0aa1b3af..aa0b2b05dc 100755 --- a/show/main.py +++ b/show/main.py @@ -835,11 +835,14 @@ def watermark(): show_default=True, help='Namespace name or all', callback=multi_asic_util.multi_asic_namespace_validation_callback) -def wm_q_uni(namespace): +@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output") +def wm_q_uni(namespace, json_output): """Show user WM for unicast queues""" command = ['watermarkstat', '-t', 'q_shared_uni'] if namespace is not None: command += ['-n', str(namespace)] + if json_output: + command += ["-j"] run_command(command) # 'multicast' subcommand ("show queue watermarks multicast") @@ -852,11 +855,14 @@ def wm_q_uni(namespace): show_default=True, help='Namespace name or all', callback=multi_asic_util.multi_asic_namespace_validation_callback) -def wm_q_multi(namespace): +@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output") +def wm_q_multi(namespace, json_output): """Show user WM for multicast queues""" command = ['watermarkstat', '-t', 'q_shared_multi'] if namespace is not None: command += ['-n', str(namespace)] + if json_output: + command += ["-j"] run_command(command) # 'all' subcommand ("show queue watermarks all") @@ -869,11 +875,14 @@ def wm_q_multi(namespace): show_default=True, help='Namespace name or all', callback=multi_asic_util.multi_asic_namespace_validation_callback) -def wm_q_all(namespace): +@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output") +def wm_q_all(namespace, json_output): """Show user WM for all queues""" command = ['watermarkstat', '-t', 'q_shared_all'] if namespace is not None: command += ['-n', str(namespace)] + if json_output: + command += ["-j"] run_command(command) # @@ -895,11 +904,14 @@ def persistent_watermark(): show_default=True, help='Namespace name or all', callback=multi_asic_util.multi_asic_namespace_validation_callback) -def pwm_q_uni(namespace): +@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output") +def pwm_q_uni(namespace, json_output): """Show persistent WM for unicast queues""" command = ['watermarkstat', '-p', '-t', 'q_shared_uni'] if namespace is not None: command += ['-n', str(namespace)] + if json_output: + command += ["-j"] run_command(command) # 'multicast' subcommand ("show queue persistent-watermarks multicast") @@ -912,11 +924,14 @@ def pwm_q_uni(namespace): show_default=True, help='Namespace name or all', callback=multi_asic_util.multi_asic_namespace_validation_callback) -def pwm_q_multi(namespace): +@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output") +def pwm_q_multi(namespace, json_output): """Show persistent WM for multicast queues""" command = ['watermarkstat', '-p', '-t', 'q_shared_multi'] if namespace is not None: command += ['-n', str(namespace)] + if json_output: + command += ["-j"] run_command(command) # 'all' subcommand ("show queue persistent-watermarks all") @@ -929,11 +944,14 @@ def pwm_q_multi(namespace): show_default=True, help='Namespace name or all', callback=multi_asic_util.multi_asic_namespace_validation_callback) -def pwm_q_all(namespace): +@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output") +def pwm_q_all(namespace, json_output): """Show persistent WM for all queues""" command = ['watermarkstat', '-p', '-t', 'q_shared_all'] if namespace is not None: command += ['-n', str(namespace)] + if json_output: + command += ["-j"] run_command(command) #