示例#1
0
    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 == '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.%s().%s attribute does not exist", method,
                            acc)
        except (NotImplementedError, AttributeError):
            self.log.debug("psutil method %s not implemented", method)
        except psutil.AccessDenied:
            self.log.debug("psutil was denied access for method %s", 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 %s with sudo failed with return code %s",
                        method, e.returncode)
                except Exception:
                    self.log.exception(
                        "trying to retrieve %s with sudo also failed", method)
        except psutil.NoSuchProcess:
            self.warning("Process %s disappeared while scanning", process.pid)

        return result
示例#2
0
    def psutil_wrapper(self, process, method, accessors=None, *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 == 'num_fds' and not Platform.is_unix():
            return result
        elif method == 'num_handles' and not Platform.is_win32():
            return result

        # Try running `num_fds` with sudo if possible
        if method == 'num_fds' and self.try_sudo:
            self.log.debug("Running num_fds using sudo")
            try:
                ls_args = ['sudo', 'ls', '/proc/{}/fd/'.format(process.pid)]
                process_ls = subprocess.check_output(ls_args)
                result = len(process_ls.splitlines())
            except Exception as e:
                self.log.exception(
                    "Trying to retrieve %s with sudo failed with error: %s",
                    method, e)

        else:
            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.%s().%s attribute does not exist",
                                method, acc)
            except (NotImplementedError, AttributeError):
                self.log.debug("psutil method %s not implemented", method)
            except psutil.AccessDenied:
                self.log.debug("psutil was denied access for method %s",
                               method)
            except psutil.NoSuchProcess:
                self.log.debug("Process %s disappeared while scanning",
                               process.pid)

        return result
示例#3
0
    def _collect_part_metrics(self, part, usage):
        metrics = {}

        for name in ['total', 'used', 'free']:
            # For legacy reasons,  the standard unit it kB
            metrics[self.METRIC_DISK.format(name)] = getattr(usage, name) / 1024

        # FIXME: 6.x, use percent, a lot more logical than in_use
        metrics[self.METRIC_DISK.format('in_use')] = usage.percent / 100

        if Platform.is_unix():
            metrics.update(self._collect_inodes_metrics(part.mountpoint))

        return metrics
示例#4
0
                                system=7469.72,
                                idle=38164.81),
        psutil._psosx.scputimes(user=3826.74,
                                nice=0.0,
                                system=2701.6,
                                idle=46981.39),
        psutil._psosx.scputimes(user=7486.51,
                                nice=0.0,
                                system=5991.36,
                                idle=40031.88),
        psutil._psosx.scputimes(user=3964.85,
                                nice=0.0,
                                system=2862.37,
                                idle=46682.5),
    ]
elif Platform.is_unix():
    CHECK_RATES = [
        'system.core.idle',
        'system.core.nice',
        'system.core.system',
        'system.core.user',
        'system.core.iowait',
        'system.core.irq',
        'system.core.softirq',
        'system.core.steal',
        'system.core.guest',
        'system.core.guest_nice',
    ]
    MOCK_PSUTIL_CPU_TIMES = [
        psutil._pslinux.scputimes(
            user=1805.64,