def get_db_datasources(self, content): from NTCMD_IIS import NamedDbDataSource, DbDataSource dbDataSources = [] if content: try: document = SAXBuilder(0).build(StringReader(content)) results = document.getRootElement().getChildren('connectionStrings') if results: for result in results: connectionEntries = result.getChildren('add') for connectionEntry in connectionEntries: connectionString = connectionEntry.getAttributeValue('connectionString') if connectionString: match = re.search("dsn\s*=\s*([a-zA-Z_0-9]+);?.*", connectionString, re.I) if match: dataSource = NamedDbDataSource(match.group(1)) else: dataSource = DbDataSource(connectionString) if dataSource.isValidDataSource(): dbDataSources.append(dataSource) else: logger.debug('DB Source did not validate') except: logger.warnException('Failed getting connection info.') return dbDataSources
def processInboundXml(operation, xmlResult, CONTEXT, debugMode, smartUpdateIgnoreFieldsList, sortCSVFieldsList): fileName = "%s-%s.xml" % (TIMESTAMP, operation) slogger.addAppender(FileAppender(SimpleLayout(), "%s%s%s_success.log" % (adapterResWorkDir, FILE_SEPARATOR, fileName))) slogger.setLevel(Level.DEBUG) flogger.addAppender(FileAppender(SimpleLayout(), "%s%s%s_failure.log" % (adapterResWorkDir, FILE_SEPARATOR, fileName))) flogger.setLevel(Level.DEBUG) saxBuilder = SAXBuilder() xmlData = saxBuilder.build(StringReader(xmlResult)) if debugMode: writeXmlFile(fileName, xmlData) try: # Process CIs cisData = xmlData.getRootElement().getChild('data').getChild('objects').getChildren('Object') if operation in ('add', 'update'): addCIs(CONTEXT, cisData, smartUpdateIgnoreFieldsList, sortCSVFieldsList) # Process Relationships try: relationsData = xmlData.getRootElement().getChild('data').getChild('links').getChildren('link') addRelations(CONTEXT, relationsData, smartUpdateIgnoreFieldsList, sortCSVFieldsList) except: pass elif operation == 'delete': deleteCIs(CONTEXT, cisData) except IOError, ioe: logger.debug(ioe) raise Exception, "Unable to process inbound XML"
def parseServerNamesFromConfig(self, configContent): names = [] if configContent: try: builder = SAXBuilder(0) doc = builder.build(StringReader(configContent)) root = doc.getRootElement() processManager = root.getChildren() processManagerIterator = processManager.iterator() while processManagerIterator.hasNext(): currProcessManager = processManagerIterator.next() currElementName = currProcessManager.getName() if currElementName == 'process-manager': iasInstance = currProcessManager.getChildren() iasInstanceIterator = iasInstance.iterator() while iasInstanceIterator.hasNext(): currIasInstance = iasInstanceIterator.next() if currIasInstance.getName() == 'ias-instance': iasName = currIasInstance.getAttributeValue( 'name' ) or currIasInstance.getAttributeValue( 'id') or 'Default Server' names.append(iasName) except: logger.error('Failed to parse iAS config file.') logger.debugException('') return names
def parsePowershellOutputXml(outputXml): xmls = getXmls(outputXml) builder = SAXBuilder(0) for xml in xmls: resultItems = [] document = builder.build(StringReader(xml)) objsElement = document.getRootElement() namespace = objsElement.getNamespace() objs = objsElement.getChildren("Obj", namespace) for obj in objs: if obj.getAttributeValue("S") == "Output": ms = obj.getChild("MS", namespace) resultItem = CmdletResultItem() if ms: properties = ms.getChildren() for property in properties: if property.getName() != "TN": name = property.getAttributeValue('N') value = property.getText() setattr(resultItem, name, value) resultItems.append(resultItem) return resultItems
def processARISXML(ARISfile, requestedObjectTypeList, requestedRelationshipTypeList, requestedLocaleID): try: builder = SAXBuilder() doc = builder.build(ARISfile) rootElement = doc.getRootElement() objList = { } # Index object list by "object definition ID" because link ends are defined based on object definition id # ########################################### # Process all the ARIS components first # These will map to UCMDB CIs # ########################################### groupElements = rootElement.getChildren('Group') if groupElements: for groupElement in groupElements: if groupElement: objectElements = groupElement.getChildren('ObjDef') if objectElements: for objectElement in objectElements: if objectElement: ## Process objects theObject = processObjectElement( objectElement, requestedObjectTypeList, requestedRelationshipTypeList, requestedLocaleID) if theObject: objList[theObject.objectDefnID] = theObject return objList except: excInfo = logger.prepareJythonStackTrace('') debugPrint('[' + SCRIPT_NAME + ':processARISXML] Exception: <%s>' % excInfo) pass
def disVMKernel(hostId, shell, Framework=None, langBund=None): ''' Discover physical memory on VMKernel str, Shell, Framework, Properties -> oshVector @raise ValueError: memory size is not a digit @command: esxcfg-info -F xml | sed -n \'/<memory-info>/,/<\/memory-info>/p\' ''' resVec = ObjectStateHolderVector() hostOsh = modeling.createOshByCmdbIdString('host', hostId) xml = shell.execCmd( 'esxcfg-info -F xml | sed -n \'/<memory-info>/,/<\/memory-info>/p\' | sed -n \'1,/<\/memory-info>/p\'' ) #Cleanup retrieved xml. Sometimes there is some debug info added xml = xml[xml.find('<'):xml.rfind('>') + 1] builder = SAXBuilder(0) document = builder.build(StringReader(xml)) rootElement = document.getRootElement() memory_values = rootElement.getChild( 'aux-source-memory-stats').getChildren('value') for value in memory_values: if value.getAttributeValue('name') == 'physical-memory-est.': memorySizeInKilobytes = int(value.getText()) memory.report(resVec, hostOsh, memorySizeInKilobytes) #TODO: Implement swap discovery for vmkernel resVec.add(hostOsh) return resVec
def parsePowershellOutputXml(outputXml): xmls = getXmls(outputXml) builder = SAXBuilder(0) for xml in xmls: resultItems = [] document = builder.build(StringReader(xml)) objsElement = document.getRootElement() namespace = objsElement.getNamespace() objs = objsElement.getChildren("Obj", namespace) for obj in objs: if obj.getAttributeValue("S") == "Output": ms = obj.getChild("MS", namespace) resultItem = CmdletResultItem() if ms: properties = ms.getChildren() for property in properties: if property.getName() != "TN": name = property.getAttributeValue("N") value = property.getText() setattr(resultItem, name, value) resultItems.append(resultItem) return resultItems
def processXmlAndCreateGRLoaderXmlFiles(operation, xmlResult): fileName = "%s-%s.xml" % (TIMESTAMP, operation) doc = Document() saxBuilder = SAXBuilder() xmlData = saxBuilder.build(StringReader(xmlResult)) try: # Process CIs cisData = xmlData.getRootElement().getChild('data').getChild( 'objects').getChildren('Object') (mamIdToAttributesMap, itemsRootElement) = addCIs(Element('GRLoader'), cisData) # Process Relationships relationsData = xmlData.getRootElement().getChild('data').getChild( 'links').getChildren('link') itemsRootElementWithLinks = addRelations(itemsRootElement, relationsData, mamIdToAttributesMap) if itemsRootElementWithLinks != None: doc.setRootElement(itemsRootElementWithLinks) createGRLoaderXmlInputFile(fileName, doc) except: raise Exception, "Unable to process inbound XML" return fileName
def syncNmapPortConfigFile(agentPath): ''' Sync nmap port config with global probe's "port number to port name" mapping ''' logger.debug('synchronizing nmap port config file') portConfigFilename = agentPath + CollectorsParameters.getDiscoveryConfigFolder( ) + CollectorsParameters.FILE_SEPARATOR + 'portNumberToPortName.xml' mamservice = File(portConfigFilename) nmapservice = File(agentPath + CollectorsParameters.getDiscoveryResourceFolder() + CollectorsParameters.FILE_SEPARATOR + 'nmap-services') if nmapservice.lastModified() > mamservice.lastModified(): return nmapFile = FileOutputStream(nmapservice) document = SAXBuilder(0).build(mamservice) # document = parse(portConfigFilename) ports = XmlWrapper(document.getRootElement().getChildren('portInfo')) for port in ports: if int(port.getAttributeValue("discover")): portNumber = port.getAttributeValue("portNumber") portName = port.getAttributeValue("portName") portProtocol = port.getAttributeValue("portProtocol") nmapFile.write("%s\t%s/%s\r\n" % (portName, portNumber, portProtocol)) nmapFile.close()
def disVMKernel(hostOsh, client, Framework=None, langBund=None): topologyBuilder = DiskTopologyBuilder(hostOsh, client) xml = client.execCmd( 'esxcfg-info -F xml | sed -n \'/<vmfs-filesystems>/,/<\/vmfs-filesystems>/p\' | sed -n \'1,/<\/vmfs-filesystems>/p\'' ) #Cleanup retrieved xml. Sometimes there is some debug info added xml = xml[xml.find('<'):xml.rfind('>') + 1] builder = SAXBuilder(0) document = builder.build(StringReader(xml)) rootElement = document.getRootElement() vm_filesystems = rootElement.getChildren('vm-filesystem') for vm_filesystem in vm_filesystems: mountPoint = '' size = '' fileSystem = '' usage = None vmfs_values = vm_filesystem.getChildren('value') for value in vmfs_values: if value.getAttributeValue('name') == 'console-path': mountPoint = value.getText() elif value.getAttributeValue('name') == 'size': size = value.getText() elif value.getAttributeValue('name') == 'usage': usage = value.getText() dlp_values = vm_filesystem.getChild('extents').getChild( 'disk-lun-partition').getChildren('value') for value in dlp_values: if value.getAttributeValue('name') == 'console-device': fileSystem = value.getText() topologyBuilder.handleDiskRow(fileSystem, mountPoint, size, usage) return topologyBuilder.getResultVector()
def disVMKernel(hostId, shell, Framework = None, langBund = None): ''' Discover physical memory on VMKernel str, Shell, Framework, Properties -> oshVector @raise ValueError: memory size is not a digit @command: esxcfg-info -F xml | sed -n \'/<memory-info>/,/<\/memory-info>/p\' ''' resVec = ObjectStateHolderVector() hostOsh = modeling.createOshByCmdbIdString('host', hostId) xml = shell.execCmd('esxcfg-info -F xml | sed -n \'/<memory-info>/,/<\/memory-info>/p\' | sed -n \'1,/<\/memory-info>/p\'') #Cleanup retrieved xml. Sometimes there is some debug info added xml = xml[xml.find('<'): xml.rfind('>') + 1] builder = SAXBuilder(0) document = builder.build(StringReader(xml)) rootElement = document.getRootElement() memory_values = rootElement.getChild('aux-source-memory-stats').getChildren('value') for value in memory_values: if value.getAttributeValue('name') == 'physical-memory-est.': memorySizeInKilobytes = int(value.getText()) memory.report(resVec, hostOsh, memorySizeInKilobytes) #TODO: Implement swap discovery for vmkernel resVec.add(hostOsh) return resVec
def processARISXML(ARISfile, requestedObjectTypeList, requestedRelationshipTypeList, requestedLocaleID): try: builder = SAXBuilder() doc = builder.build(ARISfile) rootElement = doc.getRootElement() objList = {} # Index object list by "object definition ID" because link ends are defined based on object definition id # ########################################### # Process all the ARIS components first # These will map to UCMDB CIs # ########################################### groupElements = rootElement.getChildren('Group') if groupElements: for groupElement in groupElements: if groupElement: objectElements = groupElement.getChildren('ObjDef') if objectElements: for objectElement in objectElements: if objectElement: ## Process objects theObject = processObjectElement(objectElement, requestedObjectTypeList, requestedRelationshipTypeList, requestedLocaleID) if theObject: objList[theObject.objectDefnID] = theObject return objList except: excInfo = logger.prepareJythonStackTrace('') debugPrint('[' + SCRIPT_NAME + ':processARISXML] Exception: <%s>' % excInfo) pass
def disVMKernel(hostOsh, client, Framework=None, langBund=None): topologyBuilder = DiskTopologyBuilder(hostOsh, client) xml = client.execCmd( "esxcfg-info -F xml | sed -n '/<vmfs-filesystems>/,/<\/vmfs-filesystems>/p' | sed -n '1,/<\/vmfs-filesystems>/p'" ) # Cleanup retrieved xml. Sometimes there is some debug info added xml = xml[xml.find("<") : xml.rfind(">") + 1] builder = SAXBuilder(0) document = builder.build(StringReader(xml)) rootElement = document.getRootElement() vm_filesystems = rootElement.getChildren("vm-filesystem") for vm_filesystem in vm_filesystems: mountPoint = "" size = "" fileSystem = "" usage = None vmfs_values = vm_filesystem.getChildren("value") for value in vmfs_values: if value.getAttributeValue("name") == "console-path": mountPoint = value.getText() elif value.getAttributeValue("name") == "size": size = value.getText() elif value.getAttributeValue("name") == "usage": usage = value.getText() dlp_values = vm_filesystem.getChild("extents").getChild("disk-lun-partition").getChildren("value") for value in dlp_values: if value.getAttributeValue("name") == "console-device": fileSystem = value.getText() topologyBuilder.handleDiskRow(fileSystem, mountPoint, size, usage) return topologyBuilder.getResultVector()
def get_db_datasources(self, content): from NTCMD_IIS import NamedDbDataSource, DbDataSource dbDataSources = [] if content: try: document = SAXBuilder(0).build(StringReader(content)) results = document.getRootElement().getChildren("connectionStrings") if results: for result in results: connectionEntries = result.getChildren("add") for connectionEntry in connectionEntries: connectionString = connectionEntry.getAttributeValue("connectionString") if connectionString: match = re.search("dsn\s*=\s*([a-zA-Z_0-9]+);?.*", connectionString, re.I) if match: dataSource = NamedDbDataSource(match.group(1)) else: dataSource = DbDataSource(connectionString) if dataSource.isValidDataSource(): dbDataSources.append(dataSource) else: logger.debug("DB Source did not validate") except: logger.warnException("Failed getting connection info.") return dbDataSources
def discoverITS(client, installpath, WEBSERVER_ID, OSHVResult): shellUtils = ShellUtils(client) webserverOSH = modeling.createOshByCmdbIdString('webserver', WEBSERVER_ID) sapitsOSH = ObjectStateHolder('sap_its_wgate') sapitsOSH.setAttribute('data_name', 'ITS_' + client.getIpAddress()) sapitsOSH.setContainer(webserverOSH) OSHVResult.add(sapitsOSH) mapInstanceNameToAgate = getAgates(shellUtils, installpath, sapitsOSH, OSHVResult) filePath = installpath + '\\config\\ItsRegistryALL.xml' data = shellUtils.safecat(filePath) logger.debug('got ItsRegistryALL file') # data = stripNtcmdHeaders(data) if data == None or error(data): logger.error('No data found') else: builder = SAXBuilder(0) doc = builder.build(StringReader(data)) root = doc.getRootElement() keyElem = getElementByAttrValue(root, 'key', 'name', 'AGate') instancesRoot = getElementByAttrValue(keyElem, 'key', 'name', 'Instances') instances = instancesRoot.getChildren('value') it = instances.iterator() while it.hasNext(): instance = it.next() name = instance.getText() agates = mapInstanceNameToAgate.get(name) if agates != None and agates.isEmpty() == 0: servers(name, installpath, sapitsOSH, agates, shellUtils, OSHVResult)
def DiscoveryMain(Framework): OSHVResult = ObjectStateHolderVector() DebugMode = Framework.getParameter('DebugMode') userExtDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder( ) + '\\' filePathDir = userExtDir + 'TQLExport\\ARIS\\results\\' directory = File(filePathDir) files = directory.listFiles() if files == None: logger.warn( 'Results XML not found. Perhaps no data was received from ARIS or an error occurred in the Pull_From_ARIS script.' ) return try: ## Start the work for file in files: if file != None or file != '': builder = SAXBuilder() doc = builder.build(file) # Process CIs # info( "Start processing CIs to update in the destination server..." ) allObjects = doc.getRootElement().getChild('data').getChild( 'objects').getChildren('Object') (objVector, ciDict) = processObjects(allObjects) OSHVResult.addAll(objVector) # Process Relations # info( "Start processing Relationships to update in the destination server..." ) allLinks = doc.getRootElement().getChild('data').getChild( 'links').getChildren('link') linkVector = processLinks(allLinks, ciDict) OSHVResult.addAll(linkVector) except: stacktrace = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) info(concatenate('Failure: ():\n', stacktrace)) if (DebugMode != None): DebugMode = DebugMode.lower() if DebugMode == "true": mam_utils.info( '[NOTE] UCMDB Integration is running in DEBUG mode. No data will be pushed to the destination server.' ) print OSHVResult.toXmlString() return None else: #print OSHVResult.toXmlString() return OSHVResult
def DiscoveryMain(Framework): OSHVResult = ObjectStateHolderVector() fileSeparator = File.separator DebugMode = Framework.getParameter('DebugMode') userExtDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + fileSeparator filePathDir = userExtDir + 'TQLExport' + fileSeparator + 'hpsim' + fileSeparator + 'results' + fileSeparator directory = File(filePathDir) files = directory.listFiles() if files == None: logger.warn('Results XML not found. Perhaps no data was received from SIM or an error occurred in the SIM_Discovery script.') return ## Read ignored Ci types from integration configuration ignoredCiTypes = [] rawIgnoredCiTypes = Framework.getParameter('IgnoredCiTypes') tempIgnoredCiTypes = eval(rawIgnoredCiTypes) if tempIgnoredCiTypes is not None: for item in tempIgnoredCiTypes: item != 'None' and ignoredCiTypes.append(item) ## Identify UCMDB version ucmdbVersion = modeling.CmdbClassModel().version() try: ## Start the work for file in files: if file != None or file != '': builder = SAXBuilder() doc = builder.build(file) # Process CIs # info("Start processing CIs to update in the destination server...") allObjects = doc.getRootElement().getChild('data').getChild('objects').getChildren('Object') (objVector, ciDict) = processObjects(allObjects, ignoredCiTypes, ucmdbVersion) OSHVResult.addAll(objVector) # Process Relations # info("Start processing Relationships to update in the destination server...") allLinks = doc.getRootElement().getChild('data').getChild('links').getChildren('link') linkVector = processLinks(allLinks, ciDict) OSHVResult.addAll(linkVector) except: stacktrace = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) info(concatenate('Failure: ():\n', stacktrace)) if (DebugMode != None): DebugMode = DebugMode.lower() if DebugMode == "true": mam_utils.info ('[NOTE] UCMDB Integration is running in DEBUG mode. No data will be pushed to the destination server.') print OSHVResult.toXmlString() return None else: #print OSHVResult.toXmlString() return OSHVResult
def run(self): print "** Processing : ", self.url f = File(self.url) builder = SAXBuilder() document = builder.build(FileInputStream(f)) if document == None: print "Err: Invalid Document" else: self.processXML(document)
def getAgates(shellUtils, installpath, sapitsOSH, OSHVResult): mapInstanceNameToAgate = HashMap() filePath = installpath + '\\config\\ItsRegistryWGATE.xml' data = shellUtils.safecat(filePath) logger.debug('got ItsRegistryWGATE file') if data == None or error(data): logger.error('Got: [', data, '] when performing command [ safecat ', filePath, '] - terminating script') else: builder = SAXBuilder(0) doc = builder.build(StringReader(data)) root = doc.getRootElement() localWgates = getElementByAttrValue(root, 'key', 'name', 'LocalWgates') wgates = localWgates.getChildren() it = wgates.iterator() while it.hasNext(): wgate = it.next() value = wgate.getAttributeValue('name') if value.find('WGATE_') >= 0: instancesRoot = getElementByAttrValue(wgate, 'key', 'name', 'Instances') instances = instancesRoot.getChildren() itInstances = instances.iterator() while itInstances.hasNext(): instance = itInstances.next() instanceName = instance.getAttributeValue('name') logger.debug(instanceName) agatesRoot = getElementByAttrValue(instance, 'key', 'name', 'Agates') agates = agatesRoot.getChildren() itAgates = agates.iterator() while itAgates.hasNext(): agate = itAgates.next() agateHost = getElementByAttrValue( agate, 'value', 'name', 'Host') host = agateHost.getText() agates = mapInstanceNameToAgate.get(instanceName) if agates == None: agates = ArrayList() mapInstanceNameToAgate.put(instanceName, agates) try: ip = netutils.getHostAddress(host) hostOSH = modeling.createHostOSH(ip) OSHVResult.add(hostOSH) agateOSH = modeling.createApplicationOSH( 'sap_its_agate', 'ITS_AGATE_' + ip, hostOSH) OSHVResult.add(agateOSH) agates.add(agateOSH) except: logger.warn('Failed resolving IP for agate host ', host) return mapInstanceNameToAgate
def getNatIPFromConfigurationFile(): """ Read IP or IP range from configuration file. @return: A list contains IPAddress objects and IPNetwork objects """ NATIPConfigurationFileFolder = os.path.join( CollectorsParameters.BASE_PROBE_MGR_DIR, CollectorsParameters.getDiscoveryConfigFolder()) NATIPConfigurationFile = os.path.join(NATIPConfigurationFileFolder, 'NATIpAddress.xml') if not os.path.exists(NATIPConfigurationFile): logger.info("There is no NAT IP address defined.") return # Read tags from xml file builder = SAXBuilder() configDoc = builder.build(NATIPConfigurationFile) rootElement = configDoc.getRootElement() ipElements = rootElement.getChildren('Ip') ipRangeElements = rootElement.getChildren('IpRange') NAT_IPs = [] # Read IPAddress, add valid one to NAT_IPs list if ipElements: for ipElement in ipElements: ip = ipElement.getText() if ip_addr.isValidIpAddress(ip): ipObj = ip_addr.IPAddress(ip) NAT_IPs.append(ipObj) # Read IP Ranges, create IPNetwork and add to NAT_IPs list if ipRangeElements: for ipRangeElement in ipRangeElements: ip_range_raw = ipRangeElement.getText() ips = ip_range_raw.split('-') ip_start = ips[0] ip_end = ips[1] if ip_addr.isValidIpAddress(ip_start) and ip_addr.isValidIpAddress( ip_end): ip_start = ip_addr.IPAddress(ip_start) ip_end = ip_addr.IPAddress(ip_end) ips = ip_addr.summarize_address_range(ip_start, ip_end) logger.debug(ips) NAT_IPs.extend(ips) else: logger.warn( "IP Range should start and end with valid IP address") return NAT_IPs
def parse(self, pluginConfigContent): builder = SAXBuilder(0) doc = builder.build(StringReader(pluginConfigContent)) rootElement = doc.getRootElement() pluginConfig = WebSpherePluginConfig() pluginConfig.clustersByName = self._parseClusters(rootElement) pluginConfig.uriGroupsByName = self._parseUriGroups(rootElement) pluginConfig.routes = self._parseRoutes(rootElement) return pluginConfig
def _buildDocument(self, content): r''' @types: str-> org.jdom.Document @raise ValueError: if content is None @raise InvalidXmlException: if content is not valid xml ''' if not content: raise ValueError('Empty content') builder = SAXBuilder() builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", self.__laodExternalDtd) try: return builder.build(StringReader(content)) except JDOMException, e: raise InvalidXmlException(e.getMessage())
def getAgates(shellUtils, installpath, sapitsOSH, OSHVResult): mapInstanceNameToAgate = HashMap() filePath = installpath + '\\config\\ItsRegistryWGATE.xml' data = shellUtils.safecat(filePath) logger.debug('got ItsRegistryWGATE file') if data == None or error(data): logger.error('Got: [', data, '] when performing command [ safecat ', filePath, '] - terminating script') else: builder = SAXBuilder(0) doc = builder.build(StringReader(data)) root = doc.getRootElement() localWgates = getElementByAttrValue(root, 'key', 'name', 'LocalWgates') wgates = localWgates.getChildren() it = wgates.iterator() while it.hasNext(): wgate = it.next() value = wgate.getAttributeValue('name') if value.find('WGATE_') >= 0: instancesRoot = getElementByAttrValue(wgate, 'key', 'name', 'Instances') instances = instancesRoot.getChildren() itInstances = instances.iterator() while itInstances.hasNext(): instance = itInstances.next() instanceName = instance.getAttributeValue('name') logger.debug(instanceName) agatesRoot = getElementByAttrValue(instance, 'key', 'name', 'Agates') agates = agatesRoot.getChildren() itAgates = agates.iterator() while itAgates.hasNext(): agate = itAgates.next() agateHost = getElementByAttrValue(agate, 'value', 'name', 'Host') host = agateHost.getText() agates = mapInstanceNameToAgate.get(instanceName) if agates == None: agates = ArrayList() mapInstanceNameToAgate.put(instanceName, agates) try: ip = netutils.getHostAddress(host) hostOSH = modeling.createHostOSH(ip) OSHVResult.add(hostOSH) agateOSH = modeling.createApplicationOSH('sap_its_agate', 'ITS_AGATE_' + ip, hostOSH) OSHVResult.add(agateOSH) agates.add(agateOSH) except: logger.warn('Failed resolving IP for agate host ', host) return mapInstanceNameToAgate
def getNatIPFromConfigurationFile(): """ Read IP or IP range from configuration file. @return: A list contains IPAddress objects and IPNetwork objects """ NATIPConfigurationFileFolder = os.path.join(CollectorsParameters.BASE_PROBE_MGR_DIR, CollectorsParameters.getDiscoveryConfigFolder()) NATIPConfigurationFile = os.path.join(NATIPConfigurationFileFolder, 'NATIpAddress.xml') if not os.path.exists(NATIPConfigurationFile): logger.info("There is no NAT IP address defined.") return # Read tags from xml file builder = SAXBuilder() configDoc = builder.build(NATIPConfigurationFile) rootElement = configDoc.getRootElement() ipElements = rootElement.getChildren('Ip') ipRangeElements = rootElement.getChildren('IpRange') NAT_IPs = [] # Read IPAddress, add valid one to NAT_IPs list if ipElements: for ipElement in ipElements: ip = ipElement.getText() if ip_addr.isValidIpAddress(ip): ipObj = ip_addr.IPAddress(ip) NAT_IPs.append(ipObj) # Read IP Ranges, create IPNetwork and add to NAT_IPs list if ipRangeElements: for ipRangeElement in ipRangeElements: ip_range_raw = ipRangeElement.getText() ips = ip_range_raw.split('-') ip_start = ips[0] ip_end = ips[1] if ip_addr.isValidIpAddress(ip_start) and ip_addr.isValidIpAddress(ip_end): ip_start = ip_addr.IPAddress(ip_start) ip_end = ip_addr.IPAddress(ip_end) ips = ip_addr.summarize_address_range(ip_start, ip_end) logger.debug(ips) NAT_IPs.extend(ips) else: logger.warn("IP Range should start and end with valid IP address") return NAT_IPs
def getInstallerVersion(installerFile, Framework): try: return SAXBuilder().build(installerFile).getRootElement().getChildText('version') except: errorMessage = str(sys.exc_info()[1]) logger.debugException('Failed to fetch version for local installer:' + errorMessage) Framework.reportError(inventoryerrorcodes.INVENTORY_DISCOVERY_SCANNER_VERSION_COMPARISON_FAILED, [errorMessage]) return ''
def DiscoveryMain(Framework): OSHVResult = ObjectStateHolderVector() DebugMode = Framework.getParameter('DebugMode') userExtDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + '\\' filePathDir = userExtDir + 'TQLExport\\ARIS\\results\\' directory = File(filePathDir) files = directory.listFiles() if files == None: logger.warn('Results XML not found. Perhaps no data was received from ARIS or an error occurred in the Pull_From_ARIS script.') return try: ## Start the work for file in files: if file != None or file != '': builder = SAXBuilder () doc = builder.build(file) # Process CIs # info("Start processing CIs to update in the destination server...") allObjects = doc.getRootElement().getChild('data').getChild('objects').getChildren('Object') (objVector, ciDict) = processObjects(allObjects) OSHVResult.addAll(objVector) # Process Relations # info("Start processing Relationships to update in the destination server...") allLinks = doc.getRootElement().getChild('data').getChild('links').getChildren('link') linkVector = processLinks(allLinks, ciDict) OSHVResult.addAll(linkVector) except: stacktrace = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) info(concatenate('Failure: ():\n', stacktrace)) if (DebugMode != None): DebugMode = DebugMode.lower() if DebugMode == "true": mam_utils.info ('[NOTE] UCMDB Integration is running in DEBUG mode. No data will be pushed to the destination server.') print OSHVResult.toXmlString() return None else: #print OSHVResult.toXmlString() return OSHVResult
def _getXmlRootFromString(self, xmlString): """ Parses string xml representation and returns root element str->Element @raise JavaException: XML parsing failed """ xmlString = ''.join([line.strip() for line in xmlString.split('\n') if line]) strContent = String(xmlString) return SAXBuilder().build(ByteArrayInputStream(strContent.getBytes('utf-8'))).getRootElement()
def processInboundXml(operation, xmlResult, CONTEXT, debugMode, smartUpdateIgnoreFieldsList, sortCSVFieldsList): fileName = "%s-%s.xml" % (TIMESTAMP, operation) slogger.addAppender( FileAppender( SimpleLayout(), "%s%s%s_success.log" % (adapterResWorkDir, FILE_SEPARATOR, fileName))) slogger.setLevel(Level.DEBUG) flogger.addAppender( FileAppender( SimpleLayout(), "%s%s%s_failure.log" % (adapterResWorkDir, FILE_SEPARATOR, fileName))) flogger.setLevel(Level.DEBUG) saxBuilder = SAXBuilder() xmlData = saxBuilder.build(StringReader(xmlResult)) if debugMode: writeXmlFile(fileName, xmlData) try: # Process CIs cisData = xmlData.getRootElement().getChild('data').getChild( 'objects').getChildren('Object') if operation in ('add', 'update'): addCIs(CONTEXT, cisData, smartUpdateIgnoreFieldsList, sortCSVFieldsList) # Process Relationships try: relationsData = xmlData.getRootElement().getChild( 'data').getChild('links').getChildren('link') addRelations(CONTEXT, relationsData, smartUpdateIgnoreFieldsList, sortCSVFieldsList) except: pass elif operation == 'delete': deleteCIs(CONTEXT, cisData) except IOError, ioe: logger.debug(ioe) raise Exception, "Unable to process inbound XML"
def processXmlAndCreateGRLoaderXmlFiles(operation, xmlResult): fileName = "%s-%s.xml" % (TIMESTAMP, operation) doc = Document() saxBuilder = SAXBuilder() xmlData = saxBuilder.build(StringReader(xmlResult)) try: # Process CIs cisData = xmlData.getRootElement().getChild('data').getChild('objects').getChildren('Object') (mamIdToAttributesMap, itemsRootElement) = addCIs(Element('GRLoader'), cisData) # Process Relationships relationsData = xmlData.getRootElement().getChild('data').getChild('links').getChildren('link') itemsRootElementWithLinks = addRelations(itemsRootElement, relationsData, mamIdToAttributesMap) if itemsRootElementWithLinks != None: doc.setRootElement(itemsRootElementWithLinks) createGRLoaderXmlInputFile(fileName, doc) except: raise Exception, "Unable to process inbound XML" return fileName
def loadXmlFile(self, path, container=None, fileContent=None): 'str, osh, str -> Document' saxBuilder = SAXBuilder() globalSettings = GeneralSettingsConfigFile.getInstance() #loadExternalDTD = globalSettings.getPropertyBooleanValue('loadExternalDTD', 1) loadExternalDTD = 1 saxBuilder.setFeature( "http://apache.org/xml/features/nonvalidating/load-external-dtd", loadExternalDTD) logger.debug("loadXmlFile, loadExternalDTD: ", loadExternalDTD, ", path: ", path) if loadExternalDTD: saxBuilder.setEntityResolver( XMLExternalEntityResolver(self.fileMonitor, str(path), self.shellUtils)) saxBuilder.setFeature( "http://xml.org/sax/features/use-entity-resolver2", 1) doc = None try: fileContent = fileContent or self.fileMonitor.getFileContent(path) if fileContent: try: strContent = String(fileContent) strContent = String( strContent.substring(0, strContent.lastIndexOf('>') + 1)) doc = saxBuilder.build( ByteArrayInputStream(strContent.getBytes())) if container is not None: cfOSH = self.createCF(container, path, fileContent) if cfOSH is not None: self.OSHVResult.add(cfOSH) except: logger.debugException('Failed to load xml file:', path) excMsg = traceback.format_exc() logger.debug(excMsg) except: logger.debugException('Failed to get content of file:', path) excMsg = traceback.format_exc() logger.debug(excMsg) return doc
def syncNmapPortConfigFile(agentPath): ''' Sync nmap port config with global probe's "port number to port name" mapping ''' logger.debug('synchronizing nmap port config file') portConfigFilename = agentPath + CollectorsParameters.getDiscoveryConfigFolder() + CollectorsParameters.FILE_SEPARATOR + 'portNumberToPortName.xml' mamservice = File(portConfigFilename) nmapservice = File(agentPath + CollectorsParameters.getDiscoveryResourceFolder() + CollectorsParameters.FILE_SEPARATOR + 'nmap-services') if nmapservice.lastModified() > mamservice.lastModified(): return nmapFile = FileOutputStream(nmapservice) document = SAXBuilder(0).build(mamservice) # document = parse(portConfigFilename) ports = XmlWrapper(document.getRootElement().getChildren('portInfo')) for port in ports: if int(port.getAttributeValue("discover")): portNumber = port.getAttributeValue("portNumber") portName = port.getAttributeValue("portName") portProtocol = port.getAttributeValue("portProtocol") nmapFile.write("%s\t%s/%s\r\n" % (portName, portNumber, portProtocol)) nmapFile.close()
def readModules(): mapCategoryToAppComponent = HashMap() try: builder = SAXBuilder(0) separator = CollectorsParameters.FILE_SEPARATOR fileLocation = CollectorsParameters.BASE_PROBE_MGR_DIR + separator + 'discoveryResources' + separator + 'sap-application-components.xml' doc = builder.build(FileReader(File(fileLocation))) root = doc.getRootElement() categories = root.getChildren('category') if categories != None: itCat = categories.iterator() while itCat.hasNext(): categoryElem = itCat.next() category = categoryElem.getAttributeValue('name') applicationComponents = categoryElem.getChildren('application-component') itComp = applicationComponents.iterator() while itComp.hasNext(): component = itComp.next().getAttributeValue('name') mapCategoryToAppComponent.put(component,category) except: logger.errorException('Failed to read modules') return mapCategoryToAppComponent
def readModules(): mapCategoryToAppComponent = HashMap() try: builder = SAXBuilder(0) separator = CollectorsParameters.FILE_SEPARATOR fileLocation = CollectorsParameters.BASE_PROBE_MGR_DIR + separator + 'discoveryResources' + separator + 'sap-application-components.xml' doc = builder.build(FileReader(File(fileLocation))) root = doc.getRootElement() categories = root.getChildren('category') if categories != None: itCat = categories.iterator() while itCat.hasNext(): categoryElem = itCat.next() category = categoryElem.getAttributeValue('name') applicationComponents = categoryElem.getChildren( 'application-component') itComp = applicationComponents.iterator() while itComp.hasNext(): component = itComp.next().getAttributeValue('name') mapCategoryToAppComponent.put(component, category) except: logger.errorException('Failed to read modules') return mapCategoryToAppComponent
def parseServerNamesFromConfig(self, configContent): names = [] if configContent: try: builder = SAXBuilder(0) doc = builder.build(StringReader(configContent)) root = doc.getRootElement() processManager = root.getChildren() processManagerIterator = processManager.iterator() while processManagerIterator.hasNext(): currProcessManager = processManagerIterator.next() currElementName = currProcessManager.getName() if currElementName == 'process-manager': iasInstance = currProcessManager.getChildren() iasInstanceIterator = iasInstance.iterator() while iasInstanceIterator.hasNext(): currIasInstance = iasInstanceIterator.next() if currIasInstance.getName() == 'ias-instance': iasName = currIasInstance.getAttributeValue('name') or currIasInstance.getAttributeValue('id') or 'Default Server' names.append(iasName) except: logger.error('Failed to parse iAS config file.') logger.debugException('') return names
def loadXmlFile(self, path, container = None, fileContent = None): 'str, osh, str -> Document' saxBuilder = SAXBuilder() globalSettings = GeneralSettingsConfigFile.getInstance() #loadExternalDTD = globalSettings.getPropertyBooleanValue('loadExternalDTD', 1) loadExternalDTD = 1 saxBuilder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", loadExternalDTD) logger.debug("loadXmlFile, loadExternalDTD: ", loadExternalDTD, ", path: ", path ) if loadExternalDTD : saxBuilder.setEntityResolver( XMLExternalEntityResolver( self.fileMonitor, str(path), self.shellUtils ) ) saxBuilder.setFeature("http://xml.org/sax/features/use-entity-resolver2", 1) doc = None try: fileContent = fileContent or self.fileMonitor.getFileContent(path) if fileContent: try: strContent = String(fileContent) strContent = String(strContent.substring(0, strContent.lastIndexOf('>') + 1)) doc = saxBuilder.build(ByteArrayInputStream(strContent.getBytes())) if container is not None: cfOSH = self.createCF(container, path, fileContent) if cfOSH is not None: self.OSHVResult.add(cfOSH) except: logger.debugException('Failed to load xml file:', path) excMsg = traceback.format_exc() logger.debug( excMsg ) except: logger.debugException('Failed to get content of file:', path) excMsg = traceback.format_exc() logger.debug( excMsg ) return doc
def processNmapResult(fileName, OSHVResult, discoverOsName, doServiceFingerprints, createApp, Framework): try: document = SAXBuilder(0).build(fileName) except: raise ValueError, "Can't parse XML document with nmap results. Skipped." hosts = XmlWrapper(document.getRootElement().getChildren('host')) for host in hosts: hostOsh = None ip = None macs = [] addresses = XmlWrapper(host.getChildren('address')) for address in addresses: type = address.getAttributeValue('addrtype') addr = address.getAttributeValue('addr') if type == 'ipv4': ip = addr elif type == 'mac': macs.append(addr) hostnames = host.getChild('hostnames') if (hostnames is not None) and netutils.isValidIp(ip): hostnames = map(lambda elem: elem.getAttributeValue('name'), XmlWrapper(hostnames.getChildren('hostname'))) hostname = hostnames and hostnames[0] or None #using only first dnsname os = host.getChild('os') if os and discoverOsName: osClass = os.getChild('osclass') if not osClass: osMatch = os.getChild('osmatch') osClass = osMatch.getChild('osclass') if osClass: osType = osClass.getAttributeValue("type") osFamily = osClass.getAttributeValue("osfamily") osVendor = osClass.getAttributeValue("vendor") hostClass = getHostClass(osType, osFamily) if not hostClass: Framework.reportWarning("Unknown OS detected. Vendor '%s', family '%s'" % (osVendor, osFamily)) hostClass = "host" hostOsh = modeling.createHostOSH(ip, hostClass) hostOsh.setAttribute("host_vendor", osVendor) osMatch = os.getChild('osmatch') if osMatch: separateCaption(hostOsh, osMatch.getAttributeValue("name")) hostOsh.setAttribute("host_osaccuracy", osMatch.getAttributeValue("accuracy") + '%') if not hostOsh: hostOsh = modeling.createHostOSH(ip) ipOsh = modeling.createIpOSH(ip, dnsname=hostname) OSHVResult.add(ipOsh) OSHVResult.add(finalizeHostOsh(hostOsh)) OSHVResult.add(modeling.createLinkOSH('contained', hostOsh, ipOsh)) for mac in macs: if netutils.isValidMac(mac): interfaceOsh = modeling.createInterfaceOSH(mac, hostOsh) OSHVResult.add(interfaceOsh) OSHVResult.add(modeling.createLinkOSH('containment', interfaceOsh, ipOsh)) applicationList = [] if not host.getChild('ports'): return ports = XmlWrapper(host.getChild('ports').getChildren('port')) for port in ports: portNumber = port.getAttributeValue('portid') protocol = port.getAttributeValue('protocol') serviceName = None if doServiceFingerprints: if port.getChild("state").getAttributeValue("state").find('open') == -1: continue serviceNode = port.getChild("service") if serviceNode: serviceName = serviceNode.getAttributeValue("name") serviceProduct = serviceNode.getAttributeValue("product") serviceVersion = serviceNode.getAttributeValue("version") if createApp and serviceProduct and serviceProduct not in applicationList: addApplicationCI(ip,hostOsh,serviceProduct,serviceVersion, OSHVResult) applicationList.append(serviceProduct) addServiceAddressOsh(hostOsh, OSHVResult, ip, portNumber, protocol, serviceName)
def disVMKernel(host_obj, client, Framework, langBund=None, host_is_virtual = False): resVec = ObjectStateHolderVector() xml = client.execCmd('esxcfg-info -F xml -w | sed -n \'/<cpu-info>/,/<\/cpu-info>/p\'') builder = SAXBuilder(0) document = builder.build(StringReader(xml)) rootElement = document.getRootElement() cpu_value_items = rootElement.getChildren('value') cpuModel = '' cpuCount = 1 for cpu_item in cpu_value_items: if cpu_item.getAttributeValue('name') == 'num-packages': cpuCount = int(cpu_item.getText()) elif cpu_item.getAttributeValue('name') == 'num-cores': numCores = int(cpu_item.getText()) elif cpu_item.getAttributeValue('name') == 'cpu-model-name': cpuModel = cpu_item.getText() cpu_packages = rootElement.getChild('cpupackages') if cpu_packages: #For ESXi 4.0 cpu_packages = cpu_packages.getChildren('cpupackage') for cpu_package in cpu_packages: cpuId = '' coresPerCPU = '' cpuVendor = '' cpuSpeed = '' cpu_package_values = cpu_package.getChildren('value') for value in cpu_package_values: if value.getAttributeValue('name') == 'num-cores': coresPerCPU = int(value.getText()) elif value.getAttributeValue('name') == 'id': cpuId = 'CPU' + value.getText() cpu_impl_values = cpu_package.getChild('cpu-cores').getChild('cpuimpl').getChildren('value') for value in cpu_impl_values: if value.getAttributeValue('name') == 'name': cpuVendor = value.getText() elif value.getAttributeValue('name') == 'cpu-speed': cpuSpeed = value.getText() cpuSpeed = cpuSpeed and cpuSpeed.isdigit() and (int(cpuSpeed) / 1000000) or cpuSpeed resVec.add(makeCPUOSH(host_obj, cpuId, cpuVendor, cpuSpeed, '', cpuModel, coresPerCPU)) else: #For ESX 3.5 coresPerCPU = numCores / cpuCount cpus = rootElement.getChild('cpus') for cpuId in range(cpuCount): cpuVendor = '' cpuSpeed = '' cpuIdStr = 'CPU' + str(cpuId) cpu_impl_values = cpus.getChildren('cpuimpl')[cpuId * coresPerCPU].getChildren('value') for value in cpu_impl_values: if value.getAttributeValue('name') == 'name': cpuVendor = value.getText() elif value.getAttributeValue('name') == 'cpu-speed': cpuSpeed = value.getText() cpuSpeed = cpuSpeed and cpuSpeed.isdigit() and (int(cpuSpeed) / 1000000) or cpuSpeed resVec.add(makeCPUOSH(host_obj, cpuIdStr, cpuVendor, cpuSpeed, '', cpuModel, coresPerCPU)) return resVec
def DiscoveryMain(Framework): # Prepare the maps to store the mappings of IDs objectMappings = HashMap() linkMappings = HashMap() try: ucmdbUpdateResult = None mamIdToSysIdMap = HashMap() #Stores mapping between UCMDB mamId to ServiceNow sys_id logger.debug('========================================================') logger.debug('Starting Push to Service-Now...') credentialsId = str(Framework.getDestinationAttribute('credentialsId')) credential = ProtocolDictionaryManager.getProtocolById(credentialsId) username = credential.getProtocolAttribute('protocol_username') password = credential.getProtocolAttribute('protocol_password') host = Framework.getDestinationAttribute('ServiceNowDomain') or 'service-now.com' protocol = Framework.getDestinationAttribute('protocol') or 'https' if protocol == 'http': port = Framework.getDestinationAttribute('port') or '80' else: port = Framework.getDestinationAttribute('port') or '443' instance = Framework.getDestinationAttribute('ServiceNowInstance') or 'demo' proxyServer = Framework.getDestinationAttribute('ProxyServer') or None proxyPort = Framework.getDestinationAttribute('ProxyPort') or None importSetsInUse = Framework.getDestinationAttribute('ImportSetsInUse') or 'false' insertMultiple = Framework.getDestinationAttribute('InsertMultiple') or 'false' insertMultipleBulkSize = Framework.getDestinationAttribute('InsertMultipleBulkSize') or '50' retryCount = Framework.getDestinationAttribute('RetryCount') or '3' retryDelaySeconds = Framework.getDestinationAttribute('RetryDelaySeconds') or '5' global IS_INSERT_MULTIPLE, INSERT_MULTIPLE_BULK_SIZE, RETRY_COUNT, RETRY_DELAY_SECONDS, FAIL_BULK failBulk = Framework.getDestinationAttribute('FailBulk') or 'true' FAIL_BULK = failBulk == 'true' IS_INSERT_MULTIPLE = insertMultiple == 'true' INSERT_MULTIPLE_BULK_SIZE = int (insertMultipleBulkSize) RETRY_COUNT = int (retryCount) RETRY_DELAY_SECONDS = int (retryDelaySeconds) logger.debug('Parameters: IS_INSERT_MULTIPLE:%s, INSERT_MULTIPLE_BULK_SIZE:%s, RETRY_COUNT:%s, RETRY_DELAY_SECONDS:%s' % (INSERT_MULTIPLE_BULK_SIZE, INSERT_MULTIPLE_BULK_SIZE, RETRY_COUNT, RETRY_DELAY_SECONDS)) debugPrint(1, '[DiscoveryMain] Service-Now URL: <%s://%s.%s:%s>, using proxy <%s:%s>' % (protocol, instance, host, port, proxyServer, proxyPort)) ## Are Service Now Web Service Import Sets in use? importSetUse = 0 if importSetsInUse and importSetsInUse.lower().strip() in ['yes', 'y', '1', 'true']: importSetUse = 1 #Connection parameter to ServiceNow SNConnPropMap = HashMap() SNConnPropMap.put('host', host) SNConnPropMap.put('port', port) SNConnPropMap.put('instance', instance) SNConnPropMap.put('protocol', protocol) SNConnPropMap.put('username', username) SNConnPropMap.put('password', password) SNConnPropMap.put('proxyServer', proxyServer) SNConnPropMap.put('proxyPort', proxyPort) # get add/update/delete result objects from the Framework addResult = Framework.getTriggerCIData('addResult') updateResult = Framework.getTriggerCIData('updateResult') deleteResult = Framework.getTriggerCIData('deleteResult') debugPrint(3, '****************************************************************') debugPrint(3, '************************* addResult ****************************') debugPrint(3, addResult) debugPrint(3, '****************************************************************') debugPrint(3, '************************* updateResult *************************') debugPrint(3, updateResult) debugPrint(3, '****************************************************************') debugPrint(3, '************************* deleteResult *************************') debugPrint(3, deleteResult) debugPrint(3, '****************************************************************') saxBuilder = SAXBuilder() addXml = saxBuilder.build(StringReader(addResult)) updateXml = saxBuilder.build(StringReader(updateResult)) deleteXml = saxBuilder.build(StringReader(deleteResult)) proceedToNext = 1 resultCountMap = HashMap() resultCountMap.put('add_ci', 0) resultCountMap.put('update_ci', 0) resultCountMap.put('delete_ci', 0) resultCountMap.put('add_rel', 0) resultCountMap.put('update_rel', 0) resultCountMap.put('delete_rel', 0) if addXml: debugPrint(1, '[DiscoveryMain] ========== Process items to add ==========') allObjectChildren = addXml.getRootElement().getChild('data').getChild('objects').getChildren('Object') proceedToNext = processCIs(allObjectChildren, SNConnPropMap, objectMappings, resultCountMap, mamIdToSysIdMap, importSetUse) if proceedToNext: allLinkChildren = addXml.getRootElement().getChild('data').getChild('links').getChildren('link') processRelations(allLinkChildren, SNConnPropMap, linkMappings, resultCountMap, mamIdToSysIdMap, importSetUse) else: Framework.reportError('[DiscoveryMain] Error adding CIs...please check probe logs!') return ucmdbUpdateResult else: logger.info("[DiscoveryMain] No data to add") if proceedToNext: if updateXml: debugPrint(1, '[DiscoveryMain] ========== Process updated items ==========') allObjectChildren = updateXml.getRootElement().getChild('data').getChild('objects').getChildren('Object') processCIs(allObjectChildren, SNConnPropMap, objectMappings, resultCountMap, mamIdToSysIdMap, importSetUse) allLinkChildren = updateXml.getRootElement().getChild('data').getChild('links').getChildren('link') processRelations(allLinkChildren, SNConnPropMap, linkMappings, resultCountMap, mamIdToSysIdMap, importSetUse) else: logger.info("[DiscoveryMain] No data to update") if deleteXml: debugPrint(1, '[DiscoveryMain] ========== Process deleted items ==========') allObjectChildren = deleteXml.getRootElement().getChild('data').getChild('objects').getChildren('Object') processCIs(allObjectChildren, SNConnPropMap, objectMappings, resultCountMap, mamIdToSysIdMap, importSetUse) allLinkChildren = deleteXml.getRootElement().getChild('data').getChild('links').getChildren('link') processRelations(allLinkChildren, SNConnPropMap, linkMappings, resultCountMap, mamIdToSysIdMap, importSetUse) else: logger.info("[DiscoveryMain] No data to delete") debugPrint(1, '[DiscoveryMain] --------------------------------------------------------') logger.info('[DiscoveryMain] CIs added <%s>, updated <%s>, deleted <%s>' % (resultCountMap.get('add_ci'), resultCountMap.get('update_ci'), resultCountMap.get('delete_ci'))) logger.info('[DiscoveryMain] Relationships added <%s>, updated <%s>, deleted <%s>' % (resultCountMap.get('add_rel'), resultCountMap.get('update_rel'), resultCountMap.get('delete_rel'))) debugPrint(1, '[DiscoveryMain] ========================================================') debugPrint(5, '[DiscoveryMain] MAPPING: CIs: ', objectMappings, ', links: ', linkMappings) logger.debug('Finished Push to Service-Now!') logger.debug('========================================================') except: excInfo = logger.prepareJythonStackTrace('') logger.warn('[DiscoveryMain] Exception: <%s>' % excInfo) logger.reportError('[DiscoveryMain] Exception: <%s>' % excInfo) debugPrint(5, '[DiscoveryMain] MAPPING after exception: CIs: ', objectMappings, ', links: ', linkMappings) return DataPushResultsFactory.createDataPushResults(objectMappings, linkMappings)
def processNmapResult(fileName, OSHVResult, discoverOsName, doServiceFingerprints, createApp, Framework): try: document = SAXBuilder(0).build(fileName) except: raise ValueError, "Can't parse XML document with nmap results. Skipped." hosts = XmlWrapper(document.getRootElement().getChildren('host')) for host in hosts: hostOsh = None ip = None macs = [] addresses = XmlWrapper(host.getChildren('address')) for address in addresses: type = address.getAttributeValue('addrtype') addr = address.getAttributeValue('addr') if type == 'ipv4': ip = addr elif type == 'mac': macs.append(addr) hostnames = host.getChild('hostnames') if (hostnames is not None) and netutils.isValidIp(ip): hostnames = map(lambda elem: elem.getAttributeValue('name'), XmlWrapper(hostnames.getChildren('hostname'))) hostname = hostnames and hostnames[ 0] or None #using only first dnsname os = host.getChild('os') if os and discoverOsName: osClass = os.getChild('osclass') if not osClass: osMatch = os.getChild('osmatch') osClass = osMatch.getChild('osclass') if osClass: osType = osClass.getAttributeValue("type") osFamily = osClass.getAttributeValue("osfamily") osVendor = osClass.getAttributeValue("vendor") hostClass = getHostClass(osType, osFamily) if not hostClass: Framework.reportWarning( "Unknown OS detected. Vendor '%s', family '%s'" % (osVendor, osFamily)) hostClass = "host" hostOsh = modeling.createHostOSH(ip, hostClass) hostOsh.setAttribute("host_vendor", osVendor) osMatch = os.getChild('osmatch') if osMatch: separateCaption(hostOsh, osMatch.getAttributeValue("name")) hostOsh.setAttribute( "host_osaccuracy", osMatch.getAttributeValue("accuracy") + '%') if not hostOsh: hostOsh = modeling.createHostOSH(ip) ipOsh = modeling.createIpOSH(ip, dnsname=hostname) OSHVResult.add(ipOsh) OSHVResult.add(finalizeHostOsh(hostOsh)) OSHVResult.add(modeling.createLinkOSH('contained', hostOsh, ipOsh)) for mac in macs: if netutils.isValidMac(mac): interfaceOsh = modeling.createInterfaceOSH(mac, hostOsh) OSHVResult.add(interfaceOsh) OSHVResult.add( modeling.createLinkOSH('containment', interfaceOsh, ipOsh)) applicationList = [] if not host.getChild('ports'): return ports = XmlWrapper(host.getChild('ports').getChildren('port')) for port in ports: portNumber = port.getAttributeValue('portid') protocol = port.getAttributeValue('protocol') serviceName = None if doServiceFingerprints: if port.getChild("state").getAttributeValue("state").find( 'open') == -1: continue serviceNode = port.getChild("service") if serviceNode: serviceName = serviceNode.getAttributeValue("name") serviceProduct = serviceNode.getAttributeValue( "product") serviceVersion = serviceNode.getAttributeValue( "version") if createApp and serviceProduct and serviceProduct not in applicationList: addApplicationCI(ip, hostOsh, serviceProduct, serviceVersion, OSHVResult) applicationList.append(serviceProduct) addServiceAddressOsh(hostOsh, OSHVResult, ip, portNumber, protocol, serviceName)
def parseOpmnXml(opmnXML, HOST_IP, ORACLE_HOME, MANAGER_PORT, shellUtils, OSHVResult, Framework): builder = SAXBuilder(0) doc = builder.build(StringReader(opmnXML)) root = doc.getRootElement() ucmdbVersion = modeling.CmdbClassModel().version() processManager = root.getChildren() processManagerIterator = processManager.iterator() while processManagerIterator.hasNext(): currProcessManager = processManagerIterator.next() currElementName = currProcessManager.getName() if currElementName == 'process-manager': iasInstance = currProcessManager.getChildren() iasInstanceIterator = iasInstance.iterator() while iasInstanceIterator.hasNext(): currIasInstance = iasInstanceIterator.next() if currIasInstance.getName() == 'ias-instance': OracleApplicationServerName = currIasInstance.getAttributeValue('name') or currIasInstance.getAttributeValue('id') or 'Default Server' discoveredHost = modeling.createHostOSH(HOST_IP) # Create Oracle IAS oracleIASOSH = modeling.createJ2EEServer('oracleias', HOST_IP, int(MANAGER_PORT), discoveredHost, OracleApplicationServerName) OSHVResult.add(oracleIASOSH) iasComponent = currIasInstance.getChildren() iasComponentIterator = iasComponent.iterator() while iasComponentIterator.hasNext(): currIasComponent = iasComponentIterator.next() if 'ias-component' == currIasComponent.getName(): groupName = currIasComponent.getAttributeValue('id') # Create OC4J Group oc4jGroupOSH = ObjectStateHolder('oc4jgroup') oc4jGroupOSH.setContainer(oracleIASOSH) oc4jGroupOSH.setAttribute('data_name', groupName) OSHVResult.add(oc4jGroupOSH) #'process-type' processType = currIasComponent.getChildren() processTypeIterator = processType.iterator() while processTypeIterator.hasNext(): currProcessType = processTypeIterator.next() oc4jName = currProcessType.getAttributeValue('id') moduleId = currProcessType.getAttributeValue('module-id') if 'OC4J' == moduleId: oc4jOSH = ObjectStateHolder('oc4j') oc4jOSH.setContainer(oc4jGroupOSH) oc4jOSH.setAttribute('data_name', oc4jName) OSHVResult.add(oc4jOSH) try: serverXML = shellUtils.safecat('%s/j2ee/%s/config/server.xml' % (ORACLE_HOME, oc4jName)) tg = '<application name="(\w+)"' compiled = re.compile(tg,re.S) matches = compiled.findall(serverXML) appList = {} for match in matches: if ucmdbVersion < 9: applicationOSH = modeling.createApplicationOSH('application', match, oc4jOSH) else: applicationOSH = ObjectStateHolder('oc4j_app') applicationOSH.setAttribute('data_name',match) applicationOSH.setContainer(oc4jOSH) # OSHVResult.add(applicationOSH) appList[match] = applicationOSH except: logger.debugException() logger.warn('Failed to get server.xml') # Check if it holds web service wsdlDir = shellUtils.rebuildPath('%s/j2ee/%s/application-deployments/' % (ORACLE_HOME, OracleApplicationServerName)) fileMon = file_mon_utils.FileMonitor(Framework, shellUtils, OSHVResult, None, None) files = fileMon.getFilesInPath(wsdlDir, '*.wsdl') if (files == []): wsdlDir = shellUtils.rebuildPath('%s/j2ee/%s/application-deployments/' % (ORACLE_HOME, oc4jName)) logger.info('Pi Debug - parseOpmnXml() - trying with wsdlDir = %s' % wsdlDir) files = fileMon.getFilesInPath(wsdlDir, '*.wsdl') wsdlDirRes = '\n'.join(files) if wsdlDirRes.find('File Not Found') != -1: # NO WSDL continue else: # WSDL handleWSDL(HOST_IP, MANAGER_PORT, wsdlDirRes, wsdlDir, appList, shellUtils, OSHVResult)
from org.jdom.input import SAXBuilder from org.jdom import Namespace import sys if len(sys.argv) < 2: print "Usage: %s filename" % sys.argv[0] sys.exit(0) filename = sys.argv[1] builder = SAXBuilder(0) print "Read in document from", filename doc = builder.build(filename) root = doc.getRootElement() # print "root", root print "Root element name: %s, namespace prefix: %s." \ % (root.getName(), root.getNamespacePrefix()) # --- retrieve default namespace from root element ns_root = root.getNamespace() ns_root_others = root.getAdditionalNamespaces() # print "additional namespaces", ns_root_others dns = None for ns in ns_root_others: if ns.getPrefix() == "": print "default namespace", ns.getURI() dns = ns