forked from sonic-net/sonic-snmpagent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
779 lines (651 loc) · 25.6 KB
/
Copy path__init__.py
File metadata and controls
779 lines (651 loc) · 25.6 KB
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
import pprint
import re
import os
from swsssdk import SonicV2Connector
from swsssdk import SonicDBConfig
from swsssdk import port_util
from swsssdk.port_util import get_index_from_str
from ax_interface.mib import MIBUpdater
from ax_interface.util import oid2tuple
from sonic_ax_impl import logger
COUNTERS_PORT_NAME_MAP = 'COUNTERS_PORT_NAME_MAP'
COUNTERS_QUEUE_NAME_MAP = 'COUNTERS_QUEUE_NAME_MAP'
LAG_TABLE = 'LAG_TABLE'
LAG_MEMBER_TABLE = 'LAG_MEMBER_TABLE'
LOC_CHASSIS_TABLE = 'LLDP_LOC_CHASSIS'
APPL_DB = 'APPL_DB'
ASIC_DB = 'ASIC_DB'
COUNTERS_DB = 'COUNTERS_DB'
CONFIG_DB = 'CONFIG_DB'
STATE_DB = 'STATE_DB'
SNMP_OVERLAY_DB = 'SNMP_OVERLAY_DB'
TABLE_NAME_SEPARATOR_COLON = ':'
TABLE_NAME_SEPARATOR_VBAR = '|'
"""
For non-port entity, the rule to generate entPhysicalIndex describes below:
The entPhysicalIndex is divided into 3 layers:
1. Module layer which includes modules located on system (e.g. fan drawer, PSU)
2. Device layer which includes system devices (e.g. fan )
3. Sensor layer which includes system sensors (e.g. temperature sensor, fan sensor)
The entPhysicalIndex is a 9 digits number, and each digit describes below:
Digit 1: Module Type
Digit 2~3: Module Index
Digit 4~5: Device Type
Digit 6~7: Device Index
Digit 8: Sensor Type
Digit 9: Sensor Index
Module Type describes below:
2 - Management
5 - Fan Drawer
6 - PSU
Device Type describes below:
01 - PS
02 - Fan
24 - Power Monitor (temperature, power, current, voltage...)
99 - Chassis Thermals
Sensor Type describes below:
1 - Temperature
2 - Fan Tachometers
3 - Power
4 - Current
5 - Voltage
e.g. 501000000 means the first fan drawer, 502020100 means the first fan of the second fan drawer
As we are using ifindex to generate port entPhysicalIndex and ifindex might be a valur larger
than 99, we uses a different way to generate port entPhysicalIndex.
For port entity, the entPhysicalIndex is a 10 digits number, and each digit describes below:
Digit 1: 1
Digit 2~8: ifindex
Digit 9: Sensor Type
Digit 10: Sensor Index
Port Sensor Type describes below:
1 - Temperature
2 - TX Power
3 - RX Power
4 - TX BIAS
5 - Voltage
"""
# Moduel Type Definition
MODULE_TYPE_MULTIPLE = 100000000
MODULE_INDEX_MULTIPLE = 1000000
MODULE_TYPE_MGMT = 2 * MODULE_TYPE_MULTIPLE
MODULE_TYPE_FAN_DRAWER = 5 * MODULE_TYPE_MULTIPLE
MODULE_TYPE_PSU = 6 * MODULE_TYPE_MULTIPLE
MODULE_TYPE_PORT = 1000000000
# Device Type Definition
DEVICE_TYPE_MULTIPLE = 10000
DEVICE_INDEX_MULTIPLE = 100
DEVICE_TYPE_PS = 1 * DEVICE_TYPE_MULTIPLE
DEVICE_TYPE_FAN = 2 * DEVICE_TYPE_MULTIPLE
DEVICE_TYPE_CHASSIS_THERMAL = 99 * DEVICE_TYPE_MULTIPLE
DEVICE_TYPE_POWER_MONITOR = 24 * DEVICE_TYPE_MULTIPLE
# Sensor Type Definition
SENSOR_TYPE_MULTIPLE = 10
SENSOR_TYPE_TEMP = 1 * SENSOR_TYPE_MULTIPLE
SENSOR_TYPE_FAN = 2 * SENSOR_TYPE_MULTIPLE
SENSOR_TYPE_POWER = 3 * SENSOR_TYPE_MULTIPLE
SENSOR_TYPE_CURRENT = 4 * SENSOR_TYPE_MULTIPLE
SENSOR_TYPE_VOLTAGE = 5 * SENSOR_TYPE_MULTIPLE
# Port entPhysicalIndex Definition
PORT_IFINDEX_MULTIPLE = 100
SENSOR_TYPE_PORT_TX_POWER = 2 * SENSOR_TYPE_MULTIPLE
SENSOR_TYPE_PORT_RX_POWER = 3 * SENSOR_TYPE_MULTIPLE
SENSOR_TYPE_PORT_TX_BIAS = 4 * SENSOR_TYPE_MULTIPLE
CHASSIS_SUB_ID = 1
CHASSIS_MGMT_SUB_ID = MODULE_TYPE_MGMT
# This is used in both rfc2737 and rfc3433
XCVR_SENSOR_PART_ID_MAP = {
"temperature": SENSOR_TYPE_TEMP,
"tx1power": SENSOR_TYPE_PORT_TX_POWER + 1,
"tx2power": SENSOR_TYPE_PORT_TX_POWER + 2,
"tx3power": SENSOR_TYPE_PORT_TX_POWER + 3,
"tx4power": SENSOR_TYPE_PORT_TX_POWER + 4,
"rx1power": SENSOR_TYPE_PORT_RX_POWER + 1,
"rx2power": SENSOR_TYPE_PORT_RX_POWER + 2,
"rx3power": SENSOR_TYPE_PORT_RX_POWER + 3,
"rx4power": SENSOR_TYPE_PORT_RX_POWER + 4,
"tx1bias": SENSOR_TYPE_PORT_TX_BIAS + 1,
"tx2bias": SENSOR_TYPE_PORT_TX_BIAS + 2,
"tx3bias": SENSOR_TYPE_PORT_TX_BIAS + 3,
"tx4bias": SENSOR_TYPE_PORT_TX_BIAS + 4,
"voltage": SENSOR_TYPE_VOLTAGE,
}
PSU_SENSOR_PART_ID_MAP = {
'temperature': SENSOR_TYPE_TEMP,
'power': SENSOR_TYPE_POWER,
'current': SENSOR_TYPE_CURRENT,
'voltage': SENSOR_TYPE_VOLTAGE
}
redis_kwargs = {'unix_socket_path': '/var/run/redis/redis.sock'}
def get_neigh_info(neigh_key):
"""
split neigh_key string of the format:
NEIGH_TABLE:device:ipv4_address
"""
_, device, ip = neigh_key.split(':')
return device, ip
def chassis_info_table(chassis_name):
"""
:param: chassis_name: chassis name
:return: chassis info entry for this chassis
"""
return "CHASSIS_INFO" + TABLE_NAME_SEPARATOR_VBAR + chassis_name
def fan_info_table(fan_name):
"""
:param: fan_name: fan name
:return: fan info entry for this fan
"""
return 'FAN_INFO' + TABLE_NAME_SEPARATOR_VBAR + fan_name
def fan_drawer_info_table(drawer_name):
"""
:param: drawer_name: fan drawer name
:return: fan drawer info entry for this fan
"""
return 'FAN_DRAWER_INFO' + TABLE_NAME_SEPARATOR_VBAR + drawer_name
def psu_info_table(psu_name):
"""
:param: psu_name: psu name
:return: psu info entry for this psu
"""
return "PSU_INFO" + TABLE_NAME_SEPARATOR_VBAR + psu_name
def physical_entity_info_table(name):
"""
:param: name: object name
:return: entity info entry for this object
"""
return 'PHYSICAL_ENTITY_INFO' + TABLE_NAME_SEPARATOR_VBAR + name
def counter_table(sai_id):
"""
:param if_name: given sai_id to cast.
:return: COUNTERS table key.
"""
return 'COUNTERS:oid:0x' + sai_id
def queue_table(sai_id):
"""
:param sai_id: given sai_id to cast.
:return: COUNTERS table key.
"""
return 'COUNTERS:' + sai_id
def queue_key(port_index, queue_index):
return str(port_index) + ':' + str(queue_index)
def transceiver_info_table(port_name):
"""
:param: port_name: port name
:return: transceiver info entry for this port
"""
return "TRANSCEIVER_INFO" + TABLE_NAME_SEPARATOR_VBAR + port_name
def transceiver_dom_table(port_name):
"""
:param: port_name: port name
:return: transceiver dom entry for this port
"""
return "TRANSCEIVER_DOM_SENSOR" + TABLE_NAME_SEPARATOR_VBAR + port_name
def thermal_info_table(thermal_name):
"""
:param: port_name: port name
:return: transceiver dom entry for this port
"""
return "TEMPERATURE_INFO" + TABLE_NAME_SEPARATOR_VBAR + thermal_name
def lldp_entry_table(if_name):
"""
:param if_name: given interface to cast.
:return: LLDP_ENTRY_TABLE key.
"""
return 'LLDP_ENTRY_TABLE:' + if_name
def if_entry_table(if_name):
"""
:param if_name: given interface to cast.
:return: PORT_TABLE key.
"""
return 'PORT_TABLE:' + if_name
def lag_entry_table(lag_name):
"""
:param lag_name: given lag to cast.
:return: LAG_TABLE key.
"""
return 'LAG_TABLE:' + lag_name
def mgmt_if_entry_table(if_name):
"""
:param if_name: given interface to cast
:return: MGMT_PORT_TABLE key
"""
return 'MGMT_PORT|' + if_name
def mgmt_if_entry_table_state_db(if_name):
"""
:param if_name: given interface to cast
:return: MGMT_PORT_TABLE key
"""
return 'MGMT_PORT_TABLE|' + if_name
def get_sai_id_key(namespace, sai_id):
"""
inputs:
namespace - string
sai id - bytes
Return type:
bytes
Return value: namespace:sai id or sai id
"""
if namespace != '':
return namespace + ':' + sai_id
else:
return sai_id
def split_sai_id_key(sai_id_key):
"""
Input - bytes
Return namespace string and sai id in byte string.
"""
result = sai_id_key.split(':')
if len(result) == 1:
return '', sai_id_key
else:
return result[0], result[1]
def config(**kwargs):
global redis_kwargs
redis_kwargs = {k:v for (k,v) in kwargs.items() if k in ['unix_socket_path', 'host', 'port']}
redis_kwargs['decode_responses'] = True
def init_db():
"""
Connects to DB
:return: db_conn
"""
SonicDBConfig.load_sonic_global_db_config()
# SyncD database connector. THIS MUST BE INITIALIZED ON A PER-THREAD BASIS.
# Redis PubSub objects (such as those within swsssdk) are NOT thread-safe.
db_conn = SonicV2Connector(**redis_kwargs)
return db_conn
def init_mgmt_interface_tables(db_conn):
"""
Initializes interface maps for mgmt ports
:param db_conn: db connector
:return: tuple of mgmt name to oid map and mgmt name to alias map
"""
db_conn.connect(CONFIG_DB)
db_conn.connect(STATE_DB)
mgmt_ports_keys = db_conn.keys(CONFIG_DB, mgmt_if_entry_table('*'))
if not mgmt_ports_keys:
logger.debug('No managment ports found in {}'.format(mgmt_if_entry_table('')))
return {}, {}
mgmt_ports = [key.split(mgmt_if_entry_table(''))[-1] for key in mgmt_ports_keys]
oid_name_map = {get_index_from_str(mgmt_name): mgmt_name for mgmt_name in mgmt_ports}
logger.debug('Managment port map:\n' + pprint.pformat(oid_name_map, indent=2))
if_alias_map = dict()
for if_name in oid_name_map.values():
if_entry = db_conn.get_all(CONFIG_DB, mgmt_if_entry_table(if_name), blocking=True)
if_alias_map[if_name] = if_entry.get('alias', if_name)
logger.debug("Management alias map:\n" + pprint.pformat(if_alias_map, indent=2))
return oid_name_map, if_alias_map
def init_sync_d_interface_tables(db_conn):
"""
Initializes interface maps for SyncD-connected MIB(s).
:return: tuple(if_name_map, if_id_map, oid_map, if_alias_map)
"""
if_id_map = {}
if_name_map = {}
# { if_name (SONiC) -> sai_id }
# ex: { "Ethernet76" : "1000000000023" }
if_name_map_util, if_id_map_util = port_util.get_interface_oid_map(db_conn)
for if_name, sai_id in if_name_map_util.items():
if_name_str = if_name
if (re.match(port_util.SONIC_ETHERNET_RE_PATTERN, if_name_str) or \
re.match(port_util.SONIC_ETHERNET_BP_RE_PATTERN, if_name_str)):
if_name_map[if_name] = sai_id
# As sai_id is not unique in multi-asic platform, concatenate it with
# namespace to get a unique key. Assuming that ':' is not present in namespace
# string or in sai id.
# sai_id_key = namespace : sai_id
for sai_id, if_name in if_id_map_util.items():
if (re.match(port_util.SONIC_ETHERNET_RE_PATTERN, if_name) or \
re.match(port_util.SONIC_ETHERNET_BP_RE_PATTERN, if_name)):
if_id_map[get_sai_id_key(db_conn.namespace, sai_id)] = if_name
logger.debug("Port name map:\n" + pprint.pformat(if_name_map, indent=2))
logger.debug("Interface name map:\n" + pprint.pformat(if_id_map, indent=2))
# { OID -> if_name (SONiC) }
oid_name_map = {get_index_from_str(if_name): if_name for if_name in if_name_map
# only map the interface if it's a style understood to be a SONiC interface.
if get_index_from_str(if_name) is not None}
logger.debug("OID name map:\n" + pprint.pformat(oid_name_map, indent=2))
# SyncD consistency checks.
if not oid_name_map:
# In the event no interface exists that follows the SONiC pattern, no OIDs are able to be registered.
# A RuntimeError here will prevent the 'main' module from loading. (This is desirable.)
message = "No interfaces found matching pattern '{}'. SyncD database is incoherent." \
.format(port_util.SONIC_ETHERNET_RE_PATTERN)
logger.error(message)
raise RuntimeError(message)
elif len(if_id_map) < len(if_name_map) or len(oid_name_map) < len(if_name_map):
# a length mismatch indicates a bad interface name
logger.warning("SyncD database contains incoherent interface names. Interfaces must match pattern '{}'"
.format(port_util.SONIC_ETHERNET_RE_PATTERN))
logger.warning("Port name map:\n" + pprint.pformat(if_name_map, indent=2))
if_alias_map = dict()
for if_name in if_name_map:
if_entry = db_conn.get_all(APPL_DB, if_entry_table(if_name), blocking=True)
if_alias_map[if_name] = if_entry.get('alias', if_name)
logger.debug("Chassis name map:\n" + pprint.pformat(if_alias_map, indent=2))
return if_name_map, if_alias_map, if_id_map, oid_name_map
def init_sync_d_lag_tables(db_conn):
"""
Helper method. Connects to and initializes LAG interface maps for SyncD-connected MIB(s).
:param db_conn: database connector
:return: tuple(lag_name_if_name_map, if_name_lag_name_map, oid_lag_name_map)
"""
# { lag_name (SONiC) -> [ lag_members (if_name) ] }
# ex: { "PortChannel0" : [ "Ethernet0", "Ethernet4" ] }
lag_name_if_name_map = {}
# { if_name (SONiC) -> lag_name }
# ex: { "Ethernet0" : "PortChannel0" }
if_name_lag_name_map = {}
# { OID -> lag_name (SONiC) }
oid_lag_name_map = {}
db_conn.connect(APPL_DB)
lag_entries = db_conn.keys(APPL_DB, "LAG_TABLE:*")
if not lag_entries:
return lag_name_if_name_map, if_name_lag_name_map, oid_lag_name_map
for lag_entry in lag_entries:
lag_name = lag_entry[len("LAG_TABLE:"):]
lag_members = db_conn.keys(APPL_DB, "LAG_MEMBER_TABLE:%s:*" % lag_name)
# TODO: db_conn.keys() should really return [] instead of None
if lag_members is None:
lag_members = []
def member_name_str(val, lag_name):
return val[len("LAG_MEMBER_TABLE:%s:" % lag_name):]
lag_member_names = [member_name_str(m, lag_name) for m in lag_members]
lag_name_if_name_map[lag_name] = lag_member_names
for lag_member_name in lag_member_names:
if_name_lag_name_map[lag_member_name] = lag_name
for if_name in lag_name_if_name_map.keys():
idx = get_index_from_str(if_name)
if idx:
oid_lag_name_map[idx] = if_name
return lag_name_if_name_map, if_name_lag_name_map, oid_lag_name_map
def init_sync_d_queue_tables(db_conn):
"""
Initializes queue maps for SyncD-connected MIB(s).
:return: tuple(port_queues_map, queue_stat_map)
"""
# { Port name : Queue index (SONiC) -> sai_id }
# ex: { "Ethernet0:2" : "1000000000023" }
queue_name_map = db_conn.get_all(COUNTERS_DB, COUNTERS_QUEUE_NAME_MAP, blocking=True)
logger.debug("Queue name map:\n" + pprint.pformat(queue_name_map, indent=2))
# Parse the queue_name_map and create the following maps:
# port_queues_map -> {"port_index : queue_index" : sai_oid}
# queue_stat_map -> {"port_index : queue stat table name" : {counter name : value}}
# port_queue_list_map -> {port_index: [sorted queue list]}
port_queues_map = {}
queue_stat_map = {}
port_queue_list_map = {}
for queue_name, sai_id in queue_name_map.items():
port_name, queue_index = queue_name.split(':')
queue_index = ''.join(i for i in queue_index if i.isdigit())
port_index = get_index_from_str(port_name)
key = queue_key(port_index, queue_index)
port_queues_map[key] = sai_id
queue_stat_name = queue_table(sai_id)
queue_stat = db_conn.get_all(COUNTERS_DB, queue_stat_name, blocking=False)
if queue_stat is not None:
queue_stat_key = queue_key(port_index, queue_stat_name)
queue_stat_map[queue_stat_key] = queue_stat
if not port_queue_list_map.get(int(port_index)):
port_queue_list_map[int(port_index)] = [int(queue_index)]
else:
port_queue_list_map[int(port_index)].append(int(queue_index))
# SyncD consistency checks.
if not port_queues_map:
# In the event no queue exists that follows the SONiC pattern, no OIDs are able to be registered.
# A RuntimeError here will prevent the 'main' module from loading. (This is desirable.)
logger.error("No queues found in the Counter DB. SyncD database is incoherent.")
raise RuntimeError('The port_queues_map is not defined')
elif not queue_stat_map:
logger.error("No queue stat counters found in the Counter DB. SyncD database is incoherent.")
raise RuntimeError('The queue_stat_map is not defined')
for queues in port_queue_list_map.values():
queues.sort()
return port_queues_map, queue_stat_map, port_queue_list_map
def get_device_metadata(db_conn):
"""
:param db_conn: Sonic DB connector
:return: device metadata
"""
DEVICE_METADATA = "DEVICE_METADATA|localhost"
db_conn.connect(db_conn.STATE_DB)
device_metadata = db_conn.get_all(db_conn.STATE_DB, DEVICE_METADATA)
return device_metadata
def get_chassis_thermal_sub_id(position):
"""
Returns sub OID for thermals that belong to chassis. Sub OID is calculated as follows:
sub OID = CHASSIS_MGMT_SUB_ID + DEVICE_TYPE_CHASSIS_THERMAL + position * DEVICE_INDEX_MULTIPLE + SENSOR_TYPE_TEMP,
:param position: thermal position
:return: sub OID of the thermal
"""
return (CHASSIS_MGMT_SUB_ID + DEVICE_TYPE_CHASSIS_THERMAL + position * DEVICE_INDEX_MULTIPLE + SENSOR_TYPE_TEMP, )
def get_fan_sub_id(parent_id, position):
"""
Returns sub OID for fan. Sub OID is calculated as follows:
sub OID = parent_id[0] + DEVICE_TYPE_FAN + position * DEVICE_INDEX_MULTIPLE
If parent_id is chassis OID, will use a "virtual" fan drawer OID as its parent_id
:param parent_id: parent device sub OID
:param position: fan position
:return: sub OID of the fan
"""
if parent_id[0] == CHASSIS_SUB_ID:
parent_id = MODULE_TYPE_FAN_DRAWER + position * MODULE_INDEX_MULTIPLE
else:
parent_id = parent_id[0]
return (parent_id + DEVICE_TYPE_FAN + position * DEVICE_INDEX_MULTIPLE, )
def get_fan_drawer_sub_id(position):
"""
Returns sub OID for fan drawer. Sub OID is calculated as follows:
sub OID = MODULE_TYPE_FAN_DRAWER + position * MODULE_INDEX_MULTIPLE
:param position: fan drawer position
:return: sub OID of the fan drawer
"""
return (MODULE_TYPE_FAN_DRAWER + position * MODULE_INDEX_MULTIPLE, )
def get_fan_tachometers_sub_id(parent_id):
"""
Returns sub OID for fan tachometers. Sub OID is calculated as follows:
sub OID = parent_id[0] + SENSOR_TYPE_FAN
:param parent_id: parent device sub OID
:return: sub OID of the fan tachometers
"""
return (parent_id[0] + SENSOR_TYPE_FAN, )
def get_psu_sub_id(position):
"""
Returns sub OID for PSU. Sub OID is calculated as follows:
sub OID = MODULE_TYPE_PSU + position * MODULE_INDEX_MULTIPLE
:param position: PSU position
:return: sub OID of PSU
"""
return (MODULE_TYPE_PSU + position * MODULE_INDEX_MULTIPLE, )
def get_psu_sensor_sub_id(parent_id, sensor):
"""
Returns sub OID for PSU sensor. Sub OID is calculated as follows:
sub OID = parent_id[0] + DEVICE_TYPE_POWER_MONITOR + PSU_SENSOR_PART_ID_MAP[sensor]
:param parent_id: PSU oid
:param sensor: PSU sensor name
:return: sub OID of PSU sensor
"""
return (parent_id[0] + DEVICE_TYPE_POWER_MONITOR + PSU_SENSOR_PART_ID_MAP[sensor], )
def get_transceiver_sub_id(ifindex):
"""
Returns sub OID for transceiver. Sub OID is calculated as folows:
sub OID = MODULE_TYPE_PORT + ifindex * PORT_IFINDEX_MULTIPLE
:param ifindex: interface index
:return: sub OID of a port
"""
return (MODULE_TYPE_PORT + ifindex * PORT_IFINDEX_MULTIPLE, )
def get_transceiver_sensor_sub_id(ifindex, sensor):
"""
Returns sub OID for transceiver sensor. Sub OID is calculated as folows:
sub OID = transceiver_oid + XCVR_SENSOR_PART_ID_MAP[sensor]
:param ifindex: interface index
:param sensor: sensor key
:return: sub OID = {{index}} * 1000 + {{lane}} * 10 + sensor id
"""
transceiver_oid, = get_transceiver_sub_id(ifindex)
return (transceiver_oid + XCVR_SENSOR_PART_ID_MAP[sensor],)
def get_redis_pubsub(db_conn, db_name, pattern):
redis_client = db_conn.get_redis_client(db_name)
db = db_conn.get_dbid(db_name)
pubsub = redis_client.pubsub()
pubsub.psubscribe("__keyspace@{}__:{}".format(db, pattern))
return pubsub
class RedisOidTreeUpdater(MIBUpdater):
def __init__(self, prefix_str):
super().__init__()
self.db_conn = Namespace.init_namespace_dbs()
if prefix_str.startswith('.'):
prefix_str = prefix_str[1:]
self.prefix_str = prefix_str
def get_next(self, sub_id):
"""
:param sub_id: The 1-based sub-identifier query.
:return: the next sub id.
"""
raise NotImplementedError
def reinit_data(self):
"""
Subclass update loopback information
"""
pass
def update_data(self):
"""
Update redis (caches config)
Pulls the table references for each interface.
"""
self.oid_list = []
self.oid_map = {}
keys = Namespace.dbs_keys(self.db_conn, SNMP_OVERLAY_DB, self.prefix_str + '*')
# TODO: fix db_conn.keys to return empty list instead of None if there is no match
if keys is None:
keys = []
for key in keys:
oid = oid2tuple(key, dot_prefix=False)
self.oid_list.append(oid)
value = Namespace.dbs_get_all(self.db_conn, SNMP_OVERLAY_DB, key)
if value['type'] in ['COUNTER_32', 'COUNTER_64']:
self.oid_map[oid] = int(value['data'])
else:
raise ValueError("Invalid value type")
self.oid_list.sort()
def get_oidvalue(self, oid):
if oid not in self.oid_map:
return None
return self.oid_map[oid]
class Namespace:
@staticmethod
def init_namespace_dbs():
db_conn = []
SonicDBConfig.load_sonic_global_db_config()
for namespace in SonicDBConfig.get_ns_list():
db = SonicV2Connector(use_unix_socket_path=True, namespace=namespace, decode_responses=True)
db_conn.append(db)
Namespace.connect_namespace_dbs(db_conn)
return db_conn
@staticmethod
def get_namespace_db_map(dbs):
"""
Return a map of namespace:db_conn
"""
db_map = {}
for db_conn in dbs:
db_map[db_conn.namespace] = db_conn
return db_map
@staticmethod
def connect_namespace_dbs(dbs):
list_of_dbs = [APPL_DB, COUNTERS_DB, CONFIG_DB, STATE_DB, ASIC_DB, SNMP_OVERLAY_DB]
for db_name in list_of_dbs:
Namespace.connect_all_dbs(dbs, db_name)
@staticmethod
def connect_all_dbs(dbs, db_name):
for db_conn in dbs:
db_conn.connect(db_name)
@staticmethod
def dbs_keys(dbs, db_name, pattern='*'):
"""
db keys function execute on global and all namespace DBs.
"""
result_keys=[]
for db_conn in dbs:
keys = db_conn.keys(db_name, pattern)
if keys is not None:
result_keys.extend(keys)
return result_keys
@staticmethod
def dbs_keys_namespace(dbs, db_name, pattern='*'):
"""
dbs_keys_namespace function execute on global
and all namespace DBs. Provides a map of keys
and namespace(db index).
"""
result_keys = {}
for db_index in range(len(dbs)):
keys = dbs[db_index].keys(db_name, pattern)
if keys is not None:
keys_ns = dict.fromkeys(keys, db_index)
result_keys.update(keys_ns)
return result_keys
@staticmethod
def dbs_get_all(dbs, db_name, _hash, *args, **kwargs):
"""
db get_all function executed on global and all namespace DBs.
"""
result = {}
# If there are multiple namespaces, _hash might not be
# present in all namespace, ignore if not present in a
# specfic namespace.
if len(dbs) > 1:
tmp_kwargs = kwargs.copy()
tmp_kwargs['blocking'] = False
else:
tmp_kwargs = kwargs
for db_conn in dbs:
ns_result = db_conn.get_all(db_name, _hash, *args, **tmp_kwargs)
if ns_result is not None:
result.update(ns_result)
return result
@staticmethod
def get_non_host_dbs(dbs):
"""
From the list of all dbs, return the list of dbs
which will have interface related tables.
For single namespace db, return the single db.
For multiple namespace dbs, return all dbs except the
host namespace db which is the first db in the list.
"""
if len(dbs) == 1:
return dbs
else:
return dbs[1:]
@staticmethod
def get_sync_d_from_all_namespace(per_namespace_func, dbs):
# return merged tuple of dictionaries retrieved from per
# namespace functions.
result_map = {}
# list of return values
result_list = []
for db_conn in Namespace.get_non_host_dbs(dbs):
ns_tuple = per_namespace_func(db_conn)
for idx in range(len(ns_tuple)):
if idx not in result_map:
result_map[idx] = ns_tuple[idx]
else:
result_map[idx].update(ns_tuple[idx])
for idx, ns_tuple_dict in result_map.items():
result_list.append(ns_tuple_dict)
return result_list
@staticmethod
def dbs_get_bridge_port_map(dbs, db_name):
"""
get_bridge_port_map from all namespace DBs
"""
if_br_oid_map = {}
for db_conn in Namespace.get_non_host_dbs(dbs):
if_br_oid_map_ns = port_util.get_bridge_port_map(db_conn)
if_br_oid_map.update(if_br_oid_map_ns)
return if_br_oid_map
@staticmethod
def dbs_get_vlan_id_from_bvid(dbs, bvid):
for db_conn in Namespace.get_non_host_dbs(dbs):
db_conn.connect('ASIC_DB')
vlan_obj = db_conn.keys('ASIC_DB', "ASIC_STATE:SAI_OBJECT_TYPE_VLAN:" + bvid)
if vlan_obj is not None:
return port_util.get_vlan_id_from_bvid(db_conn, bvid)