11import click
2- import ast
32from natsort import natsorted
43from tabulate import tabulate
54import show .vlan as show_vlan
87from swsscommon .swsscommon import ConfigDBConnector
98from swsscommon .swsscommon import SonicV2Connector
109
10+
1111# STATE_DB Table
12- DHCPv4_COUNTER_TABLE = 'DHCP_COUNTER_TABLE'
1312DHCPv6_COUNTER_TABLE = 'DHCPv6_COUNTER_TABLE'
1413
15- # DHCPv4 Counter Messages
16- dhcpv4_messages = [
17- "Unknown" , "Discover" , "Offer" , "Request" , "Decline" , "Ack" , "Nack" , "Release" , "Inform"
18- ]
19-
2014# DHCPv6 Counter Messages
21- dhcpv6_messages = [
22- "Unknown" , "Solicit" , "Advertise" , "Request" , "Confirm" , "Renew" , "Rebind" , "Reply" , "Release" ,
23- "Decline" , "Reconfigure" , "Information-Request" , "Relay-Forward" , "Relay-Reply" , "Malformed"
24- ]
15+ messages = ["Unknown" , "Solicit" , "Advertise" , "Request" , "Confirm" , "Renew" , "Rebind" , "Reply" , "Release" , "Decline" ,
16+ "Reconfigure" , "Information-Request" , "Relay-Forward" , "Relay-Reply" , "Malformed" ]
2517
2618# DHCP_RELAY Config Table
2719DHCP_RELAY = 'DHCP_RELAY'
@@ -47,80 +39,6 @@ def get_dhcp_helper_address(ctx, vlan):
4739
4840show_vlan .VlanBrief .register_column ('DHCP Helper Address' , get_dhcp_helper_address )
4941
50- class DHCPv4_Counter (object ):
51- def __init__ (self ):
52- self .db = SonicV2Connector (use_unix_socket_path = False )
53- self .db .connect (self .db .STATE_DB )
54- self .table_name = DHCPv4_COUNTER_TABLE + self .db .get_db_separator (self .db .STATE_DB )
55-
56- def get_interface (self ):
57- """ Get all names of all interfaces in DHCPv4_COUNTER_TABLE """
58- interfaces = []
59- for key in self .db .keys (self .db .STATE_DB ):
60- if DHCPv4_COUNTER_TABLE in key :
61- interfaces .append (key [19 :])
62- return interfaces
63-
64- def get_dhcp4relay_msg_count (self , interface , dir ):
65- """ Get count of a dhcprelay message """
66- value = self .db .get (self .db .STATE_DB , self .table_name + str (interface ), str (dir ))
67- cnts = ast .literal_eval (str (value ))
68- data = []
69- if cnts is not None :
70- for k , v in cnts .items ():
71- data .append ([k , v ])
72- return data
73-
74- def clear_table (self , interface ):
75- """ Reset all message counts to 0 """
76- v4_cnts = {}
77- for msg in dhcpv4_messages :
78- v4_cnts [msg ] = '0'
79- self .db .set (self .db .STATE_DB , self .table_name + str (interface ), str ("RX" ), str (v4_cnts ))
80- self .db .set (self .db .STATE_DB , self .table_name + str (interface ), str ("TX" ), str (v4_cnts ))
81-
82- def print_dhcpv4_count (counter , intf ):
83- """Print count of each message"""
84- rx_data = counter .get_dhcp4relay_msg_count (intf , "RX" )
85- print (tabulate (rx_data , headers = ["Message Type" , intf + "(RX)" ], tablefmt = 'simple' , stralign = 'right' ) + "\n " )
86- tx_data = counter .get_dhcp4relay_msg_count (intf , "TX" )
87- print (tabulate (tx_data , headers = ["Message Type" , intf + "(TX)" ], tablefmt = 'simple' , stralign = 'right' ) + "\n " )
88-
89- #
90- # 'dhcp4relay_counters' group ###
91- #
92-
93-
94- @click .group (cls = clicommon .AliasedGroup , name = "dhcp4relay_counters" )
95- def dhcp4relay_counters ():
96- """Show DHCPv4 counter"""
97- pass
98-
99-
100- def ipv4_counters (interface ):
101- config_db .connect ()
102- feature_tbl = config_db .get_table ("FEATURE" )
103- if is_dhcp_server_enabled (feature_tbl ):
104- click .echo ("Unsupport to check dhcp_relay ipv4 counter when dhcp_server feature is enabled" )
105- return
106- counter = DHCPv4_Counter ()
107- counter_intf = counter .get_interface ()
108-
109- if interface :
110- print_dhcpv4_count (counter , interface )
111- else :
112- for intf in counter_intf :
113- print_dhcpv4_count (counter , intf )
114-
115-
116- # 'counts' subcommand ("show dhcp4relay_counters counts")
117- @dhcp4relay_counters .command ('counts' )
118- @click .option ('-i' , '--interface' , required = False )
119- @click .option ('--verbose' , is_flag = True , help = "Enable verbose output" )
120- def counts (interface , verbose ):
121- """Show dhcp4relay message counts"""
122- ipv4_counters (interface )
123-
12442
12543class DHCPv6_Counter (object ):
12644 def __init__ (self ):
@@ -130,37 +48,30 @@ def __init__(self):
13048
13149 def get_interface (self ):
13250 """ Get all names of all interfaces in DHCPv6_COUNTER_TABLE """
133- interfaces = []
51+ vlans = []
13452 for key in self .db .keys (self .db .STATE_DB ):
13553 if DHCPv6_COUNTER_TABLE in key :
136- interfaces .append (key [21 :])
137- return interfaces
54+ vlans .append (key [21 :])
55+ return vlans
13856
139- def get_dhcp6relay_msg_count (self , interface , dir ):
57+ def get_dhcp6relay_msg_count (self , interface , msg ):
14058 """ Get count of a dhcp6relay message """
141- value = self .db .get (self .db .STATE_DB , self .table_name + str (interface ), str (dir ))
142- cnts = ast .literal_eval (str (value ))
143- data = []
144- if cnts is not None :
145- for k , v in cnts .items ():
146- data .append ([k , v ])
59+ count = self .db .get (self .db .STATE_DB , self .table_name + str (interface ), str (msg ))
60+ data = [str (msg ), count ]
14761 return data
14862
14963 def clear_table (self , interface ):
15064 """ Reset all message counts to 0 """
151- v6_cnts = {}
152- for msg in dhcpv6_messages :
153- v6_cnts [msg ] = '0'
154- self .db .set (self .db .STATE_DB , self .table_name + str (interface ), str ("RX" ), str (v6_cnts ))
155- self .db .set (self .db .STATE_DB , self .table_name + str (interface ), str ("TX" ), str (v6_cnts ))
65+ for msg in messages :
66+ self .db .set (self .db .STATE_DB , self .table_name + str (interface ), str (msg ), '0' )
15667
15768
158- def print_dhcpv6_count (counter , intf ):
69+ def print_count (counter , intf ):
15970 """Print count of each message"""
160- rx_data = counter . get_dhcp6relay_msg_count ( intf , "RX" )
161- print ( tabulate ( rx_data , headers = [ "Message Type" , intf + "(RX)" ], tablefmt = 'simple' , stralign = 'right' ) + " \n " )
162- tx_data = counter .get_dhcp6relay_msg_count (intf , "TX" )
163- print (tabulate (tx_data , headers = ["Message Type" , intf + "(TX)" ], tablefmt = 'simple' , stralign = 'right' ) + "\n " )
71+ data = []
72+ for i in messages :
73+ data . append ( counter .get_dhcp6relay_msg_count (intf , i ) )
74+ print (tabulate (data , headers = ["Message Type" , intf ], tablefmt = 'simple' , stralign = 'right' ) + "\n " )
16475
16576
16677#
@@ -179,10 +90,10 @@ def ipv6_counters(interface):
17990 counter_intf = counter .get_interface ()
18091
18192 if interface :
182- print_dhcpv6_count (counter , interface )
93+ print_count (counter , interface )
18394 else :
18495 for intf in counter_intf :
185- print_dhcpv6_count (counter , intf )
96+ print_count (counter , intf )
18697
18798
18899# 'counts' subcommand ("show dhcp6relay_counters counts")
@@ -191,6 +102,7 @@ def ipv6_counters(interface):
191102@click .option ('--verbose' , is_flag = True , help = "Enable verbose output" )
192103def counts (interface , verbose ):
193104 """Show dhcp6relay message counts"""
105+
194106 ipv6_counters (interface )
195107
196108
@@ -296,10 +208,6 @@ def dhcp_relay_ipv4_destination():
296208def dhcp_relay_ipv6_destination ():
297209 get_dhcp_relay (DHCP_RELAY , DHCPV6_SERVERS , with_header = True )
298210
299- @dhcp_relay_ipv4 .command ("counters" )
300- @click .option ('-i' , '--interface' , required = False )
301- def dhcp_relay_ip4counters (interface ):
302- ipv4_counters (interface )
303211
304212@dhcp_relay_ipv6 .command ("counters" )
305213@click .option ('-i' , '--interface' , required = False )
@@ -308,7 +216,6 @@ def dhcp_relay_ip6counters(interface):
308216
309217
310218def register (cli ):
311- cli .add_command (dhcp4relay_counters )
312219 cli .add_command (dhcp6relay_counters )
313220 cli .add_command (dhcp_relay_helper )
314221 cli .add_command (dhcp_relay )
0 commit comments