|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import _pickle as pickle |
| 6 | +import sys |
| 7 | + |
| 8 | +from natsort import natsorted |
| 9 | +from tabulate import tabulate |
| 10 | + |
| 11 | +# mock the redis for unit test purposes # |
| 12 | +try: |
| 13 | + if os.environ["UTILITIES_UNIT_TESTING"] == "2": |
| 14 | + modules_path = os.path.join(os.path.dirname(__file__), "..") |
| 15 | + tests_path = os.path.join(modules_path, "tests") |
| 16 | + sys.path.insert(0, modules_path) |
| 17 | + sys.path.insert(0, tests_path) |
| 18 | + import mock_tables.dbconnector |
| 19 | + if os.environ["UTILITIES_UNIT_TESTING_TOPOLOGY"] == "multi_asic": |
| 20 | + import mock_tables.mock_multi_asic |
| 21 | + mock_tables.dbconnector.load_namespace_config() |
| 22 | + |
| 23 | +except KeyError: |
| 24 | + pass |
| 25 | + |
| 26 | +import utilities_common.multi_asic as multi_asic_util |
| 27 | +from utilities_common.netstat import format_number_with_comma, table_as_json, ns_diff, format_prate |
| 28 | + |
| 29 | +# Flow counter meta data, new type of flow counters can extend this dictinary to reuse existing logic |
| 30 | +flow_counter_meta = { |
| 31 | + 'trap': { |
| 32 | + 'headers': ['Trap Name', 'Packets', 'Bytes', 'PPS'], |
| 33 | + 'name_map': 'COUNTERS_TRAP_NAME_MAP', |
| 34 | + } |
| 35 | +} |
| 36 | +flow_counters_fields = ['SAI_COUNTER_STAT_PACKETS', 'SAI_COUNTER_STAT_BYTES'] |
| 37 | + |
| 38 | +# Only do diff for 'Packets' and 'Bytes' |
| 39 | +diff_column_positions = set([0, 1]) |
| 40 | + |
| 41 | +FLOW_COUNTER_TABLE_PREFIX = "COUNTERS:" |
| 42 | +RATES_TABLE_PREFIX = 'RATES:' |
| 43 | +PPS_FIELD = 'RX_PPS' |
| 44 | +STATUS_NA = 'N/A' |
| 45 | + |
| 46 | + |
| 47 | +class FlowCounterStats(object): |
| 48 | + def __init__(self, args): |
| 49 | + self.db = None |
| 50 | + self.multi_asic = multi_asic_util.MultiAsic(namespace_option=args.namespace) |
| 51 | + self.args = args |
| 52 | + meta_data = flow_counter_meta[args.type] |
| 53 | + self.name_map = meta_data['name_map'] |
| 54 | + self.headers = meta_data['headers'] |
| 55 | + self.data_file = os.path.join('/tmp/{}-stats-{}'.format(args.type, os.getuid())) |
| 56 | + if self.args.delete and os.path.exists(self.data_file): |
| 57 | + os.remove(self.data_file) |
| 58 | + self.data = {} |
| 59 | + |
| 60 | + def show(self): |
| 61 | + """Show flow counter statistic |
| 62 | + """ |
| 63 | + self._collect() |
| 64 | + old_data = self._load() |
| 65 | + self._diff(old_data, self.data) |
| 66 | + |
| 67 | + table = [] |
| 68 | + if self.multi_asic.is_multi_asic: |
| 69 | + # On multi ASIC platform, an extra column "ASIC ID" must be present to avoid duplicate entry name |
| 70 | + headers = ['ASIC ID'] + self.headers |
| 71 | + for ns, stats in natsorted(self.data.items()): |
| 72 | + for name, values in natsorted(stats.items()): |
| 73 | + row = [ns, name, format_number_with_comma(values[0]), format_number_with_comma(values[1]), format_prate(values[2])] |
| 74 | + table.append(row) |
| 75 | + else: |
| 76 | + headers = self.headers |
| 77 | + for ns, stats in natsorted(self.data.items()): |
| 78 | + for name, values in natsorted(stats.items()): |
| 79 | + row = [name, format_number_with_comma(values[0]), format_number_with_comma(values[1]), format_prate(values[2])] |
| 80 | + table.append(row) |
| 81 | + if self.args.json: |
| 82 | + print(table_as_json(table, headers)) |
| 83 | + else: |
| 84 | + print(tabulate(table, headers, tablefmt='simple', stralign='right')) |
| 85 | + |
| 86 | + def clear(self): |
| 87 | + """Clear flow counter statistic. This function does not clear data from ASIC. Instead, it saves flow counter statistic to a file. When user |
| 88 | + issue show command after clear, it does a diff between new data and saved data. |
| 89 | + """ |
| 90 | + self._collect() |
| 91 | + self._save() |
| 92 | + print('Flow Counters were successfully cleared') |
| 93 | + |
| 94 | + @multi_asic_util.run_on_multi_asic |
| 95 | + def _collect(self): |
| 96 | + """Collect flow counter statistic from DB. This function is called on a multi ASIC context. |
| 97 | + """ |
| 98 | + self.data.update(self._get_stats_from_db()) |
| 99 | + |
| 100 | + def _get_stats_from_db(self): |
| 101 | + """Get flow counter statistic from DB. |
| 102 | +
|
| 103 | + Returns: |
| 104 | + dict: A dictionary. E.g: {<namespace>: {<trap_name>: [<value_in_pkts>, <value_in_bytes>, <rx_pps>]}} |
| 105 | + """ |
| 106 | + ns = self.multi_asic.current_namespace |
| 107 | + name_map = self.db.get_all(self.db.COUNTERS_DB, self.name_map) |
| 108 | + data = {ns: {}} |
| 109 | + if not name_map: |
| 110 | + return data |
| 111 | + |
| 112 | + for name, counter_oid in name_map.items(): |
| 113 | + values = [] |
| 114 | + full_table_id = FLOW_COUNTER_TABLE_PREFIX + counter_oid |
| 115 | + for field in flow_counters_fields: |
| 116 | + counter_data = self.db.get(self.db.COUNTERS_DB, full_table_id, field) |
| 117 | + values.append(STATUS_NA if counter_data is None else counter_data) |
| 118 | + |
| 119 | + full_table_id = RATES_TABLE_PREFIX + counter_oid |
| 120 | + counter_data = self.db.get(self.db.COUNTERS_DB, full_table_id, PPS_FIELD) |
| 121 | + values.append(STATUS_NA if counter_data is None else counter_data) |
| 122 | + values.append(counter_oid) |
| 123 | + data[ns][name] = values |
| 124 | + return data |
| 125 | + |
| 126 | + def _save(self): |
| 127 | + """Save flow counter statistic to a file |
| 128 | + """ |
| 129 | + try: |
| 130 | + if os.path.exists(self.data_file): |
| 131 | + os.remove(self.data_file) |
| 132 | + |
| 133 | + with open(self.data_file, 'wb') as f: |
| 134 | + pickle.dump(self.data, f) |
| 135 | + except IOError as e: |
| 136 | + print('Failed to save statistic - {}'.format(repr(e))) |
| 137 | + |
| 138 | + def _load(self): |
| 139 | + """Load flow counter statistic from a file |
| 140 | +
|
| 141 | + Returns: |
| 142 | + dict: A dictionary. E.g: {<namespace>: {<trap_name>: [<value_in_pkts>, <value_in_bytes>, <rx_pps>]}} |
| 143 | + """ |
| 144 | + if not os.path.exists(self.data_file): |
| 145 | + return None |
| 146 | + |
| 147 | + try: |
| 148 | + with open(self.data_file, 'rb') as f: |
| 149 | + data = pickle.load(f) |
| 150 | + except IOError as e: |
| 151 | + print('Failed to load statistic - {}'.format(repr(e))) |
| 152 | + return None |
| 153 | + |
| 154 | + return data |
| 155 | + |
| 156 | + def _diff(self, old_data, new_data): |
| 157 | + """Do a diff between new data and old data. |
| 158 | +
|
| 159 | + Args: |
| 160 | + old_data (dict): E.g: {<namespace>: {<trap_name>: [<value_in_pkts>, <value_in_bytes>, <rx_pps>]}} |
| 161 | + new_data (dict): E.g: {<namespace>: {<trap_name>: [<value_in_pkts>, <value_in_bytes>, <rx_pps>]}} |
| 162 | + """ |
| 163 | + if not old_data: |
| 164 | + return |
| 165 | + |
| 166 | + for ns, stats in new_data.items(): |
| 167 | + if ns not in old_data: |
| 168 | + continue |
| 169 | + old_stats = old_data[ns] |
| 170 | + for name, values in stats.items(): |
| 171 | + if name not in old_stats: |
| 172 | + continue |
| 173 | + |
| 174 | + old_values = old_stats[name] |
| 175 | + if values[-1] != old_values[-1]: |
| 176 | + # Counter OID not equal means the trap was removed and added again. Removing a trap would cause |
| 177 | + # the stats value restart from 0. To avoid get minus value here, it should not do diff in case |
| 178 | + # counter OID is changed. |
| 179 | + continue |
| 180 | + |
| 181 | + for i in diff_column_positions: |
| 182 | + values[i] = ns_diff(values[i], old_values[i]) |
| 183 | + |
| 184 | + |
| 185 | +def main(): |
| 186 | + parser = argparse.ArgumentParser(description='Display the flow counters', |
| 187 | + formatter_class=argparse.RawTextHelpFormatter, |
| 188 | + epilog=""" |
| 189 | +Examples: |
| 190 | + flow_counters_stat -c -t trap |
| 191 | + flow_counters_stat -t trap |
| 192 | + flow_counters_stat -d -t trap |
| 193 | +""") |
| 194 | + parser.add_argument('-c', '--clear', action='store_true', help='Copy & clear stats') |
| 195 | + parser.add_argument('-d', '--delete', action='store_true', help='Delete saved stats') |
| 196 | + parser.add_argument('-j', '--json', action='store_true', help='Display in JSON format') |
| 197 | + parser.add_argument('-n','--namespace', default=None, help='Display flow counters for specific namespace') |
| 198 | + parser.add_argument('-t', '--type', required=True, choices=['trap'],help='Flow counters type') |
| 199 | + |
| 200 | + args = parser.parse_args() |
| 201 | + |
| 202 | + stats = FlowCounterStats(args) |
| 203 | + if args.clear: |
| 204 | + stats.clear() |
| 205 | + else: |
| 206 | + stats.show() |
| 207 | + |
| 208 | + |
| 209 | +if __name__ == '__main__': |
| 210 | + main() |
0 commit comments