def check_worker_exceptions(process_list: List[ExceptionSafeProcess], worker_label: str, config: Optional[ConfigParser] = None, worker_function: Optional[Callable] = None) -> bool: return_value = False for worker_process in process_list: if worker_process.exception: logging.error(color_string('Exception in {} process'.format(worker_label), TerminalColors.FAIL)) logging.error(worker_process.exception[1]) terminate_process_and_childs(worker_process) process_list.remove(worker_process) if config is None or config.getboolean('ExpertSettings', 'throw_exceptions'): return_value = True elif worker_function is not None: process_index = int(worker_process.name.split('-')[-1]) logging.warning( color_string('restarting {} {} process'.format(worker_label, process_index), TerminalColors.WARNING) ) process_list.append(start_single_worker(process_index, worker_label, worker_function)) return return_value
def check_worker_exceptions( process_list: List[ExceptionSafeProcess], worker_label: str, config: Optional[ConfigParser] = None, worker_function: Optional[Callable] = None) -> bool: ''' Iterate over the `process_list` and check if exceptions occurred. In case of an exception, the process and its children will be terminated. If ``throw_exceptions`` in the FACT configuration is set to `false`, the worker may be restarted by passing a function (if the value is not set, the worker will not be restarted). In this case, the function will always return ``False``. If ``throw_exceptions`` is set to `true` and an exception occurs, the worker will not be restarted and the return value is ``True``. :param process_list: A list of worker processes. :param worker_label: A label used for logging (e.g. `Analysis` or `Unpacking`). :param config: An instance of the FACT configuration. :param worker_function: A function used for restarting the worker (optional). :return: ``True`` if an exception occurred in any process and ``throw_exceptions`` in the FACT configuration is set to `true` and ``False`` otherwise. ''' return_value = False for worker_process in process_list: if worker_process.exception: _, stack_trace = worker_process.exception logging.error( color_string( 'Exception in {} process:\n{}'.format( worker_label, stack_trace), TerminalColors.FAIL)) terminate_process_and_children(worker_process) process_list.remove(worker_process) if config is None or config.getboolean('ExpertSettings', 'throw_exceptions'): return_value = True elif worker_function is not None: process_index = int(worker_process.name.split('-')[-1]) logging.warning( color_string( 'restarting {} {} process'.format( worker_label, process_index), TerminalColors.WARNING)) process_list.append( start_single_worker(process_index, worker_label, worker_function)) return return_value
def _next_analysis_is_blacklisted(self, next_analysis: str, fw_object: FileObject): blacklist, whitelist = self._get_blacklist_and_whitelist(next_analysis) if not (blacklist or whitelist): return False if blacklist and whitelist: message = color_string(f'Configuration of plugin "{next_analysis}" erroneous', TerminalColors.FAIL) logging.error(f'{message}: found blacklist and whitelist. Ignoring blacklist.') file_type = self._get_file_type_from_object_or_db(fw_object) if whitelist: return not substring_is_in_list(file_type, whitelist) return substring_is_in_list(file_type, blacklist)
def _work_load_monitor(self): while self.stop_condition.value == 0: workload = self._get_combined_analysis_workload() unpack_queue_size = self.in_queue.qsize() if self.work_load_counter >= 25: self.work_load_counter = 0 log_function = logging.info else: self.work_load_counter += 1 log_function = logging.debug log_function( color_string( f'Queue Length (Analysis/Unpack): {workload} / {unpack_queue_size}', TerminalColors.WARNING)) if workload < int( self.config['ExpertSettings']['unpack_throttle_limit']): self.throttle_condition.value = 0 else: self.throttle_condition.value = 1 sleep(2)
def test_color_string(): colored_string = color_string(TEST_STRING, TerminalColors.BLUE) assert colored_string.startswith(TerminalColors.BLUE) assert colored_string.endswith(TerminalColors.ENDC) assert TEST_STRING in colored_string