Exemplo n.º 1
0
    def put_metric_alarm(self, context, alarm_name, comparison_operator,
                         evaluation_periods, metric_name, namespace, period,
                         statistic, threshold, alarm_actions=[],
                         insufficient_data_actions=[], ok_actions=[],
                         actions_enabled=True, alarm_description="",
                         dimensions={}, unit="", project_id=None):
        """
        Create or updates an alarm and associates it with the specified
        SPCS Synaps metric.
        
        When this operation creates an alarm, the alarm state is immediately 
        set to INSUFFICIENT_DATA. The alarm is evaluated and its StateVale is 
        set appropriately. 
        """
        if not (project_id and context.is_admin):
            project_id = context.project_id
        
        d = utils.extract_member_dict(dimensions)
        alarm_actions = utils.extract_member_list(alarm_actions)
        insufficient_data_actions = \
            utils.extract_member_list(insufficient_data_actions)
        ok_actions = utils.extract_member_list(ok_actions)
        
        self.check_alarm_description(alarm_description)
        self.check_alarm_name(alarm_name)
        self.check_comparison_operator(comparison_operator)
        self.check_dimensions(dimensions)
        self.check_metric_name(metric_name)
        self.check_namespace(namespace)
        self.check_statistic(statistic)
        self.check_unit(unit)
        self._validate_period(period)
        self._validate_evaluation_periods(evaluation_periods)
        self.validate_put_metric_alarm(period, evaluation_periods)        
        
        metricalarm = monitor.MetricAlarm(
            alarm_name=alarm_name,
            comparison_operator=comparison_operator,
            evaluation_periods=evaluation_periods,
            metric_name=metric_name,
            namespace=namespace,
            period=period,
            statistic=statistic,
            threshold=threshold,
            actions_enabled=actions_enabled,
            alarm_actions=alarm_actions,
            alarm_description=alarm_description,
            dimensions=d,
            insufficient_data_actions=insufficient_data_actions,
            ok_actions=ok_actions,
            unit=unit
        )

        self.monitor_api.put_metric_alarm(project_id, metricalarm)
        
        return {}
Exemplo n.º 2
0
    def delete_alarms(self, context, alarm_names, project_id=None):
        if not (project_id and context.is_admin):
            project_id = context.project_id

        self.check_alarm_names(alarm_names)

        alarm_names = utils.extract_member_list(alarm_names)
        self.monitor_api.delete_alarms(project_id, alarm_names)
        return {}
Exemplo n.º 3
0
    def put_metric_data(self, context, namespace, metric_data,
                        project_id=None):
        """
        Publishes metric data points to Synaps. If specified metric does not
        exist, Synaps creates the metric.
        """
        def parse_metric_data(metric):
            try:
                dimensions_ = metric.get('dimensions', {})
                dimensions = utils.extract_member_dict(dimensions_)
            except KeyError:
                err = "Unsuitable Dimensions Value - %s" % str(dimensions_)
                raise InvalidParameterValue(err)
            
            self.check_dimensions(dimensions)
        
            metric_name = metric.get('metric_name')
            unit = metric.get('unit', 'None')
            value = metric.get('value')
            req_timestamp = metric.get('timestamp')
            timestamp = req_timestamp if req_timestamp \
                        else utils.strtime(utils.utcnow())
            timebound = (datetime.datetime.utcnow() - 
                         datetime.timedelta(
                                        seconds=FLAGS.get('statistics_ttl')))
            
            if utils.parse_strtime(timestamp) < timebound:
                err = "Stale metric data - %s" % timestamp
                raise InvalidParameterValue(err)
            
            self.check_metric_name(metric_name)
            self.check_unit(unit)
            
            return metric_name, dimensions, value, unit, timestamp 
                
        
        if not (project_id and context.is_admin):
            project_id = context.project_id
        
        self.check_namespace(namespace)
        
        metrics = [parse_metric_data(metric) for metric 
                   in utils.extract_member_list(metric_data)]

        for metric in metrics:
            metric_name, dimensions, value, unit, timestamp = metric
            self.monitor_api.put_metric_data(context,
                                             project_id=project_id,
                                             namespace=namespace,
                                             metric_name=metric_name,
                                             dimensions=dimensions,
                                             value=value,
                                             unit=unit,
                                             timestamp=timestamp,
                                             is_admin=context.is_admin)           
        return {}
