|
| 1 | +import click |
| 2 | +import utilities_common.cli as clicommon |
| 3 | +from tabulate import tabulate |
| 4 | + |
| 5 | +from swsscommon.swsscommon import SonicV2Connector |
| 6 | + |
| 7 | + |
| 8 | +# STATE_DB Table |
| 9 | +DHCPv6_COUNTER_TABLE = 'DHCPv6_COUNTER_TABLE' |
| 10 | + |
| 11 | +# DHCPv6 Counter Messages |
| 12 | +messages = ["Unknown", "Solicit", "Advertise", "Request", "Confirm", "Renew", "Rebind", "Reply", "Release", "Decline", "Relay-Forward", "Relay-Reply"] |
| 13 | + |
| 14 | +class DHCPv6_Counter(object): |
| 15 | + def __init__(self): |
| 16 | + self.db = SonicV2Connector(use_unix_socket_path=False) |
| 17 | + self.db.connect(self.db.STATE_DB) |
| 18 | + self.table_name = DHCPv6_COUNTER_TABLE + self.db.get_db_separator(self.db.STATE_DB) |
| 19 | + |
| 20 | + |
| 21 | + def get_interface(self): |
| 22 | + """ Get all names of all interfaces in DHCPv6_COUNTER_TABLE """ |
| 23 | + vlans = [] |
| 24 | + for key in self.db.keys(self.db.STATE_DB): |
| 25 | + if DHCPv6_COUNTER_TABLE in key: |
| 26 | + vlans.append(key[21:]) |
| 27 | + return vlans |
| 28 | + |
| 29 | + |
| 30 | + def get_dhcp6relay_msg_count(self, interface, msg): |
| 31 | + """ Get count of a dhcp6relay message """ |
| 32 | + count = self.db.get(self.db.STATE_DB, self.table_name + str(interface), str(msg)) |
| 33 | + data = [str(msg), count] |
| 34 | + return data |
| 35 | + |
| 36 | + |
| 37 | + def clear_table(self, interface): |
| 38 | + """ Reset all message counts to 0 """ |
| 39 | + for msg in messages: |
| 40 | + self.db.set(self.db.STATE_DB, self.table_name + str(interface), str(msg), '0') |
| 41 | + |
| 42 | +def print_count(counter, intf): |
| 43 | + """Print count of each message""" |
| 44 | + data = [] |
| 45 | + for i in messages: |
| 46 | + data.append(counter.get_dhcp6relay_msg_count(intf, i)) |
| 47 | + print(tabulate(data, headers = ["Message Type", intf], tablefmt='simple', stralign='right') + "\n") |
| 48 | + |
| 49 | + |
| 50 | +# |
| 51 | +# 'dhcp6relay_counters' group ### |
| 52 | +# |
| 53 | + |
| 54 | + |
| 55 | +@click.group(cls=clicommon.AliasedGroup, name="dhcp6relay_counters") |
| 56 | +def dhcp6relay_counters(): |
| 57 | + """Show DHCPv6 counter""" |
| 58 | + pass |
| 59 | + |
| 60 | + |
| 61 | +# 'counts' subcommand ("show dhcp6relay_counters counts") |
| 62 | +@dhcp6relay_counters.command('counts') |
| 63 | +@click.option('-i', '--interface', required=False) |
| 64 | +@click.option('--verbose', is_flag=True, help="Enable verbose output") |
| 65 | +def counts(interface, verbose): |
| 66 | + """Show dhcp6relay message counts""" |
| 67 | + |
| 68 | + counter = DHCPv6_Counter() |
| 69 | + counter_intf = counter.get_interface() |
| 70 | + |
| 71 | + if interface: |
| 72 | + print_count(counter, interface) |
| 73 | + else: |
| 74 | + for intf in counter_intf: |
| 75 | + print_count(counter, intf) |
| 76 | + |
| 77 | + |
| 78 | +# 'clear' subcommand ("clear dhcp6relay_counters counts") |
| 79 | +@dhcp6relay_counters.command('clear') |
| 80 | +@click.option('-i', '--interface', required=False) |
| 81 | +@click.option('--verbose', is_flag=True, help="Enable verbose output") |
| 82 | +def clear(interface, verbose): |
| 83 | + """Clear dhcp6relay message counts""" |
| 84 | + |
| 85 | + counter = DHCPv6_Counter() |
| 86 | + counter_intf = counter.get_interface() |
| 87 | + |
| 88 | + if interface: |
| 89 | + counter.clear_table(interface) |
| 90 | + print("Cleared DHCPv6 Relay Counter " + interface) |
| 91 | + else: |
| 92 | + for intf in counter_intf: |
| 93 | + counter.clear_table(intf) |
| 94 | + print("Cleared DHCPv6 Relay Counters") |
| 95 | + |
0 commit comments