def sendActiveAlarms(active_num,traps_map,list_engine,id,firstround=False): active_trap_map = traps_map['list_traps'][0] trap_oid = active_trap_map['trap_oid'] ne_type = active_trap_map['ne_type'] v2_or_v3 = active_trap_map['v2_or_v3'] list_trap = active_trap_map['list_trap'] send_trap = [] cus_time = options.time trap_start_time = datetime.now() alarm_ids=[] for i in range(0,active_num): send_trap=deepcopy(list_trap) used_id_info = SnmpTrapUtils.organize_alarm_id_by_ne_type(ne_type, id) id_value = v2c.Gauge32(used_id_info[1]) if 'int' == used_id_info[0] else v2c.OctetString(used_id_info[1]) send_trap[-2] = (list_trap[-2][0], id_value) alarm_ids.append((used_id_info[0],id_value)) if ne_type in special_ne: alarm_sync_status = 'progress' if firstround and i==0: alarm_sync_status = 'start' elif active_num-1==i: alarm_sync_status = 'end' send_trap[-3] = (list_trap[-3][0], v2c.OctetString(alarm_sync_status)) used_str_time = (trap_start_time + timedelta(seconds = base_shift) + timedelta(seconds=i)).strftime('%Y-%m-%d %H:%M:%S.%f') r, h = SnmpTrapUtils.get_hex_from_str(used_str_time, t_format='%Y-%m-%d %H:%M:%S.%f') if ne_type in special_ne: list_times = list_trap[-1] del send_trap[-1] for tuple_time in list_times: if tuple_time[0] in ['1.3.6.1.4.1.193.114.3.2.12', '1.3.6.1.4.1.193.114.3.2.1', '1.3.6.1.4.1.193.114.3.2.11', '1.3.6.1.4.1.193.114.3.2.13', '1.3.6.1.4.1.193.114.3.2.10'] and v2c.OctetString('') == tuple_time[1]: send_trap.append(tuple_time) else: send_trap.append((tuple_time[0], v2c.OctetString(used_str_time[:-3]))) else: send_trap[-1] = (list_trap[-1][0], v2c.OctetString(hexValue=h)) if 'V2' == v2_or_v3: SnmpTrapUtils.send_trap(list_engine[0], list_engine[1], trap_oid, send_trap) elif 'V3' == v2_or_v3: msg= "start to send new alarm id:" + str(id) log0.info(msg) log0.info(str(send_trap) + "\n") SnmpTrapUtils.send_trap(list_engine[2], list_engine[3], trap_oid, send_trap) id = 0 if 4294967295 < id + 1 else id + 1 return id-1,alarm_ids
def sendClearAlarms(old_alarm_ids,active_num,traps_map,list_engine): clear_trap_map = traps_map['list_traps'][1] trap_oid = clear_trap_map['trap_oid'] ne_type = clear_trap_map['ne_type'] v2_or_v3 = clear_trap_map['v2_or_v3'] list_trap = clear_trap_map['list_trap'] trap_start_time = datetime.now() for i in range(0,active_num): send_trap = deepcopy(list_trap) if ne_type in ('MSC','HLR','3GSGSN','GGSN'): alarm_id = re.findall(r'[0-9]+',old_alarm_ids[i].split(',')[1])[1] elif ne_type in ('LTEHSS','IMSHSS'): alarm_id = re.findall(r'[0-9]+',old_alarm_ids[i].split(',')[1])[0] alarm_id_value = v2c.Gauge32(alarm_id) if 'int' == list_trap[-2][0] else v2c.OctetString(alarm_id) send_trap[-2] = (list_trap[-2][0], alarm_id_value) if ne_type in special_ne: alarm_sync_status = 'progress' if 0 == i: alarm_sync_status = 'start' send_trap[-3] = (list_trap[-3][0], v2c.OctetString(alarm_sync_status)) used_str_time = (trap_start_time + timedelta(seconds=i)).strftime('%Y-%m-%d %H:%M:%S.%f') r, h = SnmpTrapUtils.get_hex_from_str(used_str_time, t_format='%Y-%m-%d %H:%M:%S.%f') if ne_type in special_ne: list_times = list_trap[-1] del send_trap[-1] for tuple_time in list_times: if tuple_time[0] in ['1.3.6.1.4.1.193.114.3.2.12', '1.3.6.1.4.1.193.114.3.2.1', '1.3.6.1.4.1.193.114.3.2.11', '1.3.6.1.4.1.193.114.3.2.13', '1.3.6.1.4.1.193.114.3.2.10'] and v2c.OctetString('') == tuple_time[1]: send_trap.append(tuple_time) else: send_trap.append((tuple_time[0], v2c.OctetString(used_str_time[:-3]))) else: send_trap[-1] = (list_trap[-1][0], v2c.OctetString(hexValue=h)) if 'V2' == v2_or_v3: SnmpTrapUtils.send_trap(list_engine[0], list_engine[1], trap_oid, send_trap) elif 'V3' == v2_or_v3: msg= "start to send clear alarm id:" + str(alarm_id) log0.info(msg) log0.info(str(send_trap) + "\n") SnmpTrapUtils.send_trap(list_engine[2], list_engine[3], trap_oid, send_trap)
# -*- coding: utf-8 -*- import os import sys dir_cur = os.path.normpath(os.path.dirname(os.path.abspath(__file__)).split('tmp')[0]) if dir_cur not in sys.path: sys.path.insert(0, dir_cur) from com.ericsson.xn.commons import commonutils logger = commonutils.init('SNMP_TRAP', 'snmp_trap_logs') from com.ericsson.xn.snmp import SnmpTrapUtils ''' snmp_engine, noti = SnmpTrapUtils.init_trap_engine_v3(logger, '127.0.0.1', 11162, 'user', 'mypasswd') snmp_engine_v2, noti_v2 = SnmpTrapUtils.init_trap_engine_v2(logger, '127.0.0.1', 162) SnmpTrapUtils.send_trap(logger, snmp_engine, noti, '1.3.6.1.6.3.1.1.5.1', [(('1.3.6.1.2.1.1.5.0'), SnmpTrapUtils.v2c.OctetString('system name'))]) SnmpTrapUtils.send_trap(logger, snmp_engine_v2, noti_v2, '1.3.6.1.6.3.1.1.5.1', [(('1.3.6.1.2.1.1.5.0'), SnmpTrapUtils.v2c.OctetString('system name'))]) o = SnmpTrapUtils.get_and_check_options(logger) print o ''' sep = os.path.sep snmp_conf = dir_cur + sep + 'config' + sep + 'snmp_trap' template_dir = os.path.normpath(snmp_conf + sep + 'templates') mapping_file = os.path.normpath(snmp_conf + sep + 'mappings') alarm_id_file = os.path.normpath(snmp_conf + sep + 'oids' + sep + 'alarmid_oids')