Exemplo n.º 4
0
    def enable_alarm_actions(self, context, alarm_names=None,
                             project_id=None):
        if not (project_id and context.is_admin):
            project_id = context.project_id
        
        self.check_alarm_names(alarm_names)

        alarm_names = utils.extract_member_list(alarm_names)
        self.monitor_api.set_alarm_actions(project_id, alarm_names, True)
        return {}      
Exemplo n.º 5
0
    def delete_alarms(self, context, alarm_names, project_id=None):
        if not (project_id and context.is_admin):
            project_id = context.project_id

        alarm_names = utils.extract_member_list(alarm_names)

        self.check_alarm_names(alarm_names)
        self._check_admin_alarm(alarm_names, context.is_admin)
        self.monitor_api.delete_alarms(context, project_id, alarm_names)
        return {}
Exemplo n.º 6
0
    def enable_alarm_actions(self, context, alarm_names=None,
                             project_id=None):
        if not (project_id and context.is_admin):
            project_id = context.project_id
        
        alarm_names = utils.extract_member_list(alarm_names)
        self.check_alarm_names(alarm_names)
        self._check_admin_alarm(alarm_names, context.is_admin)

        self.monitor_api.set_alarm_actions(context, project_id, alarm_names,
                                           True)
        return {}      
Exemplo n.º 7
0
    def get_metric_statistics(self, context, end_time, metric_name,
                              namespace, period, start_time, statistics,
                              unit=None, dimensions=None, project_id=None):
        """
        Gets statistics for the specified metric.
        """
        
        def stat_to_datapoint(stat):
            """
            단위 변경 및 형식 변경
            """
            timestamp, values = stat
            ret = {}
            ret['Timestamp'] = timestamp 
            for statistic, value in values.iteritems(): 
                if statistic == "SampleCount":
                    ret['Unit'] = "Count"
                    ret[statistic] = value
                else:
                    ret['Unit'] = (unit if unit != 'None' else None)
                    ret[statistic] = utils.to_unit(value, unit)
                    
            return ret

        if not (project_id and context.is_admin):
            project_id = context.project_id
        end_time = utils.parse_strtime(end_time)
        start_time = utils.parse_strtime(start_time)
        dimensions = utils.extract_member_dict(dimensions)
        statistics = utils.extract_member_list(statistics)
        
        self.check_dimensions(dimensions)
        self.check_metric_name(metric_name)
        self.check_namespace(namespace)
        self.check_statistics(statistics)
        self.check_unit(unit)
        self._validate_period(period)
        self.validate_get_metric_statistics(start_time, end_time, period)
        
        stats, unit = self.monitor_api.get_metric_statistics(
                                                       project_id, end_time,
                                                       metric_name, namespace,
                                                       period, start_time,
                                                       statistics, unit,
                                                       dimensions)
    
        datapoints = map(stat_to_datapoint, stats)
        label = metric_name
        
        return {'GetMetricStatisticsResult': {'Datapoints': datapoints,
                                              'Label': label}}
Exemplo n.º 8
0
    def describe_alarms(self, context, action_prefix=None,
                        alarm_name_prefix=None, alarm_names=None,
                        max_records=None, next_token=None, state_value=None,
                        project_id=None):
        
        if not (project_id and context.is_admin):
            project_id = context.project_id
        
        max_records = int(max_records) if max_records else 100
        ret_dict = {}
        ret_alarms = []
        alarm_names = utils.extract_member_list(alarm_names) \
                      if alarm_names else None
        
        self.check_action_prefix(action_prefix)
        if alarm_names:
            self.check_alarm_names(alarm_names)
        else:
            self.check_alarm_name_prefix(alarm_name_prefix)
        self.check_state_value(state_value)
        self.check_next_token(next_token)   

        alarms = self.monitor_api.describe_alarms(project_id=project_id,
            action_prefix=action_prefix, alarm_name_prefix=alarm_name_prefix,
            alarm_names=alarm_names, max_records=max_records + 1,
            next_token=next_token, state_value=state_value,
        )
        
        for i, (k, v) in enumerate(alarms):
            if i >= max_records:
                next_token = k
                break
            ret_alarms.append(to_alarm(v))
        else:
            next_token = None
        
        ret_dict['describe_alarms_result'] = {'metric_alarms': ret_alarms}
        if next_token:
            ret_dict['describe_alarms_result']['next_token'] = str(next_token)
        
        return ret_dict
