1
1
import click
2
- import ast
3
2
from natsort import natsorted
4
3
from tabulate import tabulate
5
4
import show .vlan as show_vlan
8
7
from swsscommon .swsscommon import ConfigDBConnector
9
8
from swsscommon .swsscommon import SonicV2Connector
10
9
10
+
11
11
# STATE_DB Table
12
- DHCPv4_COUNTER_TABLE = 'DHCP_COUNTER_TABLE'
13
12
DHCPv6_COUNTER_TABLE = 'DHCPv6_COUNTER_TABLE'
14
13
15
- # DHCPv4 Counter Messages
16
- dhcpv4_messages = [
17
- "Unknown" , "Discover" , "Offer" , "Request" , "Decline" , "Ack" , "Nack" , "Release" , "Inform"
18
- ]
19
-
20
14
# 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" ]
25
17
26
18
# DHCP_RELAY Config Table
27
19
DHCP_RELAY = 'DHCP_RELAY'
@@ -45,75 +37,6 @@ def get_dhcp_helper_address(ctx, vlan):
45
37
46
38
show_vlan .VlanBrief .register_column ('DHCP Helper Address' , get_dhcp_helper_address )
47
39
48
- class DHCPv4_Counter (object ):
49
- def __init__ (self ):
50
- self .db = SonicV2Connector (use_unix_socket_path = False )
51
- self .db .connect (self .db .STATE_DB )
52
- self .table_name = DHCPv4_COUNTER_TABLE + self .db .get_db_separator (self .db .STATE_DB )
53
-
54
- def get_interface (self ):
55
- """ Get all names of all interfaces in DHCPv4_COUNTER_TABLE """
56
- interfaces = []
57
- for key in self .db .keys (self .db .STATE_DB ):
58
- if DHCPv4_COUNTER_TABLE in key :
59
- interfaces .append (key [21 :])
60
- return interfaces
61
-
62
- def get_dhcp4relay_msg_count (self , interface , dir ):
63
- """ Get count of a dhcprelay message """
64
- value = self .db .get (self .db .STATE_DB , self .table_name + str (interface ), str (dir ))
65
- cnts = ast .literal_eval (str (value ))
66
- data = []
67
- if cnts is not None :
68
- for k , v in cnts .items ():
69
- data .append ([k , v ])
70
- return data
71
-
72
- def clear_table (self , interface ):
73
- """ Reset all message counts to 0 """
74
- v4_cnts = {}
75
- for msg in dhcpv4_messages :
76
- v4_cnts [msg ] = '0'
77
- self .db .set (self .db .STATE_DB , self .table_name + str (interface ), str ("RX" ), str (v4_cnts ))
78
- self .db .set (self .db .STATE_DB , self .table_name + str (interface ), str ("TX" ), str (v4_cnts ))
79
-
80
- def print_dhcpv4_count (counter , intf ):
81
- """Print count of each message"""
82
- rx_data = counter .get_dhcp4relay_msg_count (intf , "RX" )
83
- print (tabulate (rx_data , headers = ["Message Type" , intf + "(RX)" ], tablefmt = 'simple' , stralign = 'right' ) + "\n " )
84
- tx_data = counter .get_dhcp4relay_msg_count (intf , "TX" )
85
- print (tabulate (tx_data , headers = ["Message Type" , intf + "(TX)" ], tablefmt = 'simple' , stralign = 'right' ) + "\n " )
86
-
87
- #
88
- # 'dhcp4relay_counters' group ###
89
- #
90
-
91
-
92
- @click .group (cls = clicommon .AliasedGroup , name = "dhcp4relay_counters" )
93
- def dhcp4relay_counters ():
94
- """Show DHCPv4 counter"""
95
- pass
96
-
97
-
98
- def ipv4_counters (interface ):
99
- counter = DHCPv4_Counter ()
100
- counter_intf = counter .get_interface ()
101
-
102
- if interface :
103
- print_dhcpv4_count (counter , interface )
104
- else :
105
- for intf in counter_intf :
106
- print_dhcpv4_count (counter , intf )
107
-
108
-
109
- # 'counts' subcommand ("show dhcp4relay_counters counts")
110
- @dhcp4relay_counters .command ('counts' )
111
- @click .option ('-i' , '--interface' , required = False )
112
- @click .option ('--verbose' , is_flag = True , help = "Enable verbose output" )
113
- def counts (interface , verbose ):
114
- """Show dhcp4relay message counts"""
115
- ipv4_counters (interface )
116
-
117
40
118
41
class DHCPv6_Counter (object ):
119
42
def __init__ (self ):
@@ -123,37 +46,30 @@ def __init__(self):
123
46
124
47
def get_interface (self ):
125
48
""" Get all names of all interfaces in DHCPv6_COUNTER_TABLE """
126
- interfaces = []
49
+ vlans = []
127
50
for key in self .db .keys (self .db .STATE_DB ):
128
51
if DHCPv6_COUNTER_TABLE in key :
129
- interfaces .append (key [21 :])
130
- return interfaces
52
+ vlans .append (key [21 :])
53
+ return vlans
131
54
132
- def get_dhcp6relay_msg_count (self , interface , dir ):
55
+ def get_dhcp6relay_msg_count (self , interface , msg ):
133
56
""" Get count of a dhcp6relay message """
134
- value = self .db .get (self .db .STATE_DB , self .table_name + str (interface ), str (dir ))
135
- cnts = ast .literal_eval (str (value ))
136
- data = []
137
- if cnts is not None :
138
- for k , v in cnts .items ():
139
- data .append ([k , v ])
57
+ count = self .db .get (self .db .STATE_DB , self .table_name + str (interface ), str (msg ))
58
+ data = [str (msg ), count ]
140
59
return data
141
60
142
61
def clear_table (self , interface ):
143
62
""" Reset all message counts to 0 """
144
- v6_cnts = {}
145
- for msg in dhcpv6_messages :
146
- v6_cnts [msg ] = '0'
147
- self .db .set (self .db .STATE_DB , self .table_name + str (interface ), str ("RX" ), str (v6_cnts ))
148
- self .db .set (self .db .STATE_DB , self .table_name + str (interface ), str ("TX" ), str (v6_cnts ))
63
+ for msg in messages :
64
+ self .db .set (self .db .STATE_DB , self .table_name + str (interface ), str (msg ), '0' )
149
65
150
66
151
- def print_dhcpv6_count (counter , intf ):
67
+ def print_count (counter , intf ):
152
68
"""Print count of each message"""
153
- rx_data = counter . get_dhcp6relay_msg_count ( intf , "RX" )
154
- print ( tabulate ( rx_data , headers = [ "Message Type" , intf + "(RX)" ], tablefmt = 'simple' , stralign = 'right' ) + " \n " )
155
- tx_data = counter .get_dhcp6relay_msg_count (intf , "TX" )
156
- print (tabulate (tx_data , headers = ["Message Type" , intf + "(TX)" ], tablefmt = 'simple' , stralign = 'right' ) + "\n " )
69
+ data = []
70
+ for i in messages :
71
+ data . append ( counter .get_dhcp6relay_msg_count (intf , i ) )
72
+ print (tabulate (data , headers = ["Message Type" , intf ], tablefmt = 'simple' , stralign = 'right' ) + "\n " )
157
73
158
74
159
75
#
@@ -172,10 +88,10 @@ def ipv6_counters(interface):
172
88
counter_intf = counter .get_interface ()
173
89
174
90
if interface :
175
- print_dhcpv6_count (counter , interface )
91
+ print_count (counter , interface )
176
92
else :
177
93
for intf in counter_intf :
178
- print_dhcpv6_count (counter , intf )
94
+ print_count (counter , intf )
179
95
180
96
181
97
# 'counts' subcommand ("show dhcp6relay_counters counts")
@@ -184,6 +100,7 @@ def ipv6_counters(interface):
184
100
@click .option ('--verbose' , is_flag = True , help = "Enable verbose output" )
185
101
def counts (interface , verbose ):
186
102
"""Show dhcp6relay message counts"""
103
+
187
104
ipv6_counters (interface )
188
105
189
106
@@ -282,7 +199,6 @@ def dhcp_relay_ip6counters(interface):
282
199
283
200
284
201
def register (cli ):
285
- cli .add_command (dhcp4relay_counters )
286
202
cli .add_command (dhcp6relay_counters )
287
203
cli .add_command (dhcp_relay_helper )
288
204
cli .add_command (dhcp_relay )
0 commit comments