(self, name, search_string, exact_match, ignore_ad=True)
return matching_pids
Create a set of pids of selected processes.
        Search for search_string
Create a set of pids of selected processes.
        Search for search_string
Create a set of pids of selected processes . Search for search_string
def find_pids(self, name, search_string, exact_match, ignore_ad=True):
        """
        Create a set of pids of selected processes.
        Search for search_string
        """
        if not self.should_refresh_pid_cache(name):
            return self.pid_cache[name]

        ad_error_logger = self.log.debug
        if not ignore_ad:
            ad_error_logger = self.log.error

        refresh_ad_cache = self.should_refresh_ad_cache(name)

        matching_pids = set()

        self.log.debug("Refreshing process list")

        # If refresh returns True, then the cache has been refreshed.
        # Otherwise the existing cache elements are used.
        if self.process_list_cache.refresh():
            self.log.debug("Set last ts to %s", self.process_list_cache.last_ts)
        else:
            self.log.debug("Using process list cache")

        with self.process_list_cache.read_lock():
            for proc in self.process_list_cache.elements:
                # Skip access denied processes
                if not refresh_ad_cache and proc.pid in self.ad_cache:
                    continue

                found = False
                for string in search_string:
                    try:
                        # FIXME 8.x: All has been deprecated
                        # from the doc, should be removed
                        if string == 'All':
                            found = True
                        if exact_match:
                            if os.name == 'nt':
                                if proc.name().lower() == string.lower():
                                    found = True
                            else:
                                if proc.name() == string:
                                    found = True

                        else:
                            cmdline = proc.cmdline()
                            if os.name == 'nt':
                                lstring = string.lower()
                                if re.search(lstring, ' '.join(cmdline).lower()):
                                    found = True
                            else:
                                if re.search(string, ' '.join(cmdline)):
                                    found = True
                    except psutil.NoSuchProcess:
                        # As the process list isn't necessarily scanned right after it's created
                        # (since we're using a shared cache), there can be cases where processes
                        # in the list are dead when an instance of the check tries to scan them.
                        self.log.debug('Process disappeared while scanning')
                    except psutil.AccessDenied as e:
                        ad_error_logger('Access denied to process with PID {}'.format(proc.pid))
                        ad_error_logger('Error: {}'.format(e))
                        if refresh_ad_cache:
                            self.ad_cache.add(proc.pid)
                        if not ignore_ad:
                            raise
                    else:
                        if refresh_ad_cache:
                            self.ad_cache.discard(proc.pid)
                        if found:
                            matching_pids.add(proc.pid)
                            break

            if not matching_pids:
                # Allow debug logging while preserving warning check state.
                # Uncaught psutil exceptions trigger an Error state
                try:
                    processes = sorted(proc.name() for proc in self.process_list_cache.elements)
                except (psutil.NoSuchProcess, psutil.AccessDenied):
                    pass
                else:
                    self.log.debug(
                        "Unable to find process named %s among processes: %s", search_string, ', '.join(processes)
                    )

        self.pid_cache[name] = matching_pids
        self.last_pid_cache_ts[name] = time.time()
        if refresh_ad_cache:
            self.last_ad_cache_ts[name] = time.time()
        return matching_pids
def find_pids ( self , name , search_string , exact_match , ignore_ad = True ) : if not self . should_refresh_pid_cache ( name ) : return self . pid_cache [ name ] ad_error_logger = self . log . debug if not ignore_ad : ad_error_logger = self . log . error refresh_ad_cache = self . should_refresh_ad_cache ( name ) matching_pids = set ( ) self . log . debug ( "Refreshing process list" ) # If refresh returns True, then the cache has been refreshed. # Otherwise the existing cache elements are used. if self . process_list_cache . refresh ( ) : self . log . debug ( "Set last ts to %s" , self . process_list_cache . last_ts ) else : self . log . debug ( "Using process list cache" ) with self . process_list_cache . read_lock ( ) : for proc in self . process_list_cache . elements : # Skip access denied processes if not refresh_ad_cache and proc . pid in self . ad_cache : continue found = False for string in search_string : try : # FIXME 8.x: All has been deprecated # from the doc, should be removed if string == 'All' : found = True if exact_match : if os . name == 'nt' : if proc . name ( ) . lower ( ) == string . lower ( ) : found = True else : if proc . name ( ) == string : found = True else : cmdline = proc . cmdline ( ) if os . name == 'nt' : lstring = string . lower ( ) if re . search ( lstring , ' ' . join ( cmdline ) . lower ( ) ) : found = True else : if re . search ( string , ' ' . join ( cmdline ) ) : found = True except psutil . NoSuchProcess : # As the process list isn't necessarily scanned right after it's created # (since we're using a shared cache), there can be cases where processes # in the list are dead when an instance of the check tries to scan them. self . log . debug ( 'Process disappeared while scanning' ) except psutil . AccessDenied as e : ad_error_logger ( 'Access denied to process with PID {}' . format ( proc . pid ) ) ad_error_logger ( 'Error: {}' . format ( e ) ) if refresh_ad_cache : self . ad_cache . add ( proc . pid ) if not ignore_ad : raise else : if refresh_ad_cache : self . ad_cache . discard ( proc . pid ) if found : matching_pids . add ( proc . pid ) break if not matching_pids : # Allow debug logging while preserving warning check state. # Uncaught psutil exceptions trigger an Error state try : processes = sorted ( proc . name ( ) for proc in self . process_list_cache . elements ) except ( psutil . NoSuchProcess , psutil . AccessDenied ) : pass else : self . log . debug ( "Unable to find process named %s among processes: %s" , search_string , ', ' . join ( processes ) ) self . pid_cache [ name ] = matching_pids self . last_pid_cache_ts [ name ] = time . time ( ) if refresh_ad_cache : self . last_ad_cache_ts [ name ] = time . time ( ) return matching_pids