Example #1
0
    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._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)
Example #2
0
    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._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)
Example #3
0
    def get_process_metrics(self,
                            pids,
                            cpu_check_interval,
                            ignore_denied_access=True):

        # initialize process metrics
        # process metrics available for all versions of psutil
        rss = 0
        vms = 0
        cpu = 0
        thr = 0
        voluntary_ctx_switches = 0
        involuntary_ctx_switches = 0

        # process metrics available for psutil versions 0.6.0 and later
        if Platform.is_win32() or Platform.is_solaris():
            real = None
        else:
            real = 0

        if Platform.is_unix():
            open_file_descriptors = 0
        else:
            open_file_descriptors = None

        # process I/O counters (agent might not have permission to access)
        read_count = 0
        write_count = 0
        read_bytes = 0
        write_bytes = 0

        got_denied = False

        for pid in set(pids):
            try:
                p = psutil.Process(pid)
                try:
                    if real is not None:
                        mem = p.memory_info_ex()
                        real += mem.rss - mem.shared
                    else:
                        mem = p.memory_info()

                    if Platform.is_unix():
                        ctx_switches = p.num_ctx_switches()
                        voluntary_ctx_switches += ctx_switches.voluntary
                        involuntary_ctx_switches += ctx_switches.involuntary

                    rss += mem.rss
                    vms += mem.vms
                    thr += p.num_threads()
                    cpu += p.cpu_percent(cpu_check_interval)

                    if open_file_descriptors is not None:
                        open_file_descriptors += p.num_fds()

                except NotImplementedError:
                    # Handle old Kernels which don't provide this info.
                    voluntary_ctx_switches = None
                    involuntary_ctx_switches = None
                except AttributeError:
                    self.log.debug(
                        "process attribute not supported on this platform")
                except psutil.AccessDenied:
                    got_denied = True

                # user agent might not have permission to call io_counters()
                # user agent might have access to io counters for some processes and not others
                if read_count is not None:
                    try:
                        io_counters = p.io_counters()
                        read_count += io_counters.read_count
                        write_count += io_counters.write_count
                        read_bytes += io_counters.read_bytes
                        write_bytes += io_counters.write_bytes
                    except AttributeError:
                        self.log.debug(
                            "process attribute not supported on this platform")
                    except psutil.AccessDenied:
                        log_func = self.log.debug if ignore_denied_access else self.log.info
                        log_func('dd-agent user does not have access \
                            to I/O counters for process %d: %s' %
                                 (pid, p.name()))
                        read_count = None
                        write_count = None
                        read_bytes = None
                        write_bytes = None

            # Skip processes dead in the meantime
            except psutil.NoSuchProcess:
                self.warning('Process %s disappeared while scanning' % pid)

        if got_denied and not ignore_denied_access:
            self.warning('The Datadog Agent was denied access '
                         'when trying to get the number of file descriptors')

        # Memory values are in Byte
        return (thr, cpu, rss, vms, real, open_file_descriptors, read_count,
                write_count, read_bytes, write_bytes, voluntary_ctx_switches,
                involuntary_ctx_switches)
Example #4
0
    def get_process_metrics(self, pids, cpu_check_interval, ignore_denied_access=True):

        # initialize process metrics
        # process metrics available for all versions of psutil
        rss = 0
        vms = 0
        cpu = 0
        thr = 0
        voluntary_ctx_switches = 0
        involuntary_ctx_switches = 0

        # process metrics available for psutil versions 0.6.0 and later
        if Platform.is_win32() or Platform.is_solaris():
            real = None
        else:
            real = 0

        if Platform.is_unix():
            open_file_descriptors = 0
        else:
            open_file_descriptors = None

        # process I/O counters (agent might not have permission to access)
        read_count = 0
        write_count = 0
        read_bytes = 0
        write_bytes = 0

        got_denied = False

        for pid in set(pids):
            try:
                p = psutil.Process(pid)
                try:
                    if real is not None:
                        mem = p.memory_info_ex()
                        real += mem.rss - mem.shared
                    else:
                        mem = p.memory_info()

                    if Platform.is_unix():
                        ctx_switches = p.num_ctx_switches()
                        voluntary_ctx_switches += ctx_switches.voluntary
                        involuntary_ctx_switches += ctx_switches.involuntary

                    rss += mem.rss
                    vms += mem.vms
                    thr += p.num_threads()
                    cpu += p.cpu_percent(cpu_check_interval)

                    if open_file_descriptors is not None:
                        open_file_descriptors += p.num_fds()

                except NotImplementedError:
                    # Handle old Kernels which don't provide this info.
                    voluntary_ctx_switches = None
                    involuntary_ctx_switches = None
                except AttributeError:
                        self.log.debug("process attribute not supported on this platform")
                except psutil.AccessDenied:
                    got_denied = True

                # user agent might not have permission to call io_counters()
                # user agent might have access to io counters for some processes and not others
                if read_count is not None:
                    try:
                        io_counters = p.io_counters()
                        read_count += io_counters.read_count
                        write_count += io_counters.write_count
                        read_bytes += io_counters.read_bytes
                        write_bytes += io_counters.write_bytes
                    except AttributeError:
                        self.log.debug("process attribute not supported on this platform")
                    except psutil.AccessDenied:
                        log_func = self.log.debug if ignore_denied_access else self.log.info
                        log_func('dd-agent user does not have access \
                            to I/O counters for process %d: %s' % (pid, p.name()))
                        read_count = None
                        write_count = None
                        read_bytes = None
                        write_bytes = None

            # Skip processes dead in the meantime
            except psutil.NoSuchProcess:
                self.warning('Process %s disappeared while scanning' % pid)

        if got_denied and not ignore_denied_access:
            self.warning("The Datadog Agent was denied access when trying to get the number of file descriptors")

        #Memory values are in Byte
        return (thr, cpu, rss, vms, real, open_file_descriptors,
            read_count, write_count, read_bytes, write_bytes, voluntary_ctx_switches, involuntary_ctx_switches)