forked from sonic-net/sonic-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintfstat
executable file
·364 lines (312 loc) · 13.3 KB
/
intfstat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env python3
#####################################################################
#
# intfstat is a tool for summarizing l3 network statistics.
#
#####################################################################
import _pickle as pickle
import argparse
import datetime
import sys
import os
import time
# mock the redis for unit test purposes #
try:
if os.environ["UTILITIES_UNIT_TESTING"] == "2":
modules_path = os.path.join(os.path.dirname(__file__), "..")
test_path = os.path.join(modules_path, "tests")
sys.path.insert(0, modules_path)
sys.path.insert(0, test_path)
import mock_tables.dbconnector
except KeyError:
pass
from collections import namedtuple, OrderedDict
from natsort import natsorted
from tabulate import tabulate
from utilities_common.netstat import ns_diff, table_as_json, STATUS_NA, format_brate, format_prate
from utilities_common.cli import UserCache
from swsscommon.swsscommon import SonicV2Connector
nstat_fields = (
"rx_b_ok",
"rx_p_ok",
"tx_b_ok",
"tx_p_ok",
"rx_b_err",
"rx_p_err",
"tx_b_err",
"tx_p_err"
)
NStats = namedtuple("NStats", nstat_fields)
header = [
'IFACE',
'RX_OK',
'RX_BPS',
'RX_PPS',
'RX_ERR',
'TX_OK',
'TX_BPS',
'TX_PPS',
'TX_ERR'
]
rates_key_list = [ 'RX_BPS', 'RX_PPS', 'TX_BPS', 'TX_PPS']
ratestat_fields = ("rx_bps", "rx_pps", "tx_bps", "tx_pps")
RateStats = namedtuple("RateStats", ratestat_fields)
counter_names = (
'SAI_ROUTER_INTERFACE_STAT_IN_OCTETS',
'SAI_ROUTER_INTERFACE_STAT_IN_PACKETS',
'SAI_ROUTER_INTERFACE_STAT_OUT_OCTETS',
'SAI_ROUTER_INTERFACE_STAT_OUT_PACKETS',
'SAI_ROUTER_INTERFACE_STAT_IN_ERROR_OCTETS',
'SAI_ROUTER_INTERFACE_STAT_IN_ERROR_PACKETS',
'SAI_ROUTER_INTERFACE_STAT_OUT_ERROR_OCTETS',
'SAI_ROUTER_INTERFACE_STAT_OUT_ERROR_PACKETS'
)
RATES_TABLE_PREFIX = "RATES:"
COUNTER_TABLE_PREFIX = "COUNTERS:"
COUNTERS_RIF_NAME_MAP = "COUNTERS_RIF_NAME_MAP"
class Intfstat(object):
def __init__(self):
self.db = SonicV2Connector(use_unix_socket_path=False)
self.db.connect(self.db.COUNTERS_DB)
self.db.connect(self.db.APPL_DB)
def get_cnstat(self, rif=None):
"""
Get the counters info from database.
"""
def get_counters(table_id):
"""
Get the counters from specific table.
"""
fields = [STATUS_NA] * len(nstat_fields)
for pos, counter_name in enumerate(counter_names):
full_table_id = COUNTER_TABLE_PREFIX + table_id
counter_data = self.db.get(self.db.COUNTERS_DB, full_table_id, counter_name)
if counter_data:
fields[pos] = str(counter_data)
cntr = NStats._make(fields)
return cntr
def get_rates(table_id):
"""
Get the rates from specific table.
"""
fields = ["0","0","0","0"]
for pos, name in enumerate(rates_key_list):
full_table_id = RATES_TABLE_PREFIX + table_id
counter_data = self.db.get(self.db.COUNTERS_DB, full_table_id, name)
if counter_data is None:
fields[pos] = STATUS_NA
elif fields[pos] != STATUS_NA:
fields[pos] = float(counter_data)
cntr = RateStats._make(fields)
return cntr
# Build a dictionary of the stats
cnstat_dict = OrderedDict()
cnstat_dict['time'] = datetime.datetime.now()
ratestat_dict = OrderedDict()
# Get the info from database
counter_rif_name_map = self.db.get_all(self.db.COUNTERS_DB, COUNTERS_RIF_NAME_MAP)
if counter_rif_name_map is None:
print("No %s in the DB!" % COUNTERS_RIF_NAME_MAP)
sys.exit(1)
if rif and not rif in counter_rif_name_map:
print("Interface %s missing from %s! Make sure it exists" % (rif, COUNTERS_RIF_NAME_MAP))
sys.exit(2)
if rif:
cnstat_dict[rif] = get_counters(counter_rif_name_map[rif])
ratestat_dict[rif] = get_rates(counter_rif_name_map[rif])
return cnstat_dict, ratestat_dict
for rif in natsorted(counter_rif_name_map):
cnstat_dict[rif] = get_counters(counter_rif_name_map[rif])
ratestat_dict[rif] = get_rates(counter_rif_name_map[rif])
return cnstat_dict, ratestat_dict
def cnstat_print(self, cnstat_dict, ratestat_dict, use_json):
"""
Print the cnstat.
"""
table = []
for key, data in cnstat_dict.items():
if key == 'time':
continue
rates = ratestat_dict.get(key, RateStats._make([STATUS_NA] * len(rates_key_list)))
table.append((key,
data.rx_p_ok,
format_brate(rates.rx_bps),
format_prate(rates.rx_pps),
data.rx_p_err,
data.tx_p_ok,
format_brate(rates.tx_bps),
format_prate(rates.tx_pps),
data.tx_p_err))
if use_json:
print(table_as_json(table, header))
else:
print(tabulate(table, header, tablefmt='simple', stralign='right'))
def cnstat_diff_print(self, cnstat_new_dict, cnstat_old_dict, ratestat_dict, use_json):
"""
Print the difference between two cnstat results.
"""
table = []
for key, cntr in cnstat_new_dict.items():
if key == 'time':
continue
old_cntr = None
if key in cnstat_old_dict:
old_cntr = cnstat_old_dict.get(key)
rates = ratestat_dict.get(key, RateStats._make([STATUS_NA] * len(rates_key_list)))
if old_cntr is not None:
table.append((key,
ns_diff(cntr.rx_p_ok, old_cntr.rx_p_ok),
format_brate(rates.rx_bps),
format_prate(rates.rx_pps),
ns_diff(cntr.rx_p_err, old_cntr.rx_p_err),
ns_diff(cntr.tx_p_ok, old_cntr.tx_p_ok),
format_brate(rates.tx_bps),
format_prate(rates.tx_pps),
ns_diff(cntr.tx_p_err, old_cntr.tx_p_err)))
else:
table.append((key,
cntr.rx_p_ok,
format_brate(rates.rx_bps),
format_prate(rates.rx_pps),
cntr.rx_p_err,
cntr.tx_p_ok,
format_brate(rates.tx_bps),
format_prate(rates.tx_pps),
cntr.tx_p_err))
if use_json:
print(table_as_json(table, header))
else:
print(tabulate(table, header, tablefmt='simple', stralign='right'))
def cnstat_single_interface(self, rif, cnstat_new_dict, cnstat_old_dict):
header = rif + '\n' + '-'*len(rif)
body = """
RX:
%10s packets
%10s bytes
%10s error packets
%10s error bytes
TX:
%10s packets
%10s bytes
%10s error packets
%10s error bytes"""
cntr = cnstat_new_dict.get(rif)
if cnstat_old_dict and cnstat_old_dict.get(rif):
old_cntr = cnstat_old_dict.get(rif)
body = body % (ns_diff(cntr.rx_p_ok, old_cntr.rx_p_ok),
ns_diff(cntr.rx_b_ok, old_cntr.rx_b_ok),
ns_diff(cntr.rx_p_err, old_cntr.rx_p_err),
ns_diff(cntr.rx_b_err, old_cntr.rx_b_err),
ns_diff(cntr.tx_p_ok, old_cntr.tx_p_ok),
ns_diff(cntr.tx_b_ok, old_cntr.tx_b_ok),
ns_diff(cntr.tx_p_err, old_cntr.tx_p_err),
ns_diff(cntr.tx_b_err, old_cntr.tx_b_err))
else:
body = body % (cntr.rx_p_ok, cntr.rx_b_ok, cntr.rx_p_err,cntr.rx_b_err,
cntr.tx_p_ok, cntr.tx_b_ok, cntr.tx_p_err, cntr.tx_b_err)
print(header)
print(body)
def main():
parser = argparse.ArgumentParser(description='Display the interfaces state and counters',
formatter_class=argparse.RawTextHelpFormatter,
epilog="""
Port state: (U)-Up (D)-Down (X)-Disabled
Examples:
intfstat -c -t test
intfstat -t test
intfstat -d -t test
intfstat
intfstat -r
intfstat -a
intfstat -p 20
intfstat -i Vlan1000
""")
parser.add_argument('-c', '--clear', action='store_true', help='Copy & clear stats')
parser.add_argument('-d', '--delete', action='store_true', help='Delete saved stats, either the uid or the specified tag')
parser.add_argument('-D', '--delete-all', action='store_true', help='Delete all saved stats')
parser.add_argument('-j', '--json', action='store_true', help='Display in JSON format')
parser.add_argument('-t', '--tag', type=str, help='Save stats with name TAG', default=None)
parser.add_argument('-i', '--interface', type=str, help='Show stats for a single interface', required=False)
parser.add_argument('-p', '--period', type=int, help='Display stats over a specified period (in seconds).', default=0)
parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0')
args = parser.parse_args()
save_fresh_stats = args.clear
delete_saved_stats = args.delete
delete_all_stats = args.delete_all
use_json = args.json
tag_name = args.tag
wait_time_in_seconds = args.period
interface_name = args.interface if args.interface else ""
cnstat_file = "intfstat"
cache = UserCache(tag=tag_name)
cache_general = UserCache()
cnstat_dir = cache.get_directory()
cnstat_general_dir = cache_general.get_directory()
cnstat_fqn_general_file = cnstat_general_dir + "/" + cnstat_file
cnstat_fqn_file = cnstat_dir + "/" + cnstat_file
if delete_all_stats:
cache.remove_all()
if delete_saved_stats:
cache.remove()
intfstat = Intfstat()
cnstat_dict, ratestat_dict = intfstat.get_cnstat(rif=interface_name)
if save_fresh_stats:
try:
# Add the information also to the general file - i.e. without the tag name
if tag_name is not None:
if os.path.isfile(cnstat_fqn_general_file):
try:
general_data = pickle.load(open(cnstat_fqn_general_file, 'rb'))
for key, val in cnstat_dict.items():
general_data[key] = val
pickle.dump(general_data, open(cnstat_fqn_general_file, 'wb'))
except IOError as e:
sys.exit(e.errno)
# Add the information also to tag specific file
if os.path.isfile(cnstat_fqn_file):
data = pickle.load(open(cnstat_fqn_file, 'rb'))
for key, val in cnstat_dict.items():
data[key] = val
pickle.dump(data, open(cnstat_fqn_file, 'wb'))
else:
pickle.dump(cnstat_dict, open(cnstat_fqn_file, 'wb'))
except IOError as e:
sys.exit(e.errno)
else:
print("Cleared counters")
sys.exit(0)
if wait_time_in_seconds == 0:
if os.path.isfile(cnstat_fqn_file) or (os.path.isfile(cnstat_fqn_general_file)):
try:
cnstat_cached_dict = {}
if os.path.isfile(cnstat_fqn_file):
cnstat_cached_dict = pickle.load(open(cnstat_fqn_file, 'rb'))
else:
cnstat_cached_dict = pickle.load(open(cnstat_fqn_general_file, 'rb'))
print("Last cached time was " + str(cnstat_cached_dict.get('time')))
if interface_name:
intfstat.cnstat_single_interface(interface_name, cnstat_dict, cnstat_cached_dict)
else:
intfstat.cnstat_diff_print(cnstat_dict, cnstat_cached_dict, ratestat_dict, use_json)
except IOError as e:
print(e.errno, e)
else:
if tag_name:
print("\nFile '%s' does not exist" % cnstat_fqn_file)
print("Did you run 'intfstat -c -t %s' to record the counters via tag %s?\n" % (tag_name, tag_name))
else:
if interface_name:
intfstat.cnstat_single_interface(interface_name, cnstat_dict, None)
else:
intfstat.cnstat_print(cnstat_dict, ratestat_dict, use_json)
else:
#wait for the specified time and then gather the new stats and output the difference.
time.sleep(wait_time_in_seconds)
print("The rates are calculated within %s seconds period" % wait_time_in_seconds)
cnstat_new_dict, ratestat_new_dict = intfstat.get_cnstat(rif=interface_name)
if interface_name:
intfstat.cnstat_single_interface(interface_name, cnstat_new_dict, cnstat_dict)
else:
intfstat.cnstat_diff_print(cnstat_new_dict, cnstat_dict, ratestat_new_dict, use_json)
if __name__ == "__main__":
main()