forked from xcme/swtoolz-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.py
650 lines (515 loc) · 24 KB
/
helpers.py
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
import math
from telnetlib import Telnet
import logging
import re
import time
import easysnmp
from easysnmp.exceptions import EasySNMPTimeoutError, EasySNMPError
from swconfig import telnet_user, telnet_password, users, snmp_retries, snmp_timeout, dyn_port_idx_update_interval
gDynamicSNMPIndex = {}
class Port:
_factors = {
'gpon': {
't': 125,
'psh': 8,
'c': 0,
},
'ethernet': {
't': 7,
'psh': 6,
'c': 0,
},
}
def __init__(self, port):
try:
self.__index = int(port)
self.__technology, self.__frame, self.__slot, self.__port = self.get_port_from_index(self.__index)
except ValueError:
port_data = re.match(
r'^(?P<technology>\S*)(?P<frameid>0)/(?P<slotid>\d{1,2})/(?P<portid>\d{1,2})$', port)
if port_data:
self.__technology = port_data.group('technology')
self.__frame = int(port_data.group('frameid'))
self.__slot = int(port_data.group('slotid'))
self.__port = int(port_data.group('portid'))
self.__index = self.get_index(self.__technology, self.__frame, self.__slot, self.__port)
else:
raise ValueError("incorrect port number")
def __repr__(self):
return '{} (ifIndex: {})'.format(self, self.__index)
def __str__(self):
return '{}{}/{}/{}'.format(self.__technology, self.__frame, self.__slot, self.__port)
@property
def index(self):
return self.__index
@property
def frame(self):
return self.__frame
@property
def slot(self):
return self.__slot
@property
def port(self):
return self.__port
@classmethod
def get_index(cls, technology, frame, slot, port):
if technology not in cls._factors:
raise ValueError("incorrect technology")
if frame > 0:
raise ValueError("frameid can't be greater than 0")
if slot > 22:
raise ValueError("slotid can't be greater than 22")
if port > 47:
raise ValueError("portid can't be greater than 47")
t = cls._factors[technology]['t']
psh = cls._factors[technology]['psh']
c = cls._factors[technology]['c']
return (t << 25) + (frame << 19) + (slot << 13) + (port << psh) + c
@classmethod
def get_port_from_index(cls, index):
if index < 234881024:
raise ValueError("Wrong index")
technology = 'ethernet' if index < 4194304000 else 'gpon'
# Frameid always 0. I did not find the case when frameid is grater than 0.
frame = 0
for slot in range(23):
new_index = cls.get_index(technology, frame, slot, 0)
if new_index > index:
slot -= 1
break
for port in range(48):
new_index = cls.get_index(technology, frame, slot, port)
if new_index > index:
port -= 1
break
return technology, frame, slot, port
def snr_diag_parser(incoming_value, host):
"""
Приводит диагностику порта в формате SNR к виду похожему на формат D-Link.
:param dict incoming_value: словарь с диагностикой от SNR
:param str host: ip-адрес устройства для которого выполняется запрос
:rtype: dict
:return: словарь с диагностикой в формате D-Link
"""
# словарик для сопоставления числового и строкового статусов
statuses = {'well': 0,
'open': 1,
'short': 2,
'abnormal': 3,
'fail': 4}
# получаем индекс порта и строку с результатами диагностики из входящих данных
port_index, input_diag = incoming_value['cableDiag'].popitem()
# паттерн для строк вида "(1, 2) open\t\t 0"
pair_pattern = re.compile(r'^(?P<pair>\(\d,\s\d\))\s+(?P<status>\S+)\s+(?P<length>\d+)$')
vct_result = {'cdLinkStatus': incoming_value['ActualStatus']}
diag_line_index = 0 # индекс строки с состоянием пары (для определения номера пары)
for line in input_diag.splitlines():
pair_match = pair_pattern.match(line)
if pair_match:
diag_line_index += 1
vct_result['cdPair{}Status'.format(diag_line_index)] = {
str(port_index): statuses[pair_match.group('status')]}
vct_result['cdPair{}Length'.format(diag_line_index)] = {str(port_index): pair_match.group('length')}
return vct_result
def dlink_clear_errors_on_port(incoming_value, host):
"""
Посылает на коммутатор D-Link команды для сброса количества ошибок на порту.
:param dict incoming_value: словарь с входящими данными
:param str host: ip-адрес устройства для которого выполняется запрос
:rtype: dict
:return: словать с результатом выполнения сброса
"""
# получаем индекс порта и количество CRC ошибок
port_index, input_crc_count = incoming_value['CRC'].popitem()
try:
conn = Telnet(host, port=23, timeout=3)
# если подключение прошло успешно, то передаем логин/пароль пользователя
conn.write(telnet_user.encode('ascii') + b'\n')
conn.write(telnet_password.encode('ascii') + b'\n')
# шлем команду на сброс счетчиков
clear_command = 'clear counters ports {}'.format(port_index)
conn.write(clear_command.encode('ascii') + b'\n')
# разлогиниваемся
conn.write(b'logout\n')
logging.debug(conn.read_all())
except Exception as err:
logging.exception(err)
return {'clear_errors': {str(port_index): 'Failed'}}
else:
return {'clear_errors': {str(port_index): 'Success'}}
def snr_clear_errors_on_port(incoming_value, host):
"""
Посылает на коммутатор SNR команды для сброса количества ошибок на порту.
:param dict incoming_value: словарь с входящими данными
:param str host: ip-адрес устройства для которого выполняется запрос
:rtype: dict
:return: словать с результатом выполнения сброса
"""
# получаем индекс порта и количество CRC ошибок
port_index, input_crc_count = incoming_value['CRC'].popitem()
try:
conn = Telnet(host, port=23, timeout=3)
conn.read_until(b'login:', timeout=3)
# если подключение прошло успешно, то передаем логин/пароль пользователя
conn.write(telnet_user.encode('ascii') + b'\n')
conn.write(telnet_password.encode('ascii') + b'\n')
# шлем команду на сброс счетчиков
clear_command = 'clear counters interface ethernet 1/0/{}'.format(port_index)
conn.write(clear_command.encode('ascii') + b'\n')
# разлогиниваемся
conn.write(b'exit\n')
logging.debug(conn.read_all())
except Exception as err:
logging.exception(err)
return {'clear_errors': {str(port_index): 'Failed'}}
else:
return {'clear_errors': {str(port_index): 'Success'}}
def repair_big_indexes(incoming_value, host):
"""
Устройства Huawei серии MA5600 из-за особенностей формирования индекса по SNMP отдают некоторые индексы
отрицательными (не помещаются в Integer). Этот хелпер это исправляет. А так же отрезает данные с маленькими
индексами (vlanif и прочие ненужные интерфейсы).
:param dict incoming_value: словарь с входящими данными
:param str host: ip-адрес устройства для которого выполняется запрос
:rtype: dict
:return: словать с результатом выполнения сброса
"""
new_port_indexes = {key_index: key_index for key_index, val_index in incoming_value['PortIndex'].items() if
int(key_index) > 234881024}
incoming_value['PortIndex'] = new_port_indexes
return incoming_value
def remove_small_indexes(incoming_value, host):
"""
Этот хелпер отрезает данные с маленькими индексами (vlanif и прочие ненужные интерфейсы).
:param dict incoming_value: словарь с входящими данными
:param str host: ip-адрес устройства для которого выполняется запрос
:rtype: dict
:return: словать с результатом выполнения сброса
"""
for dict_name, values_dict in incoming_value.items():
new_values = {key_index: val_index for key_index, val_index in incoming_value[dict_name].items() if
int(key_index) > 234881024}
incoming_value[dict_name] = new_values
return incoming_value
def fake_board_index(incoming_value, host):
"""
Этот хелпер заменяет индексы для плат в соответствии с логикой PortViewer2.
:param dict incoming_value: словарь с входящими данными
:param str host: ip-адрес устройства для которого выполняется запрос
:rtype: dict
:return: словать с результатом выполнения сброса
"""
boards_tech = {
'H801GPBC': 'gpon',
'H802GPBD': 'gpon',
'H805GPBD': 'gpon',
'H806GPBH': 'gpon',
'H807GPBH': 'gpon',
'H802GPFD': 'gpon',
'H805GPFD': 'gpon',
'H801GPMD': 'gpon',
'H807GPBD': 'gpon',
'H802SCUN': 'ethernet',
'H801X2CS': 'ethernet',
}
new_data = {}
for board_index, board_name in incoming_value['BoardDescr'].items():
board = re.match(r'(?P<model>\w*)_(?P<frameid>\d{1,2})_(?P<slotid>\d{1,2})', board_name)
first_index = Port.get_index(boards_tech[board.group('model')], int(board.group('frameid')),
int(board.group('slotid')), 0)
pv_slotid = int(math.floor(first_index / 8192.0)) + 1
new_data[str(pv_slotid)] = board.group('model')
incoming_value['BoardDescr'] = new_data
return incoming_value
def make_dynamic_map(incoming_value, host):
"""
Этот формирует карту портов "DeviceMap" по индексам портов.
:param dict incoming_value: словарь с входящими данными
:param str host: ip-адрес устройства для которого выполняется запрос
:rtype: dict
:return: словать с картой портов
"""
ports = {}
map = []
for index in sorted(incoming_value['PortName'], key=int):
try:
port = Port(index)
except ValueError:
continue
else:
if port.slot not in ports:
ports[port.slot] = []
ports[port.slot].append(str(port.index))
for slot in sorted(ports):
map.append([ports[slot]])
return {'DeviceMap': map}
def make_dynamic_map_for_6509(incoming_value, host):
"""
Этот формирует карту портов "DeviceMap" по индексам портов для Cisco WS-C6509-E
:param dict incoming_value: словарь с входящими данными
:param str host: ip-адрес устройства для которого выполняется запрос
:rtype: dict
:return: словать с картой портов
"""
ports = {}
map = []
for index, port_name in incoming_value['PortName'].items():
if port_name.startswith('Gi') or port_name.startswith('Te'):
try:
slot, port_number = [int(n) for n in port_name[2:].split('/')]
except ValueError:
continue
else:
if slot not in ports:
ports[slot] = []
ports[slot].append(str(index))
for slot in sorted(ports):
map.append([ports[slot][i:i + 12] for i in range(0, len(ports[slot]), 12)])
return {'DeviceMap': map}
def make_ports_for_nexus(incoming_value, host):
map = {}
for name in incoming_value:
ports = {}
for index, port_name in sorted(incoming_value[name].items()):
if int(index) >= 436207616:
port_name = port_name.replace('Ethernet', 'Eth')
port_idx = int((int(index) - 436207616) / 4096) + 1
if name == 'PortIndex':
ports[port_idx] = port_idx
else:
ports[port_idx] = port_name
map[name] = ports
return map
def make_ports_for_nexus32(incoming_value, host):
#очень строгая привязка к нашей конфигурации
#а именно: первые 24 порта 40G переключены в 96 портов 10G и 8 портов 40G
map = {}
for name in incoming_value:
ports = {}
for index, port_name in sorted(incoming_value[name].items()):
if int(index) >= 436207616:
port_idx = int((int(index) - 436207616) / 4096) + 1
port_name = port_name.replace('Ethernet', 'Eth')
if port_idx >= 25 and port_idx<=32:
port_idx = port_idx + 72
if port_idx > 122880:
port_idx = math.floor((int(port_idx)-122880)/10)*4+(int(port_idx)-122880)%10
if name == 'PortIndex':
ports[port_idx] = port_idx
else:
ports[port_idx] = port_name
map[name] = ports
return map
def make_redback_ports(incoming_value, host):
# заполняем словарь типа порта одним и тем же значением (fiber)
incoming_value['MediumType'] = dict.fromkeys(range(1, 25), 6)
incoming_value['AdminSpeed'] = incoming_value['ActualSpeed']
incoming_value['AdminFlow'] = dict.fromkeys(range(1, 25), 1)
return incoming_value
def update_dynamic_snmp_ports_qfx(host):
ticks = time.time()
if host not in gDynamicSNMPIndex or ticks - gDynamicSNMPIndex[host]['ticks'] > dyn_port_idx_update_interval:
ports = {}
session = easysnmp.Session(hostname=host, community=users['journal']['1'], version=2,
retries=snmp_retries, use_numeric=True, timeout=snmp_timeout)
try:
# Выполняем опрос параметров из default_info
snmp_response = session.walk('.1.3.6.1.2.1.31.1.1.1.1')
except EasySNMPTimeoutError as err:
logging.exception(err)
pass
else:
for x in snmp_response:
match = re.search('(([gx]e)|(et))\-0\/0\/([0-9]+)$', x.value)
if match:
ports[x.oid_index] = match.group(4)
gDynamicSNMPIndex[host] = {'ticks': ticks, 'ports': ports}
logging.debug(gDynamicSNMPIndex)
else:
logging.debug('cached')
return
def make_ports_for_qfx5120(incoming_value, host):
update_dynamic_snmp_ports_qfx(host)
qfx5120ports = gDynamicSNMPIndex[host]['ports']
map = {}
for name in incoming_value:
ports = {}
for index, port_name in sorted(incoming_value[name].items()):
try:
journalkey = qfx5120ports.get(index)
if journalkey is not None:
port_name = port_name.replace('Ethernet', 'Eth')
port_idx = journalkey
if name == 'PortIndex':
ports[port_idx] = port_idx
else:
ports[port_idx] = port_name
map[name] = ports
except KeyError:
logger.error("Can't find index {index}.")
return map
def huawei_fdb(incoming_value, host):
"""
Преобразовывает огромную HEX-STRING от шасси и разбивает ее по определенным правилам На удобочитаемую структуру.
На выходе: ключи первого уровня - номера портов ONT, ключи второго уровря - номера виланов. Пример:
"1": {
"586": [
"F8:4D:FC:22:3C:67"
]
}
:param str incoming_value: входящая HEX-STRING
:param str host: ip-адрес устройства для которого выполняется запрос
:return: структура с MAC-адресами
"""
macs = {}
_, all_macs = incoming_value['macs'].popitem()
for i in range(0, len(all_macs), 20):
item = all_macs[i:i + 20]
if item != '00000000000000000000':
mac_addr = ':'.join(split_nth(item[-12:], 2))
port_id = int(item[2:4], 16)
vlanid = int(item[4:8], 16)
if port_id not in macs:
macs[port_id] = {vlanid: [mac_addr]}
else:
if vlanid not in macs[port_id]:
macs[port_id][vlanid] = [mac_addr]
else:
macs[port_id][vlanid].append(mac_addr)
return {'macs': macs}
def split_nth(input_string, n):
""" Просто разбивает строку на кусочки по `n` символов.
:param str input_string: входная строка
:param int n: по сколько символов разбивать строку
:rtype: list
:return: список подстрок по `n` символов
"""
return [input_string[i:i + n] for i in range(0, len(input_string), n)]
def hex_string(input_string):
"""
Возвращает HEX-STRING.
:param str input_string: строка для преобразования в HEX
:rtype: str
:return: HEX-STRING
"""
return ''.join([f'{ord(c):02X}' for c in input_string])
def ljust_string(input_string):
"""
Добавляет нулей к входной строке до 16 символов. Используется в основоном для масок портов.
:param input_string: входная строка
:rtype: str
:return: строка дополненная нулями до 16 символов
"""
return hex_string(input_string).ljust(16, '0')
def mac(input_string):
"""
Входную строку превращает в HEX-STRING и форматирует как MAC-адрес (через ':').
:param str input_string: входная строка
:rtype: str
:return: MAC-адрес
"""
return ':'.join(split_nth(hex_string(input_string), 2))
def make_items(input_dict):
"""
Преобразует входной словарь с метриками в словарь с сущностями :)
Пример:
на входе
{
'sn': {'0': '485754432D9B039B', '1': '44443132B35CF35D'},
'line-profile-name': {'0': 'activate', '1': 'line-P000-VLAN2200'},
'srv-profile-name': {'0': 'activate', '1': 'srv-P000-VLAN2200'}
}
на выходе
{
'0': {
'sn': '485754432D9B039B', 'line-profile-name': 'activate', 'srv-profile-name': 'activate'
},
'1': {
'sn': '44443132B35CF35D', 'line-profile-name': 'line-P000-VLAN2200', 'srv-profile-name': 'srv-P000-VLAN2200'
},
}
:param dict input_dict: входной словарь
:rtype: dict
:return: выходной словарь
"""
items = {}
for property_name, values_dict in input_dict.items():
for ont_number, value in values_dict.items():
if ont_number not in items:
items[ont_number] = {}
items[ont_number][property_name] = value
return items
def huawei_get_inactive_onts(input_value, host=None):
"""
Выбирает серийные номера ONT к которым привязаны line-profile и srv-profile с имененм "activate".
:param dict input_value: входные данные
:param str host: ip-адрес устройства для которого выполняется запрос
:rtype: dict
:return: словарь с серийниками "незарегистрированных" ONT
"""
inactive_onts = {}
onts = make_items(input_value)
for ont_id, ont_info in onts.items():
if ont_info['line-profile-name'] == 'activate' and ont_info['srv-profile-name'] == 'activate':
inactive_onts[ont_id] = ont_info['sn']
return {'sn': inactive_onts}
def huawei_get_service_ports(input_value, host=None):
"""
Превращает несвязанные данные о сервисных портах в словарь с информацией о сервисных портах.
:param dict input_value: входные данные
:param str host: ip-адрес устройства для которого выполняется запрос
:rtype: dict
:return: словарь с сервисными портами
"""
return {'service_ports': make_items(input_value)}
def huawei_get_service_ports_numbers(input_value, host=None):
"""
Превращает несвязанные данные о сервисных портах в словарь со списком
номеров сервисных портов.
:param dict input_value: входные данные
:param str host: ip-адрес устройства для которого выполняется запрос
:rtype: dict
:return: словарь с сервисными портами
"""
items = make_items(input_value)
return {'service_ports': [int(i) for i in items.keys()]}
def str_from_index(input_string):
"""
Возвращает человекопонятную строку из SNMP-индекса.
Например, на входе - '108.105.110.101.45.80.48.48.48.45.86.76.65.78.50.48.48.48', на выходе - 'line-P000-VLAN2000'
:param input_string:
:return:
"""
return ''.join([chr(int(p)) for p in input_string.split('.')])
def huawei_walk_profiles(input_value, host=None):
"""
Ключами выходного словаря запроса профилей являются SNMP-индексы, мы их преобразовываем в строку и возвращаем в
виде списка.
:param input_value:
:param host:
:return:
"""
for command_name, profiles_dict in input_value.items():
return {command_name: [str_from_index(profile_index) for profile_index, _ in profiles_dict.items()]}
def eltex_walk_user_helper(input_val, host=None):
if input_val.get('tableOfGroupUsers') is None:
return {'tableOfGroupUsers': {}}
keys = {
'3': "UserID",
'4': "RegState",
'5': "NumPlan",
'6': "Number",
'7': "IP",
'8': "Port",
'9': "SIP-Domain",
'10': "MaxActiveLines",
'11': "ActiveCallCount",
'12': "RegExpires",
}
result = {}
for key, val in input_val['tableOfGroupUsers'].items():
index = key.split('.')[0]
if keys.get(index) is None:
continue
result[keys[index]] = val
return {'tableOfGroupUsers': result}