Esempio n. 1
0
    def show_watch(self, context, watch_name):
        '''
        The show_watch method returns the attributes of one watch/alarm
        arg1 -> RPC context.
        arg2 -> Name of the watch you want to see, or None to see all
        '''
        if watch_name:
            try:
                wr = db_api.watch_rule_get(context, watch_name)
            except Exception as ex:
                logger.warn('show_watch (%s) db error %s' %
                            (watch_name, str(ex)))
            if wr:
                wrs = [wr]
            else:
                raise AttributeError('Unknown watch name %s' % watch_name)
        else:
            try:
                wrs = db_api.watch_rule_get_all(context)
            except Exception as ex:
                logger.warn('show_watch (all) db error %s' % str(ex))
                return

        result = [api.format_watch(w) for w in wrs]
        return result
Esempio n. 2
0
    def create_watch_data(self, context, watch_name, stats_data):
        '''
        This could be used by CloudWatch and WaitConditions
        and treat HA service events like any other CloudWatch.
        '''
        wr = db_api.watch_rule_get(None, watch_name)
        if wr is None:
            logger.warn('NoSuch watch:%s' % (watch_name))
            return ['NoSuch Watch Rule', None]

        if not wr.rule['MetricName'] in stats_data:
            logger.warn('new data has incorrect metric:%s' %
                        (wr.rule['MetricName']))
            return ['MetricName %s missing' % wr.rule['MetricName'], None]

        watch_data = {
            'data': stats_data,
            'watch_rule_id': wr.id
        }
        wd = db_api.watch_data_create(None, watch_data)
        logger.debug('new watch:%s data:%s' % (watch_name, str(wd.data)))
        if wr.rule['Statistic'] == 'SampleCount':
            self.run_rule(None, wr)

        return [None, wd.data]
Esempio n. 3
0
    def set_watch_state(self, context, watch_name, state):
        '''
        Temporarily set the state of a given watch
        arg1 -> RPC context.
        arg2 -> Name of the watch
        arg3 -> State (must be one defined in WatchRule class
        '''

        if state not in watchrule.WatchRule.WATCH_STATES:
            raise AttributeError('Unknown watch state %s' % state)

        if watch_name:
            try:
                wr = db_api.watch_rule_get(context, watch_name)
            except Exception as ex:
                logger.warn('show_watch (%s) db error %s' %
                            (watch_name, str(ex)))

            if not wr:
                raise AttributeError('Unknown watch name %s' % watch_name)

        else:
            raise AttributeError('Must pass watch_name')

        if state != wr.state:
            if self.rule_action(wr, state):
                logger.debug("Overriding state %s for watch %s with %s" %
                         (wr.state, watch_name, state))
            else:
                logger.warning("Unable to override state %s for watch %s" %
                         (wr.state, watch_name))

        # Return the watch with the state overriden to indicate success
        # We do not update the timestamps as we are not modifying the DB
        result = api.format_watch(wr)
        result[api.WATCH_STATE_VALUE] = state
        return result
Esempio n. 4
0
 def get_by_id(cls, context, rule_id):
     db_rule = db_api.watch_rule_get(context, rule_id)
     return cls._from_db_object(context, cls(), db_rule)
Esempio n. 5
0
 def get_by_id(cls, context, rule_id):
     db_rule = db_api.watch_rule_get(context, rule_id)
     return cls._from_db_object(context, cls(), db_rule)