def check(self, instance): if instance is None: instance = {} self._excluded_ifaces = instance.get('excluded_interfaces', []) self._collect_cx_state = instance.get( 'collect_connection_state', False) self._collect_rate_metrics = instance.get( 'collect_rate_metrics', True) self._collect_count_metrics = instance.get( 'collect_count_metrics', False) # This decides whether we should split or combine connection states, # along with a few other things self._setup_metrics(instance) self._exclude_iface_re = None exclude_re = instance.get('excluded_interface_re', None) if exclude_re: self.log.debug("Excluding network devices matching: %s" % exclude_re) self._exclude_iface_re = re.compile(exclude_re) if Platform.is_linux(): self._check_linux(instance) elif Platform.is_bsd(): self._check_bsd(instance) elif Platform.is_solaris(): self._check_solaris(instance) elif Platform.is_windows(): self._check_psutil(instance)
def psutil_wrapper(self, process, method, accessors, try_sudo, *args, **kwargs): """ A psutil wrapper that is calling * psutil.method(*args, **kwargs) and returns the result OR * psutil.method(*args, **kwargs).accessor[i] for each accessors given in a list, the result being indexed in a dictionary by the accessor name """ if accessors is None: result = None else: result = {} # Ban certain method that we know fail if method == 'memory_info' and Platform.is_win32() or Platform.is_solaris(): return result elif method == 'num_fds' and not Platform.is_unix(): return result elif method == 'num_handles' and not Platform.is_win32(): return result try: res = getattr(process, method)(*args, **kwargs) if accessors is None: result = res else: for acc in accessors: try: result[acc] = getattr(res, acc) except AttributeError: self.log.debug("psutil.{}().{} attribute does" "not exist".format(method, acc)) except (NotImplementedError, AttributeError): self.log.debug("psutil method {} not implemented".format(method)) except psutil.AccessDenied: self.log.debug("psutil was denied access for method {}".format(method)) if method == 'num_fds' and Platform.is_unix() and try_sudo: try: # It is up the agent's packager to grant # corresponding sudo policy on unix platforms ls_args = ['sudo', 'ls', '/proc/{}/fd/'.format(process.pid)] process_ls = subprocess.check_output(ls_args) result = len(process_ls.splitlines()) except subprocess.CalledProcessError as e: self.log.exception("trying to retrieve {} with sudo failed with " "return code {}".format(method, e.returncode)) except Exception: self.log.exception("trying to retrieve {} with sudo also failed".format(method)) except psutil.NoSuchProcess: self.warning("Process {} disappeared while scanning".format(process.pid)) return result