def get_nec_neighbords(ip_address, hosts): links = [] neighbors=quicksnmp.get_table(ip_address, lldp_table_named_oid,credentials) hosts_ids = [ne['device-id'] for ne in hosts] for link in neighbors: link_i = dict() """ link[0][1] : value of lldpremChassisId link[1][1] : value of lldpremPortDesc """ if str(link[1][1]) != "Not received" : link_i["local_int_index"]= link[0][0][-2] loc_port_desc='.1.0.8802.1.1.2.1.3.7.1.4.'+str(link_i["local_int_index"]) #Can't do it with MIBS because of an exceeding ValueConstraint on NEC devices link_i["local_intf"]= str(quicksnmp.get_oid(ip_address,loc_port_desc,credentials)[0][1]) link_i["neighbor"]= link[0][1].prettyPrint() neighbor=[chassis for chassis in hosts_ids if link_i["neighbor"][:-4] in chassis] if neighbor != [] : link_i["neighbor"]=neighbor[0] #for exemple if lldpremPortDesc = "FastEthernet 1/255/4 Interface" # n_int = str(link[1][1]).split(' ') = ['FastEthernet', '1/255/4', 'Interface'] n_int = str(link[1][1]).split(' ') if len(n_int) == 3 : link_i["neighbor_intf"]=n_int[1] else : link_i["neighbor_intf"]=n_int[0] if link_i["local_intf"] != 'NMS': links.append(link_i) return links
def get_vlans_conf_by_host(hostname): interfaces = get_host_ports_id(hostname) #data_interf_list = quicksnmp.get_table(hostname,get_nec_eth_list, credentials) data_vlan_config = quicksnmp.get_table(hostname, get_nec_vlan_config, credentials) vlan_config = [] #List of Ethernet Interface eth_list = { item: interfaces[item] for item in interfaces if "Ethernet" in interfaces[item] } """ for interface in data_interf_list: #interface[1].prettyPrint() = Port Name if "Ethernet" in interface[1].prettyPrint() : eth_list[interface[0][-1]] = interface[1].prettyPrint() """ for config in data_vlan_config: temp_ = {} temp_['vlan'] = config[0][-1] temp_['port_id'] = config[0][-2] temp_['mode'] = config[1].prettyPrint() if temp_['port_id'] in [*eth_list]: temp_['port_name'] = eth_list[temp_['port_id']] else: temp_['port_name'] = "Modem-" + str(temp_['port_id'])[0] vlan_config.append(temp_) return vlan_config
def get_huawei_neighbors(ip_address, hosts): links = [] serie_fulloutdor = [92, 138, 151, 160] hosts_ids = [ne['device-id'] for ne in hosts] ne_serie = str(quicksnmp.get_oid(str(ip_address), hua_ne_serie,credentials)[0][-1]) if int(ne_serie) in serie_fulloutdor: """ Case of full outdor NE """ if_links = quicksnmp.get_table_oids(str(ip_address),hua_link_search_table, credentials) for link in if_links : link_i = dict() link_i["local_int_index"] = "" link_i["local_intf"] = str(link[1][-1]) +"/"+ str(link[2][-1]) +"/"+ str(link[3][-1]) link_i["neighbor"] = get_hua_id_from_inventory(str(link[4][-1]), hosts) link_i["neighbor_intf"] = str(link[5][-1]) +"/"+ str(link[6][-1]) +"/"+ str(link[7][-1]) links.append(link_i) neighbors=quicksnmp.get_table(ip_address, lldp_table_hua,credentials) #For NE Interface with LLDP for link in neighbors: link_i = dict() if str(link[0][1]) != "Not received": link_i["local_int_index"]= link[0][0][-1] link_i["local_intf"]= str(link[0][0][-4])+'/'+str(link[0][0][-3])+'/'+str(link[0][0][-2]) link_i["neighbor"]= '0x'+(link[0][1].prettyPrint()).replace('-','').lower() neighbor=[chassis for chassis in hosts_ids if link_i["neighbor"][:-4] in chassis] if neighbor != [] : link_i["neighbor"]=neighbor[0] n_int = str(link[1][1]).split(' ') if len(n_int) == 3 : link_i["neighbor_intf"]=n_int[1] else : link_i["neighbor_intf"]=n_int[0] links.append(link_i) return links
def get_host_ports_id(hostname): data_interf_list = quicksnmp.get_table(hostname, get_nec_eth_list, credentials) interfaces = dict() for interface in data_interf_list: interfaces[interface[0][-1]] = interface[1].prettyPrint() return interfaces
def get_vlans_by_host(hostname): #Check workspace and load data files data_vlan_list = quicksnmp.get_table(hostname, get_nec_vlan_list, credentials) vlan_dict = {} for vlan in data_vlan_list: vlan_id = vlan[0][-1] vlan_name = vlan[1].prettyPrint() vlan_dict[str(vlan_id)] = vlan_name return vlan_dict
def set_nec_lldp_port(host): nec_port_lldp_to_enable = [] comp = 0 statuts = quicksnmp.get_table(host['system-ipv4'], nec_port_lldp_status,credentials) for stat in statuts: if stat[1].prettyPrint() != 'txAndRx' : comp +=1 nec_port_lldp_to_enable.append(('.1.0.8802.1.1.2.1.1.6.1.2.'+str(stat[0][-1]),rfc1902.Integer32(3))) print('Port to Set : ', comp) if nec_port_lldp_to_enable != [] : lldp_on = quicksnmp.add_row_oids(host['system-ipv4'],nec_port_lldp_to_enable,credentials) prinn('LLDP Set')
def set_hua_lldp_port(host) : hua_port_lldp_to_enable = [] comp = 0 statuts = quicksnmp.get_table(host['system-ipv4'], hua_port_lldp_status,credentials) for stat in statuts: if stat[1].prettyPrint() != 'txrx' : comp +=1 hua_port_lldp_to_enable.append(('.1.3.6.1.4.1.2011.2.25.4.50.27.1.5.1.4.'+str(stat[0][-3])+'.'+str(stat[0][-2])+'.'+str(stat[0][-1]),rfc1902.Integer32(3))) print('Ports to Set : ', comp) if hua_port_lldp_to_enable != [] : lldp_on = quicksnmp.add_row_oids(host['system-ipv4'],hua_port_lldp_to_enable,credentials) print('LLDP Set')
def main_with_args(args): log("############") log("# Starting #") log("############") ''' Lets create a data model of the topology from the above information ''' topology_model = NetworkTopology() ''' Lets go via devices and get data ''' config = configparser.ConfigParser() config.read('config.ini') config.sections() if 'DEVICES' not in config or 'DEFAULT' not in config: log('We failed to load the configuration file, and sections from it, exiting') exit(1) for device in json.loads(config['DEVICES']['devices']): device = device.replace('\n', '') print("") print("############################################") print(f'Testing access to {device:15} ...', end=" ") log("Testing access to " + device) ''' Try getting first the device hostname to check if SNMP works ''' try: sSNMPHostname = quicksnmp.get(device, sysName_named_oid, hlapi.CommunityData(config['DEFAULT']['SnmpCommunityString'])) log(' = '.join([x.prettyPrint() for x in sSNMPHostname])) log(" This is how to get to numeric OID: " + str(tuple(sSNMPHostname[0][0]))) log(" This is how to get data only: " + str(sSNMPHostname[0][1])) except RuntimeError as e: log("Runtime Error: " + str(e)) continue except (ValueError, TypeError): log("Error " + str(TypeError)) continue log("# Connection worked, will retrieve data next") ''' INTERFACES TABLE GET ''' try: log("INTERFACES TABLE: ") rawInterfacesTable = quicksnmp.get_table(device, interfaces_table_named_oid, hlapi.CommunityData(config['DEFAULT']['SnmpCommunityString'])) for row in rawInterfacesTable: for item in row: log(' = '.join([x.prettyPrint() for x in item])) log('') except RuntimeError as e: log("Runtime Error: " + str(e)) continue except (ValueError, TypeError): log("Error " + str(TypeError)) continue ''' LLDP TABLE GET ''' try: log("LLDP TABLE: ") log("############") rawTable = quicksnmp.get_table(device, lldp_table_named_oid, hlapi.CommunityData(config['DEFAULT']['SnmpCommunityString'])) for row in rawTable: for item in row: log(' = '.join([x.prettyPrint() for x in item])) log('') except RuntimeError as e: log("Runtime Error: " + str(e)) continue except (ValueError, TypeError): log("Error " + str(TypeError)) continue ''' Populate data model with this device data ''' topology_model.addDevice(str(sSNMPHostname[0][1])) for row in rawInterfacesTable: # index number from OID oid = tuple(row[0][0]) log('INDEX: ' + str(oid[-1])) # ifDescr log('ifDescr: ' + str(row[0][1])) # ifType log('ifType: ' + str(row[1][1])) # ifMtu log('ifMtu: ' + str(row[2][1])) # ifSpeed log('ifSpeed: ' + str(row[3][1])) # ifPhysAddress log('ifPhysAddress: ' + str(row[4][1].prettyPrint())) # ifAdminStatus log('ifAdminStatus: ' + str(row[5][1])) # ifOperStatus log('ifOperStatus: ' + str(row[6][1])) # ifHCInOctets log('ifHCInOctets: ' + str(row[7][1])) # ifHCOutOctets log('ifHCOutOctets: ' + str(row[8][1])) # ifHighSpeed log('ifHighSpeed: ' + str(row[9][1])) log("") topology_model.addDeviceInterface(str(sSNMPHostname[0][1]), # deviceid oid[-1], # INDEX str(row[0][1]), # ifDescr str(row[1][1]), # ifType str(row[2][1]), # ifMtu str(row[3][1]), # ifSpeed str(row[4][1].prettyPrint()), # ifPhysAddress str(row[5][1]), # ifAdminStats str(row[6][1])) # ifOperStatus topology_model.addInterfaceStats(str(sSNMPHostname[0][1]), # deviceid oid[-1], # INDEX str(row[0][1]), # ifDescr str(row[1][1]), # ifType str(row[6][1]), # ifOperStatus str(row[7][1]), # ifHCInOctets str(row[8][1]), # ifHCOutOctets str(row[9][1])) # ifHighSpeed log('links from LLDP') for row in rawTable: # lldpRemSysName log('lldpRemSysName: ' + str(row[0][1])) # lldpRemSysDesc log('lldpRemSysDesc: ' + str(row[1][1])) # lldpRemPortId log('lldpRemPortId: ' + str(row[2][1])) # lldpRemPortDesc log('lldpRemPortDesc: ' + str(row[3][1])) # Add to neighborships oid = tuple(row[0][0]) local_in_index = oid[-2] # Get interface name via LLDP local int table local_interface_name = quicksnmp.get(device, [('LLDP-MIB', 'lldpLocPortId', local_in_index)], hlapi.CommunityData(config['DEFAULT']['SnmpCommunityString'])) # Repairing H3Cs bad indexes by searching for index via name local_in_index = topology_model.getDeviceInterfaceIndex(str(sSNMPHostname[0][1]), str(local_interface_name[0][1])) log("Local_interface_name: " + str(local_interface_name[0][1])) log(' = '.join([x.prettyPrint() for x in local_interface_name])) # Add to links topology_model.addLink(str(sSNMPHostname[0][1]), # node_a str(row[0][1]), # node_b topology_model.getLinkSpeedFromName(str(row[2][1])), local_in_index, # a_local_int_index str(local_interface_name[0][1]), # a_local_int_name str(row[2][1]) # lldpRemPortId ) log("Localintindex : " + str(local_in_index)) topology_model.addNeighborships(str(sSNMPHostname[0][1]), local_in_index, str(local_interface_name[0][1]), str(row[0][1]), str(row[2][1])) ''' The model should be populated now, lets dump the JSONs ''' topology_model.dumpToJSON() del topology_model return 0;
def update(): #Check workspace and load data files cwd = os.getcwd() if cwd.split('/')[-1] != 'snmp': os.chdir("Scripts/python/snmp/") #Get Devices list try: f = open("inventory.json", ) devices = json.load(f) f.close() except: devices = dict() elines = dict() #UNI2NNI for node in devices: id = node['device_id'] if node['vendor'] == 'huawei': eline_list = list() elines[id] = dict() elinetable = quicksnmp.get_table(node['address'], eline_hua, credentials) for eline_e in elinetable: index = '.'.join( (str(eline_e[3][0][-3]), str(eline_e[3][0][-2]), str(eline_e[3][0][-1]))) nms_index = eline_e[3][1].prettyPrint() name = eline_e[4][1].prettyPrint() board = eline_e[5][1].prettyPrint() subboard = eline_e[6][1].prettyPrint() port = eline_e[7][1].prettyPrint() port = '/'.join((board, subboard, port)) vlan_list = eline_e[8][1].prettyPrint().replace('0x', '') vlan_list = str(hex2bin(vlan_list)) vlan_list = [ str(i + 1) for i in range(len(vlan_list)) if vlan_list[i] == '1' ] qinqid = eline_e[9][1].prettyPrint() status = eline_e[10][1].prettyPrint() values = [ str(index), nms_index, name, port, vlan_list, qinqid, status ] #keys=['nms_index','name','port','vlan_list','qinqid','status'] keys = [ 'link-id', 'nms-index', 'link-name', 'port', 'vlan-list', 'qinq-link-id', 'status' ] eline_list.append(make_dict(keys, values)) elines[id]['uni2nni'] = eline_list.copy() #UNI2UNI for node in devices: id = node['device_id'] if node['vendor'] == 'huawei': eline_list = list() elinetable = quicksnmp.get_table(node['address'], uni2uni_hua, credentials) for eline_e in elinetable: #Main info index = '.'.join( (str(eline_e[3][0][-3]), str(eline_e[3][0][-2]), str(eline_e[3][0][-1]))) nms_index = eline_e[3][1].prettyPrint() name = eline_e[4][1].prettyPrint() #UNI 1 board = eline_e[5][1].prettyPrint() subboard = eline_e[6][1].prettyPrint() port = eline_e[7][1].prettyPrint() port_a = '/'.join((board, subboard, port)) vlan_list = eline_e[8][1].prettyPrint().replace('0x', '') vlan_list = str(hex2bin(vlan_list)) vlan_list_a = [ str(i + 1) for i in range(len(vlan_list)) if vlan_list[i] == '1' ] #UNI 2 board = eline_e[9][1].prettyPrint() subboard = eline_e[10][1].prettyPrint() port = eline_e[11][1].prettyPrint() port_b = '/'.join((board, subboard, port)) vlan_list = eline_e[12][1].prettyPrint().replace('0x', '') vlan_list = str(hex2bin(vlan_list)) vlan_list_b = [ str(i + 1) for i in range(len(vlan_list)) if vlan_list[i] == '1' ] #Status status = eline_e[13][1].prettyPrint() keys = [ 'link-id', 'nms-index', 'link-name', 'port-a', 'vlan-list-a', 'port-b', 'vlan-list-b', 'status' ] values = [ str(index), nms_index, name, port_a, vlan_list_a, port_b, vlan_list_b, status ] eline_list.append(make_dict(keys, values)) elines[id]['uni2uni'] = eline_list.copy() #NNI2NNI for node in devices: id = node['device_id'] if node['vendor'] == 'huawei': eline_list = list() elinetable = quicksnmp.get_table(node['address'], nni2nni_hua, credentials) for eline_e in elinetable: #Main info index = '.'.join( (str(eline_e[3][0][-3]), str(eline_e[3][0][-2]), str(eline_e[3][0][-1]))) nms_index = eline_e[3][1].prettyPrint() name = eline_e[4][1].prettyPrint() qinqid_a = eline_e[5][1].prettyPrint() qinqid_b = eline_e[6][1].prettyPrint() #Status status = eline_e[7][1].prettyPrint() keys = [ 'link-id', 'nms-index', 'name', 'qinq-link-id-a', 'qinq-link-id-b', 'status' ] values = [ str(index), nms_index, name, qinqid_a, qinqid_b, status ] eline_list.append(make_dict(keys, values)) elines[id]['nni2nni'] = eline_list.copy() #AAI Data Normalize Operations aai_elines_data = dict() aai_elines_data['elines'] = dict() for node in elines: current_node_elines_data = dict() #Get Current Elines current_elinelinks = dict() URL_GET_DEVICE_ELINES = URL_GET_DEVICES + '/device/{device_id}?depth=all'.format( device_id=node) req_get_elines = get_request(URL_GET_DEVICE_ELINES) try: current_elinelinks['uni2unis'] = { link['link-id']: link['resource-version'] for link in req_get_elines[1]['uni-2-unis']['uni-2-uni'] } current_elinelinks['uni2nnis'] = { link['link-id']: link['resource-version'] for link in req_get_elines[1]['uni-2-nnis']['uni-2-nni'] } current_elinelinks['nni2nnis'] = { link['link-id']: link['resource-version'] for link in req_get_elines[1]['nni-2-nnis']['nni-2-nni'] } except: current_elinelinks['uni2unis'] = dict() current_elinelinks['uni2nnis'] = dict() current_elinelinks['nni2nnis'] = dict() #UNI2NNI current_node_elines_data['uni2nnis'] = list() for conf in elines[node]['uni2nni']: uni2nni_data = json.loads( uni2nni_model.render(link_id=conf['link-id'], nms_index=config['nms-index'], link_name=config['link-name'], port=config['port'], qinq_link_id=config['qinq-link-id'], vlan_list=config['vlan-list'], status=config['status'])) if config['link-id'] in current_elinelinks['uni2nnis'].keys: uni2nni_data['resource-version'] = current_elinelinks[ 'uni2nnis'][config['link-id']] current_node_elines_data['uni2nnis'].append(uni2nni_data) #URL_PUT_DEVICE_UNI2NNIS = URL_GET_DEVICE_UNI2NNIS +'/uni-2-nni/{link_id}'.format(link_id=config['link-id']) #req_put_uni2nnis = put_request(URL_PUT_DEVICE_UNI2NNIS, uni2nni_data) #UNI2UNI current_node_elines_data['uni2unis'] = list() for conf in elines[node]['uni2uni']: uni2uni_data = json.loads( uni2uni_model.render(link_id=conf['link-id'], nms_index=config['nms-index'], link_name=config['link-name'], port_a=config['port-a'], vlan_list_a=config['vlan-list-a'], port_b=config['port-b'], vlan_list_b=config['vlan-list-b'], status=config['status'])) if config['link-id'] in current_elinelinks['uni2unis'].keys: uni2uni_data['resource-version'] = current_elinelinks[ 'uni2unis'][config['link-id']] current_node_elines_data['uni2unis'].append(uni2uni_data) #URL_PUT_DEVICE_UNI2UNIS = URL_GET_DEVICE_UNI2UNIS +'/uni-2-uni/{link_id}'.format(link_id=config['link-id']) #req_put_uni2unis = put_request(URL_PUT_DEVICE_UNI2UNIS, uni2uni_data) #NNI2NNI current_node_elines_data['nni2nnis'] = list() for conf in elines[node]['nni2nni']: nni2nni_data = json.loads( nni2nni_model.render(link_id=conf['link-id'], nms_index=config['nms-index'], link_name=config['link-name'], qinq_link_id_a=config['qinq-link-id-a'], qinq_link_i_b=config['qinq-link-id-b'], status=config['status'])) if config['link-id'] in current_elinelinks['nni2nnis'].keys: nni2nni_data['resource-version'] = current_elinelinks[ 'nni2nnis'][config['link-id']] current_node_elines_data['nni2nnis'].append(nni2nni_data) #URL_PUT_DEVICE_NNI2NNIS = URL_GET_DEVICE_NNI2NNIS +'/nni-2-nni/{link_id}'.format(link_id=config['link-id']) #req_put_nni2nnis = put_request(URL_PUT_DEVICE_NNI2NNIS, nni2nni_data) aai_elines_data['elines'][node] = current_node_elines_data """ with open('json/huawei/elines.json', 'w') as fp: json.dump(elines, fp) """ print(aai_elines_data)
def update(): #Check workspace and load data files cwd = os.getcwd() if cwd.split('/')[-1] != 'snmp': os.chdir("Scripts/python/snmp/") #Get Devices list try: f = open("inventory.json", ) devices = json.load(f) f.close() except: devices = dict() qinqlinks = dict() #Sending SNMP request and processing values for each huawei devices for node in devices: id = node['device_id'] if node['vendor'] == 'huawei': qinq_list = list() qinqtable = quicksnmp.get_table(node['address'], qinq_hua, credentials) for qinq_e in qinqtable: index = qinq_e[1][0][-1] board = qinq_e[1][1].prettyPrint() subboard = qinq_e[2][1].prettyPrint() port = board + '/' + subboard + '/' + qinq_e[3][1].prettyPrint( ) vlan = qinq_e[4][1].prettyPrint() status = qinq_e[5][1].prettyPrint() keys = ['qinq-link-id', 'port-name', 'qinq-vlan-id', 'status'] values = [str(index), port, vlan, status] qinq_list.append(make_dict(keys, values)) qinqlinks[id] = qinq_list.copy() #AAI Operations aai_qinqlinks_data = dict() aai_qinqlinks_data['qinqlinks'] = dict() for node in qinqlinks: current_node_qinqlinks_data = list() #Get Current qinqlinks config in device current_qinqlinks = dict() URL_GET_DEVICE_QINQ_LINK = URL_GET_DEVICES + '/device/{device_id}/qinq-links'.format( device_id=node) req_get_qinq_links = get_request(URL_GET_DEVICE_QINQ_LINK) try: current_qinqlinks = { link['qinq-link-id']: link['resource-version'] for link in req_get_qinq_links[1]['qinq-link'] } except: current_qinqlinks = dict() #Create or Update a qinqlink in AAI for config in qinqlinks[node]: qinqlink_data = json.loads( qinqlink_model.render(qinq_link_id=config['qinq-link-id'], port_name=config['port-name'], qinq_vlan_id=config['qinq-vlan-id'], status=config['status'])) if config['qinq-link-id'] in current_qinqlinks.keys: qinqlink_data['resource-version'] = current_qinqlinks[ config['qinq-link-id']] current_node_qinqlinks_data.append(qinqlink_data) #URL_PUT_DEVICE_QINQ_LINK = URL_GET_DEVICE_QINQ_LINK + '/qinq-link/{qinq_link_id}'.format(qinq_link_id=config['qinq-link-id']) #req_put_qinq_links = put_request(URL_PUT_DEVICE_QINQ_LINK, qinqlink_data) aai_qinqlinks_data['qinqlinks'][node] = current_node_qinqlinks_data """ with open('json/huawei/qinqlinks.json', 'w') as fp: json.dump(qinqlinks, fp) """ print(aai_qinqlinks_data)