Example #1
0
    def check(self, instance):
        """Capture load stats

        """

        dimensions = self._set_dimensions(None, instance)

        if util.Platform.is_linux():
            try:
                if self.process_fs_path_config:
                    log.debug(
                        'The path of the process filesystem set to %s',
                        self.process_fs_path_config)
                    loadAvrgProc = open(self.process_fs_path_config + '/loadavg', 'r')
                else:
                    loadAvrgProc = open('/proc/loadavg', 'r')
                uptime = loadAvrgProc.readlines()
                loadAvrgProc.close()
            except Exception:
                log.exception('Cannot extract load using /proc/loadavg')
                return

            uptime = uptime[0]  # readlines() provides a list but we want a string

        elif sys.platform in ('darwin', 'sunos5') or sys.platform.startswith("freebsd"):
            # Get output from uptime
            try:
                uptime = subprocess.Popen(['uptime'],
                                          stdout=subprocess.PIPE,
                                          close_fds=True).communicate()[0]
            except Exception:
                log.exception('Cannot extract load using uptime')
                return

        # Split out the 3 load average values
        load = [res.replace(',', '.') for res in re.findall(r'([0-9]+[\.,]\d+)', uptime)]

        #
        # Normalize the load averages by number of cores
        # so the metric is useful for alarming across
        # hosts with varying core numbers
        #
        num_cores = psutil.cpu_count(logical=True)

        self.gauge('load.avg_1_min',
                   round((float(load[0]) / num_cores), 3),
                   dimensions=dimensions)
        self.gauge('load.avg_5_min',
                   round((float(load[1]) / num_cores), 3),
                   dimensions=dimensions)
        self.gauge('load.avg_15_min',
                   round((float(load[2]) / num_cores), 3),
                   dimensions=dimensions)

        log.debug("Collected 3 load metrics (normalized by {} cores)".format(num_cores))
Example #2
0
    def check(self, instance):
        """Capture load stats

        """

        dimensions = self._set_dimensions(None, instance)

        if util.Platform.is_linux():
            try:
                loadAvrgProc = open('/proc/loadavg', 'r')
                uptime = loadAvrgProc.readlines()
                loadAvrgProc.close()
            except Exception:
                log.exception('Cannot extract load using /proc/loadavg')
                return

            uptime = uptime[
                0]  # readlines() provides a list but we want a string

        elif sys.platform in ('darwin',
                              'sunos5') or sys.platform.startswith("freebsd"):
            # Get output from uptime
            try:
                uptime = subprocess.Popen(['uptime'],
                                          stdout=subprocess.PIPE,
                                          close_fds=True).communicate()[0]
            except Exception:
                log.exception('Cannot extract load using uptime')
                return

        # Split out the 3 load average values
        load = [
            res.replace(',', '.')
            for res in re.findall(r'([0-9]+[\.,]\d+)', uptime)
        ]

        #
        # Normalize the load averages by number of cores
        # so the metric is useful for alarming across
        # hosts with varying core numbers
        #
        num_cores = psutil.cpu_count(logical=True)

        self.gauge('load.avg_1_min',
                   round((float(load[0]) / num_cores), 3),
                   dimensions=dimensions)
        self.gauge('load.avg_5_min',
                   round((float(load[1]) / num_cores), 3),
                   dimensions=dimensions)
        self.gauge('load.avg_15_min',
                   round((float(load[2]) / num_cores), 3),
                   dimensions=dimensions)

        log.debug("Collected 3 load metrics (normalized by {} cores)".format(
            num_cores))
Example #3
0
    def check(self, instance):
        """Capture cpu stats
        """
        num_of_metrics = 0
        dimensions = self._set_dimensions(None, instance)

        if instance is not None:
            send_rollup_stats = instance.get("send_rollup_stats", False)
        else:
            send_rollup_stats = False

        cpu_stats = psutil.cpu_times_percent(interval=None, percpu=False)
        cpu_times = psutil.cpu_times(percpu=False)
        cpu_perc = psutil.cpu_percent(interval=None, percpu=False)

        data = {
            'cpu.user_perc': cpu_stats.user + cpu_stats.nice,
            'cpu.system_perc':
            cpu_stats.system + cpu_stats.irq + cpu_stats.softirq,
            'cpu.wait_perc': cpu_stats.iowait,
            'cpu.idle_perc': cpu_stats.idle,
            'cpu.stolen_perc': cpu_stats.steal,
            'cpu.percent': cpu_perc,
            'cpu.idle_time': cpu_times.idle,
            'cpu.wait_time': cpu_times.iowait,
            'cpu.user_time': cpu_times.user + cpu_times.nice,
            'cpu.system_time':
            cpu_times.system + cpu_times.irq + cpu_times.softirq
        }

        # Call lscpu command to get cpu frequency
        self._add_cpu_freq(data)

        for key, value in data.items():
            if data[key] is None or instance.get(
                    'cpu_idle_only') and 'idle_perc' not in key:
                continue
            self.gauge(key, value, dimensions)
            num_of_metrics += 1

        if send_rollup_stats:
            self.gauge('cpu.total_logical_cores',
                       psutil.cpu_count(logical=True), dimensions)
            num_of_metrics += 1
        log.debug('Collected {0} cpu metrics'.format(num_of_metrics))
Example #4
0
    def check(self, instance):
        """Capture cpu stats
        """
        num_of_metrics = 0
        dimensions = self._set_dimensions(None, instance)

        if instance is not None:
            send_rollup_stats = instance.get("send_rollup_stats", False)
        else:
            send_rollup_stats = False

        cpu_stats = psutil.cpu_times_percent(interval=None, percpu=False)
        cpu_times = psutil.cpu_times(percpu=False)
        cpu_perc = psutil.cpu_percent(interval=None, percpu=False)

        data = {'cpu.user_perc': cpu_stats.user + cpu_stats.nice,
                'cpu.system_perc': cpu_stats.system + cpu_stats.irq + cpu_stats.softirq,
                'cpu.wait_perc': cpu_stats.iowait,
                'cpu.idle_perc': cpu_stats.idle,
                'cpu.stolen_perc': cpu_stats.steal,
                'cpu.percent': cpu_perc,
                'cpu.idle_time': cpu_times.idle,
                'cpu.wait_time': cpu_times.iowait,
                'cpu.user_time': cpu_times.user + cpu_times.nice,
                'cpu.system_time': cpu_times.system + cpu_times.irq + cpu_times.softirq}

        # Call lscpu command to get cpu frequency
        self._add_cpu_freq(data)

        for key, value in data.items():
            if data[key] is None or instance.get('cpu_idle_only') and 'idle_perc' not in key:
                continue
            self.gauge(key, value, dimensions)
            num_of_metrics += 1

        if send_rollup_stats:
            self.gauge('cpu.total_logical_cores', psutil.cpu_count(logical=True), dimensions)
            num_of_metrics += 1
        log.debug('Collected {0} cpu metrics'.format(num_of_metrics))