def UDUniqueIDCheck(Framework): ipTaggingList = Framework.getTriggerCIDataAsList('ipTaggingList') hasShortIp = 0 for tagging in ipTaggingList: if tagging == '1': hasShortIp = 1 break if not hasShortIp: Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS) return nodeGUID = Framework.getDestinationAttribute('nodeGUID') client = Framework.getConnectedClient() client_options = LockUtils.getClientOptionsMap(client) logger.debug("Get client options map for GUID:", client_options) remoteGUID = client_options.get(InventoryUtils.AGENT_OPTION_CALLHOME_GUID) if nodeGUID and nodeGUID != 'NA' and remoteGUID and nodeGUID == remoteGUID or isScanOverdue( Framework): #Go discovery if the nodes are same or overdue scan Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS) else: Framework.reportWarning( "GUID is different, may be they are different hosts!") Framework.setStepExecutionStatus(WorkflowStepStatus.CANCEL)
def getRemoteScanLogFilelocation(Framework): client = Framework.getConnectedClient() remoteScanLogFileLocation = Framework.getProperty(InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_LOG_FILE_LOCATION) if not InventoryUtils.isPathValid(remoteScanLogFileLocation): options = LockUtils.getClientOptionsMap(client) remoteScanLogFileLocation = options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCANLOGFILENAME) if InventoryUtils.isPathValid(remoteScanLogFileLocation): Framework.setProperty( InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_LOG_FILE_LOCATION, remoteScanLogFileLocation ) logger.debug( "Got agent option " + InventoryUtils.AGENT_OPTION_DISCOVERY_SCANLOGFILENAME + " ", remoteScanLogFileLocation, ) else: logger.debug("Remote scan log file location from agent options:", remoteScanLogFileLocation) else: logger.debug("Got scan log file location from properties ", remoteScanLogFileLocation) if remoteScanLogFileLocation is None: remoteScanFileLocation = Framework.getProperty(InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_FILE_LOCATION) if remoteScanFileLocation is not None: remoteScanLogFileLocation = os.path.splitext(remoteScanFileLocation)[0] + ".log" return remoteScanLogFileLocation
def getScanFilelocation(Framework): try: client = Framework.getConnectedClient() except: logger.warn( "Connect failed during [Check Scanner Finished], parking for next try " ) return None #on previous Download Scan File step execution we can already obtain remote scan file path #but failed to copy it to base dir. That is why we can try to get it from properties remoteScanFileLocation = Framework.getProperty( InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_FILE_LOCATION) if not InventoryUtils.isPathValid(remoteScanFileLocation): options = LockUtils.getClientOptionsMap(client) remoteScanFileLocation = options.get( InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME) if InventoryUtils.isPathValid(remoteScanFileLocation): Framework.setProperty( InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_FILE_LOCATION, remoteScanFileLocation) logger.debug( 'Got agent option ' + InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME + ' ', remoteScanFileLocation) else: logger.debug('Remote scan file location from agent options:', remoteScanFileLocation) else: logger.debug('Got scan file location from properties ', remoteScanFileLocation) return remoteScanFileLocation
def retrieveScannerStatus(Framework, success=1): client = Framework.getConnectedClient() options = LockUtils.getClientOptionsMap(client) completionCode = options.get( InventoryUtils.AGENT_OPTION_DISCOVERY_SCAN_EXITCODE) status = options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCAN_STATUS) # just put the information into Framework to avoid duplicate log Framework.setProperty( "scanner_state_process_id", options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCAN_PID)) Framework.setProperty( "scanner_status_exit_code", options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCAN_EXITCODE)) Framework.setProperty( "scanner_status_scanner_stage", options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCAN_STAGE)) Framework.setProperty("scanner_status_retrieve_status", str(success)) Framework.setProperty("scanner_status_retrieve_date", Date()) # record Scanner status here recordScannerStatus(Framework, success) completion = "" if (completionCode is not None) and len(completionCode): completion = InventoryUtils.SCANNER_EXIT_CODE_MAPPING.get( str(completionCode)) if (str(completionCode) != '0' and str(completionCode) != '11' and str(completionCode) != '10' and str(completionCode) != '7'): # If the scanner exits with fatal failure, download the scanner log file before the entire workflow exits DownloadScanFile.downloadScanLogFile(Framework) Framework.setStepExecutionStatus(WorkflowStepStatus.FATAL_FAILURE) Framework.reportError( inventoryerrorcodes.INVENTORY_DISCOVERY_SCANNER_STATUS_FAILED, [completion, status]) return 0 elif str(completionCode) != '0': Framework.reportWarning(completion) else: pid = options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCAN_PID) if not pid: startTime = Framework.getProperty( "START_TIME_OF_SCANNER_STATUS_CHECKING") if not startTime: Framework.setProperty("START_TIME_OF_SCANNER_STATUS_CHECKING", System.currentTimeMillis()) else: startTime = long(startTime) now = System.currentTimeMillis() if (now - startTime) > maxWaitingTimeForScannerStartUp: Framework.setStepExecutionStatus( WorkflowStepStatus.FATAL_FAILURE) Framework.reportError( "Scanner can't be executed. Check if the scanner can run on the target host." ) return 0 return 1
def runScanner(Framework): client = Framework.getConnectedClient() scannersConfigFile = Framework.getConfigFile( CollectorsConstants.SCANNERSBYPLATFORM_FILE_NAME) platform = Framework.getProperty(InventoryUtils.STATE_PROPERTY_PLATFORM) architecture = Framework.getProperty( InventoryUtils.STATE_PROPERTY_ARCHITECTURE) logger.debug('Platform:', platform, ' architecture ', architecture) scannerPlatformConfig = scannersConfigFile.getPlatformConfiguration( platform, architecture) BASEDIR = Framework.getProperty( InventoryUtils.STATE_PROPERTY_RESOLVED_BASEDIR) lockValue = Framework.getProperty(LockUtils.ScannerNodeLock) client_options = LockUtils.getClientOptionsMap(client) logger.debug('Settings agent options') options = HashMap() options.put(InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME, '') options.put(LockUtils.ScannerNodeLock, lockValue) options.put(InventoryUtils.STATE_PROPERTY_EXECUTION_STARTED, str(System.currentTimeMillis())) logger.debug('Agent option ', InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME, ':', '') logger.debug('Agent option ', LockUtils.ScannerNodeLock, ':', lockValue) logger.debug('Agent option ', InventoryUtils.STATE_PROPERTY_EXECUTION_STARTED, ':', str(System.currentTimeMillis())) filterOptionsByBlackList( client_options) #filter the options of client by black list client_options.putAll(options) client.setOptionsMap(client_options, 1) shell = shellutils.ShellUtils(client, skip_set_session_locale=True) shell.execCmd('cd ' + BASEDIR) runScannerCommand = scannerPlatformConfig.getRunScannerCommand() logger.debug('Launching scanner on remote machine') commandLine = runScannerCommand.getCommand() commandLine = updateCmdWithLogLevel(commandLine, Framework) commandLine = updateCmdForDeltaScanning(commandLine, Framework) commandLine = InventoryUtils.handleBaseDirPath(Framework, commandLine) commandLine = updateWithPrePostScriptCmd(Framework, commandLine) logger.debug('Scanner execution command:', commandLine) shell.execCmd(commandLine) logger.debug('Scanner Launched on remote machine') Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def checkScanFileExistance(Framework): # this step is always finished with success since we DON'T require scan file from previous execution, just nice to have Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS) DownloadScanFileBeforeExecution = Boolean.parseBoolean(Framework.getParameter('DownloadScanFileBeforeExecution')) if DownloadScanFileBeforeExecution: try: client = Framework.getConnectedClient() options = LockUtils.getClientOptionsMap(client) previousExecutionStarted = options.get(InventoryUtils.STATE_PROPERTY_EXECUTION_STARTED) if (previousExecutionStarted is None) or (len(previousExecutionStarted.strip()) == 0): logger.debug('Previous execution timestamp no found, continuing with workflow') return remoteScanFileLocation = options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME) if not InventoryUtils.isPathValid(remoteScanFileLocation): logger.debug('No scan file path found on remote machine, continuing with workflow') return lastSuccessExecuton = Framework.getState().getJobLastSuccessfulRun() logger.debug('Last success execution ' + str(lastSuccessExecuton)) logger.debug('Remote scan file execution ' + str(previousExecutionStarted)) if long(lastSuccessExecuton) > long(previousExecutionStarted): logger.debug('Scan file on probe side is newer than on remote machine, skipping downloading') return logger.debug('Last success execution ' + str(lastSuccessExecuton) + ' older than scan file on remote machine ' + str(remoteScanFileLocation) + '. Going to download scan file:' + str(remoteScanFileLocation)) tempScanFileFolder = CollectorsParameters.PROBE_MGR_TEMPDOWNLOAD + Framework.getDiscoveryJobId() + CollectorsParameters.FILE_SEPARATOR File(tempScanFileFolder).mkdirs() extension = InventoryUtils.getFileExtension(remoteScanFileLocation) tempScanFileName = InventoryUtils.generateScanFileName(Framework, extension) tempScanFile = File(tempScanFileFolder, tempScanFileName) tempScanFilePath = tempScanFile.getCanonicalPath() logger.debug('Try to download scan file to the:', tempScanFilePath) if not InventoryUtils.copyRemoteFileToLocal(Framework, remoteScanFileLocation, tempScanFilePath, 0): logger.debug('Failed to download scan file before current execution') Framework.setProperty(InventoryUtils.STATE_PROPERTY_TEMP_SCAN_FILE, tempScanFilePath) except: reason = str(sys.exc_info()[1]) logger.debug('Failed to check/download scan file from previous execution. Reason:', reason) else: logger.debug('Even not checking whether scan file exists on remote machine or not.')
def UDUniqueIDCheck(Framework): nodeGUID = Framework.getDestinationAttribute('nodeGUID') client = Framework.getConnectedClient() client_options = LockUtils.getClientOptionsMap(client) logger.debug("Get client options map for UdUid:", client_options) remoteGUID = client_options.get(InventoryUtils.AGENT_OPTION_CALLHOME_GUID) if (nodeGUID and nodeGUID != 'NA' and remoteGUID and nodeGUID == remoteGUID): Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS) else: logger.debug("node UdUid:", nodeGUID) Framework.reportWarning("GUID is different, may be they are different hosts!") Framework.setStepExecutionStatus(WorkflowStepStatus.CANCEL)
def UDUniqueIDCheck(Framework): nodeGUID = Framework.getDestinationAttribute('nodeGUID') client = Framework.getConnectedClient() client_options = LockUtils.getClientOptionsMap(client) logger.debug("Get client options map for UdUid:", client_options) remoteGUID = client_options.get(InventoryUtils.AGENT_OPTION_CALLHOME_GUID) if (nodeGUID and nodeGUID != 'NA' and remoteGUID and nodeGUID == remoteGUID): Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS) else: logger.debug("node UdUid:", nodeGUID) Framework.reportWarning( "GUID is different, may be they are different hosts!") Framework.setStepExecutionStatus(WorkflowStepStatus.CANCEL)
def runScanner(Framework): client = Framework.getConnectedClient() scannersConfigFile = Framework.getConfigFile(CollectorsConstants.SCANNERSBYPLATFORM_FILE_NAME) platform = Framework.getProperty(InventoryUtils.STATE_PROPERTY_PLATFORM) architecture = Framework.getProperty(InventoryUtils.STATE_PROPERTY_ARCHITECTURE) logger.debug('Platform:', platform, ' architecture ', architecture) scannerPlatformConfig = scannersConfigFile.getPlatformConfiguration(platform, architecture) BASEDIR = Framework.getProperty(InventoryUtils.STATE_PROPERTY_RESOLVED_BASEDIR) lockValue = Framework.getProperty(LockUtils.ScannerNodeLock) client_options = LockUtils.getClientOptionsMap(client) logger.debug('Settings agent options') options = HashMap() options.put(InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME, '') options.put(LockUtils.ScannerNodeLock, lockValue) options.put(InventoryUtils.STATE_PROPERTY_EXECUTION_STARTED, str(System.currentTimeMillis())) logger.debug('Agent option ', InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME, ':', '') logger.debug('Agent option ', LockUtils.ScannerNodeLock, ':', lockValue) logger.debug('Agent option ', InventoryUtils.STATE_PROPERTY_EXECUTION_STARTED, ':', str(System.currentTimeMillis())) filterOptionsByBlackList(client_options) #filter the options of client by black list client_options.putAll(options) client.setOptionsMap(client_options, 1) shell = shellutils.ShellUtils(client, skip_set_session_locale=True) shell.execCmd('cd ' + BASEDIR) runScannerCommand = scannerPlatformConfig.getRunScannerCommand() logger.debug('Launching scanner on remote machine') commandLine = runScannerCommand.getCommand() commandLine = updateCmdWithLogLevel(commandLine, Framework) commandLine = updateCmdForDeltaScanning(commandLine, Framework) commandLine = InventoryUtils.handleBaseDirPath(Framework, commandLine) commandLine = updateWithPrePostScriptCmd(Framework, commandLine) logger.debug('Scanner execution command:', commandLine) shell.execCmd(commandLine) logger.debug('Scanner Launched on remote machine') Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def getRemoteScanLogFilelocation(Framework): client = Framework.getConnectedClient() remoteScanLogFileLocation = Framework.getProperty(InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_LOG_FILE_LOCATION) if not InventoryUtils.isPathValid(remoteScanLogFileLocation): options = LockUtils.getClientOptionsMap(client) remoteScanLogFileLocation = options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCANLOGFILENAME) if InventoryUtils.isPathValid(remoteScanLogFileLocation): Framework.setProperty(InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_LOG_FILE_LOCATION, remoteScanLogFileLocation) logger.debug('Got agent option ' + InventoryUtils.AGENT_OPTION_DISCOVERY_SCANLOGFILENAME + ' ', remoteScanLogFileLocation) else: logger.debug('Remote scan log file location from agent options:', remoteScanLogFileLocation) else: logger.debug('Got scan log file location from properties ', remoteScanLogFileLocation) if remoteScanLogFileLocation is None: remoteScanFileLocation = Framework.getProperty(InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_FILE_LOCATION) if remoteScanFileLocation is not None: remoteScanLogFileLocation = os.path.splitext(remoteScanFileLocation)[0] + '.log' return remoteScanLogFileLocation
def getScanFilelocation(Framework): try: client = Framework.getConnectedClient() except: logger.warn("Connect failed during [Check Scanner Finished], parking for next try ") return None #on previous Download Scan File step execution we can already obtain remote scan file path #but failed to copy it to base dir. That is why we can try to get it from properties remoteScanFileLocation = Framework.getProperty(InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_FILE_LOCATION) if not InventoryUtils.isPathValid(remoteScanFileLocation): options = LockUtils.getClientOptionsMap(client) remoteScanFileLocation = options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME) if InventoryUtils.isPathValid(remoteScanFileLocation): Framework.setProperty(InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_FILE_LOCATION, remoteScanFileLocation) logger.debug('Got agent option ' + InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME + ' ', remoteScanFileLocation) else: logger.debug('Remote scan file location from agent options:', remoteScanFileLocation) else: logger.debug('Got scan file location from properties ', remoteScanFileLocation) return remoteScanFileLocation
def retrieveScannerStatus(Framework, success = 1): client = Framework.getConnectedClient() options = LockUtils.getClientOptionsMap(client) completionCode = options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCAN_EXITCODE) status = options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCAN_STATUS) # just put the information into Framework to avoid duplicate log Framework.setProperty("scanner_state_process_id", options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCAN_PID)) Framework.setProperty("scanner_status_exit_code", options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCAN_EXITCODE)) Framework.setProperty("scanner_status_scanner_stage", options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCAN_STAGE)) Framework.setProperty("scanner_status_retrieve_status", str(success)) Framework.setProperty("scanner_status_retrieve_date", Date()) # record Scanner status here recordScannerStatus(Framework, success) completion = "" if (completionCode is not None) and len(completionCode): completion = InventoryUtils.SCANNER_EXIT_CODE_MAPPING.get(str(completionCode)) if (str(completionCode) != '0' and str(completionCode) != '11' and str(completionCode) != '10' and str(completionCode) != '7'): # If the scanner exits with fatal failure, download the scanner log file before the entire workflow exits DownloadScanFile.downloadScanLogFile(Framework) Framework.setStepExecutionStatus(WorkflowStepStatus.FATAL_FAILURE) Framework.reportError(inventoryerrorcodes.INVENTORY_DISCOVERY_SCANNER_STATUS_FAILED, [completion, status]) return 0 elif str(completionCode) != '0': Framework.reportWarning(completion) else: pid = options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCAN_PID) if not pid: startTime = Framework.getProperty("START_TIME_OF_SCANNER_STATUS_CHECKING") if not startTime: Framework.setProperty("START_TIME_OF_SCANNER_STATUS_CHECKING", System.currentTimeMillis()) else: startTime = long(startTime) now = System.currentTimeMillis() if (now - startTime) > maxWaitingTimeForScannerStartUp: Framework.setStepExecutionStatus(WorkflowStepStatus.FATAL_FAILURE) Framework.reportError("Scanner can't be executed. Check if the scanner can run on the target host.") return 0 return 1
def UDUniqueIDCheck(Framework): ipTaggingList = Framework.getTriggerCIDataAsList('ipTaggingList') hasShortIp = 0 for tagging in ipTaggingList: if tagging == '1': hasShortIp = 1 break if not hasShortIp: Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS) return nodeGUID = Framework.getDestinationAttribute('nodeGUID') client = Framework.getConnectedClient() client_options = LockUtils.getClientOptionsMap(client) logger.debug("Get client options map for GUID:", client_options) remoteGUID = client_options.get(InventoryUtils.AGENT_OPTION_CALLHOME_GUID) if nodeGUID and nodeGUID != 'NA' and remoteGUID and nodeGUID == remoteGUID or isScanOverdue(Framework):#Go discovery if the nodes are same or overdue scan Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS) else: Framework.reportWarning("GUID is different, may be they are different hosts!") Framework.setStepExecutionStatus(WorkflowStepStatus.CANCEL)
def checkScanFileExistance(Framework): # this step is always finished with success since we DON'T require scan file from previous execution, just nice to have Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS) DownloadScanFileBeforeExecution = Boolean.parseBoolean( Framework.getParameter('DownloadScanFileBeforeExecution')) if DownloadScanFileBeforeExecution: try: client = Framework.getConnectedClient() options = LockUtils.getClientOptionsMap(client) previousExecutionStarted = options.get( InventoryUtils.STATE_PROPERTY_EXECUTION_STARTED) if (previousExecutionStarted is None) or (len( previousExecutionStarted.strip()) == 0): logger.debug( 'Previous execution timestamp no found, continuing with workflow' ) return remoteScanFileLocation = options.get( InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME) if not InventoryUtils.isPathValid(remoteScanFileLocation): logger.debug( 'No scan file path found on remote machine, continuing with workflow' ) return lastSuccessExecuton = Framework.getState().getJobLastSuccessfulRun( ) logger.debug('Last success execution ' + str(lastSuccessExecuton)) logger.debug('Remote scan file execution ' + str(previousExecutionStarted)) if long(lastSuccessExecuton) > long(previousExecutionStarted): logger.debug( 'Scan file on probe side is newer than on remote machine, skipping downloading' ) return logger.debug('Last success execution ' + str(lastSuccessExecuton) + ' older than scan file on remote machine ' + str(remoteScanFileLocation) + '. Going to download scan file:' + str(remoteScanFileLocation)) tempScanFileFolder = CollectorsParameters.PROBE_MGR_TEMPDOWNLOAD + Framework.getDiscoveryJobId( ) + CollectorsParameters.FILE_SEPARATOR File(tempScanFileFolder).mkdirs() extension = InventoryUtils.getFileExtension(remoteScanFileLocation) tempScanFileName = InventoryUtils.generateScanFileName( Framework, extension) tempScanFile = File(tempScanFileFolder, tempScanFileName) tempScanFilePath = tempScanFile.getCanonicalPath() logger.debug('Try to download scan file to the:', tempScanFilePath) if not InventoryUtils.copyRemoteFileToLocal( Framework, remoteScanFileLocation, tempScanFilePath, 0): logger.debug( 'Failed to download scan file before current execution') Framework.setProperty(InventoryUtils.STATE_PROPERTY_TEMP_SCAN_FILE, tempScanFilePath) except: reason = str(sys.exc_info()[1]) logger.debug( 'Failed to check/download scan file from previous execution. Reason:', reason) else: logger.debug( 'Even not checking whether scan file exists on remote machine or not.' )