示例#1
0
    def get_process_metrics(self, pids, name):
        processes_to_remove = set(self._cached_processes[name].keys()) - pids
        for pid in processes_to_remove:
            del self._cached_processes[name][pid]
        got_denied = False
        io_permission = True

        # initialize aggregation values
        total_thr = None
        total_cpu = None
        total_rss = None
        total_open_file_descriptors = None
        total_read_count = None
        total_write_count = None
        total_read_kbytes = None
        total_write_kbytes = None

        for pid in set(pids):
            try:
                added_process = False
                if pid not in self._cached_processes[name]:
                    p = psutil.Process(pid)
                    self._cached_processes[name][pid] = p
                    added_process = True
                else:
                    p = self._cached_processes[name][pid]

                mem = p.memory_info_ex()
                total_rss = self._safely_increment_var(total_rss, float(mem.rss / 1048576))
                total_thr = self._safely_increment_var(total_thr, p.num_threads())

                try:
                    total_open_file_descriptors = self._safely_increment_var(
                        total_open_file_descriptors, float(p.num_fds()))
                except psutil.AccessDenied:
                    got_denied = True

                if not added_process:
                    total_cpu = self._safely_increment_var(total_cpu, p.cpu_percent(interval=None))
                else:
                    p.cpu_percent(interval=None)

                # user might not have permission to call io_counters()
                if io_permission:
                    try:
                        io_counters = p.io_counters()
                        total_read_count = self._safely_increment_var(
                            total_read_count, io_counters.read_count)
                        total_write_count = self._safely_increment_var(
                            total_write_count, io_counters.write_count)
                        total_read_kbytes = self._safely_increment_var(
                            total_read_kbytes, float(io_counters.read_bytes / 1024))
                        total_write_kbytes = self._safely_increment_var(
                            total_write_kbytes, float(io_counters.write_bytes / 1024))
                    except psutil.AccessDenied:
                        self.log.debug('monasca-agent user does not have ' +
                                       'access to I/O counters for process' +
                                       ' %d: %s'
                                       % (pid, p.as_dict(['name'])['name']))
                        io_permission = False
                        total_read_count = None
                        total_write_count = None
                        total_read_kbytes = None
                        total_write_kbytes = None

            # Skip processes dead in the meantime
            except psutil.NoSuchProcess:
                self.log.warn('Process %s disappeared while metrics were being collected' % pid)
                pass

        if got_denied:
            self.log.debug("The Monitoring Agent was denied access " +
                           "when trying to get the number of file descriptors")

        return dict(zip(ProcessCheck.PROCESS_GAUGE,
                        (total_thr,
                         total_cpu,
                         total_rss,
                         total_open_file_descriptors,
                         total_read_count,
                         total_write_count,
                         total_read_kbytes,
                         total_write_kbytes)))
示例#2
0
    def get_process_metrics(self, pids, name):
        processes_to_remove = set(self._cached_processes[name].keys()) - pids
        for pid in processes_to_remove:
            del self._cached_processes[name][pid]
        got_denied = False
        io_permission = True

        # initialize aggregation values
        total_thr = None
        total_cpu = None
        total_rss = None
        total_open_file_descriptors = None
        total_read_count = None
        total_write_count = None
        total_read_kbytes = None
        total_write_kbytes = None

        for pid in set(pids):
            try:
                added_process = False
                if pid not in self._cached_processes[name]:
                    p = psutil.Process(pid)
                    self._cached_processes[name][pid] = p
                    added_process = True
                else:
                    p = self._cached_processes[name][pid]

                mem = p.memory_info_ex()
                total_rss = self._safely_increment_var(
                    total_rss, float(mem.rss / 1048576))
                total_thr = self._safely_increment_var(total_thr,
                                                       p.num_threads())

                try:
                    total_open_file_descriptors = self._safely_increment_var(
                        total_open_file_descriptors, float(p.num_fds()))
                except psutil.AccessDenied:
                    got_denied = True

                if not added_process:
                    total_cpu = self._safely_increment_var(
                        total_cpu, p.cpu_percent(interval=None))
                else:
                    p.cpu_percent(interval=None)

                if self._use_sudo:
                    self.log.debug('Get I/O counters for process %d' % pid)
                    euid = os.geteuid()
                    fname = "/proc/%s/io" % pid
                    fields = {}
                    if euid != 0:
                        args = ['sudo', sys.executable
                                ] + sys.argv + [os.environ]
                        os.execlpe('sudo', *args)
                    with open(fname, 'rb') as f:
                        for line in f:
                            line = line.strip()
                            if line:
                                name, value = line.split(b': ')
                                fields[name] = int(value)
                    if fields:
                        total_read_count = self._safely_increment_var(
                            total_read_count, fields[b'syscr'])
                        total_write_count = self._safely_increment_var(
                            total_write_count, fields[b'syscw'])
                        total_read_kbytes = self._safely_increment_var(
                            total_read_kbytes,
                            float(fields[b'read_bytes'] / 1024))
                        total_write_kbytes = self._safely_increment_var(
                            total_write_kbytes,
                            float(fields[b'write_bytes'] / 1024))
                    else:
                        total_read_count = None
                        total_write_count = None
                        total_read_kbytes = None
                        total_write_kbytes = None
                        self.log.debug('%s file was empty' % fname)

                else:
                    # user might not have permission to call io_counters()
                    if io_permission:
                        try:
                            io_counters = p.io_counters()
                            total_read_count = self._safely_increment_var(
                                total_read_count, io_counters.read_count)
                            total_write_count = self._safely_increment_var(
                                total_write_count, io_counters.write_count)
                            total_read_kbytes = self._safely_increment_var(
                                total_read_kbytes,
                                float(io_counters.read_bytes / 1024))
                            total_write_kbytes = self._safely_increment_var(
                                total_write_kbytes,
                                float(io_counters.write_bytes / 1024))
                        except psutil.AccessDenied:
                            self.log.debug(
                                'monasca-agent user does not have ' +
                                'access to I/O counters for process' +
                                ' %d: %s' % (pid, p.as_dict(['name'])['name']))
                            io_permission = False
                            total_read_count = None
                            total_write_count = None
                            total_read_kbytes = None
                            total_write_kbytes = None

            # Skip processes dead in the meantime
            except psutil.NoSuchProcess:
                self.log.warn(
                    'Process %s disappeared while metrics were being collected'
                    % pid)
                pass

        if got_denied:
            self.log.debug("The Monitoring Agent was denied access " +
                           "when trying to get the number of file descriptors")

        return dict(
            zip(ProcessCheck.PROCESS_GAUGE,
                (total_thr, total_cpu, total_rss, total_open_file_descriptors,
                 total_read_count, total_write_count, total_read_kbytes,
                 total_write_kbytes)))