def get_ifnametable(self): '''get interface table dict: [ifindex] <-> [ifname] version 2 (slower, need to fetch two tables) ''' self.ifnametable = {} rs = snmpwalk(self.address, OIDS['ifName'], self.community, self.snmpver) #if len(rs['value']) == 0 or len([i for i in rs['value'] if rs['value'].count(i) > 1]) > 0: rs1 = snmpwalk(self.address, OIDS['ifInOctets'], self.community, self.snmpver, exception=True) # этот массив будет меньше или равен предыдущему и будет содержать только те интерфейсы, с которых может собираться статистика oid_table = {} for i in xrange(len(rs1['value'])): oid_table[rs1['oid'][i].split('.')[-1]] = rs1['oid'][i].split('.')[-1] if len(rs['value']) != 0: # добавляем дескрипшены к oid_table там, где это возможно descr_table = {} for i in xrange(len(rs['value'])): descr_table[rs['value'][i]] = rs['oid'][i].split('.')[-1] self.ifnametable = {} for k, v in oid_table.iteritems(): #keyname = [key for key, val in descr_table.iteritems() if val == v] keyname = next((key for key, val in descr_table.iteritems() if val == v), None) if keyname: self.ifnametable[keyname] = v else: self.ifnametable[k] = v else: self.ifnametable = oid_table # else: # for i in xrange(len(rs['value'])): # self.ifnametable[rs['value'][i]] = rs['oid'][i].split('.')[-1] return self.ifnametable
def get_iftable(self, address=None, community=None, snmpver=None): '''getting interface table with full interface names and descriptions ''' if address is None: address = self.address if community is None: community = self.community if snmpver is None: snmpver = self.snmpver rsN = snmpwalk(address, OIDS['ifName'], community, snmpver, exception=False) rsA = snmpwalk(address, OIDS['ifAlias'], community, snmpver, exception=False) rsD = snmpwalk(address, OIDS['ifDescr'], community, snmpver, exception=False) max_items = max(rsN['oid'], rsA['oid'], rsD['oid']) iftable = [] for i in max_items: iface = { 'ifid': None, 'ifname': None, 'ifalias': None, 'ifdescr': None } iface['ifid'] = i.split('.')[-1] # id is unique port number for ii in xrange(len(rsN['oid'])): if iface['ifid'] == rsN['oid'][ii].split('.')[-1]: iface['ifname'] = rsN['value'][ii] break for ii in xrange(len(rsA['oid'])): if iface['ifid'] == rsA['oid'][ii].split('.')[-1]: iface['ifalias'] = rsA['value'][ii] break for ii in xrange(len(rsD['oid'])): if iface['ifid'] == rsD['oid'][ii].split('.')[-1]: iface['ifdescr'] = rsD['value'][ii] break iftable.append(iface) #print iftable return iftable
def get_iftable(self, address=None, community=None, snmpver=None): '''getting interface table with full interface names and descriptions ''' if address is None: address = self.address if community is None: community = self.community if snmpver is None: snmpver = self.snmpver rsN = snmpwalk(address, OIDS['ifName'], community, snmpver, exception=False) rsA = snmpwalk(address, OIDS['ifAlias'], community, snmpver, exception=False) rsD = snmpwalk(address, OIDS['ifDescr'], community, snmpver, exception=False) rsAdmin = snmpwalk(address, OIDS['ifAdminStatus'], community, snmpver, exception=False) rsOper = snmpwalk(address, OIDS['ifOperStatus'], community, snmpver, exception=False) max_items = max(rsN['oid'], rsA['oid'], rsD['oid']) iftable = [] for i in max_items: iface = { 'ifid': None, 'ifname': None, 'ifalias': None, 'ifdescr': None } iface['ifid'] = i.split('.')[-1] # id is unique port number for ii in xrange(len(rsN['oid'])): if iface['ifid'] == rsN['oid'][ii].split('.')[-1]: iface['ifname'] = rsN['value'][ii] break for ii in xrange(len(rsA['oid'])): if iface['ifid'] == rsA['oid'][ii].split('.')[-1]: iface['ifalias'] = rsA['value'][ii] break for ii in xrange(len(rsD['oid'])): if iface['ifid'] == rsD['oid'][ii].split('.')[-1]: iface['ifdescr'] = rsD['value'][ii] break for ii in xrange(len(rsAdmin['oid'])): if iface['ifid'] == rsAdmin['oid'][ii].split('.')[-1]: iface['ifadm'] = rsAdmin['value'][ii] break for ii in xrange(len(rsOper['oid'])): if iface['ifid'] == rsOper['oid'][ii].split('.')[-1]: iface['ifoper'] = rsOper['value'][ii] break iftable.append(iface) #print iftable return iftable
def get_cdp_nbs(self, address=None, community=None, snmpver=None): '''getting interface table with full interface names and descriptions ''' if address is None: address = self.address if community is None: community = self.community if snmpver is None: snmpver = self.snmpver rs_ifnames = snmpwalk(address, OIDS['ifName'], community, snmpver, exception=False) rs_devs = snmpwalk(address, OIDS['cdpCacheDeviceId'], community, snmpver, exception=False) rs_dtypes = snmpwalk(address, OIDS['cdpCachePlatform'], community, snmpver, exception=False) rs_rports = snmpwalk(address, OIDS['cdpCacheDevicePort'], community, snmpver, exception=False) max_items = max(rs_devs['oid'], rs_dtypes['oid'], rs_rports['oid']) cdp_table = [] def get_id(oid): return oid.split('.')[-2] + '.' + oid.split('.')[-1] for item in max_items: row = { 'ifname': None, 'dev': None, 'type': None, 'rport': None } row['ifid'] = get_id(item) # id is unique for i in xrange(len(rs_ifnames['oid'])): if item.split('.')[-2] == rs_ifnames['oid'][i].split('.')[-1]: row['ifname'] = rs_ifnames['value'][i] break for i in xrange(len(rs_devs['oid'])): if row['ifid'] == get_id(rs_devs['oid'][i]): row['dev'] = rs_devs['value'][i] break for i in xrange(len(rs_dtypes['oid'])): if row['ifid'] == get_id(rs_dtypes['oid'][i]): row['type'] = rs_dtypes['value'][i] break for i in xrange(len(rs_rports['oid'])): if row['ifid'] == get_id(rs_rports['oid'][i]): row['rport'] = rs_rports['value'][i] break cdp_table.append(row) #print cdp_table return cdp_table
def get_arptable(self, address=None, community=None, snmpver=None): '''ARP address table data collection returns list of empty class instances with such attributes: .mac .ipaddr''' if address is None: address = self.address if community is None: community = self.community if snmpver is None: snmpver = self.snmpver rsM = snmpwalk(address, OIDS['ipNetToMediaPhysAddress'], community, snmpver, exception=True) # id.mac = mac rsI = snmpwalk(address, OIDS['ipNetToMediaNetAddress'], community, snmpver, exception=False) # id.mac = port (ifid) max_items = max(rsM['oid'], rsI['oid']) arptable = [] mac_changed = False # True if MAC Address table is changed for i in max_items: arp_obj = { 'ipaddr': None, 'mac': None } id = i[len(OIDS['ipNetToMediaPhysAddress']):] # id is unique; function of ip address for ii in xrange(len(rsM['oid'])): if id in rsM['oid'][ii]: # find mac # print(rsM['varBinds'][ii][1]) try: arp_obj['mac'] = self._convert_mac(rsM['varBinds'][ii][0][1]) except TypeError: self.log_write('* WARNING! Unknown value in MAC address table: ' + rsA['value'][ii]) break for ii in xrange(len(rsI['oid'])): if id in rsI['oid'][ii]: arp_obj['ipaddr'] = rsI['value'][ii] break if arp_obj['mac'] is None or arp_obj['ipaddr'] is None: mac_changed = True arptable.append(arp_obj) if mac_changed: self.log_write('* Warning! ARP address table has changed during data collection. Try again to get the actual one.') return arptable
def get_ifnametable(self): '''get interface table dict: [ifindex] <-> [ifname] version 2 (slower, need to fetch two tables) ''' self.ifnametable = {} rs = snmpwalk(self.address, OIDS['ifName'], self.community, self.snmpver) #if len(rs['value']) == 0 or len([i for i in rs['value'] if rs['value'].count(i) > 1]) > 0: rs1 = snmpwalk( self.address, OIDS['ifInOctets'], self.community, self.snmpver, exception=True ) # этот массив будет меньше или равен предыдущему и будет содержать только те интерфейсы, с которых может собираться статистика oid_table = {} for i in xrange(len(rs1['value'])): oid_table[rs1['oid'][i].split('.')[-1]] = rs1['oid'][i].split( '.')[-1] if len(rs['value']) != 0: # добавляем дескрипшены к oid_table там, где это возможно descr_table = {} for i in xrange(len(rs['value'])): descr_table[rs['value'][i]] = rs['oid'][i].split('.')[-1] self.ifnametable = {} for k, v in oid_table.iteritems(): #keyname = [key for key, val in descr_table.iteritems() if val == v] keyname = next( (key for key, val in descr_table.iteritems() if val == v), None) if keyname: self.ifnametable[keyname] = v else: self.ifnametable[k] = v else: self.ifnametable = oid_table # else: # for i in xrange(len(rs['value'])): # self.ifnametable[rs['value'][i]] = rs['oid'][i].split('.')[-1] return self.ifnametable
def get_vlans_cisco(self, address=None, community=None, snmpver=None): '''VLAN table data collection returns list of vlans ''' if address is None: address = self.address if community is None: community = self.community if snmpver is None: snmpver = self.snmpver # works for Cisco devices: rs = snmpwalk(address, OIDS['vtpVlanState'], community, snmpver, exception=True) # id.vlan = status self.collect_vlans = True vlans = [] # for i in rs['oid']: # vlans.append(i.split('.')[-1]) # print vlans return [i.split('.')[-1] for i in rs['oid']]
def get_mactable(self, address=None, community=None, snmpver=None, vlans=[None, ]): '''MAC address table data collection returns list of instances of empty class with attributes: .ifid .ifmac .vlan''' if address is None: address = self.address if community is None: community = self.community if snmpver is None: snmpver = self.snmpver #!FIXME testing - first iteration without vlans #if vlans != [None, ]: # vlans.insert(0, None) mactable = [] mac_changed = False # True if MAC Address table is changed during collecting for vlan in vlans: if vlan: rsA = snmpwalk(address, OIDS['dot1dTpFdbAddress'], community + '@' + str(vlan), snmpver, exception=True) # id.mac = mac rsP = snmpwalk(address, OIDS['dot1dTpFdbPort'], community + '@' + str(vlan), snmpver, exception=False) # id.mac = port (ifid) rsPI = snmpwalk(address, OIDS['dot1dBasePortIfIndex'], community + '@' + str(vlan), snmpver, exception=False) # bport = ifindex else: rsA = snmpwalk(address, OIDS['dot1dTpFdbAddress'], community, snmpver, exception=True) # id.mac = mac rsP = snmpwalk(address, OIDS['dot1dTpFdbPort'], community, snmpver, exception=False) # id.mac = bport rsPI = snmpwalk(address, OIDS['dot1dBasePortIfIndex'], community, snmpver, exception=False) # bport = ifindex max_items = max(rsA['oid'], rsP['oid']) #, rsS.iid) #print max_items for i in max_items: mac_obj = { 'ifid': None, 'mac': None, 'vlan': vlan #'status': None, } id = i[len(OIDS['dot1dTpFdbAddress']):] # get id. id is the same for rsA and rsP # find MAC: for ii in xrange(len(rsA['oid'])): if id in rsA['oid'][ii]: try: mac_obj['mac'] = self._convert_mac(rsA['varBinds'][ii][0][1]) except TypeError: self.log_write('* WARNING! Unknown value in MAC address table: ' + rsA['value'][ii]) continue break # find bport: for ii in xrange(len(rsP['oid'])): if id in rsP['oid'][ii]: bport = rsP['value'][ii].strip() # bridge port is usually equal to ifindex, but -> #print 'bport: ', bport for pi in xrange(len(rsPI['oid'])): # -> we need translate bport to ifindex #print rsPI['oid'][pi].split('.')[-1].strip() #print rsPI['oid'][pi][len(OIDS['dot1dBasePortIfIndex']):] if bport == rsPI['oid'][pi].split('.')[-1].strip(): mac_obj['ifid'] = rsPI['value'][pi] #print 'mac_obj.ifid', mac_obj.ifid break if mac_obj['ifid'] is None or mac_obj['mac'] is None: # or mac_obj.status is None mac_changed = True mactable.append(mac_obj) #print('found:', len(mactable)) if mac_changed: self.log_write('* ВНИАНИЕ! Таблица MAC адресов устройства изменилась во время сбора статистики. Чтобы получить последние данные можете обновить страницу.') return mactable