def get_instance_cpu_usage(self, instance):
        metric_name = self.METRIC_NAMES[
            self.config.datasource]['instance_cpu_usage']
        if self.config.datasource == "ceilometer":
            return self.ceilometer.statistic_aggregation(
                resource_id=instance.uuid,
                meter_name=metric_name,
                period=self.period,
                aggregate='avg')
        elif self.config.datasource == "monasca":
            statistics = self.monasca.statistic_aggregation(
                meter_name=metric_name,
                dimensions=dict(resource_id=instance.uuid),
                period=self.period,
                aggregate='avg')
            cpu_usage = None
            for stat in statistics:
                avg_col_idx = stat['columns'].index('avg')
                values = [r[avg_col_idx] for r in stat['statistics']]
                value = float(sum(values)) / len(values)
                cpu_usage = value
            return cpu_usage

        raise exception.UnsupportedDataSource(
            strategy=self.name, datasource=self.config.datasource)
Example #2
0
    def statistic_aggregation(self,
                              resource_id=None,
                              meter_name=None,
                              period=300,
                              granularity=300,
                              dimensions=None,
                              aggregation='avg',
                              group_by='*'):
        """Representing a statistic aggregate by operators

        :param resource_id: id of resource to list statistics for.
                            This param isn't used in Monasca datasource.
        :param meter_name: meter names of which we want the statistics.
        :param period: Sampling `period`: In seconds. If no period is given,
                       only one aggregate statistic is returned. If given, a
                       faceted result will be returned, divided into given
                       periods. Periods with no data are ignored.
        :param granularity: frequency of marking metric point, in seconds.
                            This param isn't used in Ceilometer datasource.
        :param dimensions: dimensions (dict).
        :param aggregation: Should be either 'avg', 'count', 'min' or 'max'.
        :param group_by: list of columns to group the metrics to be returned.
        :return: A list of dict with each dict being a distinct result row
        """

        if dimensions is None:
            raise exception.UnsupportedDataSource(datasource='Monasca')

        stop_time = datetime.datetime.utcnow()
        start_time = stop_time - datetime.timedelta(seconds=(int(period)))

        if aggregation == 'mean':
            aggregation = 'avg'

        raw_kwargs = dict(
            name=meter_name,
            start_time=start_time.isoformat(),
            end_time=stop_time.isoformat(),
            dimensions=dimensions,
            period=period,
            statistics=aggregation,
            group_by=group_by,
        )

        kwargs = {k: v for k, v in raw_kwargs.items() if k and v}

        statistics = self.query_retry(f=self.monasca.metrics.list_statistics,
                                      **kwargs)

        cpu_usage = None
        for stat in statistics:
            avg_col_idx = stat['columns'].index(aggregation)
            values = [r[avg_col_idx] for r in stat['statistics']]
            value = float(sum(values)) / len(values)
            cpu_usage = value

        return cpu_usage
Example #3
0
    def get_node_cpu_usage(self, node):
        metric_name = self.METRIC_NAMES[
            self.config.datasource]['host_cpu_usage']
        if self.config.datasource == "ceilometer":
            resource_id = "%s_%s" % (node.uuid, node.hostname)
            return self.ceilometer.statistic_aggregation(
                resource_id=resource_id,
                meter_name=metric_name,
                period=self.period,
                aggregate='avg',
            )
        elif self.config.datasource == "gnocchi":
            resource_id = "%s_%s" % (node.uuid, node.hostname)
            stop_time = datetime.datetime.utcnow()
            start_time = stop_time - datetime.timedelta(
                seconds=int(self.period))
            return self.gnocchi.statistic_aggregation(
                resource_id=resource_id,
                metric=metric_name,
                granularity=self.granularity,
                start_time=start_time,
                stop_time=stop_time,
                aggregation='mean')
        elif self.config.datasource == "monasca":
            statistics = self.monasca.statistic_aggregation(
                meter_name=metric_name,
                dimensions=dict(hostname=node.uuid),
                period=self.period,
                aggregate='avg')
            cpu_usage = None
            for stat in statistics:
                avg_col_idx = stat['columns'].index('avg')
                values = [r[avg_col_idx] for r in stat['statistics']]
                value = float(sum(values)) / len(values)
                cpu_usage = value

            return cpu_usage

        raise exception.UnsupportedDataSource(
            strategy=self.name, datasource=self.config.datasource)