3
3
import click
4
4
import swsssdk
5
5
from tabulate import tabulate
6
+ from utilities_common import multi_asic as multi_asic_util
7
+ from sonic_py_common import multi_asic
6
8
7
9
class Crm :
8
- def __init__ (self ):
10
+ def __init__ (self , db = None ):
9
11
self .cli_mode = None
10
12
self .addr_family = None
11
13
self .res_type = None
14
+ self .db = None
15
+ self .cfgdb = db
16
+ self .multi_asic = multi_asic_util .MultiAsic ()
12
17
18
+ @multi_asic_util .run_on_multi_asic
13
19
def config (self , attr , val ):
14
20
"""
15
21
CRM handler for 'config' CLI commands.
16
22
"""
17
- configdb = swsssdk .ConfigDBConnector ()
18
- configdb .connect ()
19
-
20
- configdb .mod_entry ("CRM" , 'Config' , {attr : val })
23
+ if self .cfgdb :
24
+ self .config_db = self .cfgdb
25
+ self .config_db .mod_entry ("CRM" , 'Config' , {attr : val })
21
26
22
27
def show_summary (self ):
23
28
"""
24
29
CRM Handler to display general information.
25
30
"""
26
- configdb = swsssdk .ConfigDBConnector ()
27
- configdb .connect ()
31
+
32
+ configdb = self .cfgdb
33
+ if configdb is None :
34
+ # Get the namespace list
35
+ namespaces = multi_asic .get_namespace_list ()
36
+
37
+ configdb = swsssdk .ConfigDBConnector (namespace = namespaces [0 ])
38
+ configdb .connect ()
28
39
29
40
crm_info = configdb .get_entry ('CRM' , 'Config' )
30
41
@@ -37,8 +48,14 @@ def show_thresholds(self, resource):
37
48
"""
38
49
CRM Handler to display thresholds information.
39
50
"""
40
- configdb = swsssdk .ConfigDBConnector ()
41
- configdb .connect ()
51
+
52
+ configdb = self .cfgdb
53
+ if configdb is None :
54
+ # Get the namespace list
55
+ namespaces = multi_asic .get_namespace_list ()
56
+
57
+ configdb = swsssdk .ConfigDBConnector (namespace = namespaces [0 ])
58
+ configdb .connect ()
42
59
43
60
crm_info = configdb .get_entry ('CRM' , 'Config' )
44
61
@@ -60,16 +77,11 @@ def show_thresholds(self, resource):
60
77
click .echo (tabulate (data , headers = header , tablefmt = "simple" , missingval = "" ))
61
78
click .echo ()
62
79
63
- def show_resources (self , resource ):
80
+ def get_resources (self , resource ):
64
81
"""
65
- CRM Handler to display resources information.
82
+ CRM Handler to get resources information.
66
83
"""
67
- countersdb = swsssdk .SonicV2Connector (host = '127.0.0.1' )
68
- countersdb .connect (countersdb .COUNTERS_DB )
69
-
70
- crm_stats = countersdb .get_all (countersdb .COUNTERS_DB , 'CRM:STATS' )
71
-
72
- header = ("Resource Name" , "Used Count" , "Available Count" )
84
+ crm_stats = self .db .get_all (self .db .COUNTERS_DB , 'CRM:STATS' )
73
85
data = []
74
86
75
87
if crm_stats :
@@ -79,26 +91,18 @@ def show_resources(self, resource):
79
91
data .append ([res , crm_stats ['crm_stats_' + res + "_used" ], crm_stats ['crm_stats_' + res + "_available" ]])
80
92
else :
81
93
data .append ([resource , crm_stats ['crm_stats_' + resource + "_used" ], crm_stats ['crm_stats_' + resource + "_available" ]])
82
- else :
83
- click .echo ('\n CRM counters are not ready. They would be populated after the polling interval.' )
84
94
85
- click .echo ()
86
- click .echo (tabulate (data , headers = header , tablefmt = "simple" , missingval = "" ))
87
- click .echo ()
95
+ return data
88
96
89
- def show_acl_resources (self ):
97
+ def get_acl_resources (self ):
90
98
"""
91
- CRM Handler to display ACL recources information.
99
+ CRM Handler to get ACL recources information.
92
100
"""
93
- countersdb = swsssdk .SonicV2Connector (host = '127.0.0.1' )
94
- countersdb .connect (countersdb .COUNTERS_DB )
95
-
96
- header = ("Stage" , "Bind Point" , "Resource Name" , "Used Count" , "Available Count" )
97
101
data = []
98
102
99
103
for stage in ["INGRESS" , "EGRESS" ]:
100
104
for bind_point in ["PORT" , "LAG" , "VLAN" , "RIF" , "SWITCH" ]:
101
- crm_stats = countersdb . get_all (countersdb .COUNTERS_DB , 'CRM:ACL_STATS:{0}:{1}' .format (stage , bind_point ))
105
+ crm_stats = self . db . get_all (self . db .COUNTERS_DB , 'CRM:ACL_STATS:{0}:{1}' .format (stage , bind_point ))
102
106
103
107
if crm_stats :
104
108
for res in ["acl_group" , "acl_table" ]:
@@ -108,51 +112,102 @@ def show_acl_resources(self):
108
112
crm_stats ['crm_stats_' + res + "_available" ]
109
113
])
110
114
111
- click .echo ()
112
- click .echo (tabulate (data , headers = header , tablefmt = "simple" , missingval = "" ))
113
- click .echo ()
114
-
115
- def show_acl_table_resources (self ):
115
+ return data
116
+ def get_acl_table_resources (self ):
116
117
"""
117
118
CRM Handler to display ACL table information.
118
119
"""
119
- countersdb = swsssdk .SonicV2Connector (host = '127.0.0.1' )
120
- countersdb .connect (countersdb .COUNTERS_DB )
121
-
122
- header = ("Table ID" , "Resource Name" , "Used Count" , "Available Count" )
123
-
124
120
# Retrieve all ACL table keys from CRM:ACL_TABLE_STATS
125
- crm_acl_keys = countersdb .keys (countersdb .COUNTERS_DB , 'CRM:ACL_TABLE_STATS*' )
121
+ crm_acl_keys = self .db .keys (self .db .COUNTERS_DB , 'CRM:ACL_TABLE_STATS*' )
122
+ data = []
126
123
127
124
for key in crm_acl_keys or [None ]:
128
- data = []
129
-
130
125
if key :
131
126
id = key .replace ('CRM:ACL_TABLE_STATS:' , '' )
132
127
133
- crm_stats = countersdb . get_all (countersdb .COUNTERS_DB , key )
128
+ crm_stats = self . db . get_all (self . db .COUNTERS_DB , key )
134
129
135
130
for res in ['acl_entry' , 'acl_counter' ]:
136
131
if ('crm_stats_' + res + '_used' in crm_stats ) and ('crm_stats_' + res + '_available' in crm_stats ):
137
132
data .append ([id , res , crm_stats ['crm_stats_' + res + '_used' ], crm_stats ['crm_stats_' + res + '_available' ]])
138
133
134
+ return data
135
+
136
+ @multi_asic_util .run_on_multi_asic
137
+ def show_resources (self , resource ):
138
+ """
139
+ CRM Handler to display resources information.
140
+ """
141
+ if multi_asic .is_multi_asic ():
142
+ header = (self .multi_asic .current_namespace .upper () + "\n \n Resource Name" , "\n \n Used Count" , "\n \n Available Count" )
143
+ err_msg = '\n CRM counters are not ready for ' + self .multi_asic .current_namespace .upper () + '. They would be populated after the polling interval.'
144
+ else :
145
+ header = ("Resource Name" , "Used Count" , "Available Count" )
146
+ err_msg = '\n CRM counters are not ready. They would be populated after the polling interval.'
147
+
148
+ data = []
149
+ data = self .get_resources (resource )
150
+
151
+ if data :
139
152
click .echo ()
140
153
click .echo (tabulate (data , headers = header , tablefmt = "simple" , missingval = "" ))
141
154
click .echo ()
155
+ else :
156
+ click .echo (err_msg )
157
+
158
+ @multi_asic_util .run_on_multi_asic
159
+ def show_acl_resources (self ):
160
+ """
161
+ CRM Handler to display ACL recources information.
162
+ """
163
+
164
+ if multi_asic .is_multi_asic ():
165
+ header = (self .multi_asic .current_namespace .upper () + "\n \n Stage" , "\n \n Bind Point" , "\n \n Resource Name" , "\n \n Used Count" , "\n \n Available Count" )
166
+ else :
167
+ header = ("Stage" , "Bind Point" , "Resource Name" , "Used Count" , "Available Count" )
168
+
169
+ data = []
170
+ data = self .get_acl_resources ()
171
+
172
+ click .echo ()
173
+ click .echo (tabulate (data , headers = header , tablefmt = "simple" , missingval = "" ))
174
+ click .echo ()
142
175
176
+ @multi_asic_util .run_on_multi_asic
177
+ def show_acl_table_resources (self ):
178
+ """
179
+ CRM Handler to display ACL table information.
180
+ """
181
+ if multi_asic .is_multi_asic ():
182
+ header = (self .multi_asic .current_namespace .upper () + "\n \n Table ID" , "\n \n Resource Name" , "\n \n Used Count" , "\n \n Available Count" )
183
+ else :
184
+ header = ("Table ID" , "Resource Name" , "Used Count" , "Available Count" )
185
+
186
+ data = []
187
+ data = self .get_acl_table_resources ()
188
+
189
+ click .echo ()
190
+ click .echo (tabulate (data , headers = header , tablefmt = "simple" , missingval = "" ))
191
+ click .echo ()
143
192
144
193
@click .group ()
145
194
@click .pass_context
146
195
def cli (ctx ):
147
196
"""
148
197
Utility entry point.
149
198
"""
199
+ # Use the db object if given as input.
200
+ db = None if ctx .obj is None else ctx .obj .cfgdb
201
+
150
202
context = {
151
- "crm" : Crm ()
203
+ "crm" : Crm (db )
152
204
}
153
205
154
206
ctx .obj = context
155
207
208
+ # Load the global config file database_global.json once.
209
+ swsssdk .SonicDBConfig .load_sonic_global_db_config ()
210
+
156
211
@cli .group ()
157
212
@click .pass_context
158
213
def config (ctx ):
0 commit comments