From 2d95529dce9ef3f23b859ec09135c40d87d4f4d5 Mon Sep 17 00:00:00 2001 From: Neetha John Date: Thu, 16 Mar 2023 17:31:49 -0700 Subject: [PATCH 1/4] Revert "Update load minigraph to load backend acl (#2236)" (#2735) This reverts commit 1518ca92df1e794222bf45100246c8ef956d7af6. --- config/main.py | 43 ++----------------------------------------- tests/config_test.py | 43 ------------------------------------------- 2 files changed, 2 insertions(+), 84 deletions(-) diff --git a/config/main.py b/config/main.py index d14d392355..44f633bf44 100644 --- a/config/main.py +++ b/config/main.py @@ -1162,41 +1162,6 @@ def validate_gre_type(ctx, _, value): except ValueError: raise click.UsageError("{} is not a valid GRE type".format(value)) -def _is_storage_device(cfg_db): - """ - Check if the device is a storage device or not - """ - device_metadata = cfg_db.get_entry("DEVICE_METADATA", "localhost") - return device_metadata.get("storage_device", "Unknown") == "true" - -def _is_acl_table_present(cfg_db, acl_table_name): - """ - Check if acl table exists - """ - return acl_table_name in cfg_db.get_keys("ACL_TABLE") - -def load_backend_acl(cfg_db, device_type): - """ - Load acl on backend storage device - """ - - BACKEND_ACL_TEMPLATE_FILE = os.path.join('/', "usr", "share", "sonic", "templates", "backend_acl.j2") - BACKEND_ACL_FILE = os.path.join('/', "etc", "sonic", "backend_acl.json") - - if device_type and device_type == "BackEndToRRouter" and _is_storage_device(cfg_db) and _is_acl_table_present(cfg_db, "DATAACL"): - if os.path.isfile(BACKEND_ACL_TEMPLATE_FILE): - clicommon.run_command( - "{} -d -t {},{}".format( - SONIC_CFGGEN_PATH, - BACKEND_ACL_TEMPLATE_FILE, - BACKEND_ACL_FILE - ), - display_cmd=True - ) - if os.path.isfile(BACKEND_ACL_FILE): - clicommon.run_command("acl-loader update incremental {}".format(BACKEND_ACL_FILE), display_cmd=True) - - # This is our main entrypoint - the main 'config' command @click.group(cls=clicommon.AbbreviationGroup, context_settings=CONTEXT_SETTINGS) @click.pass_context @@ -1774,12 +1739,6 @@ def load_minigraph(db, no_service_restart, traffic_shift_away, override_config, if os.path.isfile('/etc/sonic/acl.json'): clicommon.run_command("acl-loader update full /etc/sonic/acl.json", display_cmd=True) - # get the device type - device_type = _get_device_type() - - # Load backend acl - load_backend_acl(db.cfgdb, device_type) - # Load port_config.json try: load_port_config(db.cfgdb, '/etc/sonic/port_config.json') @@ -1789,6 +1748,8 @@ def load_minigraph(db, no_service_restart, traffic_shift_away, override_config, # generate QoS and Buffer configs clicommon.run_command("config qos reload --no-dynamic-buffer --no-delay", display_cmd=True) + # get the device type + device_type = _get_device_type() if device_type != 'MgmtToRRouter' and device_type != 'MgmtTsToR' and device_type != 'BmcMgmtToRRouter' and device_type != 'EPMS': clicommon.run_command("pfcwd start_default", display_cmd=True) diff --git a/tests/config_test.py b/tests/config_test.py index fff66d47e6..4ebc14cd14 100644 --- a/tests/config_test.py +++ b/tests/config_test.py @@ -354,49 +354,6 @@ def test_load_minigraph_with_port_config(self, get_cmd_module, setup_single_broa port_config = [{"PORT": {"Ethernet0": {"admin_status": "up"}}}] self.check_port_config(db, config, port_config, "config interface startup Ethernet0") - def test_load_backend_acl(self, get_cmd_module, setup_single_broadcom_asic): - db = Db() - db.cfgdb.set_entry("DEVICE_METADATA", "localhost", {"storage_device": "true"}) - self.check_backend_acl(get_cmd_module, db, device_type='BackEndToRRouter', condition=True) - - def test_load_backend_acl_not_storage(self, get_cmd_module, setup_single_broadcom_asic): - db = Db() - self.check_backend_acl(get_cmd_module, db, device_type='BackEndToRRouter', condition=False) - - def test_load_backend_acl_storage_leaf(self, get_cmd_module, setup_single_broadcom_asic): - db = Db() - db.cfgdb.set_entry("DEVICE_METADATA", "localhost", {"storage_device": "true"}) - self.check_backend_acl(get_cmd_module, db, device_type='BackEndLeafRouter', condition=False) - - def test_load_backend_acl_storage_no_dataacl(self, get_cmd_module, setup_single_broadcom_asic): - db = Db() - db.cfgdb.set_entry("DEVICE_METADATA", "localhost", {"storage_device": "true"}) - db.cfgdb.set_entry("ACL_TABLE", "DATAACL", None) - self.check_backend_acl(get_cmd_module, db, device_type='BackEndToRRouter', condition=False) - - def check_backend_acl(self, get_cmd_module, db, device_type='BackEndToRRouter', condition=True): - def is_file_side_effect(filename): - return True if 'backend_acl' in filename else False - with mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)): - with mock.patch('config.main._get_device_type', mock.MagicMock(return_value=device_type)): - with mock.patch( - "utilities_common.cli.run_command", - mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command: - (config, show) = get_cmd_module - runner = CliRunner() - result = runner.invoke(config.config.commands["load_minigraph"], ["-y"], obj=db) - print(result.exit_code) - expected_output = ['Running command: acl-loader update incremental /etc/sonic/backend_acl.json', - 'Running command: /usr/local/bin/sonic-cfggen -d -t /usr/share/sonic/templates/backend_acl.j2,/etc/sonic/backend_acl.json' - ] - print(result.output) - assert result.exit_code == 0 - output = result.output.split('\n') - if condition: - assert set(expected_output).issubset(set(output)) - else: - assert not(set(expected_output).issubset(set(output))) - def check_port_config(self, db, config, port_config, expected_output): def read_json_file_side_effect(filename): return port_config From f27dea0cfdefbdcfc03d19136e4ae47ea72fd51f Mon Sep 17 00:00:00 2001 From: Stepan Blyshchak <38952541+stepanblyschak@users.noreply.github.com> Date: Fri, 17 Mar 2023 09:10:47 +0200 Subject: [PATCH 2/4] [route_check] remove check-frr_patch mock (#2732) The test fails with python3.7 (works in 3.9) when stopping patch which hasn't been started. We can always mock check_output call and if FRR_ROUTES is not defined return empty dictionary by the mock. #### What I did Removed check_frr_patch mock to fix UT running on python3.7 #### How I did it Removed the mock #### How to verify it Run unit test in stretch env --- tests/route_check_test.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/tests/route_check_test.py b/tests/route_check_test.py index 4d93c74e2d..118e9eab56 100644 --- a/tests/route_check_test.py +++ b/tests/route_check_test.py @@ -277,17 +277,12 @@ def test_route_check(self, mock_dbs, test_num): with patch('sys.argv', ct_data[ARGS].split()), \ patch('route_check.subprocess.check_output') as mock_check_output: - check_frr_patch = patch('route_check.check_frr_pending_routes', lambda: []) + routes = ct_data.get(FRR_ROUTES, {}) - if FRR_ROUTES in ct_data: - routes = ct_data[FRR_ROUTES] + def side_effect(*args, **kwargs): + return json.dumps(routes) - def side_effect(*args, **kwargs): - return json.dumps(routes) - - mock_check_output.side_effect = side_effect - else: - check_frr_patch.start() + mock_check_output.side_effect = side_effect ret, res = route_check.main() expect_ret = ct_data[RET] if RET in ct_data else 0 @@ -299,8 +294,6 @@ def side_effect(*args, **kwargs): assert ret == expect_ret assert res == expect_res - check_frr_patch.stop() - def test_timeout(self, mock_dbs, force_hang): # Test timeout ex_raised = False From 05fa7513355cf333818c480fade157bdff969811 Mon Sep 17 00:00:00 2001 From: abdosi <58047199+abdosi@users.noreply.github.com> Date: Fri, 17 Mar 2023 16:27:48 -0700 Subject: [PATCH 3/4] Fix the `show interface counters` throwing exception on device with no external interfaces (#2703) Fix the `show interface counters` throwing exception issue where device do not have any external ports and all are internal links (ethernet or fabric) which is possible in chassis --- scripts/portstat | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/scripts/portstat b/scripts/portstat index 09ad88b08d..43746cc1c3 100755 --- a/scripts/portstat +++ b/scripts/portstat @@ -333,13 +333,13 @@ class Portstat(object): format_number_with_comma(data['tx_err']), format_number_with_comma(data['tx_drop']), format_number_with_comma(data['tx_ovr']))) - - if use_json: - print(table_as_json(table, header)) - else: - print(tabulate(table, header, tablefmt='simple', stralign='right')) - if multi_asic.is_multi_asic() or device_info.is_chassis(): - print("\nReminder: Please execute 'show interface counters -d all' to include internal links\n") + if table: + if use_json: + print(table_as_json(table, header)) + else: + print(tabulate(table, header, tablefmt='simple', stralign='right')) + if multi_asic.is_multi_asic() or device_info.is_chassis() and not use_json: + print("\nReminder: Please execute 'show interface counters -d all' to include internal links\n") def cnstat_intf_diff_print(self, cnstat_new_dict, cnstat_old_dict, intf_list): """ @@ -551,13 +551,13 @@ class Portstat(object): format_number_with_comma(cntr['tx_err']), format_number_with_comma(cntr['tx_drop']), format_number_with_comma(cntr['tx_ovr']))) - - if use_json: - print(table_as_json(table, header)) - else: - print(tabulate(table, header, tablefmt='simple', stralign='right')) - if multi_asic.is_multi_asic() or device_info.is_chassis(): - print("\nReminder: Please execute 'show interface counters -d all' to include internal links\n") + if table: + if use_json: + print(table_as_json(table, header)) + else: + print(tabulate(table, header, tablefmt='simple', stralign='right')) + if multi_asic.is_multi_asic() or device_info.is_chassis() and not use_json: + print("\nReminder: Please execute 'show interface counters -d all' to include internal links\n") def main(): parser = argparse.ArgumentParser(description='Display the ports state and counters', From 10f31ea6fb0876f913cfcfce8c95011e675a99f6 Mon Sep 17 00:00:00 2001 From: Mai Bui Date: Tue, 21 Mar 2023 00:25:39 -0400 Subject: [PATCH 4/4] Revert "Replace pickle by json (#2636)" (#2746) This reverts commit 54e26359fccf45d2e40800cf5598a725798634cd. Due to https://github.com/sonic-net/sonic-buildimage/issues/14089 Signed-off-by: Mai Bui --- scripts/dropstat | 14 +-- scripts/flow_counters_stat | 10 +- scripts/intfstat | 64 +++++----- scripts/pfcstat | 62 +++++----- scripts/pg-drop | 8 +- scripts/portstat | 239 +++++++++++++++++++------------------ scripts/queuestat | 34 +++--- scripts/tunnelstat | 40 +++---- 8 files changed, 236 insertions(+), 235 deletions(-) diff --git a/scripts/dropstat b/scripts/dropstat index 4e9f5bb4d0..f98fc29197 100755 --- a/scripts/dropstat +++ b/scripts/dropstat @@ -11,7 +11,7 @@ # - Refactor calls to COUNTERS_DB to reduce redundancy # - Cache DB queries to reduce # of expensive queries -import json +import _pickle as pickle import argparse import os import socket @@ -117,10 +117,10 @@ class DropStat(object): """ try: - json.dump(self.get_counts_table(self.gather_counters(std_port_rx_counters + std_port_tx_counters, DEBUG_COUNTER_PORT_STAT_MAP), COUNTERS_PORT_NAME_MAP), - open(self.port_drop_stats_file, 'w+')) - json.dump(self.get_counts(self.gather_counters([], DEBUG_COUNTER_SWITCH_STAT_MAP), self.get_switch_id()), - open(self.switch_drop_stats_file, 'w+')) + pickle.dump(self.get_counts_table(self.gather_counters(std_port_rx_counters + std_port_tx_counters, DEBUG_COUNTER_PORT_STAT_MAP), COUNTERS_PORT_NAME_MAP), + open(self.port_drop_stats_file, 'wb+')) + pickle.dump(self.get_counts(self.gather_counters([], DEBUG_COUNTER_SWITCH_STAT_MAP), self.get_switch_id()), + open(self.switch_drop_stats_file, 'wb+')) except IOError as e: print(e) sys.exit(e.errno) @@ -135,7 +135,7 @@ class DropStat(object): # Grab the latest clear checkpoint, if it exists if os.path.isfile(self.port_drop_stats_file): - port_drop_ckpt = json.load(open(self.port_drop_stats_file, 'r')) + port_drop_ckpt = pickle.load(open(self.port_drop_stats_file, 'rb')) counters = self.gather_counters(std_port_rx_counters + std_port_tx_counters, DEBUG_COUNTER_PORT_STAT_MAP, group, counter_type) headers = std_port_description_header + self.gather_headers(counters, DEBUG_COUNTER_PORT_STAT_MAP) @@ -162,7 +162,7 @@ class DropStat(object): # Grab the latest clear checkpoint, if it exists if os.path.isfile(self.switch_drop_stats_file): - switch_drop_ckpt = json.load(open(self.switch_drop_stats_file, 'r')) + switch_drop_ckpt = pickle.load(open(self.switch_drop_stats_file, 'rb')) counters = self.gather_counters([], DEBUG_COUNTER_SWITCH_STAT_MAP, group, counter_type) headers = std_switch_description_header + self.gather_headers(counters, DEBUG_COUNTER_SWITCH_STAT_MAP) diff --git a/scripts/flow_counters_stat b/scripts/flow_counters_stat index 49b97e335b..ac5ef94beb 100755 --- a/scripts/flow_counters_stat +++ b/scripts/flow_counters_stat @@ -2,7 +2,7 @@ import argparse import os -import json +import _pickle as pickle import sys from natsort import natsorted @@ -185,8 +185,8 @@ class FlowCounterStats(object): if os.path.exists(self.data_file): os.remove(self.data_file) - with open(self.data_file, 'w') as f: - json.dump(data, f) + with open(self.data_file, 'wb') as f: + pickle.dump(data, f) except IOError as e: print('Failed to save statistic - {}'.format(repr(e))) @@ -200,8 +200,8 @@ class FlowCounterStats(object): return None try: - with open(self.data_file, 'r') as f: - data = json.load(f) + with open(self.data_file, 'rb') as f: + data = pickle.load(f) except IOError as e: print('Failed to load statistic - {}'.format(repr(e))) return None diff --git a/scripts/intfstat b/scripts/intfstat index b4a770adeb..30cfbf084d 100755 --- a/scripts/intfstat +++ b/scripts/intfstat @@ -6,7 +6,7 @@ # ##################################################################### -import json +import _pickle as pickle import argparse import datetime import sys @@ -28,7 +28,7 @@ from collections import namedtuple, OrderedDict from natsort import natsorted from tabulate import tabulate from utilities_common.netstat import ns_diff, table_as_json, STATUS_NA, format_brate, format_prate -from utilities_common.cli import json_serial, UserCache +from utilities_common.cli import UserCache from swsscommon.swsscommon import SonicV2Connector nstat_fields = ( @@ -96,7 +96,7 @@ class Intfstat(object): counter_data = self.db.get(self.db.COUNTERS_DB, full_table_id, counter_name) if counter_data: fields[pos] = str(counter_data) - cntr = NStats._make(fields)._asdict() + cntr = NStats._make(fields) return cntr def get_rates(table_id): @@ -153,14 +153,14 @@ class Intfstat(object): rates = ratestat_dict.get(key, RateStats._make([STATUS_NA] * len(rates_key_list))) table.append((key, - data['rx_p_ok'], + data.rx_p_ok, format_brate(rates.rx_bps), format_prate(rates.rx_pps), - data['rx_p_err'], - data['tx_p_ok'], + data.rx_p_err, + data.tx_p_ok, format_brate(rates.tx_bps), format_prate(rates.tx_pps), - data['tx_p_err'])) + data.tx_p_err)) if use_json: print(table_as_json(table, header)) @@ -186,24 +186,24 @@ class Intfstat(object): if old_cntr is not None: table.append((key, - ns_diff(cntr['rx_p_ok'], old_cntr['rx_p_ok']), + ns_diff(cntr.rx_p_ok, old_cntr.rx_p_ok), format_brate(rates.rx_bps), format_prate(rates.rx_pps), - ns_diff(cntr['rx_p_err'], old_cntr['rx_p_err']), - ns_diff(cntr['tx_p_ok'], old_cntr['tx_p_ok']), + ns_diff(cntr.rx_p_err, old_cntr.rx_p_err), + ns_diff(cntr.tx_p_ok, old_cntr.tx_p_ok), format_brate(rates.tx_bps), format_prate(rates.tx_pps), - ns_diff(cntr['tx_p_err'], old_cntr['tx_p_err']))) + ns_diff(cntr.tx_p_err, old_cntr.tx_p_err))) else: table.append((key, - cntr['rx_p_ok'], + cntr.rx_p_ok, format_brate(rates.rx_bps), format_prate(rates.rx_pps), - cntr['rx_p_err'], - cntr['tx_p_ok'], + cntr.rx_p_err, + cntr.tx_p_ok, format_brate(rates.tx_bps), format_prate(rates.tx_pps), - cntr['tx_p_err'])) + cntr.tx_p_err)) if use_json: print(table_as_json(table, header)) @@ -229,17 +229,17 @@ class Intfstat(object): if cnstat_old_dict and cnstat_old_dict.get(rif): old_cntr = cnstat_old_dict.get(rif) - body = body % (ns_diff(cntr['rx_p_ok'], old_cntr['rx_p_ok']), - ns_diff(cntr['rx_b_ok'], old_cntr['rx_b_ok']), - ns_diff(cntr['rx_p_err'], old_cntr['rx_p_err']), - ns_diff(cntr['rx_b_err'], old_cntr['rx_b_err']), - ns_diff(cntr['tx_p_ok'], old_cntr['tx_p_ok']), - ns_diff(cntr['tx_b_ok'], old_cntr['tx_b_ok']), - ns_diff(cntr['tx_p_err'], old_cntr['tx_p_err']), - ns_diff(cntr['tx_b_err'], old_cntr['tx_b_err'])) + body = body % (ns_diff(cntr.rx_p_ok, old_cntr.rx_p_ok), + ns_diff(cntr.rx_b_ok, old_cntr.rx_b_ok), + ns_diff(cntr.rx_p_err, old_cntr.rx_p_err), + ns_diff(cntr.rx_b_err, old_cntr.rx_b_err), + ns_diff(cntr.tx_p_ok, old_cntr.tx_p_ok), + ns_diff(cntr.tx_b_ok, old_cntr.tx_b_ok), + ns_diff(cntr.tx_p_err, old_cntr.tx_p_err), + ns_diff(cntr.tx_b_err, old_cntr.tx_b_err)) else: - body = body % (cntr['rx_p_ok'], cntr['rx_b_ok'], cntr['rx_p_err'],cntr['rx_b_err'], - cntr['tx_p_ok'], cntr['tx_b_ok'], cntr['tx_p_err'], cntr['tx_b_err']) + body = body % (cntr.rx_p_ok, cntr.rx_b_ok, cntr.rx_p_err,cntr.rx_b_err, + cntr.tx_p_ok, cntr.tx_b_ok, cntr.tx_p_err, cntr.tx_b_err) print(header) print(body) @@ -305,20 +305,20 @@ def main(): if tag_name is not None: if os.path.isfile(cnstat_fqn_general_file): try: - general_data = json.load(open(cnstat_fqn_general_file, 'r')) + general_data = pickle.load(open(cnstat_fqn_general_file, 'rb')) for key, val in cnstat_dict.items(): general_data[key] = val - json.dump(general_data, open(cnstat_fqn_general_file, 'w')) + pickle.dump(general_data, open(cnstat_fqn_general_file, 'wb')) except IOError as e: sys.exit(e.errno) # Add the information also to tag specific file if os.path.isfile(cnstat_fqn_file): - data = json.load(open(cnstat_fqn_file, 'r')) + data = pickle.load(open(cnstat_fqn_file, 'rb')) for key, val in cnstat_dict.items(): data[key] = val - json.dump(data, open(cnstat_fqn_file, 'w')) + pickle.dump(data, open(cnstat_fqn_file, 'wb')) else: - json.dump(cnstat_dict, open(cnstat_fqn_file, 'w'), default=json_serial) + pickle.dump(cnstat_dict, open(cnstat_fqn_file, 'wb')) except IOError as e: sys.exit(e.errno) else: @@ -330,9 +330,9 @@ def main(): try: cnstat_cached_dict = {} if os.path.isfile(cnstat_fqn_file): - cnstat_cached_dict = json.load(open(cnstat_fqn_file, 'r')) + cnstat_cached_dict = pickle.load(open(cnstat_fqn_file, 'rb')) else: - cnstat_cached_dict = json.load(open(cnstat_fqn_general_file, 'r')) + cnstat_cached_dict = pickle.load(open(cnstat_fqn_general_file, 'rb')) print("Last cached time was " + str(cnstat_cached_dict.get('time'))) if interface_name: diff --git a/scripts/pfcstat b/scripts/pfcstat index 094c6e9380..fb7e6018b6 100755 --- a/scripts/pfcstat +++ b/scripts/pfcstat @@ -6,7 +6,7 @@ # ##################################################################### -import json +import _pickle as pickle import argparse import datetime import os.path @@ -37,7 +37,7 @@ except KeyError: from utilities_common.netstat import ns_diff, STATUS_NA, format_number_with_comma from utilities_common import multi_asic as multi_asic_util from utilities_common import constants -from utilities_common.cli import json_serial, UserCache +from utilities_common.cli import UserCache PStats = namedtuple("PStats", "pfc0, pfc1, pfc2, pfc3, pfc4, pfc5, pfc6, pfc7") @@ -101,7 +101,7 @@ class Pfcstat(object): fields[pos] = STATUS_NA else: fields[pos] = str(int(counter_data)) - cntr = PStats._make(fields)._asdict() + cntr = PStats._make(fields) return cntr # Get the info from database @@ -144,14 +144,14 @@ class Pfcstat(object): if key == 'time': continue table.append((key, - format_number_with_comma(data['pfc0']), - format_number_with_comma(data['pfc1']), - format_number_with_comma(data['pfc2']), - format_number_with_comma(data['pfc3']), - format_number_with_comma(data['pfc4']), - format_number_with_comma(data['pfc5']), - format_number_with_comma(data['pfc6']), - format_number_with_comma(data['pfc7']))) + format_number_with_comma(data.pfc0), + format_number_with_comma(data.pfc1), + format_number_with_comma(data.pfc2), + format_number_with_comma(data.pfc3), + format_number_with_comma(data.pfc4), + format_number_with_comma(data.pfc5), + format_number_with_comma(data.pfc6), + format_number_with_comma(data.pfc7))) if rx: print(tabulate(table, header_Rx, tablefmt='simple', stralign='right')) @@ -173,24 +173,24 @@ class Pfcstat(object): if old_cntr is not None: table.append((key, - ns_diff(cntr['pfc0'], old_cntr['pfc0']), - ns_diff(cntr['pfc1'], old_cntr['pfc1']), - ns_diff(cntr['pfc2'], old_cntr['pfc2']), - ns_diff(cntr['pfc3'], old_cntr['pfc3']), - ns_diff(cntr['pfc4'], old_cntr['pfc4']), - ns_diff(cntr['pfc5'], old_cntr['pfc5']), - ns_diff(cntr['pfc6'], old_cntr['pfc6']), - ns_diff(cntr['pfc7'], old_cntr['pfc7']))) + ns_diff(cntr.pfc0, old_cntr.pfc0), + ns_diff(cntr.pfc1, old_cntr.pfc1), + ns_diff(cntr.pfc2, old_cntr.pfc2), + ns_diff(cntr.pfc3, old_cntr.pfc3), + ns_diff(cntr.pfc4, old_cntr.pfc4), + ns_diff(cntr.pfc5, old_cntr.pfc5), + ns_diff(cntr.pfc6, old_cntr.pfc6), + ns_diff(cntr.pfc7, old_cntr.pfc7))) else: table.append((key, - format_number_with_comma(cntr['pfc0']), - format_number_with_comma(cntr['pfc1']), - format_number_with_comma(cntr['pfc2']), - format_number_with_comma(cntr['pfc3']), - format_number_with_comma(cntr['pfc4']), - format_number_with_comma(cntr['pfc5']), - format_number_with_comma(cntr['pfc6']), - format_number_with_comma(cntr['pfc7']))) + format_number_with_comma(cntr.pfc0), + format_number_with_comma(cntr.pfc1), + format_number_with_comma(cntr.pfc2), + format_number_with_comma(cntr.pfc3), + format_number_with_comma(cntr.pfc4), + format_number_with_comma(cntr.pfc5), + format_number_with_comma(cntr.pfc6), + format_number_with_comma(cntr.pfc7))) if rx: print(tabulate(table, header_Rx, tablefmt='simple', stralign='right')) @@ -256,8 +256,8 @@ Examples: if save_fresh_stats: try: - json.dump(cnstat_dict_rx, open(cnstat_fqn_file_rx, 'w'), default=json_serial) - json.dump(cnstat_dict_tx, open(cnstat_fqn_file_tx, 'w'), default=json_serial) + pickle.dump(cnstat_dict_rx, open(cnstat_fqn_file_rx, 'wb')) + pickle.dump(cnstat_dict_tx, open(cnstat_fqn_file_tx, 'wb')) except IOError as e: print(e.errno, e) sys.exit(e.errno) @@ -271,7 +271,7 @@ Examples: """ if os.path.isfile(cnstat_fqn_file_rx): try: - cnstat_cached_dict = json.load(open(cnstat_fqn_file_rx, 'r')) + cnstat_cached_dict = pickle.load(open(cnstat_fqn_file_rx, 'rb')) print("Last cached time was " + str(cnstat_cached_dict.get('time'))) pfcstat.cnstat_diff_print(cnstat_dict_rx, cnstat_cached_dict, True) except IOError as e: @@ -286,7 +286,7 @@ Examples: """ if os.path.isfile(cnstat_fqn_file_tx): try: - cnstat_cached_dict = json.load(open(cnstat_fqn_file_tx, 'r')) + cnstat_cached_dict = pickle.load(open(cnstat_fqn_file_tx, 'rb')) print("Last cached time was " + str(cnstat_cached_dict.get('time'))) pfcstat.cnstat_diff_print(cnstat_dict_tx, cnstat_cached_dict, False) except IOError as e: diff --git a/scripts/pg-drop b/scripts/pg-drop index 7741593081..40b4e863d3 100755 --- a/scripts/pg-drop +++ b/scripts/pg-drop @@ -5,7 +5,7 @@ # pg-drop is a tool for show/clear ingress pg dropped packet stats. # ##################################################################### -import json +import _pickle as pickle import argparse import os import sys @@ -144,7 +144,7 @@ class PgDropStat(object): port_drop_ckpt = {} # Grab the latest clear checkpoint, if it exists if os.path.isfile(self.port_drop_stats_file): - port_drop_ckpt = json.load(open(self.port_drop_stats_file, 'r')) + port_drop_ckpt = pickle.load(open(self.port_drop_stats_file, 'rb')) # Header list contains the port name followed by the PGs. Fields is used to populate the pg values fields = ["0"]* (len(self.header_list) - 1) @@ -216,10 +216,10 @@ class PgDropStat(object): counter_pg_drop_array = [ "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS"] try: - json.dump(self.get_counts_table( + pickle.dump(self.get_counts_table( counter_pg_drop_array, COUNTERS_PG_NAME_MAP), - open(self.port_drop_stats_file, 'w+')) + open(self.port_drop_stats_file, 'wb+')) except IOError as e: print(e) sys.exit(e.errno) diff --git a/scripts/portstat b/scripts/portstat index 43746cc1c3..399733f69c 100755 --- a/scripts/portstat +++ b/scripts/portstat @@ -6,7 +6,7 @@ # ##################################################################### -import json +import _pickle as pickle import argparse import datetime import os.path @@ -40,7 +40,7 @@ from utilities_common.intf_filter import parse_interface_in_filter import utilities_common.multi_asic as multi_asic_util from utilities_common.netstat import ns_diff, table_as_json, format_brate, format_prate, format_util, format_number_with_comma -from utilities_common.cli import json_serial, UserCache +from utilities_common.cli import UserCache """ The order and count of statistics mentioned below needs to be in sync with the values in portstat script @@ -181,7 +181,7 @@ class Portstat(object): elif fields[pos] != STATUS_NA: fields[pos] = str(int(fields[pos]) + int(fvs[counter_name])) - cntr = NStats._make(fields)._asdict() + cntr = NStats._make(fields) return cntr def get_rates(table_id): @@ -278,61 +278,62 @@ class Portstat(object): if print_all: header = header_all table.append((key, self.get_port_state(key), - format_number_with_comma(data['rx_ok']), + format_number_with_comma(data.rx_ok), format_brate(rates.rx_bps), format_prate(rates.rx_pps), format_util(rates.rx_bps, port_speed), - format_number_with_comma(data['rx_err']), - format_number_with_comma(data['rx_drop']), - format_number_with_comma(data['rx_ovr']), - format_number_with_comma(data['tx_ok']), + format_number_with_comma(data.rx_err), + format_number_with_comma(data.rx_drop), + format_number_with_comma(data.rx_ovr), + format_number_with_comma(data.tx_ok), format_brate(rates.tx_bps), format_prate(rates.tx_pps), format_util(rates.tx_bps, port_speed), - format_number_with_comma(data['tx_err']), - format_number_with_comma(data['tx_drop']), - format_number_with_comma(data['tx_ovr']))) + format_number_with_comma(data.tx_err), + format_number_with_comma(data.tx_drop), + format_number_with_comma(data.tx_ovr))) elif errors_only: header = header_errors_only table.append((key, self.get_port_state(key), - format_number_with_comma(data['rx_err']), - format_number_with_comma(data['rx_drop']), - format_number_with_comma(data['rx_ovr']), - format_number_with_comma(data['tx_err']), - format_number_with_comma(data['tx_drop']), - format_number_with_comma(data['tx_ovr']))) + format_number_with_comma(data.rx_err), + format_number_with_comma(data.rx_drop), + format_number_with_comma(data.rx_ovr), + format_number_with_comma(data.tx_err), + format_number_with_comma(data.tx_drop), + format_number_with_comma(data.tx_ovr))) elif fec_stats_only: header = header_fec_only table.append((key, self.get_port_state(key), - format_number_with_comma(data['fec_corr']), - format_number_with_comma(data['fec_uncorr']), - format_number_with_comma(data['fec_symbol_err']))) + format_number_with_comma(data.fec_corr), + format_number_with_comma(data.fec_uncorr), + format_number_with_comma(data.fec_symbol_err))) elif rates_only: header = header_rates_only table.append((key, self.get_port_state(key), - format_number_with_comma(data['rx_ok']), + format_number_with_comma(data.rx_ok), format_brate(rates.rx_bps), format_prate(rates.rx_pps), format_util(rates.rx_bps, port_speed), - format_number_with_comma(data['tx_ok']), + format_number_with_comma(data.tx_ok), format_brate(rates.tx_bps), format_prate(rates.tx_pps), format_util(rates.tx_bps, port_speed))) else: header = header_std table.append((key, self.get_port_state(key), - format_number_with_comma(data['rx_ok']), + format_number_with_comma(data.rx_ok), format_brate(rates.rx_bps), format_util(rates.rx_bps, port_speed), - format_number_with_comma(data['rx_err']), - format_number_with_comma(data['rx_drop']), - format_number_with_comma(data['rx_ovr']), - format_number_with_comma(data['tx_ok']), + format_number_with_comma(data.rx_err), + format_number_with_comma(data.rx_drop), + format_number_with_comma(data.rx_ovr), + format_number_with_comma(data.tx_ok), format_brate(rates.tx_bps), format_util(rates.tx_bps, port_speed), - format_number_with_comma(data['tx_err']), - format_number_with_comma(data['tx_drop']), - format_number_with_comma(data['tx_ovr']))) + + format_number_with_comma(data.tx_err), + format_number_with_comma(data.tx_drop), + format_number_with_comma(data.tx_ovr))) if table: if use_json: print(table_as_json(table, header)) @@ -353,51 +354,51 @@ class Portstat(object): if key in cnstat_old_dict: old_cntr = cnstat_old_dict.get(key) else: - old_cntr = NStats._make([0] * BUCKET_NUM)._asdict() + old_cntr = NStats._make([0] * BUCKET_NUM) if intf_list and key not in intf_list: continue - print("Packets Received 64 Octets..................... {}".format(ns_diff(cntr['rx_64'], old_cntr['rx_64']))) - print("Packets Received 65-127 Octets................. {}".format(ns_diff(cntr['rx_65_127'], old_cntr['rx_65_127']))) - print("Packets Received 128-255 Octets................ {}".format(ns_diff(cntr['rx_128_255'], old_cntr['rx_128_255']))) - print("Packets Received 256-511 Octets................ {}".format(ns_diff(cntr['rx_256_511'], old_cntr['rx_256_511']))) - print("Packets Received 512-1023 Octets............... {}".format(ns_diff(cntr['rx_512_1023'], old_cntr['rx_512_1023']))) - print("Packets Received 1024-1518 Octets.............. {}".format(ns_diff(cntr['rx_1024_1518'], old_cntr['rx_1024_1518']))) - print("Packets Received 1519-2047 Octets.............. {}".format(ns_diff(cntr['rx_1519_2047'], old_cntr['rx_1519_2047']))) - print("Packets Received 2048-4095 Octets.............. {}".format(ns_diff(cntr['rx_2048_4095'], old_cntr['rx_2048_4095']))) - print("Packets Received 4096-9216 Octets.............. {}".format(ns_diff(cntr['rx_4096_9216'], old_cntr['rx_4096_9216']))) - print("Packets Received 9217-16383 Octets............. {}".format(ns_diff(cntr['rx_9217_16383'], old_cntr['rx_9217_16383']))) + print("Packets Received 64 Octets..................... {}".format(ns_diff(cntr.rx_64, old_cntr.rx_64))) + print("Packets Received 65-127 Octets................. {}".format(ns_diff(cntr.rx_65_127, old_cntr.rx_65_127))) + print("Packets Received 128-255 Octets................ {}".format(ns_diff(cntr.rx_128_255, old_cntr.rx_128_255))) + print("Packets Received 256-511 Octets................ {}".format(ns_diff(cntr.rx_256_511, old_cntr.rx_256_511))) + print("Packets Received 512-1023 Octets............... {}".format(ns_diff(cntr.rx_512_1023, old_cntr.rx_512_1023))) + print("Packets Received 1024-1518 Octets.............. {}".format(ns_diff(cntr.rx_1024_1518, old_cntr.rx_1024_1518))) + print("Packets Received 1519-2047 Octets.............. {}".format(ns_diff(cntr.rx_1519_2047, old_cntr.rx_1519_2047))) + print("Packets Received 2048-4095 Octets.............. {}".format(ns_diff(cntr.rx_2048_4095, old_cntr.rx_2048_4095))) + print("Packets Received 4096-9216 Octets.............. {}".format(ns_diff(cntr.rx_4096_9216, old_cntr.rx_4096_9216))) + print("Packets Received 9217-16383 Octets............. {}".format(ns_diff(cntr.rx_9217_16383, old_cntr.rx_9217_16383))) print("") - print("Total Packets Received Without Errors.......... {}".format(ns_diff(cntr['rx_all'], old_cntr['rx_all']))) - print("Unicast Packets Received....................... {}".format(ns_diff(cntr['rx_uca'], old_cntr['rx_uca']))) - print("Multicast Packets Received..................... {}".format(ns_diff(cntr['rx_mca'], old_cntr['rx_mca']))) - print("Broadcast Packets Received..................... {}".format(ns_diff(cntr['rx_bca'], old_cntr['rx_bca']))) + print("Total Packets Received Without Errors.......... {}".format(ns_diff(cntr.rx_all, old_cntr.rx_all))) + print("Unicast Packets Received....................... {}".format(ns_diff(cntr.rx_uca, old_cntr.rx_uca))) + print("Multicast Packets Received..................... {}".format(ns_diff(cntr.rx_mca, old_cntr.rx_mca))) + print("Broadcast Packets Received..................... {}".format(ns_diff(cntr.rx_bca, old_cntr.rx_bca))) print("") - print("Jabbers Received............................... {}".format(ns_diff(cntr['rx_jbr'], old_cntr['rx_jbr']))) - print("Fragments Received............................. {}".format(ns_diff(cntr['rx_frag'], old_cntr['rx_frag']))) - print("Undersize Received............................. {}".format(ns_diff(cntr['rx_usize'], old_cntr['rx_usize']))) - print("Overruns Received.............................. {}".format(ns_diff(cntr['rx_ovrrun'], old_cntr['rx_ovrrun']))) + print("Jabbers Received............................... {}".format(ns_diff(cntr.rx_jbr, old_cntr.rx_jbr))) + print("Fragments Received............................. {}".format(ns_diff(cntr.rx_frag, old_cntr.rx_frag))) + print("Undersize Received............................. {}".format(ns_diff(cntr.rx_usize, old_cntr.rx_usize))) + print("Overruns Received.............................. {}".format(ns_diff(cntr.rx_ovrrun, old_cntr.rx_ovrrun))) print("") - print("Packets Transmitted 64 Octets.................. {}".format(ns_diff(cntr['tx_64'], old_cntr['tx_64']))) - print("Packets Transmitted 65-127 Octets.............. {}".format(ns_diff(cntr['tx_65_127'], old_cntr['tx_65_127']))) - print("Packets Transmitted 128-255 Octets............. {}".format(ns_diff(cntr['tx_128_255'], old_cntr['tx_128_255']))) - print("Packets Transmitted 256-511 Octets............. {}".format(ns_diff(cntr['tx_256_511'], old_cntr['tx_256_511']))) - print("Packets Transmitted 512-1023 Octets............ {}".format(ns_diff(cntr['tx_512_1023'], old_cntr['tx_512_1023']))) - print("Packets Transmitted 1024-1518 Octets........... {}".format(ns_diff(cntr['tx_1024_1518'], old_cntr['tx_1024_1518']))) - print("Packets Transmitted 1519-2047 Octets........... {}".format(ns_diff(cntr['tx_1519_2047'], old_cntr['tx_1519_2047']))) - print("Packets Transmitted 2048-4095 Octets........... {}".format(ns_diff(cntr['tx_2048_4095'], old_cntr['tx_2048_4095']))) - print("Packets Transmitted 4096-9216 Octets........... {}".format(ns_diff(cntr['tx_4096_9216'], old_cntr['tx_4096_9216']))) - print("Packets Transmitted 9217-16383 Octets.......... {}".format(ns_diff(cntr['tx_9217_16383'], old_cntr['tx_9217_16383']))) + print("Packets Transmitted 64 Octets.................. {}".format(ns_diff(cntr.tx_64, old_cntr.tx_64))) + print("Packets Transmitted 65-127 Octets.............. {}".format(ns_diff(cntr.tx_65_127, old_cntr.tx_65_127))) + print("Packets Transmitted 128-255 Octets............. {}".format(ns_diff(cntr.tx_128_255, old_cntr.tx_128_255))) + print("Packets Transmitted 256-511 Octets............. {}".format(ns_diff(cntr.tx_256_511, old_cntr.tx_256_511))) + print("Packets Transmitted 512-1023 Octets............ {}".format(ns_diff(cntr.tx_512_1023, old_cntr.tx_512_1023))) + print("Packets Transmitted 1024-1518 Octets........... {}".format(ns_diff(cntr.tx_1024_1518, old_cntr.tx_1024_1518))) + print("Packets Transmitted 1519-2047 Octets........... {}".format(ns_diff(cntr.tx_1519_2047, old_cntr.tx_1519_2047))) + print("Packets Transmitted 2048-4095 Octets........... {}".format(ns_diff(cntr.tx_2048_4095, old_cntr.tx_2048_4095))) + print("Packets Transmitted 4096-9216 Octets........... {}".format(ns_diff(cntr.tx_4096_9216, old_cntr.tx_4096_9216))) + print("Packets Transmitted 9217-16383 Octets.......... {}".format(ns_diff(cntr.tx_9217_16383, old_cntr.tx_9217_16383))) print("") - print("Total Packets Transmitted Successfully......... {}".format(ns_diff(cntr['tx_all'], old_cntr['tx_all']))) - print("Unicast Packets Transmitted.................... {}".format(ns_diff(cntr['tx_uca'], old_cntr['tx_uca']))) - print("Multicast Packets Transmitted.................. {}".format(ns_diff(cntr['tx_mca'], old_cntr['tx_mca']))) - print("Broadcast Packets Transmitted.................. {}".format(ns_diff(cntr['tx_bca'], old_cntr['tx_bca']))) + print("Total Packets Transmitted Successfully......... {}".format(ns_diff(cntr.tx_all, old_cntr.tx_all))) + print("Unicast Packets Transmitted.................... {}".format(ns_diff(cntr.tx_uca, old_cntr.tx_uca))) + print("Multicast Packets Transmitted.................. {}".format(ns_diff(cntr.tx_mca, old_cntr.tx_mca))) + print("Broadcast Packets Transmitted.................. {}".format(ns_diff(cntr.tx_bca, old_cntr.tx_bca))) print("Time Since Counters Last Cleared............... " + str(cnstat_old_dict.get('time'))) @@ -434,88 +435,88 @@ class Portstat(object): header = header_all if old_cntr is not None: table.append((key, self.get_port_state(key), - ns_diff(cntr['rx_ok'], old_cntr['rx_ok']), + ns_diff(cntr.rx_ok, old_cntr.rx_ok), format_brate(rates.rx_bps), format_prate(rates.rx_pps), format_util(rates.rx_bps, port_speed), - ns_diff(cntr['rx_err'], old_cntr['rx_err']), - ns_diff(cntr['rx_drop'], old_cntr['rx_drop']), - ns_diff(cntr['rx_ovr'], old_cntr['rx_ovr']), - ns_diff(cntr['tx_ok'], old_cntr['tx_ok']), + ns_diff(cntr.rx_err, old_cntr.rx_err), + ns_diff(cntr.rx_drop, old_cntr.rx_drop), + ns_diff(cntr.rx_ovr, old_cntr.rx_ovr), + ns_diff(cntr.tx_ok, old_cntr.tx_ok), format_brate(rates.tx_bps), format_prate(rates.tx_pps), format_util(rates.tx_bps, port_speed), - ns_diff(cntr['tx_err'], old_cntr['tx_err']), - ns_diff(cntr['tx_drop'], old_cntr['tx_drop']), - ns_diff(cntr['tx_ovr'], old_cntr['tx_ovr']))) + ns_diff(cntr.tx_err, old_cntr.tx_err), + ns_diff(cntr.tx_drop, old_cntr.tx_drop), + ns_diff(cntr.tx_ovr, old_cntr.tx_ovr))) else: table.append((key, self.get_port_state(key), - format_number_with_comma(cntr['rx_ok']), + format_number_with_comma(cntr.rx_ok), format_brate(rates.rx_bps), format_prate(rates.rx_pps), format_util(rates.rx_bps, port_speed), - format_number_with_comma(cntr['rx_err']), - format_number_with_comma(cntr['rx_drop']), - format_number_with_comma(cntr['rx_ovr']), - format_number_with_comma(cntr['tx_ok']), + format_number_with_comma(cntr.rx_err), + format_number_with_comma(cntr.rx_drop), + format_number_with_comma(cntr.rx_ovr), + format_number_with_comma(cntr.tx_ok), format_brate(rates.tx_bps), format_prate(rates.tx_pps), format_util(rates.tx_bps, port_speed), - format_number_with_comma(cntr['tx_err']), - format_number_with_comma(cntr['tx_drop']), - format_number_with_comma(cntr['tx_ovr']))) + format_number_with_comma(cntr.tx_err), + format_number_with_comma(cntr.tx_drop), + format_number_with_comma(cntr.tx_ovr))) elif errors_only: header = header_errors_only if old_cntr is not None: table.append((key, self.get_port_state(key), - ns_diff(cntr['rx_err'], old_cntr['rx_err']), - ns_diff(cntr['rx_drop'], old_cntr['rx_drop']), - ns_diff(cntr['rx_ovr'], old_cntr['rx_ovr']), - ns_diff(cntr['tx_err'], old_cntr['tx_err']), - ns_diff(cntr['tx_drop'], old_cntr['tx_drop']), - ns_diff(cntr['tx_ovr'], old_cntr['tx_ovr']))) + ns_diff(cntr.rx_err, old_cntr.rx_err), + ns_diff(cntr.rx_drop, old_cntr.rx_drop), + ns_diff(cntr.rx_ovr, old_cntr.rx_ovr), + ns_diff(cntr.tx_err, old_cntr.tx_err), + ns_diff(cntr.tx_drop, old_cntr.tx_drop), + ns_diff(cntr.tx_ovr, old_cntr.tx_ovr))) else: table.append((key, self.get_port_state(key), - format_number_with_comma(cntr['rx_err']), - format_number_with_comma(cntr['rx_drop']), - format_number_with_comma(cntr['rx_ovr']), - format_number_with_comma(cntr['tx_err']), - format_number_with_comma(cntr['tx_drop']), - format_number_with_comma(cntr['tx_ovr']))) + format_number_with_comma(cntr.rx_err), + format_number_with_comma(cntr.rx_drop), + format_number_with_comma(cntr.rx_ovr), + format_number_with_comma(cntr.tx_err), + format_number_with_comma(cntr.tx_drop), + format_number_with_comma(cntr.tx_ovr))) elif fec_stats_only: header = header_fec_only if old_cntr is not None: table.append((key, self.get_port_state(key), - ns_diff(cntr['fec_corr'], old_cntr['fec_corr']), - ns_diff(cntr['fec_uncorr'], old_cntr['fec_uncorr']), - ns_diff(cntr['fec_symbol_err'], old_cntr['fec_symbol_err']))) + ns_diff(cntr.fec_corr, old_cntr.fec_corr), + ns_diff(cntr.fec_uncorr, old_cntr.fec_uncorr), + ns_diff(cntr.fec_symbol_err, old_cntr.fec_symbol_err))) else: table.append((key, self.get_port_state(key), - format_number_with_comma(cntr['fec_corr']), - format_number_with_comma(cntr['fec_uncorr']), - format_number_with_comma(cntr['fec_symbol_err']))) + format_number_with_comma(cntr.fec_corr), + format_number_with_comma(cntr.fec_uncorr), + format_number_with_comma(cntr.fec_symbol_err))) elif rates_only: header = header_rates_only if old_cntr is not None: table.append((key, self.get_port_state(key), - ns_diff(cntr['rx_ok'], old_cntr['rx_ok']), + ns_diff(cntr.rx_ok, old_cntr.rx_ok), format_brate(rates.rx_bps), format_prate(rates.rx_pps), format_util(rates.rx_bps, port_speed), - ns_diff(cntr['tx_ok'], old_cntr['tx_ok']), + ns_diff(cntr.tx_ok, old_cntr.tx_ok), format_brate(rates.tx_bps), format_prate(rates.tx_pps), format_util(rates.tx_bps, port_speed))) else: table.append((key, self.get_port_state(key), - format_number_with_comma(cntr['rx_ok']), + format_number_with_comma(cntr.rx_ok), format_brate(rates.rx_bps), format_prate(rates.rx_pps), format_util(rates.rx_bps, port_speed), - format_number_with_comma(cntr['tx_ok']), + format_number_with_comma(cntr.tx_ok), format_brate(rates.tx_bps), format_prate(rates.tx_pps), format_util(rates.tx_bps, port_speed))) @@ -524,33 +525,33 @@ class Portstat(object): if old_cntr is not None: table.append((key, self.get_port_state(key), - ns_diff(cntr['rx_ok'], old_cntr['rx_ok']), + ns_diff(cntr.rx_ok, old_cntr.rx_ok), format_brate(rates.rx_bps), format_util(rates.rx_bps, port_speed), - ns_diff(cntr['rx_err'], old_cntr['rx_err']), - ns_diff(cntr['rx_drop'], old_cntr['rx_drop']), - ns_diff(cntr['rx_ovr'], old_cntr['rx_ovr']), - ns_diff(cntr['tx_ok'], old_cntr['tx_ok']), + ns_diff(cntr.rx_err, old_cntr.rx_err), + ns_diff(cntr.rx_drop, old_cntr.rx_drop), + ns_diff(cntr.rx_ovr, old_cntr.rx_ovr), + ns_diff(cntr.tx_ok, old_cntr.tx_ok), format_brate(rates.tx_bps), format_util(rates.tx_bps, port_speed), - ns_diff(cntr['tx_err'], old_cntr['tx_err']), - ns_diff(cntr['tx_drop'], old_cntr['tx_drop']), - ns_diff(cntr['tx_ovr'], old_cntr['tx_ovr']))) + ns_diff(cntr.tx_err, old_cntr.tx_err), + ns_diff(cntr.tx_drop, old_cntr.tx_drop), + ns_diff(cntr.tx_ovr, old_cntr.tx_ovr))) else: table.append((key, self.get_port_state(key), - format_number_with_comma(cntr['rx_ok']), + format_number_with_comma(cntr.rx_ok), format_brate(rates.rx_bps), format_util(rates.rx_bps, port_speed), - format_number_with_comma(cntr['rx_err']), - format_number_with_comma(cntr['rx_drop']), - format_number_with_comma(cntr['rx_ovr']), - format_number_with_comma(cntr['tx_ok']), + format_number_with_comma(cntr.rx_err), + format_number_with_comma(cntr.rx_drop), + format_number_with_comma(cntr.rx_ovr), + format_number_with_comma(cntr.tx_ok), format_brate(rates.tx_bps), format_util(rates.tx_bps, port_speed), - format_number_with_comma(cntr['tx_err']), - format_number_with_comma(cntr['tx_drop']), - format_number_with_comma(cntr['tx_ovr']))) + format_number_with_comma(cntr.tx_err), + format_number_with_comma(cntr.tx_drop), + format_number_with_comma(cntr.tx_ovr))) if table: if use_json: print(table_as_json(table, header)) @@ -641,7 +642,7 @@ Examples: if save_fresh_stats: try: - json.dump(cnstat_dict, open(cnstat_fqn_file, 'w'), default=json_serial) + pickle.dump(cnstat_dict, open(cnstat_fqn_file, 'wb')) except IOError as e: sys.exit(e.errno) else: @@ -652,7 +653,7 @@ Examples: cnstat_cached_dict = OrderedDict() if os.path.isfile(cnstat_fqn_file): try: - cnstat_cached_dict = json.load(open(cnstat_fqn_file, 'r')) + cnstat_cached_dict = pickle.load(open(cnstat_fqn_file, 'rb')) if not detail: print("Last cached time was " + str(cnstat_cached_dict.get('time'))) portstat.cnstat_diff_print(cnstat_dict, cnstat_cached_dict, ratestat_dict, intf_list, use_json, print_all, errors_only, fec_stats_only, rates_only, detail) diff --git a/scripts/queuestat b/scripts/queuestat index d82e7e4a6a..96a24b51a3 100755 --- a/scripts/queuestat +++ b/scripts/queuestat @@ -6,7 +6,7 @@ # ##################################################################### -import json +import _pickle as pickle import argparse import datetime import os.path @@ -33,7 +33,7 @@ except KeyError: pass from swsscommon.swsscommon import SonicV2Connector -from utilities_common.cli import json_serial, UserCache +from utilities_common.cli import UserCache from utilities_common import constants import utilities_common.multi_asic as multi_asic_util @@ -186,7 +186,7 @@ class Queuestat(object): fields[pos] = STATUS_NA elif fields[pos] != STATUS_NA: fields[pos] = str(int(counter_data)) - cntr = QueueStats._make(fields)._asdict() + cntr = QueueStats._make(fields) return cntr # Build a dictionary of the stats @@ -211,9 +211,9 @@ class Queuestat(object): if json_opt: json_output[port][key] = data continue - table.append((port, data['queuetype'] + str(data['queueindex']), - data['totalpacket'], data['totalbytes'], - data['droppacket'], data['dropbytes'])) + table.append((port, data.queuetype + str(data.queueindex), + data.totalpacket, data.totalbytes, + data.droppacket, data.dropbytes)) if json_opt: json_output[port].update(build_json(port, table)) @@ -241,15 +241,15 @@ class Queuestat(object): old_cntr = cnstat_old_dict.get(key) if old_cntr is not None: - table.append((port, cntr['queuetype'] + str(cntr['queueindex']), - ns_diff(cntr['totalpacket'], old_cntr['totalpacket']), - ns_diff(cntr['totalbytes'], old_cntr['totalbytes']), - ns_diff(cntr['droppacket'], old_cntr['droppacket']), - ns_diff(cntr['dropbytes'], old_cntr['dropbytes']))) + table.append((port, cntr.queuetype + str(cntr.queueindex), + ns_diff(cntr.totalpacket, old_cntr.totalpacket), + ns_diff(cntr.totalbytes, old_cntr.totalbytes), + ns_diff(cntr.droppacket, old_cntr.droppacket), + ns_diff(cntr.dropbytes, old_cntr.dropbytes))) else: - table.append((port, cntr['queuetype'] + str(cntr['queueindex']), - cntr['totalpacket'], cntr['totalbytes'], - cntr['droppacket'], cntr['dropbytes'])) + table.append((port, cntr.queuetype + str(cntr.queueindex), + cntr.totalpacket, cntr.totalbytes, + cntr.droppacket, cntr.dropbytes)) if json_opt: json_output[port].update(build_json(port, table)) @@ -273,7 +273,7 @@ class Queuestat(object): cnstat_fqn_file_name = cnstat_fqn_file + port if os.path.isfile(cnstat_fqn_file_name): try: - cnstat_cached_dict = json.load(open(cnstat_fqn_file_name, 'r')) + cnstat_cached_dict = pickle.load(open(cnstat_fqn_file_name, 'rb')) if json_opt: json_output[port].update({"cached_time":cnstat_cached_dict.get('time')}) json_output.update(self.cnstat_diff_print(port, cnstat_dict, cnstat_cached_dict, json_opt)) @@ -307,7 +307,7 @@ class Queuestat(object): json_output[port] = {} if os.path.isfile(cnstat_fqn_file_name): try: - cnstat_cached_dict = json.load(open(cnstat_fqn_file_name, 'r')) + cnstat_cached_dict = pickle.load(open(cnstat_fqn_file_name, 'rb')) if json_opt: json_output[port].update({"cached_time":cnstat_cached_dict.get('time')}) json_output.update(self.cnstat_diff_print(port, cnstat_dict, cnstat_cached_dict, json_opt)) @@ -330,7 +330,7 @@ class Queuestat(object): for port in natsorted(self.counter_port_name_map): cnstat_dict = self.get_cnstat(self.port_queues_map[port]) try: - json.dump(cnstat_dict, open(cnstat_fqn_file + port, 'w'), default=json_serial) + pickle.dump(cnstat_dict, open(cnstat_fqn_file + port, 'wb')) except IOError as e: print(e.errno, e) sys.exit(e.errno) diff --git a/scripts/tunnelstat b/scripts/tunnelstat index 3d7423e86b..8b045ec684 100755 --- a/scripts/tunnelstat +++ b/scripts/tunnelstat @@ -6,7 +6,7 @@ # ##################################################################### -import json +import _pickle as pickle import argparse import datetime import sys @@ -29,7 +29,7 @@ from collections import namedtuple, OrderedDict from natsort import natsorted from tabulate import tabulate from utilities_common.netstat import ns_diff, table_as_json, STATUS_NA, format_prate -from utilities_common.cli import json_serial, UserCache +from utilities_common.cli import UserCache from swsscommon.swsscommon import SonicV2Connector @@ -80,7 +80,7 @@ class Tunnelstat(object): counter_data = self.db.get(self.db.COUNTERS_DB, full_table_id, counter_name) if counter_data: fields[pos] = str(counter_data) - cntr = NStats._make(fields)._asdict() + cntr = NStats._make(fields) return cntr def get_rates(table_id): @@ -149,8 +149,8 @@ class Tunnelstat(object): continue rates = ratestat_dict.get(key, RateStats._make([STATUS_NA] * len(rates_key_list))) - table.append((key, data['rx_p_ok'], data['rx_b_ok'], format_prate(rates.rx_pps), - data['tx_p_ok'], data['tx_b_ok'], format_prate(rates.tx_pps))) + table.append((key, data.rx_p_ok, data.rx_b_ok, format_prate(rates.rx_pps), + data.tx_p_ok, data.tx_b_ok, format_prate(rates.tx_pps))) if use_json: print(table_as_json(table, header)) @@ -175,19 +175,19 @@ class Tunnelstat(object): rates = ratestat_dict.get(key, RateStats._make([STATUS_NA] * len(rates_key_list))) if old_cntr is not None: table.append((key, - ns_diff(cntr['rx_p_ok'], old_cntr['rx_p_ok']), - ns_diff(cntr['rx_b_ok'], old_cntr['rx_b_ok']), + ns_diff(cntr.rx_p_ok, old_cntr.rx_p_ok), + ns_diff(cntr.rx_b_ok, old_cntr.rx_b_ok), format_prate(rates.rx_pps), - ns_diff(cntr['tx_p_ok'], old_cntr['tx_p_ok']), - ns_diff(cntr['tx_b_ok'], old_cntr['tx_b_ok']), + ns_diff(cntr.tx_p_ok, old_cntr.tx_p_ok), + ns_diff(cntr.tx_b_ok, old_cntr.tx_b_ok), format_prate(rates.tx_pps))) else: table.append((key, - cntr['rx_p_ok'], - cntr['rx_b_ok'], + cntr.rx_p_ok, + cntr.rx_b_ok, format_prate(rates.rx_pps), - cntr['tx_p_ok'], - cntr['tx_b_ok'], + cntr.tx_p_ok, + cntr.tx_b_ok, format_prate(rates.tx_pps))) if use_json: print(table_as_json(table, header)) @@ -210,12 +210,12 @@ class Tunnelstat(object): if cnstat_old_dict: old_cntr = cnstat_old_dict.get(tunnel) if old_cntr: - body = body % (ns_diff(cntr['rx_p_ok'], old_cntr['rx_p_ok']), - ns_diff(cntr['rx_b_ok'], old_cntr['rx_b_ok']), - ns_diff(cntr['tx_p_ok'], old_cntr['tx_p_ok']), - ns_diff(cntr['tx_b_ok'], old_cntr['tx_b_ok'])) + body = body % (ns_diff(cntr.rx_p_ok, old_cntr.rx_p_ok), + ns_diff(cntr.rx_b_ok, old_cntr.rx_b_ok), + ns_diff(cntr.tx_p_ok, old_cntr.tx_p_ok), + ns_diff(cntr.tx_b_ok, old_cntr.tx_b_ok)) else: - body = body % (cntr['rx_p_ok'], cntr['rx_b_ok'], cntr['tx_p_ok'], cntr['tx_b_ok']) + body = body % (cntr.rx_p_ok, cntr.rx_b_ok, cntr.tx_p_ok, cntr.tx_b_ok) print(header) print(body) @@ -273,7 +273,7 @@ def main(): if save_fresh_stats: try: - json.dump(cnstat_dict, open(cnstat_fqn_file, 'w'), default=json_serial) + pickle.dump(cnstat_dict, open(cnstat_fqn_file, 'wb')) except IOError as e: sys.exit(e.errno) else: @@ -283,7 +283,7 @@ def main(): if wait_time_in_seconds == 0: if os.path.isfile(cnstat_fqn_file): try: - cnstat_cached_dict = json.load(open(cnstat_fqn_file, 'r')) + cnstat_cached_dict = pickle.load(open(cnstat_fqn_file, 'rb')) print("Last cached time was " + str(cnstat_cached_dict.get('time'))) if tunnel_name: tunnelstat.cnstat_single_tunnel(tunnel_name, cnstat_dict, cnstat_cached_dict)