Exemplo n.º 9
0
    def describe_alarms(self, context, action_prefix=None,
                        alarm_name_prefix=None, alarm_names=None,
                        max_records=None, next_token=None, state_value=None,
                        project_id=None):
        
        if not (project_id and context.is_admin):
            project_id = context.project_id
        
        max_records = int(max_records) if max_records else 100
        ret_dict = {}
        ret_alarms = []
        alarm_names = utils.extract_member_list(alarm_names) \
                      if alarm_names else None
        
        self.check_action_prefix(action_prefix)
        if alarm_names:
            self.check_alarm_names(alarm_names)
        else:
            self.check_alarm_name_prefix(alarm_name_prefix)
        self.check_state_value(state_value)
        self.check_next_token(next_token)   

        alarms = self.monitor_api.describe_alarms(project_id=project_id,
            action_prefix=action_prefix, alarm_name_prefix=alarm_name_prefix,
            alarm_names=alarm_names, max_records=max_records + 1,
            next_token=next_token, state_value=state_value,
        )
        
        for i, (k, v) in enumerate(alarms):
            if i >= max_records:
                next_token = k
                break
            ret_alarms.append(to_alarm(v))
        else:
            next_token = None
        
        ret_dict['describe_alarms_result'] = {'metric_alarms': ret_alarms}
        if next_token:
            ret_dict['describe_alarms_result']['next_token'] = str(next_token)
        
        return ret_dict
Exemplo n.º 10
0
    def put_metric_alarm(self, context, alarm_name, comparison_operator,
                         evaluation_periods, metric_name, namespace, period,
                         statistic, threshold, alarm_actions=[],
                         insufficient_data_actions=[], ok_actions=[],
                         actions_enabled=True, alarm_description="",
                         dimensions={}, unit="", project_id=None):
        """
        Create or updates an alarm and associates it with the specified
        SPCS Synaps metric.
        
        When this operation creates an alarm, the alarm state is immediately 
        set to INSUFFICIENT_DATA. The alarm is evaluated and its StateVale is 
        set appropriately. 
        """
        if not (project_id and context.is_admin):
            project_id = context.project_id
        
        d = utils.extract_member_dict(dimensions)
        alarm_actions = utils.extract_member_list(alarm_actions)
        insufficient_data_actions = \
            utils.extract_member_list(insufficient_data_actions)
        ok_actions = utils.extract_member_list(ok_actions)
        
        self.check_alarm_description(alarm_description)
        self.check_alarm_name(alarm_name)
        self._check_admin_alarm([alarm_name], context.is_admin)
        self.check_comparison_operator(comparison_operator)
        self.check_dimensions(dimensions)
        self.check_metric_name(metric_name)
        self.check_namespace(namespace)
        self.check_statistic(statistic)
        self.check_unit(unit)
        self._validate_period(period)
        self._validate_evaluation_periods(evaluation_periods)
        self.validate_put_metric_alarm(period, evaluation_periods)   
        self._validate_instanceaction(ok_actions, project_id, context)
        self._validate_instanceaction(insufficient_data_actions, project_id,
                                      context)
        self._validate_instanceaction(alarm_actions, project_id, context)
        
        try:
            metricalarm = monitor.MetricAlarm(
                alarm_name=alarm_name,
                comparison_operator=comparison_operator,
                evaluation_periods=evaluation_periods,
                metric_name=metric_name,
                namespace=namespace,
                period=period,
                statistic=statistic,
                threshold=threshold,
                actions_enabled=actions_enabled,
                alarm_actions=alarm_actions,
                alarm_description=alarm_description,
                dimensions=d,
                insufficient_data_actions=insufficient_data_actions,
                ok_actions=ok_actions,
                unit=unit
            )
        except AssertionError as e:
            LOG.exception(e)
            err = "Unsuitable MetricAlarm Value(%s)" % str(e)
            raise InvalidParameterValue(err)

        self.monitor_api.put_metric_alarm(context, project_id, metricalarm)
        
        return {}