def DiscoveryMain(Framework): logger.info('Start Phase 2 ....Apply Mapping file to ARIS CIs') userExtUcmdbDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + '\\TQLExport\\ARIS\\' inputFilesDirectory = File(userExtUcmdbDir + 'inter\\') inputFiles = inputFilesDirectory.listFiles() filePathDir = userExtUcmdbDir + 'results\\' directory = File(filePathDir) files = directory.listFiles() ## Clean up the existing result XML files if (files != None): for file in files: file.delete() ## Make sure we have XML files in the intermediate directory xmlFileInIntermediatesDirectory = 0 for inputFile in inputFiles: inputFileName = inputFile.getName() if inputFileName[len(inputFileName)-4:].lower() == '.xml' and inputFile.length() > 0: xmlFileInIntermediatesDirectory = 1 if not xmlFileInIntermediatesDirectory: logger.warn('Intermediate XML not found or invalid. Perhaps no data was received from ARIS or an error occurred in the Pull_from_ARIS script.') return ## Connect to the UCMDB Server, retrieve the results of the TQL ## and generate the output XML files in results directory ip = CollectorsParameters.getValue(CollectorsParameters.KEY_SERVER_NAME) exportTQL(ip, userExtUcmdbDir) logger.info('End Phase 2 ....Apply Mapping file to ARIS CIs')
def queryRegistry(self, client, regKey, valueName): if not (client and regKey and valueName): logger.warn('registry query is incomplete') return logger.debug('RegistryBasedPlugin.queryRegistry') ntcmdErrStr = 'Remote command returned 1(0x1)' queryStr = ' query "%s" /v "%s"' % (regKey, valueName) system32Link = client.createSystem32Link() or '' buffer = client.execCmd(system32Link + "reg.exe" + queryStr) if client.getLastCmdReturnCode() != 0 or buffer.find(ntcmdErrStr) != -1: localFile = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + CollectorsParameters.FILE_SEPARATOR + 'reg_mam.exe' remoteFile = client.copyFileIfNeeded(localFile) if not remoteFile: logger.warn('Failed copying reg_mam.exe to the destination') return buffer = client.execCmd(remoteFile + queryStr) if not buffer or client.getLastCmdReturnCode() != 0: logger.warn("Failed getting registry info.") return match = re.search(r'%s\s+%s\s+\w+\s+(.*)' % (regKey.replace('\\', '\\\\'), valueName), buffer, re.I) client.removeSystem32Link() if match: val = match.group(1) return val.strip() logger.warn('Cannot parse registry key')
def buildFullPathToScenario(self, scenarioFileName): return ( CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + CollectorsParameters.FILE_SEPARATOR + scenarioFileName )
def DiscoveryMain(Framework): OSHVResult = ObjectStateHolderVector() logger.debug('Start nmap_osfingerprint.py') ip = Framework.getDestinationAttribute('ip_address') timeout = Framework.getParameter('nmap_host_timeout') if not str(timeout).isdigit(): msg = "Timeout parameter value must be a digit" logger.debug(msg) errormessages.resolveAndReport(msg, ClientsConsts.LOCAL_SHELL_PROTOCOL_NAME, Framework) return OSHVResult timeout = int(timeout) * 1000 scanKnownPortsOnly = Boolean.parseBoolean( Framework.getParameter('scan_known_ports_only')) portstoscan = Framework.getParameter('scan_these_ports_only') doServiceFingerprints = Boolean.parseBoolean( Framework.getParameter('Perform_Port_Fingerprints')) createApp = Boolean.parseBoolean( Framework.getParameter('Create_Application_CI')) discoverOsName = Boolean.parseBoolean( Framework.getParameter('discover_os_name')) nmapLocation = Framework.getParameter('nmap_location') #discover_UDP_Ports = int(Framework.getParameter('Discover_UDP_Ports')) discoverUdpPorts = 0 agent_root_dir = CollectorsParameters.BASE_PROBE_MGR_DIR agent_ext_dir = agent_root_dir + CollectorsParameters.getDiscoveryResourceFolder( ) + CollectorsParameters.FILE_SEPARATOR tmp_file_name = agent_ext_dir + string.replace( ip, '.', '_') + time.strftime("%H%M%S", time.gmtime( time.time())) + 'nmap.xml' syncNmapPortConfigFile(agent_root_dir) logger.debug('temp file for storing nmap results: ', tmp_file_name) try: client = Framework.createClient( ClientsConsts.LOCAL_SHELL_PROTOCOL_NAME) try: performNmapDiscover(client, ip, tmp_file_name, timeout, agent_ext_dir, scanKnownPortsOnly, portstoscan, doServiceFingerprints, discoverUdpPorts, nmapLocation) if os.path.exists(tmp_file_name): logger.debug('start processing the nmap results') processNmapResult(tmp_file_name, OSHVResult, discoverOsName, doServiceFingerprints, createApp, Framework) else: raise ValueError, 'Error nmap result file is missing: %s' % tmp_file_name finally: client.close() File(tmp_file_name).delete() except Exception, e: msg = str(e.getMessage()) logger.debug(msg) errormessages.resolveAndReport(msg, ClientsConsts.LOCAL_SHELL_PROTOCOL_NAME, Framework)
def discoverMemory(shell, myVec, hostOSH): ''' Discover physical memory by NTCMD Shell, oshVector, osh @command: meminfo ''' cmdMemInfo = 'meminfo' ntcmdErrStr = 'Remote command returned 1(0x1)' localFile = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + CollectorsParameters.FILE_SEPARATOR + 'meminfo.exe' remoteFile = shell.copyFileIfNeeded(localFile) if not remoteFile: logger.warn('Failed copying %s' % cmdMemInfo) return buffer = shell.execCmd(remoteFile)#V@@CMD_PERMISION ntcmd protocol execution logger.debug('Output of ', remoteFile, ': ', buffer) if buffer.find(ntcmdErrStr) != -1: logger.warn('Failed getting memory info') else: logger.debug('Got memory info - parsing...') buffer = buffer.strip() size = 0 try: matchSize = re.search('Total: (\d+) KB', buffer) if matchSize: size = int(matchSize.group(1)) memory.report(myVec, hostOSH, size) except: logger.errorException('Error in discoverMemory()')
def getWindowsShellFileVer(shell, path): # path - file name with full path for which we'll look Version Information # shell - NTCMD shell fileVerVBS = 'getfilever.vbs' localFile = (CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + CollectorsParameters.FILE_SEPARATOR + fileVerVBS) remoteFile = shell.copyFileIfNeeded(localFile) if not remoteFile: logger.warn('Failed copying file ' + fileVerVBS) return None errString = 'No version information available.' resultBuffer = shell.execCmd('Cscript.exe /nologo ' + fileVerVBS + ' \"' + path + '\"') if resultBuffer.find(errString) != -1 or shell.getLastCmdReturnCode() != 0: logger.warn('Failed getting file version info for file %s' % path) return None fileVersion = re.search('\s*([\d., ]+).*', resultBuffer) if fileVersion: return fileVersion.group(1).strip() else: logger.warn('Failed getting file version info for file %s' % path) return None
def DiscoveryMain(Framework): fileSeparator = File.separator # Destination Data userExtUcmdbDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + fileSeparator + 'TQLExport' + fileSeparator + 'hpsim' + fileSeparator inputFilesDirectory = File(userExtUcmdbDir + 'inter' + fileSeparator) inputFiles = inputFilesDirectory.listFiles() filePathDir = userExtUcmdbDir + 'results' + fileSeparator directory = File(filePathDir) files = directory.listFiles() ## Clean up the existing result XML files if (files != None): for file in files: file.delete() ## Make sure we have XML files in the intermediate directory xmlFileInIntermediatesDirectory = 0 for inputFile in inputFiles: inputFileName = inputFile.getName() if inputFileName[len(inputFileName)-4:].lower() == '.xml' and inputFile.length() > 0: xmlFileInIntermediatesDirectory = 1 if not xmlFileInIntermediatesDirectory: logger.warn('Intermediate XML not found or invalid. Perhaps no data was received from SIM or an error occurred in the SIM_Discovery script.') return ## Connect to the UCMDB Server, retrieve the results of the TQL ## and generate the output XML files in results directory ip = CollectorsParameters.getValue(CollectorsParameters.KEY_SERVER_NAME) exportTQL(ip, userExtUcmdbDir) info('End exportTQL_for_SIM_to_UCMDB.py')
def queryRegistryViaShell(shellUtils, key, filter = None): missingKeyError = 'The system was unable to find the specified registry key or value' filter = filter and " | find \"%s\"" % filter or '' query = "query %s /S%s" % (key, filter) cmd = ' '.join(['reg', query]) output, code = getCommandOutput(shellUtils, cmd) if output and re.search(missingKeyError, output): return if code != 0: output = None if not output: localFile = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + CollectorsParameters.FILE_SEPARATOR + 'reg_mam.exe' remoteFile = shellUtils.copyFileIfNeeded(localFile) if not remoteFile: logger.warn("Failed copying re_mam.exe") return cmd = ' '.join([remoteFile, query]) output, code = getCommandOutput(shellUtils, cmd) if output and re.search(missingKeyError, output) or code != 0: output = None return output
def doQuery(self, queryStr): cmdRemoteAgent = self.DEFAULT_REG_TOOL + queryStr ntcmdErrStr = 'Remote command returned 1(0x1)' timeout = 180000 buffer = self.shell.execCmd(cmdRemoteAgent, timeout) # @@CMD_PERMISION ntcmd protocol execution logger.debug('Outputing ', cmdRemoteAgent, ': ...') reg_mamRc = self.shell.getLastCmdReturnCode() if (reg_mamRc != 0) or (buffer.find(ntcmdErrStr) != -1): logger.debug('reg ended unsuccessfully with return code:%d, error:%s' % (reg_mamRc, buffer)) logger.debug('Failed getting services info using reg.exe trying the reg_mam.exe') localFile = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + CollectorsParameters.FILE_SEPARATOR + self.REG_MAM_REG_TOOL remoteFile = self.shell.copyFileIfNeeded(localFile) cmdRemote = self.REG_MAM_REG_TOOL if not remoteFile: logger.warn('Failed copying %s' % cmdRemote) return cmdRemoteAgent = remoteFile + queryStr buffer = self.shell.execCmd(cmdRemoteAgent, timeout) # @@CMD_PERMISION ntcmd protocol execution regRc = self.shell.getLastCmdReturnCode() if (regRc != 0) or (buffer.find(ntcmdErrStr) != -1): errMessage = 'NTCMD: Failed getting services info.' errobj = errorobject.createError(errorcodes.FAILED_GETTING_INFORMATION, ['NTCMD', 'services info'], errMessage) logger.reportWarningObject(errobj) logger.debug('Failed getting services info, reg_mam.exe ended with %d, error:%s' % (regRc, buffer)) return return buffer
def DiscoveryMain(Framework): logger.info('Start Phase 2 ... Apply Mapping transformation to Atrium CIs') userExtUcmdbDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + '\\TQLExport\\Atrium\\' inputFilesDirectory = File(userExtUcmdbDir + 'inter\\') inputFiles = inputFilesDirectory.listFiles() filePathDir = userExtUcmdbDir + 'results\\' directory = File(filePathDir) files = directory.listFiles() ## Clean up the existing result XML files if (files != None): for file in files: file.delete() ## Make sure we have XML files in the intermediate directory xmlFileInIntermediatesDirectory = 0 for inputFile in inputFiles: inputFileName = inputFile.getName() if inputFileName[len(inputFileName)-4:].lower() == '.xml' and inputFile.length() > 0: xmlFileInIntermediatesDirectory = 1 if not xmlFileInIntermediatesDirectory: logger.warn('Intermediate XML not found or invalid. Perhaps no data was received from Atrium or an error occurred in the atrium_query script.') return ## Generate the output XML files in results directory ip = CollectorsParameters.getValue(CollectorsParameters.KEY_SERVER_NAME) integrationAPI = IntegrationAPI(ip, "atrium_map.py") integrationAPI.processDir(userExtUcmdbDir) logger.info('End Phase 2 ... Apply Mapping transformation to Atrium CIs')
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 queryRegistryViaShell(shellUtils, key, filter=None): missingKeyError = 'The system was unable to find the specified registry key or value' filter = filter and " | find \"%s\"" % filter or '' query = "query %s /S%s" % (key, filter) cmd = ' '.join(['reg', query]) output, code = getCommandOutput(shellUtils, cmd) if output and re.search(missingKeyError, output): return if code != 0: output = None if not output: localFile = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder( ) + CollectorsParameters.FILE_SEPARATOR + 'reg_mam.exe' remoteFile = shellUtils.copyFileIfNeeded(localFile) if not remoteFile: logger.warn("Failed copying re_mam.exe") return cmd = ' '.join([remoteFile, query]) output, code = getCommandOutput(shellUtils, cmd) if output and re.search(missingKeyError, output) or code != 0: output = None return output
def DiscoveryMain(Framework): logger.info('Start Phase 1 ... Pull from ARIS') # Set global framework global theFramework theFramework = Framework ## Make sure we have an input data file from ARIS ARISfileName = Framework.getParameter('ARIS_XML_file') or None ARISfile = File(ARISfileName) if not (ARISfile and ARISfile.exists() and ARISfile.canRead()): excInfo = ('ARIS XML input file is not specified or is invalid!') Framework.reportError(excInfo) logger.error(excInfo) return None ## Check that the language parameter is set - default to US English requestedLocaleID = Framework.getParameter('ARISLocaleId') or '&LocaleId.USen;' if not requestedLocaleID: logger.warn('ARIS LocaleID parameter is not set...defaulting to US English') requestedLocaleID = '&LocaleId.USen;' # File and directory names userExtDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + '\\TQLExport\\ARIS\\' intermediatesDir = userExtDir + 'inter\\' mapingFilesListFileName = userExtDir + 'tqls.txt' mappingFileNameList = checkDiscoveryResources(mapingFilesListFileName, userExtDir, Framework, intermediatesDir) if not mappingFileNameList: return None ## Get attribute names from mapping file(s) ## This is a list of extended attributes to be retrieved from ARIS for mappingFileName in mappingFileNameList: (requestedSourceObjectTypeList, requestedSourceRelationshipTypeList) = getMapping(userExtDir + 'data\\' + mappingFileName + '.xml') if requestedSourceObjectTypeList and requestedSourceRelationshipTypeList: arisObjectMap = processARISXML(ARISfile, requestedSourceObjectTypeList, requestedSourceRelationshipTypeList, requestedLocaleID) intermediateXmlDoc = None if arisObjectMap: intermediateXmlDoc = buildIntermediateXML(arisObjectMap) intermediateXmlLocation = intermediatesDir + mappingFileName + '.xml' else: Framework.reportWarning('No CIs found in the ARIS XML file') if intermediateXmlDoc: try: xmlOutputter = XMLOutputter() xmlOutputter.output(intermediateXmlDoc, FileOutputStream(intermediateXmlLocation)) except: excInfo = logger.prepareJythonStackTrace('') Framework.reportError('Error writing intermediate file: <%s>' % intermediateXmlLocation) logger.warn('[' + SCRIPT_NAME + ':DiscoveryMain] Exception: <%s>' % excInfo) pass else: Framework.reportWarning('Error creating intermediate XML') else: logger.warn('[' + SCRIPT_NAME + ':DiscoveryMain] Unable to process mapping file: <%s>' % mappingFileName) Framework.reportError(' Unable to process mapping file: <%s>' % mappingFileName) logger.info('End Phase 1.... Pull from ARIS')
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 DiscoveryMain(Framework): logger.info('Start ', SCRIPT_NAME) logger.info('Start Phase 1 ... Query Remedy Atrium for data') # dryrunMode = Framework.getParameter('DryRunMode') # Get BMC Namespace bmcNamespace = Framework.getParameter('BMC_NameSpace') if bmcNamespace == None or bmcNamespace == "": bmcNamespace = "BMC.CORE" # Get chunk size - size of data in every query to Remedy/Atrium maxPerCall = Framework.getParameter('ChunkSize') if maxPerCall != None and maxPerCall.isnumeric(): maxPerCall = int(maxPerCall) else: maxPerCall = 500 # Get MAX CI size - size of data in every query to Remedy/Atrium maxCIs = Framework.getParameter('MaxCIs') if maxCIs != None and maxCIs.isnumeric(): maxCIs = int(maxCIs) else: maxCIs = 100000 ucmdbServerIp = CollectorsParameters.getValue(CollectorsParameters.KEY_SERVER_NAME) # File and directory names userExtDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + '\\TQLExport\\Atrium\\' intermediatesDir = userExtDir + 'inter\\' mapingFilesListFileName = userExtDir + 'tqls.txt' mappingFileNameList = checkDiscoveryResources(mapingFilesListFileName, userExtDir, Framework, intermediatesDir) if not mappingFileNameList: return None # GET ARS context - login information, etc. that is needed to make ARS connection context = getArsContext(Framework, ucmdbServerIp) if context != None: for mappingFileName in mappingFileNameList: (classList, linkList) = getMapping(userExtDir + 'data\\' + mappingFileName + '.xml', bmcNamespace, ucmdbServerIp) if (dryrunMode != None): dryrunMode = dryrunMode.lower() if dryrunMode == 'true': logger.info('[NOTE] UCMDB Integration is running in DryRun Mode, No query executed against ATRIUM.') debugPrint(4, '[' + SCRIPT_NAME + ':DiscoveryMain] Got classList: <%s>' % classList) debugPrint(4, '[' + SCRIPT_NAME + ':DiscoveryMain] Got linkList: <%s>' % linkList) return pullDataFromAtrium(context, classList, linkList, maxPerCall, maxCIs, intermediatesDir, mappingFileName) else: logger.error("Unable to create Remedy/Atrium login context. Check that username, password, server and port are defined correctly.") return None logger.info('End ', SCRIPT_NAME)
def DiscoveryMain(Framework): logger.info('Start Phase 3 ... Push transformed data to UCDMB') OSHVResult = ObjectStateHolderVector() DebugMode = Framework.getParameter('DebugMode') DateParsePattern = Framework.getParameter('DateParsePattern') userExtDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder( ) + '\\' if (DebugMode != None): DebugMode = DebugMode.lower() if DebugMode == "true": logger.info( '[NOTE] UCMDB Integration is running in DEBUG mode. No data will be pushed to the destination server.' ) return filePathDir = userExtDir + 'TQLExport\\Atrium\\results\\' directory = File(filePathDir) files = directory.listFiles() try: for file in files: if file != None or file != '': builder = SAXBuilder() doc = builder.build(file) # Process CIs # logger.info( "Start processing CIs to update in the destination server..." ) allObjects = doc.getRootElement().getChild('data').getChild( 'objects').getChildren('Object') (objVector, ciDict) = processObjects(allObjects, DateParsePattern) OSHVResult.addAll(objVector) # Process Relations # logger.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) #print OSHVResult.toXmlString() except: stacktrace = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) logger.info('Failure in processing data %s' % stacktrace) logger.info('Ending Push to UCMDB') logger.info('End Phase 3 ... Push transformed data to UCDMB') return OSHVResult
def enable_shell_proxy(self): localFile = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + \ CollectorsParameters.FILE_SEPARATOR + 'pdadmin_proxy.bat' remote_file = self.shell.copyFileIfNeeded(localFile) if not remote_file: raise ValueError("Failed to set up pdadmin call proxy.") self.binary_name = 'pdadmin_proxy.bat' m = re.search('\$(.+)pdadmin_proxy.bat', remote_file) if not m: raise ValueError("Failed to set up pdadmin call proxy.") self.prefix = '%SystemRoot%' + m.group(1)
def executeSoftwareQueryByPath(client, reg_path, prefix=''): ''' Shell, str, str = '' -> list(list(str), str) @command: <prefix>reg query <reg_path>\Uninstall /S @command: reg_mam query <reg_path>\Uninstall /S ''' ntcmdErrStr = 'Remote command returned 1(0x1)' non64BitOsErrStr = 'The system was unable to find the specified registry key or value' queryStr = ' query '+reg_path+'\Uninstall /S' #First trying the default reg.exe(might not work on Win2k or NT) if len(prefix)>0 and (not prefix.endswith('\\')): prefix += '\\' cmdRemoteAgent = prefix+'reg' + queryStr buffer = client.execCmd(cmdRemoteAgent,120000)#@@CMD_PERMISION ntcmd protocol execution logger.debug('Outputting ', cmdRemoteAgent, ': ...') reg_mamRc = client.getLastCmdReturnCode() if (reg_mamRc != 0) or (buffer.find(ntcmdErrStr) != -1): if (reg_mamRc == 1) and (buffer.find('ERROR: More data is available.') != -1): errMsg = 'reg command returned \'More data is available\' error, not all software might be reported' logger.warn(errMsg) pass else: logger.debug('reg query command ended unsuccessfully with return code:%d, error:%s' % (reg_mamRc,buffer)) logger.debug('Failed getting software info using default reg.exe trying the reg_mam.exe') cmdRemote = 'reg_mam' localFile = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + CollectorsParameters.FILE_SEPARATOR + 'reg_mam.exe' remoteFile = client.copyFileIfNeeded(localFile) if not remoteFile: logger.warn('Failed copying %s' % cmdRemote) return [[], ''] cmdRemoteAgent = remoteFile + queryStr buffer = client.execCmd(cmdRemoteAgent,120000)#@@CMD_PERMISION ntcmd protocol execution regRc = client.getLastCmdReturnCode() if (regRc != 0 ) or (buffer.find(ntcmdErrStr) != -1): if (regRc == 1) and (buffer.find('ERROR: More data is available.') != -1): errMsg = 'reg_mam command returned \'More data is available\' error, not all software might be reported' logger.warn(errMsg) pass else: if buffer.find(non64BitOsErrStr) == -1: logger.debug('Failed getting software info, reg.exe ended with %d, error:%s' % (regRc,buffer)) return [[], ''] logger.debug('got software buffer from remote registry - parsing...') keys = buffer.split(reg_path) return [keys, buffer]
def queryRegistry(self, regQuery): ntcmdErrStr = 'Remote command returned 1(0x1)' queryStr = " query "+regQuery buffer = self.shell.execCmd(self.prefix64bit + "reg.exe " + queryStr) if self.shell.getLastCmdReturnCode() != 0 or buffer.find(ntcmdErrStr) != -1: localFile = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + CollectorsParameters.FILE_SEPARATOR + 'reg_mam.exe' remoteFile = self.shell.copyFileIfNeeded(localFile) if not remoteFile: logger.warn('Failed copying reg_mam.exe to the destination') return buffer = self.shell.execCmd(remoteFile + queryStr) if not buffer or self.shell.getLastCmdReturnCode() != 0: logger.warn("Failed getting registry info.") return return buffer
def queryRegistry(self, regQuery): ntcmdErrStr = 'Remote command returned 1(0x1)' queryStr = " query " + regQuery buffer = self.shell.execCmd(self.prefix64bit + "reg.exe " + queryStr) if self.shell.getLastCmdReturnCode() != 0 or buffer.find( ntcmdErrStr) != -1: localFile = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder( ) + CollectorsParameters.FILE_SEPARATOR + 'reg_mam.exe' remoteFile = self.shell.copyFileIfNeeded(localFile) if not remoteFile: logger.warn('Failed copying reg_mam.exe to the destination') return buffer = self.shell.execCmd(remoteFile + queryStr) if not buffer or self.shell.getLastCmdReturnCode() != 0: logger.warn("Failed getting registry info.") return return buffer
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() logger.debug('Start nmap_osfingerprint.py') ip = Framework.getDestinationAttribute('ip_address') timeout = Framework.getParameter('nmap_host_timeout') if not str(timeout).isdigit(): msg = "Timeout parameter value must be a digit" logger.debug(msg) errormessages.resolveAndReport(msg, ClientsConsts.LOCAL_SHELL_PROTOCOL_NAME, Framework) return OSHVResult timeout = int(timeout) * 1000 scanKnownPortsOnly = Boolean.parseBoolean(Framework.getParameter('scan_known_ports_only')) portstoscan = Framework.getParameter('scan_these_ports_only') doServiceFingerprints =Boolean.parseBoolean(Framework.getParameter('Perform_Port_Fingerprints')) createApp = Boolean.parseBoolean(Framework.getParameter('Create_Application_CI')) discoverOsName =Boolean.parseBoolean(Framework.getParameter('discover_os_name')) nmapLocation = Framework.getParameter('nmap_location') #discover_UDP_Ports = int(Framework.getParameter('Discover_UDP_Ports')) discoverUdpPorts = 0 agent_root_dir=CollectorsParameters.BASE_PROBE_MGR_DIR agent_ext_dir = agent_root_dir + CollectorsParameters.getDiscoveryResourceFolder() + CollectorsParameters.FILE_SEPARATOR tmp_file_name = agent_ext_dir + string.replace(ip,'.','_') + time.strftime("%H%M%S",time.gmtime(time.time())) + 'nmap.xml' syncNmapPortConfigFile(agent_root_dir) logger.debug('temp file for storing nmap results: ', tmp_file_name) try: client = Framework.createClient(ClientsConsts.LOCAL_SHELL_PROTOCOL_NAME) try: performNmapDiscover(client, ip, tmp_file_name,timeout,agent_ext_dir,scanKnownPortsOnly,portstoscan,doServiceFingerprints, discoverUdpPorts, nmapLocation) if os.path.exists(tmp_file_name): logger.debug('start processing the nmap results') processNmapResult(tmp_file_name, OSHVResult, discoverOsName, doServiceFingerprints, createApp, Framework) else: raise ValueError, 'Error nmap result file is missing: %s' % tmp_file_name finally: client.close() File(tmp_file_name).delete() except Exception, e: msg = str(e.getMessage()) logger.debug(msg) errormessages.resolveAndReport(msg, ClientsConsts.LOCAL_SHELL_PROTOCOL_NAME, Framework)
def DiscoveryMain(Framework): logger.info('Start Phase 3 ... Push transformed data to UCDMB') OSHVResult = ObjectStateHolderVector() DebugMode = Framework.getParameter('DebugMode') DateParsePattern = Framework.getParameter('DateParsePattern') userExtDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + '\\' if (DebugMode != None): DebugMode = DebugMode.lower() if DebugMode == "true": logger.info ('[NOTE] UCMDB Integration is running in DEBUG mode. No data will be pushed to the destination server.') return filePathDir = userExtDir + 'TQLExport\\Atrium\\results\\' directory = File(filePathDir) files = directory.listFiles() try: for file in files: if file != None or file != '': builder = SAXBuilder () doc = builder.build(file) # Process CIs # logger.info("Start processing CIs to update in the destination server...") allObjects = doc.getRootElement().getChild('data').getChild('objects').getChildren('Object') (objVector, ciDict) = processObjects(allObjects, DateParsePattern) OSHVResult.addAll(objVector) # Process Relations # logger.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) #print OSHVResult.toXmlString() except: stacktrace = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) logger.info('Failure in processing data %s' % stacktrace) logger.info('Ending Push to UCMDB') logger.info('End Phase 3 ... Push transformed data to UCDMB') return OSHVResult
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 DiscoveryMain(Framework): logger.info("Start Phase 2 ....Apply Mapping file to ARIS CIs") userExtUcmdbDir = ( CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + "\\TQLExport\\ARIS\\" ) inputFilesDirectory = File(userExtUcmdbDir + "inter\\") inputFiles = inputFilesDirectory.listFiles() filePathDir = userExtUcmdbDir + "results\\" directory = File(filePathDir) files = directory.listFiles() ## Clean up the existing result XML files if files != None: for file in files: file.delete() ## Make sure we have XML files in the intermediate directory xmlFileInIntermediatesDirectory = 0 for inputFile in inputFiles: inputFileName = inputFile.getName() if inputFileName[len(inputFileName) - 4 :].lower() == ".xml" and inputFile.length() > 0: xmlFileInIntermediatesDirectory = 1 if not xmlFileInIntermediatesDirectory: logger.warn( "Intermediate XML not found or invalid. Perhaps no data was received from ARIS or an error occurred in the Pull_from_ARIS script." ) return ## Connect to the UCMDB Server, retrieve the results of the TQL ## and generate the output XML files in results directory ip = CollectorsParameters.getValue(CollectorsParameters.KEY_SERVER_NAME) exportTQL(ip, userExtUcmdbDir) logger.info("End Phase 2 ....Apply Mapping file to ARIS CIs")
def DiscoveryMain(Framework): logger.info('Start Phase 2 ....Apply Mapping file to Troux CIs') # Destination Data userExtUcmdbDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + '\\TQLExport\\Troux\\' outfilePathDir = userExtUcmdbDir + 'results\\' infilePathDir = userExtUcmdbDir + 'inter' directory = File(outfilePathDir) outfiles = directory.listFiles() directory = File(infilePathDir) infiles = directory.listFiles() ## Clean up the output directory before we run if (outfiles != None): for file in outfiles: file.delete() ## We can only process if Phase 1 created a Intermediate file to process ## Connect to the UCMDB Server, retrieve the results of the Mapping File ## and generate the output XML files in results directory try: ip = CollectorsParameters.getValue(CollectorsParameters.KEY_SERVER_NAME) exportTQL(ip, userExtUcmdbDir) except: pass #Framework.reportWarning(msg) #logger.warnException(msg) logger.info('End Phase 2 ....Apply Mapping file to Troux CIs')
def DiscoveryMain(Framework): logger.info('Start Phase 2 ... Apply Mapping transformation to Atrium CIs') userExtUcmdbDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder( ) + '\\TQLExport\\Atrium\\' inputFilesDirectory = File(userExtUcmdbDir + 'inter\\') inputFiles = inputFilesDirectory.listFiles() filePathDir = userExtUcmdbDir + 'results\\' directory = File(filePathDir) files = directory.listFiles() ## Clean up the existing result XML files if (files != None): for file in files: file.delete() ## Make sure we have XML files in the intermediate directory xmlFileInIntermediatesDirectory = 0 for inputFile in inputFiles: inputFileName = inputFile.getName() if inputFileName[len(inputFileName) - 4:].lower() == '.xml' and inputFile.length() > 0: xmlFileInIntermediatesDirectory = 1 if not xmlFileInIntermediatesDirectory: logger.warn( 'Intermediate XML not found or invalid. Perhaps no data was received from Atrium or an error occurred in the atrium_query script.' ) return ## Generate the output XML files in results directory ip = CollectorsParameters.getValue(CollectorsParameters.KEY_SERVER_NAME) integrationAPI = IntegrationAPI(ip, "atrium_map.py") integrationAPI.processDir(userExtUcmdbDir) logger.info('End Phase 2 ... Apply Mapping transformation to Atrium CIs')
def DiscoveryMain(Framework): fileSeparator = File.separator # Destination Data userExtUcmdbDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder( ) + fileSeparator + 'TQLExport' + fileSeparator + 'hpsim' + fileSeparator inputFilesDirectory = File(userExtUcmdbDir + 'inter' + fileSeparator) inputFiles = inputFilesDirectory.listFiles() filePathDir = userExtUcmdbDir + 'results' + fileSeparator directory = File(filePathDir) files = directory.listFiles() ## Clean up the existing result XML files if (files != None): for file in files: file.delete() ## Make sure we have XML files in the intermediate directory xmlFileInIntermediatesDirectory = 0 for inputFile in inputFiles: inputFileName = inputFile.getName() if inputFileName[len(inputFileName) - 4:].lower() == '.xml' and inputFile.length() > 0: xmlFileInIntermediatesDirectory = 1 if not xmlFileInIntermediatesDirectory: logger.warn( 'Intermediate XML not found or invalid. Perhaps no data was received from SIM or an error occurred in the SIM_Discovery script.' ) return ## Connect to the UCMDB Server, retrieve the results of the TQL ## and generate the output XML files in results directory ip = CollectorsParameters.getValue(CollectorsParameters.KEY_SERVER_NAME) exportTQL(ip, userExtUcmdbDir) info('End exportTQL_for_SIM_to_UCMDB.py')
def DiscoveryMain(Framework): logger.info('Start Phase 2 ....Apply Mapping file to Troux CIs') # Destination Data userExtUcmdbDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder( ) + '\\TQLExport\\Troux\\' outfilePathDir = userExtUcmdbDir + 'results\\' infilePathDir = userExtUcmdbDir + 'inter' directory = File(outfilePathDir) outfiles = directory.listFiles() directory = File(infilePathDir) infiles = directory.listFiles() ## Clean up the output directory before we run if (outfiles != None): for file in outfiles: file.delete() ## We can only process if Phase 1 created a Intermediate file to process ## Connect to the UCMDB Server, retrieve the results of the Mapping File ## and generate the output XML files in results directory try: ip = CollectorsParameters.getValue( CollectorsParameters.KEY_SERVER_NAME) exportTQL(ip, userExtUcmdbDir) except: pass #Framework.reportWarning(msg) #logger.warnException(msg) logger.info('End Phase 2 ....Apply Mapping file to Troux CIs')
CONNECTION_FAILED = "[ERROR] Connection to Remedy/Atrium failed. See Probe Wrapper log for reason." mamIdToAttributesMap = HashMap() mamIdToKeyAttributes = HashMap() mamIdToEntryId = HashMap() mamIdToName = HashMap() classToArsAttributes = HashMap( ) # a map of namespace:calssname->hashMap. the value hashmap contains mapping of: fieldName->fieldKey TIMESTAMP = Date().getTime() smartUpdateIgnoreFieldsList = None # discoveryResources\CaCmdbPushAdapter adapterResBaseDir = "%s%s%s%s" % ( CollectorsParameters.BASE_PROBE_MGR_DIR, CollectorsParameters.getDiscoveryResourceFolder(), FILE_SEPARATOR, ADAPTER_NAME) adapterResBaseDirFile = File(adapterResBaseDir) # discoveryResources\CaCmdbPushAdapter\work adapterResWorkDir = "%s%s%s" % (adapterResBaseDir, FILE_SEPARATOR, WORK_DIR) adapterResWorkDirFile = File(adapterResWorkDir) # discoveryConfigFiles\CaCmdbPushAdapter adapterConfigBaseDir = "%s%s%s%s" % ( CollectorsParameters.BASE_PROBE_MGR_DIR, CollectorsParameters.getDiscoveryConfigFolder(), FILE_SEPARATOR, ADAPTER_NAME) # logs slogger = Logger.getLogger("successLogger")
def discoverProcesses(client, OSHVResult, hostID, Framework, pid2Process = None): cmdProcessInfo = 'processlist' ntcmdErrStr = 'Remote command returned 1(0x1)' localFile = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + CollectorsParameters.FILE_SEPARATOR + 'processlist.exe' remoteFile = client.copyFileIfNeeded(localFile) if not remoteFile: logger.warn('Failed copying %s' % cmdProcessInfo) return 0 buffer = client.execCmd(remoteFile)#V@@CMD_PERMISION ntcmd protocol execution logger.debug('Output of ', remoteFile, ': ', buffer) if buffer.find(ntcmdErrStr) != -1: logger.warn('Failed getting process info') else: logger.debug('Got process info - parsing...') processes = buffer.split('\n') processList = [] pdu = None try: pdu = processdbutils.ProcessDbUtils(Framework) hostOSH = None count = 0 for process in processes: process = process.strip() name = '' nameLower = '' pid = '-1' try: # Get process name matchName = re.search('\d*\s(.+)', process) if matchName: name = matchName.group(1) name = name.strip() nameLower = name.lower() if name == '[System Process]': continue # Get free space matchPid = re.search('(\d+)\s.*', process) if matchPid: pid = matchPid.group(1) if(pid != '-1' and pid.isnumeric()): pdu.addProcess(hostID, name, pid) if ((pid in processList) != 0): logger.debug('process: ',name,' already reported..') continue count = count + 1 processList.append(pid) if OSHVResult is not None: if hostOSH == None: hostOSH = modeling.createOshByCmdbIdString('host', hostID) processOsh = modeling.createProcessOSH(name, hostOSH, None, pid) OSHVResult.add(processOsh) else: logger.debug('process: ',name,' is system process or has non numeric pid') except: logger.errorException('Error in discoverProcesses()') pdu.flushHostProcesses(hostID) if pid2Process is not None: pid2Process.putAll(pdu.getProcessCmdMap()) finally: if pdu != None: pdu.close() logger.debug("Discovered ", str(count), " processes") return 1
def buildFullPathToScenario(self, scenarioFileName): return CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + CollectorsParameters.FILE_SEPARATOR + scenarioFileName
def getRegValues(localClient, wmiClient, keyPath, keyFilter): shell = None system_root = '' try: debugPrint( 3, '[' + SCRIPT_NAME + ':getRegValues] Got key <%s> and filter <%s>' % (keyPath, keyFilter)) returnTable = {} ## Check if we have a WMI Client if wmiClient != None and wmiClient.getClientType() == 'wmi': wmiTable = wmiClient.getRegistryKeyValues(keyPath, 1, keyFilter) regKeys = wmiTable.get(0) regValues = wmiTable.get(1) for i in range(regKeys.size()): regKey = regKeys.get(i) keyEnd = regKey.rfind('\\' + keyFilter) regKey = regKey[0:keyEnd] returnTable.update({regKey: regValues.get(i)}) debugPrint( 3, '[' + SCRIPT_NAME + ':getRegValues] Got value <%s> for key <%s>' % (regValues.get(i), regKey)) ## If not, it must be NTCMD or SSH elif localClient != None and localClient.getClientType( ) != 'snmp' and localClient.getClientType() != 'wmi': errorCode = 'Remote command returned 1(0x1)' reg_mam = 'reg_mam.exe' shell = shellutils.ShellUtils(localClient) #in case this is a 64 bit os we need to use 64 it reg exe first if shell.is64BitMachine(): try: system_root = shell.createSystem32Link() except: logger.debug( 'Failed to create system32 link. Will use regular reg first.' ) remoteFile = system_root + 'reg.exe' # remoteFile = 'reg' ## Run the registry query theQuery = remoteFile + ' query "HKEY_LOCAL_MACHINE\\' + keyPath + '" /s | find "' + keyFilter + '"' returnBuffer = shell.execCmd(theQuery) returnCode = shell.getLastCmdReturnCode() ## If query didn't run, try reg.exe on remote server if returnCode != 0 or returnBuffer.find(errorCode) != -1: ## Copy reg_mam.exe over to remote box debugPrint( 3, '[' + SCRIPT_NAME + ':getRegValues] Error executing <%s>. Will try reg_mam.exe on server: <%s>' % (theQuery, returnBuffer)) localFile = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder( ) + '\\%s' % reg_mam remoteFile = shell.copyFileIfNeeded(localFile) if remoteFile: theQuery = remoteFile + ' query "HKEY_LOCAL_MACHINE\\' + keyPath + '" /s | find "' + keyFilter + '"' returnBuffer = shell.execCmd(theQuery) returnCode = shell.getLastCmdReturnCode() if returnCode != 0 or returnBuffer.find(errorCode) != -1: debugPrint( 3, '[' + SCRIPT_NAME + ':getRegValues] Error executing <%s> too: <%s>' % (theQuery, returnBuffer)) else: debugPrint( 3, '[' + SCRIPT_NAME + ':getRegValues] Error copying <%s> to remote machine.' % reg_mam) ## If query didn't run, try reg.exe with "reg:64" on remote server which may be 64 bit but we've failed to get a valid 64 bit binary if returnCode != 0 or returnBuffer.find(errorCode) != -1: theQuery = 'reg query "HKEY_LOCAL_MACHINE\\' + keyPath + '" /s /reg:64 | find "' + keyFilter + '"' returnBuffer = shell.execCmd(theQuery) returnCode = shell.getLastCmdReturnCode() if returnCode != 0 or returnBuffer.find(errorCode) != -1: debugPrint( 3, '[' + SCRIPT_NAME + ':getRegValues] Error executing <%s> too: <%s>' % (theQuery, returnBuffer)) return None ## If we're here, we have query output debugPrint( 3, '[' + SCRIPT_NAME + ':getRegValues] Got output: <%s> for registry query <%s>' % (returnBuffer, theQuery)) regKeys = returnBuffer.split('\n') for regKey in regKeys: regvalMatch = re.search(keyFilter + '\s+REG_.*?\s+(.+)$', regKey) if (regvalMatch): returnVal = regvalMatch.group(1) returnVal = string.replace(returnVal, r'\0', '\n') returnVal = returnVal.strip() debugPrint( 3, '[' + SCRIPT_NAME + ':getRegValues] Got value <%s> for key <%s> with filter <%s>' % (returnVal, keyPath, keyFilter)) returnTable.update({keyPath: returnVal}) else: ## This client doesn't support registry lookup debugPrint( 2, '[' + SCRIPT_NAME + ':getRegValues] <%s> client doesn\'t support registry lookup' % localClient.getClientType()) return None return returnTable except: excInfo = logger.prepareJythonStackTrace('') debugPrint('[' + SCRIPT_NAME + ':getRegValues] Exception: <%s>' % excInfo) finally: shell and shell.is64BitMachine( ) and system_root and shell.removeSystem32Link(system_root)
modificationTime = None escapedFileName = fileName.replace('\\', '\\\\') command = "Select LastModified from CIM_Datafile Where name = '%s'" % escapedFileName resultTable = client.executeQuery(command).asTable() if resultTable and resultTable[0]: try: modificationTime = getDateFromUtcString(resultTable[0][0]) except: logger.warn('Failed getting last modification time for file: %s' % fileName) else: logger.warn('Failed getting last modification time for file: %s' % fileName) return modificationTime __VBS_FILE_NAME = 'GetFileModificationDate.vbs' __VBS_LOCAL_FILE_PATH = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + CollectorsParameters.FILE_SEPARATOR + __VBS_FILE_NAME __ERROR_STRING = 'No file name or incorrect path specified.' def __getWindowsVbsFileLastModificationTime(shell, fileName): remoteFile = shell.copyFileIfNeeded(__VBS_LOCAL_FILE_PATH) if not remoteFile: raise ValueError("Failed copying file %s" % __VBS_LOCAL_FILE_PATH) command = 'Cscript.exe /nologo "%s" "%s"' % (remoteFile, fileName) buffer = shell.execCmd(command) #TODO: vbs should be modified to exit with non-zero exit code when failing if buffer and shell.getLastCmdReturnCode() == 0 and buffer.find(__ERROR_STRING) == -1: matcher = re.match("\s*(\d{4}\-\d{2}-\d{2} \d{2}\:\d{2}\:\d{2}).*", buffer) if matcher: dateString = matcher.group(1)
def getRegValues(localClient, wmiClient, keyPath, keyFilter): shell = None system_root = "" try: debugPrint(3, "[" + SCRIPT_NAME + ":getRegValues] Got key <%s> and filter <%s>" % (keyPath, keyFilter)) returnTable = {} ## Check if we have a WMI Client if wmiClient != None and wmiClient.getClientType() == "wmi": wmiTable = wmiClient.getRegistryKeyValues(keyPath, 1, keyFilter) regKeys = wmiTable.get(0) regValues = wmiTable.get(1) for i in range(regKeys.size()): regKey = regKeys.get(i) keyEnd = regKey.rfind("\\" + keyFilter) regKey = regKey[0:keyEnd] returnTable.update({regKey: regValues.get(i)}) debugPrint( 3, "[" + SCRIPT_NAME + ":getRegValues] Got value <%s> for key <%s>" % (regValues.get(i), regKey) ) ## If not, it must be NTCMD or SSH elif localClient != None and localClient.getClientType() != "snmp" and localClient.getClientType() != "wmi": errorCode = "Remote command returned 1(0x1)" reg_mam = "reg_mam.exe" shell = shellutils.ShellUtils(localClient) # in case this is a 64 bit os we need to use 64 it reg exe first if shell.is64BitMachine(): try: system_root = shell.createSystem32Link() except: logger.debug("Failed to create system32 link. Will use regular reg first.") remoteFile = system_root + "reg.exe" # remoteFile = 'reg' ## Run the registry query theQuery = remoteFile + ' query "HKEY_LOCAL_MACHINE\\' + keyPath + '" /s | find "' + keyFilter + '"' returnBuffer = shell.execCmd(theQuery) returnCode = shell.getLastCmdReturnCode() ## If query didn't run, try reg.exe on remote server if returnCode != 0 or returnBuffer.find(errorCode) != -1: ## Copy reg_mam.exe over to remote box debugPrint( 3, "[" + SCRIPT_NAME + ":getRegValues] Error executing <%s>. Will try reg_mam.exe on server: <%s>" % (theQuery, returnBuffer), ) localFile = ( CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + "\\%s" % reg_mam ) remoteFile = shell.copyFileIfNeeded(localFile) if remoteFile: theQuery = remoteFile + ' query "HKEY_LOCAL_MACHINE\\' + keyPath + '" /s | find "' + keyFilter + '"' returnBuffer = shell.execCmd(theQuery) returnCode = shell.getLastCmdReturnCode() if returnCode != 0 or returnBuffer.find(errorCode) != -1: debugPrint( 3, "[" + SCRIPT_NAME + ":getRegValues] Error executing <%s> too: <%s>" % (theQuery, returnBuffer), ) else: debugPrint(3, "[" + SCRIPT_NAME + ":getRegValues] Error copying <%s> to remote machine." % reg_mam) ## If query didn't run, try reg.exe with "reg:64" on remote server which may be 64 bit but we've failed to get a valid 64 bit binary if returnCode != 0 or returnBuffer.find(errorCode) != -1: theQuery = 'reg query "HKEY_LOCAL_MACHINE\\' + keyPath + '" /s /reg:64 | find "' + keyFilter + '"' returnBuffer = shell.execCmd(theQuery) returnCode = shell.getLastCmdReturnCode() if returnCode != 0 or returnBuffer.find(errorCode) != -1: debugPrint( 3, "[" + SCRIPT_NAME + ":getRegValues] Error executing <%s> too: <%s>" % (theQuery, returnBuffer), ) return None ## If we're here, we have query output debugPrint( 3, "[" + SCRIPT_NAME + ":getRegValues] Got output: <%s> for registry query <%s>" % (returnBuffer, theQuery), ) regKeys = returnBuffer.split("\n") for regKey in regKeys: regvalMatch = re.search(keyFilter + "\s+REG_.*?\s+(.+)$", regKey) if regvalMatch: returnVal = regvalMatch.group(1) returnVal = string.replace(returnVal, r"\0", "\n") returnVal = returnVal.strip() debugPrint( 3, "[" + SCRIPT_NAME + ":getRegValues] Got value <%s> for key <%s> with filter <%s>" % (returnVal, keyPath, keyFilter), ) returnTable.update({keyPath: returnVal}) else: ## This client doesn't support registry lookup debugPrint( 2, "[" + SCRIPT_NAME + ":getRegValues] <%s> client doesn't support registry lookup" % localClient.getClientType(), ) return None return returnTable except: excInfo = logger.prepareJythonStackTrace("") debugPrint("[" + SCRIPT_NAME + ":getRegValues] Exception: <%s>" % excInfo) finally: shell and shell.is64BitMachine() and system_root and shell.removeSystem32Link(system_root)
def discoverProcesses(client, OSHVResult, hostID, Framework, pid2Process=None): cmdProcessInfo = 'processlist' ntcmdErrStr = 'Remote command returned 1(0x1)' localFile = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder( ) + CollectorsParameters.FILE_SEPARATOR + 'processlist.exe' remoteFile = client.copyFileIfNeeded(localFile) if not remoteFile: logger.warn('Failed copying %s' % cmdProcessInfo) return 0 buffer = client.execCmd( remoteFile) #V@@CMD_PERMISION ntcmd protocol execution logger.debug('Output of ', remoteFile, ': ', buffer) if buffer.find(ntcmdErrStr) != -1: logger.warn('Failed getting process info') else: logger.debug('Got process info - parsing...') processes = buffer.split('\n') processList = [] pdu = None try: pdu = processdbutils.ProcessDbUtils(Framework) hostOSH = None count = 0 for process in processes: process = process.strip() name = '' nameLower = '' pid = '-1' try: # Get process name matchName = re.search('\d*\s(.+)', process) if matchName: name = matchName.group(1) name = name.strip() nameLower = name.lower() if name == '[System Process]': continue # Get free space matchPid = re.search('(\d+)\s.*', process) if matchPid: pid = matchPid.group(1) if (pid != '-1' and pid.isnumeric()): pdu.addProcess(hostID, name, pid) if ((pid in processList) != 0): logger.debug('process: ', name, ' already reported..') continue count = count + 1 processList.append(pid) if OSHVResult is not None: if hostOSH == None: hostOSH = modeling.createOshByCmdbIdString( 'host', hostID) processOsh = modeling.createProcessOSH( name, hostOSH, None, pid) OSHVResult.add(processOsh) else: logger.debug( 'process: ', name, ' is system process or has non numeric pid') except: logger.errorException('Error in discoverProcesses()') pdu.flushHostProcesses(hostID) if pid2Process is not None: pid2Process.putAll(pdu.getProcessCmdMap()) finally: if pdu != None: pdu.close() logger.debug("Discovered ", str(count), " processes") return 1
def DiscoveryMain(Framework): logger.info('Start ', SCRIPT_NAME) logger.info('Start Phase 1 ... Query Remedy Atrium for data') # dryrunMode = Framework.getParameter('DryRunMode') # Get BMC Namespace bmcNamespace = Framework.getParameter('BMC_NameSpace') if bmcNamespace == None or bmcNamespace == "": bmcNamespace = "BMC.CORE" # Get chunk size - size of data in every query to Remedy/Atrium maxPerCall = Framework.getParameter('ChunkSize') if maxPerCall != None and maxPerCall.isnumeric(): maxPerCall = int(maxPerCall) else: maxPerCall = 500 # Get MAX CI size - size of data in every query to Remedy/Atrium maxCIs = Framework.getParameter('MaxCIs') if maxCIs != None and maxCIs.isnumeric(): maxCIs = int(maxCIs) else: maxCIs = 100000 ucmdbServerIp = CollectorsParameters.getValue( CollectorsParameters.KEY_SERVER_NAME) # File and directory names userExtDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder( ) + '\\TQLExport\\Atrium\\' intermediatesDir = userExtDir + 'inter\\' mapingFilesListFileName = userExtDir + 'tqls.txt' mappingFileNameList = checkDiscoveryResources(mapingFilesListFileName, userExtDir, Framework, intermediatesDir) if not mappingFileNameList: return None # GET ARS context - login information, etc. that is needed to make ARS connection context = getArsContext(Framework, ucmdbServerIp) if context != None: for mappingFileName in mappingFileNameList: (classList, linkList) = getMapping( userExtDir + 'data\\' + mappingFileName + '.xml', bmcNamespace, ucmdbServerIp) if (dryrunMode != None): dryrunMode = dryrunMode.lower() if dryrunMode == 'true': logger.info( '[NOTE] UCMDB Integration is running in DryRun Mode, No query executed against ATRIUM.' ) debugPrint( 4, '[' + SCRIPT_NAME + ':DiscoveryMain] Got classList: <%s>' % classList) debugPrint( 4, '[' + SCRIPT_NAME + ':DiscoveryMain] Got linkList: <%s>' % linkList) return pullDataFromAtrium(context, classList, linkList, maxPerCall, maxCIs, intermediatesDir, mappingFileName) else: logger.error( "Unable to create Remedy/Atrium login context. Check that username, password, server and port are defined correctly." ) return None logger.info('End ', SCRIPT_NAME)
''' Initialize variables ''' ADAPTER_NAME = "CaCmdbPushAdapter" FILE_SEPARATOR = "\\" WORK_DIR = "work" GR_ERROR_LOG = "connection_error.log" PUSH_PROPERTIES_FILE = "push.properties" TEST_FILE_NAME = "test.xml" CONNECTION_FAILED = "[ERROR] Connection to CA CMDB failed. See Probe Wrapper log for reason." TIMESTAMP = Date().getTime() # discoveryResources\CaCmdbPushAdapter adapterResBaseDir = "%s%s%s%s" % (CollectorsParameters.BASE_PROBE_MGR_DIR, CollectorsParameters.getDiscoveryResourceFolder(), FILE_SEPARATOR, ADAPTER_NAME) adapterResBaseDirFile = File(adapterResBaseDir) # discoveryResources\CaCmdbPushAdapter\work adapterResWorkDir = "%s%s%s" % (adapterResBaseDir, FILE_SEPARATOR, WORK_DIR) adapterResWorkDirFile = File(adapterResWorkDir) # discoveryConfigFiles\CaCmdbPushAdapter adapterConfigBaseDir = "%s%s%s%s" % (CollectorsParameters.BASE_PROBE_MGR_DIR, CollectorsParameters.getDiscoveryConfigFolder(), FILE_SEPARATOR, ADAPTER_NAME) ''' Connection Class ''' class Connection: def __init__(self, targetHost, targetPort, username, password, libDirName, timeout=None): self.host = targetHost
def copyRegFromProbe(self): localFile = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder( ) + CollectorsParameters.FILE_SEPARATOR + 'reg_mam.exe' self.command = self.client.copyFileIfNeeded(localFile) if not self.command: raise ValueError, "Failed copying reg_mam.exe to remote machine"
def DiscoveryMain(Framework): logger.info("Start Phase 1 ... Pull from Troux") ######################################################## # Set up the Directory that will be used for the intermediate XML output # and the input # Get the input file that we will use from Troux ######################################################## userExtTrouxDir = ( CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + "\\TQLExport\\Troux\\" ) ucmdbServerIp = CollectorsParameters.getValue(CollectorsParameters.KEY_SERVER_NAME) Trouxfile = None f = None Trouxfile = Framework.getParameter("Troux_TUX_file") if Trouxfile == None: logger.info("Troux TUX input file is not specified") ######################################################## # Open the output directory and clean up old files ######################################################## interPathDir = userExtTrouxDir + "inter\\" directory = File(interPathDir) files = directory.listFiles() logger.info(Trouxfile) # Delete the files in the output directory so we have a clean area if files != None: for file in files: file.delete() # Make sure we have a valid input file if os.path.exists(Trouxfile): foundfile = "false" TQLSFile = "%sTQLS.txt" % (userExtTrouxDir) tqlsFile = "%stqls.txt" % (userExtTrouxDir) if os.path.exists(TQLSFile): foundfile = "true" f = open(TQLSFile) logger.info("Reading file ", TQLSFile) elif os.path.exists(tqlsFile): foundfile = "true" f = open(tqlsFile) logger.info("Reading file ", TQLSFile) else: logger.info("Error \\TQLExport\\Troux\\tqls.txt file missing") if foundfile == "true": for nextName in f.readlines(): if notNull(nextName) and nextName[0:1] != "#": Outfile = nextName.strip() processTrouxXML(userExtTrouxDir, interPathDir, Trouxfile, Outfile) f.close() else: msg = "Input TUX File does not Exist" Framework.reportWarning(msg) logger.info("End Phase 1.... Pull from Troux")
def executeSoftwareQueryByPath(client, reg_path, prefix=''): ''' Shell, str, str = '' -> list(list(str), str) @command: <prefix>reg query <reg_path>\Uninstall /S @command: reg_mam query <reg_path>\Uninstall /S ''' ntcmdErrStr = 'Remote command returned 1(0x1)' non64BitOsErrStr = 'The system was unable to find the specified registry key or value' queryStr = ' query ' + reg_path + '\Uninstall /S' #First trying the default reg.exe(might not work on Win2k or NT) if len(prefix) > 0 and (not prefix.endswith('\\')): prefix += '\\' cmdRemoteAgent = prefix + 'reg' + queryStr buffer = client.execCmd(cmdRemoteAgent, 120000) #@@CMD_PERMISION ntcmd protocol execution logger.debug('Outputting ', cmdRemoteAgent, ': ...') reg_mamRc = client.getLastCmdReturnCode() if (reg_mamRc != 0) or (buffer.find(ntcmdErrStr) != -1): if (reg_mamRc == 1) and (buffer.find('ERROR: More data is available.') != -1): errMsg = 'reg command returned \'More data is available\' error, not all software might be reported' logger.warn(errMsg) pass else: logger.debug( 'reg query command ended unsuccessfully with return code:%d, error:%s' % (reg_mamRc, buffer)) logger.debug( 'Failed getting software info using default reg.exe trying the reg_mam.exe' ) cmdRemote = 'reg_mam' localFile = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder( ) + CollectorsParameters.FILE_SEPARATOR + 'reg_mam.exe' remoteFile = client.copyFileIfNeeded(localFile) if not remoteFile: logger.warn('Failed copying %s' % cmdRemote) return [[], ''] cmdRemoteAgent = remoteFile + queryStr buffer = client.execCmd( cmdRemoteAgent, 120000) #@@CMD_PERMISION ntcmd protocol execution regRc = client.getLastCmdReturnCode() if (regRc != 0) or (buffer.find(ntcmdErrStr) != -1): if (regRc == 1) and ( buffer.find('ERROR: More data is available.') != -1): errMsg = 'reg_mam command returned \'More data is available\' error, not all software might be reported' logger.warn(errMsg) pass else: if buffer.find(non64BitOsErrStr) == -1: logger.debug( 'Failed getting software info, reg.exe ended with %d, error:%s' % (regRc, buffer)) return [[], ''] logger.debug('got software buffer from remote registry - parsing...') keys = buffer.split(reg_path) return [keys, buffer]
def discoverDisk(client, myVec, hostOSH): cmdDiskInfo = "diskinfo.exe" localFile = ( CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + CollectorsParameters.FILE_SEPARATOR + "diskinfo.exe" ) remoteFile = client.copyFileIfNeeded(localFile) if not remoteFile: logger.debug("Failed copying %s" % cmdDiskInfo) return ntcmdErrStr = "Remote command returned 1(0x1)" buffer = client.execCmd(remoteFile, DISKINFO_TIMEOUT) # V@@CMD_PERMISION ntcmd protocol execution logger.debug("Output of ", remoteFile, ": ", buffer) if buffer.find(ntcmdErrStr) != -1: logger.warn("Failed getting disk info") else: logger.debug("Got disk info - parsing...") disks = buffer.split("\n") for disk in disks: disk = disk.strip() name = "" size = 0 usedSize = None diskType = "" try: # Get disk size matchSize = re.search("Size: (\d+) MB", disk) if matchSize: size = int(matchSize.group(1)) matchFreeSize = re.search("Free: (\d+) MB", disk) if matchFreeSize: freeSize = int(matchFreeSize.group(1)) usedSize = size - freeSize # Get disk type matchType = re.search("Type: (.*)", disk) if matchType: diskType = matchType.group(1) diskType = diskType.strip() if diskType == "FLOPPY" and size > 5: diskType = "FLASH" # Get disk name matchName = re.search(r"Name: (\w):\\,", disk) if matchName: name = matchName.group(1) name = name.strip() # Create DISK OSH if name != "": if diskType in STORAGE_NAME_TO_STORAGE_TYPE: storageType = STORAGE_NAME_TO_STORAGE_TYPE[diskType] else: storageType = modeling.OTHER_STORAGE_TYPE diskOsh = modeling.createDiskOSH(hostOSH, name, storageType, size, name=name, usedSize=usedSize) myVec.add(diskOsh) except: logger.errorException("Error in discoverDisk()")
def DiscoveryMain(Framework): logger.info('Start Phase 1 ... Pull from Troux') ######################################################## # Set up the Directory that will be used for the intermediate XML output # and the input # Get the input file that we will use from Troux ######################################################## userExtTrouxDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + '\\TQLExport\\Troux\\' ucmdbServerIp = CollectorsParameters.getValue(CollectorsParameters.KEY_SERVER_NAME) Trouxfile = None f = None Trouxfile = Framework.getParameter('Troux_TUX_file') if (Trouxfile == None): logger.info('Troux TUX input file is not specified') ######################################################## # Open the output directory and clean up old files ######################################################## interPathDir = userExtTrouxDir + 'inter\\' directory = File(interPathDir) files = directory.listFiles() logger.info (Trouxfile) # Delete the files in the output directory so we have a clean area if (files != None): for file in files: file.delete() # Make sure we have a valid input file if os.path.exists(Trouxfile): foundfile = 'false' TQLSFile = '%sTQLS.txt' % (userExtTrouxDir) tqlsFile = '%stqls.txt' % (userExtTrouxDir) if os.path.exists(TQLSFile): foundfile = 'true' f = open(TQLSFile) logger.info('Reading file ', TQLSFile) elif os.path.exists(tqlsFile): foundfile = 'true' f = open(tqlsFile) logger.info('Reading file ', TQLSFile) else: logger.info ('Error \\TQLExport\\Troux\\tqls.txt file missing') if foundfile =='true': for nextName in f.readlines(): if notNull(nextName) and nextName[0:1] != '#': Outfile = nextName.strip() processTrouxXML ( userExtTrouxDir, interPathDir, Trouxfile, Outfile) f.close() else: msg = "Input TUX File does not Exist" Framework.reportWarning(msg) logger.info('End Phase 1.... Pull from Troux')
def discoverDisk(client, myVec, hostOSH): cmdDiskInfo = 'diskinfo.exe' localFile = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder( ) + CollectorsParameters.FILE_SEPARATOR + 'diskinfo.exe' remoteFile = client.copyFileIfNeeded(localFile) if not remoteFile: logger.debug('Failed copying %s' % cmdDiskInfo) return ntcmdErrStr = 'Remote command returned 1(0x1)' buffer = client.execCmd( remoteFile, DISKINFO_TIMEOUT) # V@@CMD_PERMISION ntcmd protocol execution logger.debug('Output of ', remoteFile, ': ', buffer) if buffer.find(ntcmdErrStr) != -1: logger.warn('Failed getting disk info') else: logger.debug('Got disk info - parsing...') disks = buffer.split('\n') for disk in disks: disk = disk.strip() name = '' size = 0 usedSize = None diskType = '' try: # Get disk size matchSize = re.search('Size: (\d+) MB', disk) if matchSize: size = int(matchSize.group(1)) matchFreeSize = re.search('Free: (\d+) MB', disk) if matchFreeSize: freeSize = int(matchFreeSize.group(1)) usedSize = size - freeSize # Get disk type matchType = re.search('Type: (.*)', disk) if matchType: diskType = matchType.group(1) diskType = diskType.strip() if (diskType == 'FLOPPY' and size > 5): diskType = 'FLASH' # Get disk name matchName = re.search(r'Name: (\w):\\,', disk) if matchName: name = matchName.group(1) name = name.strip() # Create DISK OSH if name != '': if diskType in STORAGE_NAME_TO_STORAGE_TYPE: storageType = STORAGE_NAME_TO_STORAGE_TYPE[diskType] else: storageType = modeling.OTHER_STORAGE_TYPE diskOsh = modeling.createDiskOSH(hostOSH, name, storageType, size, name=name, usedSize=usedSize) myVec.add(diskOsh) except: logger.errorException('Error in discoverDisk()')
def getRegValues_Orig(localClient, wmiClient, keyPath, keyFilter): try: debugPrint(3, '[' + SCRIPT_NAME + ':getRegValues] Got key <%s> and filter <%s>' % (keyPath, keyFilter)) returnTable = {} ## Check if we have a WMI Client if wmiClient != None and wmiClient.getClientType() == 'wmi': wmiTable = wmiClient.getRegistryKeyValues(keyPath, 1, keyFilter) regKeys = wmiTable.get(0) regValues = wmiTable.get(1) for i in range(regKeys.size()): regKey = regKeys.get(i) keyEnd = regKey.rfind('\\' + keyFilter) regKey = regKey[0:keyEnd] returnTable.update({regKey:regValues.get(i)}) debugPrint(3, '[' + SCRIPT_NAME + ':getRegValues] Got value <%s> for key <%s>' % (regValues.get(i), regKey)) ## If not, it must be NTCMD or SSH elif localClient != None and localClient.getClientType() != 'snmp' and localClient.getClientType() != 'wmi': errorCode = 'Remote command returned 1(0x1)' remoteCommand = 'reg_mam' shell = shellutils.ShellUtils(localClient) ## Copy reg_mam.exe over to remote box localFile = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + '\\reg_mam.exe' remoteFile = shell.copyFileIfNeeded(localFile) if not remoteFile: debugPrint(3, '[' + SCRIPT_NAME + ':getRegValues] Error copying <%s> to remote machine' % remoteCommand) remoteFile = 'reg' ## Run the registry query theQuery = remoteFile + ' query "HKEY_LOCAL_MACHINE\\' + keyPath + '" /s | find "' + keyFilter + '"' returnBuffer = shell.execCmd(theQuery) returnCode = shell.getLastCmdReturnCode() ## If query didn't run, try reg.exe on remote server if returnCode != 0 or returnBuffer.find(errorCode) != -1: if remoteFile == 'reg': # or returnBuffer.find('unable to find the specified registry key'): debugPrint(3, '[' + SCRIPT_NAME + ':getRegValues] Error executing <%s>: <%s>' % (theQuery, returnBuffer)) return None debugPrint(3, '[' + SCRIPT_NAME + ':getRegValues] Error executing <%s>. Will try reg.exe on server: <%s>' % (theQuery, returnBuffer)) theQuery = 'reg query "HKEY_LOCAL_MACHINE\\' + keyPath + '" /s | find "' + keyFilter + '"' returnBuffer = shell.execCmd(theQuery) returnCode = shell.getLastCmdReturnCode() if returnCode != 0 or returnBuffer.find(errorCode) != -1: debugPrint(3, '[' + SCRIPT_NAME + ':getRegValues] Error executing <%s> too: <%s>' % (theQuery, returnBuffer)) return None ## If we're here, we have query output debugPrint(3, '[' + SCRIPT_NAME + ':getRegValues] Got output: <%s> for registry query <%s>' % (returnBuffer, theQuery)) regKeys = returnBuffer.split('\n') for regKey in regKeys: regvalMatch = re.search(keyFilter + '\s+REG_.*?\s+(.+)$', regKey) if (regvalMatch): returnVal = regvalMatch.group(1) returnVal = string.replace(returnVal, r'\0', '\n') returnVal = returnVal.strip() debugPrint(3, '[' + SCRIPT_NAME + ':getRegValues] Got value <%s> for key <%s> with filter <%s>' % (returnVal, keyPath, keyFilter)) returnTable.update({keyPath:returnVal}) else: ## This client doesn't support registry lookup debugPrint(2, '[' + SCRIPT_NAME + ':getRegValues] <%s> client doesn\'t support registry lookup' % localClient.getClientType()) return None return returnTable except: excInfo = logger.prepareJythonStackTrace('') debugPrint('[' + SCRIPT_NAME + ':getRegValues] Exception: <%s>' % excInfo) pass
def DiscoveryMain(Framework): logger.info('Start Phase 1 ... Pull from ARIS') # Set global framework global theFramework theFramework = Framework ## Make sure we have an input data file from ARIS ARISfileName = Framework.getParameter('ARIS_XML_file') or None ARISfile = File(ARISfileName) if not (ARISfile and ARISfile.exists() and ARISfile.canRead()): excInfo = ('ARIS XML input file is not specified or is invalid!') Framework.reportError(excInfo) logger.error(excInfo) return None ## Check that the language parameter is set - default to US English requestedLocaleID = Framework.getParameter( 'ARISLocaleId') or '&LocaleId.USen;' if not requestedLocaleID: logger.warn( 'ARIS LocaleID parameter is not set...defaulting to US English') requestedLocaleID = '&LocaleId.USen;' # File and directory names userExtDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder( ) + '\\TQLExport\\ARIS\\' intermediatesDir = userExtDir + 'inter\\' mapingFilesListFileName = userExtDir + 'tqls.txt' mappingFileNameList = checkDiscoveryResources(mapingFilesListFileName, userExtDir, Framework, intermediatesDir) if not mappingFileNameList: return None ## Get attribute names from mapping file(s) ## This is a list of extended attributes to be retrieved from ARIS for mappingFileName in mappingFileNameList: (requestedSourceObjectTypeList, requestedSourceRelationshipTypeList) = getMapping(userExtDir + 'data\\' + mappingFileName + '.xml') if requestedSourceObjectTypeList and requestedSourceRelationshipTypeList: arisObjectMap = processARISXML( ARISfile, requestedSourceObjectTypeList, requestedSourceRelationshipTypeList, requestedLocaleID) intermediateXmlDoc = None if arisObjectMap: intermediateXmlDoc = buildIntermediateXML(arisObjectMap) intermediateXmlLocation = intermediatesDir + mappingFileName + '.xml' else: Framework.reportWarning('No CIs found in the ARIS XML file') if intermediateXmlDoc: try: xmlOutputter = XMLOutputter() xmlOutputter.output( intermediateXmlDoc, FileOutputStream(intermediateXmlLocation)) except: excInfo = logger.prepareJythonStackTrace('') Framework.reportError( 'Error writing intermediate file: <%s>' % intermediateXmlLocation) logger.warn('[' + SCRIPT_NAME + ':DiscoveryMain] Exception: <%s>' % excInfo) pass else: Framework.reportWarning('Error creating intermediate XML') else: logger.warn( '[' + SCRIPT_NAME + ':DiscoveryMain] Unable to process mapping file: <%s>' % mappingFileName) Framework.reportError(' Unable to process mapping file: <%s>' % mappingFileName) logger.info('End Phase 1.... Pull from ARIS')
def copyRegFromProbe(self): localFile = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + CollectorsParameters.FILE_SEPARATOR + 'reg_mam.exe' self.command = self.client.copyFileIfNeeded(localFile) if not self.command: raise ValueError, "Failed copying reg_mam.exe to remote machine"