def getIpSubnetDict(localDbClient, ipAddressList, netDeviceID, netDeviceElementID, netDeviceName): try: ipSubnetDict = {} ipSubnetQuery = 'SELECT IPAddress, SubnetMask FROM dba.IPProtocolEndPoint WHERE NetworkElementId=%s' % netDeviceElementID ipSubnetResultSet = ciscoworks_utils.doQuery(localDbClient, ipSubnetQuery) ## Return if query returns no results if ipSubnetResultSet == None: logger.info('[' + SCRIPT_NAME + ':getIpSubnetDict] No IP Addresses found for Net Device <%s>' % netDeviceName) return None ## We have query results! while ipSubnetResultSet.next(): ipAddress = ciscoworks_utils.getStringFromResultSet(ipSubnetResultSet, 1) subnetMask = ciscoworks_utils.getStringFromResultSet(ipSubnetResultSet, 2) if ipAddress: ## Discard duplicate IPs if ipAddress in ipAddressList: ciscoworks_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getIpSubnetDict] Duplicate IP address <%s> on Network Device <%s> with ID <%s>!! Discarding...' % (ipAddress, netDeviceName, netDeviceElementID)) continue else: ipAddressList.append(ipAddress) ## Create IP OSH if subnetMask: ciscoworks_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getIpSubnetDict] Got IP <%s> with subnet mask <%s> for Net Device <%s> with ID <%s>' % (ipAddress, subnetMask, netDeviceName, netDeviceElementID)) ipSubnetDict[ipAddress] = netutils.parseNetMask(subnetMask) else: ciscoworks_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getIpSubnetDict] Got IP <%s> without subnet mask for Net Device <%s> with ID <%s>' % (ipAddress, netDeviceName, netDeviceElementID)) ipSubnetDict[ipAddress] = '' return ipSubnetDict except: excInfo = logger.prepareJythonStackTrace('') logger.warn('[' + SCRIPT_NAME + ':getIpSubnetDict] Exception: <%s>' % excInfo) pass
def parseTransportAdaptersForNode(node, scconfOutput): adaptersList = [] matcher = re.search(r"\(%s\) Node transport adapters:([^\n]+)" % re.escape(node.name), scconfOutput) if matcher: adaptersListStr = matcher.group(1) and matcher.group(1).strip() if adaptersListStr: adaptersList = re.split(r"\s+", adaptersListStr) adaptersList = [adapter for adapter in adaptersList if adapter] if adaptersList: for adapterName in adaptersList: adapterStatus = None tuple = (re.escape(node.name), re.escape(adapterName)) matcher = re.search(r"\(%s:%s\) Adapter enabled:([^\n]+)" % tuple, scconfOutput) if matcher: adapterStatus = matcher.group(1) and matcher.group(1).strip() if adapterStatus != 'yes': logger.debug("Skipping disabled transport adapter '%s'" % adapterName) continue logger.debug("Found transport adapter '%s'" % adapterName) transportAdapter = TransportAdapter(adapterName) properties = {} results = re.findall(r"\(%s:%s\) Adapter property:([^\n]+)" % tuple, scconfOutput) if results: for row in results: elements = re.split(r"=", row, 1) if len(elements) == 2: propertyName = elements[0] and elements[0].strip() propertyValue = elements[1] and elements[1].strip() if propertyName and propertyValue: properties[propertyName] = propertyValue ip = properties.get('ip_address') if ip and netutils.isValidIp(ip): transportAdapter.ip = ip logger.debug("Adapter's private IP is '%s'" % ip) else: logger.warn("Could not find private IP for transport adapter '%s' on node '%s'" % (adapterName, node.name)) netmask = properties.get('netmask') if netmask: try: transportAdapter.netmask = netutils.parseNetMask(netmask) except: logger.warn("Failed parsing netmask: %s" % netmask) node.transportAdaptersByName[adapterName] = transportAdapter else: logger.warn("No transport adapters found for node '%s'" % node.name)
def _parseMask(self, mask): try: return mask and netutils.parseNetMask(mask) except: raise InvalidNetmaskException(mask)
def processNodeInfo(ipAddress, macAddress, nodeName, subnetMask, ipAddressList, macAddressList, nodeOshDict, allowDnsLookup): try: nodeOSH = interfaceOSH = ipOSH = None ## Try and get a DNS name for this node nodeDnsName = None if allowDnsLookup and ipAddress and netutils.isValidIp(ipAddress): nodeDnsName = netutils.getHostName(ipAddress) ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processNodeInfo] Got DNS name <%s> for Node <%s> using IP <%s>' % (nodeDnsName, nodeName, ipAddress)) if nodeDnsName: nodeName = nodeDnsName ## Discard IP if this is a duplicate if ipAddress and netutils.isValidIp(ipAddress) and ipAddress in ipAddressList: logger.debug('Ignoring duplicate IP <%s> on Node <%s>...' % (ipAddress, nodeName)) ipAddress = None else: ipAddressList.append(ipAddress) ## Set the real name of the netDevice nodeRealName = nodeName if nodeName and netutils.isValidIp(nodeName): nodeRealName = '' ## Build a host key and create OSHs ## Check for and clean up MAC macAddy = None if macAddress: macAddy = netutils.parseMac(macAddress) ## Check for duplicate MAC addresses if macAddy in macAddressList: logger.debug('Ignoring duplicate MAC Address <%s> on Node <%s>...' % (macAddy, nodeName)) macAddy = None else: macAddressList.append(macAddy) if netutils.isValidMac(macAddy): ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processNodeInfo] Got MAC Address <%s> for Node <%s>' % (macAddy, nodeName)) nodeOSH = modeling.createCompleteHostOSH('node', macAddy, None, nodeName) interfaceOSH = modeling.createInterfaceOSH(macAddy, nodeOSH) else: ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processNodeInfo] Got invalid MAC Address <%s> for Node <%s>' % (macAddress, nodeName)) macAddy = None ## Check for a valid IP if ipAddress and netutils.isValidIp(ipAddress): subnetMask = None if subnetMask: subnetMask = netutils.parseNetMask(subnetMask) ipOSH = modeling.createIpOSH(ipAddress, subnetMask, ipAddress, None) ## Use IP as a host key if a MAC is not available if not macAddy: nodeOSH = modeling.createHostOSH(ipAddress, 'node', None, nodeRealName) else: logger.debug('IP address not available for Node <%s> with MAC address <%s>' % (nodeName, macAddress)) if not nodeOSH: logger.debug('Ignoring Node <%s> because a valid IP/MAC address was not found for it...' % nodeName) return (nodeOSH, interfaceOSH, ipOSH) except: excInfo = logger.prepareJythonStackTrace('') logger.warn('[' + SCRIPT_NAME + ':processNodeInfo] Exception: <%s>' % excInfo) pass