Пример #1
0
    def test_create_watch_data_suspended(self):
        rule = {
            u"EvaluationPeriods": u"1",
            u"AlarmDescription": u"test alarm",
            u"Period": u"300",
            u"ComparisonOperator": u"GreaterThanThreshold",
            u"Statistic": u"SampleCount",
            u"Threshold": u"2",
            u"MetricName": u"CreateDataMetric",
        }
        self.wr = watchrule.WatchRule(
            context=self.ctx,
            watch_name="create_data_test",
            stack_id=self.stack_id,
            rule=rule,
            state=watchrule.WatchRule.SUSPENDED,
        )

        self.wr.store()

        data = {u"CreateDataMetric": {"Unit": "Counter", "Value": "1", "Dimensions": []}}
        self.wr.create_watch_data(data)

        dbwr = db_api.watch_rule_get_by_name(self.ctx, "create_data_test")
        self.assertEqual(dbwr.watch_data, [])
Пример #2
0
    def test_create_watch_data(self):
        rule = {u'EvaluationPeriods': u'1',
                u'AlarmDescription': u'test alarm',
                u'Period': u'300',
                u'ComparisonOperator': u'GreaterThanThreshold',
                u'Statistic': u'SampleCount',
                u'Threshold': u'2',
                u'MetricName': u'CreateDataMetric'}
        wr = watchrule.WatchRule(context=self.ctx,
                                 watch_name='create_data_test',
                                 stack_id=self.stack_id, rule=rule)

        wr.store()

        data = {u'CreateDataMetric': {"Unit": "Counter",
                                      "Value": "1",
                                      "Dimensions": []}}
        wr.create_watch_data(data)

        dbwr = db_api.watch_rule_get_by_name(self.ctx, 'create_data_test')
        self.assertEqual(dbwr.watch_data[0].data, data)

        # Note, would be good to write another datapoint and check it
        # but sqlite seems to not interpret the backreference correctly
        # so dbwr.watch_data is always a list containing only the latest
        # datapoint.  In non-test use on mysql this is not the case, we
        # correctly get a list of all datapoints where watch_rule_id ==
        # watch_rule.id, so leave it as a single-datapoint test for now.

        # Cleanup
        db_api.watch_rule_delete(self.ctx, 'create_data_test')
Пример #3
0
    def test_store(self):
        rule = {
            u'EvaluationPeriods': u'1',
            u'AlarmActions': [u'WebServerRestartPolicy'],
            u'AlarmDescription': u'Restart the WikiDatabase',
            u'Namespace': u'system/linux',
            u'Period': u'300',
            u'ComparisonOperator': u'GreaterThanThreshold',
            u'Statistic': u'SampleCount',
            u'Threshold': u'2',
            u'MetricName': u'ServiceFailure'
        }
        wr = watchrule.WatchRule(context=self.ctx,
                                 watch_name='storetest',
                                 stack_id=self.stack_id,
                                 rule=rule)
        wr.store()

        dbwr = db_api.watch_rule_get_by_name(self.ctx, 'storetest')
        self.assertNotEqual(dbwr, None)
        self.assertEqual(dbwr.name, 'storetest')
        self.assertEqual(dbwr.state, watchrule.WatchRule.NODATA)
        self.assertEqual(dbwr.rule, rule)

        # Cleanup
        db_api.watch_rule_delete(self.ctx, 'storetest')
Пример #4
0
 def load(cls, context, watch_name=None, watch=None):
     '''
     Load the watchrule object, either by name or via an existing DB object
     '''
     if watch is None:
         try:
             watch = db_api.watch_rule_get_by_name(context, watch_name)
         except Exception as ex:
             LOG.warn(
                 _('WatchRule.load (%(watch_name)s) db error '
                   '%(ex)s') % {
                       'watch_name': watch_name,
                       'ex': ex
                   })
     if watch is None:
         raise exception.WatchRuleNotFound(watch_name=watch_name)
     else:
         return cls(context=context,
                    watch_name=watch.name,
                    rule=watch.rule,
                    stack_id=watch.stack_id,
                    state=watch.state,
                    wid=watch.id,
                    watch_data=watch.watch_data,
                    last_evaluated=watch.last_evaluated)
Пример #5
0
    def test_create_watch_data_suspended(self):
        rule = {
            u'EvaluationPeriods': u'1',
            u'AlarmDescription': u'test alarm',
            u'Period': u'300',
            u'ComparisonOperator': u'GreaterThanThreshold',
            u'Statistic': u'SampleCount',
            u'Threshold': u'2',
            u'MetricName': u'CreateDataMetric'
        }
        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name='create_data_test',
                                      stack_id=self.stack_id,
                                      rule=rule,
                                      state=watchrule.WatchRule.SUSPENDED)

        self.wr.store()

        data = {
            u'CreateDataMetric': {
                "Unit": "Counter",
                "Value": "1",
                "Dimensions": []
            }
        }
        self.wr.create_watch_data(data)

        dbwr = db_api.watch_rule_get_by_name(self.ctx, 'create_data_test')
        self.assertEqual([], dbwr.watch_data)
Пример #6
0
    def test_show_watch_metric(self):
        # Insert dummy watch rule into the DB
        values = {
            'stack_id': self.stack.id,
            'state': 'NORMAL',
            'name': u'HttpFailureAlarm',
            'rule': {
                u'EvaluationPeriods': u'1',
                u'AlarmActions': [u'WebServerRestartPolicy'],
                u'AlarmDescription': u'Restart the WikiDatabase',
                u'Namespace': u'system/linux',
                u'Period': u'300',
                u'ComparisonOperator': u'GreaterThanThreshold',
                u'Statistic': u'SampleCount',
                u'Threshold': u'2',
                u'MetricName': u'ServiceFailure'
            }
        }
        db_ret = db_api.watch_rule_create(self.ctx, values)
        self.assertNotEqual(db_ret, None)

        # And add a metric datapoint
        watch = db_api.watch_rule_get_by_name(self.ctx, "HttpFailureAlarm")
        self.assertNotEqual(watch, None)
        values = {
            'watch_rule_id': watch.id,
            'data': {
                u'Namespace': u'system/linux',
                u'ServiceFailure': {
                    u'Units': u'Counter',
                    u'Value': 1
                }
            }
        }
        watch = db_api.watch_data_create(self.ctx, values)

        # Check there is one result returned
        result = self.eng.show_watch_metric(self.ctx,
                                            metric_namespace=None,
                                            metric_name=None)
        self.assertEqual(1, len(result))

        # Create another metric datapoint and check we get two
        watch = db_api.watch_data_create(self.ctx, values)
        result = self.eng.show_watch_metric(self.ctx,
                                            metric_namespace=None,
                                            metric_name=None)
        self.assertEqual(2, len(result))

        # Cleanup, delete the dummy rule
        db_api.watch_rule_delete(self.ctx, "HttpFailureAlarm")

        # Check the response has all keys defined in the engine API
        for key in engine_api.WATCH_DATA_KEYS:
            self.assertTrue(key in result[0])
Пример #7
0
    def test_show_watch_metric(self):
        # Insert dummy watch rule into the DB
        values = {'stack_id': self.stack.id,
                  'state': 'NORMAL',
                  'name': u'HttpFailureAlarm',
                  'rule': {u'EvaluationPeriods': u'1',
                           u'AlarmActions': [u'WebServerRestartPolicy'],
                           u'AlarmDescription': u'Restart the WikiDatabase',
                           u'Namespace': u'system/linux',
                           u'Period': u'300',
                           u'ComparisonOperator': u'GreaterThanThreshold',
                           u'Statistic': u'SampleCount',
                           u'Threshold': u'2',
                           u'MetricName': u'ServiceFailure'}}
        db_ret = db_api.watch_rule_create(self.ctx, values)
        self.assertNotEqual(db_ret, None)

        # And add a metric datapoint
        watch = db_api.watch_rule_get_by_name(self.ctx, "HttpFailureAlarm")
        self.assertNotEqual(watch, None)
        values = {'watch_rule_id': watch.id,
                  'data': {u'Namespace': u'system/linux',
                           u'ServiceFailure': {
                           u'Units': u'Counter', u'Value': 1}}}
        watch = db_api.watch_data_create(self.ctx, values)

        # Check there is one result returned
        result = self.eng.show_watch_metric(self.ctx,
                                            metric_namespace=None,
                                            metric_name=None)
        self.assertEqual(1, len(result))

        # Create another metric datapoint and check we get two
        watch = db_api.watch_data_create(self.ctx, values)
        result = self.eng.show_watch_metric(self.ctx,
                                            metric_namespace=None,
                                            metric_name=None)
        self.assertEqual(2, len(result))

        # Cleanup, delete the dummy rule
        db_api.watch_rule_delete(self.ctx, "HttpFailureAlarm")

        # Check the response has all keys defined in the engine API
        for key in engine_api.WATCH_DATA_KEYS:
            self.assertTrue(key in result[0])
Пример #8
0
    def test_store(self):
        rule = {u'EvaluationPeriods': u'1',
                u'AlarmActions': [u'WebServerRestartPolicy'],
                u'AlarmDescription': u'Restart the WikiDatabase',
                u'Namespace': u'system/linux',
                u'Period': u'300',
                u'ComparisonOperator': u'GreaterThanThreshold',
                u'Statistic': u'SampleCount',
                u'Threshold': u'2',
                u'MetricName': u'ServiceFailure'}
        self.wr = watchrule.WatchRule(context=self.ctx, watch_name='storetest',
                                      stack_id=self.stack_id, rule=rule)
        self.wr.store()

        dbwr = db_api.watch_rule_get_by_name(self.ctx, 'storetest')
        self.assertIsNotNone(dbwr)
        self.assertEqual('storetest', dbwr.name)
        self.assertEqual(watchrule.WatchRule.NODATA, dbwr.state)
        self.assertEqual(rule, dbwr.rule)
Пример #9
0
    def test_store(self):
        rule = {
            u"EvaluationPeriods": u"1",
            u"AlarmActions": [u"WebServerRestartPolicy"],
            u"AlarmDescription": u"Restart the WikiDatabase",
            u"Namespace": u"system/linux",
            u"Period": u"300",
            u"ComparisonOperator": u"GreaterThanThreshold",
            u"Statistic": u"SampleCount",
            u"Threshold": u"2",
            u"MetricName": u"ServiceFailure",
        }
        self.wr = watchrule.WatchRule(context=self.ctx, watch_name="storetest", stack_id=self.stack_id, rule=rule)
        self.wr.store()

        dbwr = db_api.watch_rule_get_by_name(self.ctx, "storetest")
        self.assertNotEqual(dbwr, None)
        self.assertEqual(dbwr.name, "storetest")
        self.assertEqual(dbwr.state, watchrule.WatchRule.NODATA)
        self.assertEqual(dbwr.rule, rule)
Пример #10
0
 def load(cls, context, watch_name):
     '''
     Load the watchrule from the DB by name
     '''
     dbwr = None
     try:
         dbwr = db_api.watch_rule_get_by_name(context, watch_name)
     except Exception as ex:
         logger.warn('show_watch (%s) db error %s' % (watch_name, str(ex)))
     if not dbwr:
         raise AttributeError('Unknown watch name %s' % watch_name)
     else:
         return cls(context=context,
                    watch_name=dbwr.name,
                    rule=dbwr.rule,
                    stack_name=dbwr.stack_name,
                    state=dbwr.state,
                    wid=dbwr.id,
                    watch_data=dbwr.watch_data,
                    last_evaluated=dbwr.last_evaluated)
Пример #11
0
    def test_create_watch_data(self):
        rule = {u'EvaluationPeriods': u'1',
                u'AlarmDescription': u'test alarm',
                u'Period': u'300',
                u'ComparisonOperator': u'GreaterThanThreshold',
                u'Statistic': u'SampleCount',
                u'Threshold': u'2',
                u'MetricName': u'CreateDataMetric'}
        self.wr = watchrule.WatchRule(context=self.ctx,
                                      watch_name='create_data_test',
                                      stack_id=self.stack_id, rule=rule)

        self.wr.store()

        data = {u'CreateDataMetric': {"Unit": "Counter",
                                      "Value": "1",
                                      "Dimensions": []}}
        self.wr.create_watch_data(data)

        dbwr = db_api.watch_rule_get_by_name(self.ctx, 'create_data_test')
        self.assertEqual(data, dbwr.watch_data[0].data)
Пример #12
0
 def load(cls, context, watch_name):
     '''
     Load the watchrule from the DB by name
     '''
     dbwr = None
     try:
         dbwr = db_api.watch_rule_get_by_name(context, watch_name)
     except Exception as ex:
         logger.warn('show_watch (%s) db error %s' %
                     (watch_name, str(ex)))
     if not dbwr:
         raise AttributeError('Unknown watch name %s' % watch_name)
     else:
         return cls(context=context,
                    watch_name=dbwr.name,
                    rule=dbwr.rule,
                    stack_name=dbwr.stack_name,
                    state=dbwr.state,
                    wid=dbwr.id,
                    watch_data=dbwr.watch_data,
                    last_evaluated=dbwr.last_evaluated)
Пример #13
0
 def load(cls, context, watch_name=None, watch=None):
     '''
     Load the watchrule object, either by name or via an existing DB object
     '''
     if watch is None:
         try:
             watch = db_api.watch_rule_get_by_name(context, watch_name)
         except Exception as ex:
             logger.warn('WatchRule.load (%s) db error %s' %
                         (watch_name, str(ex)))
     if watch is None:
         raise exception.WatchRuleNotFound(watch_name=watch_name)
     else:
         return cls(context=context,
                    watch_name=watch.name,
                    rule=watch.rule,
                    stack_id=watch.stack_id,
                    state=watch.state,
                    wid=watch.id,
                    watch_data=watch.watch_data,
                    last_evaluated=watch.last_evaluated)
Пример #14
0
 def load(cls, context, watch_name=None, watch=None):
     '''
     Load the watchrule object, either by name or via an existing DB object
     '''
     if watch == None:
         try:
             watch = db_api.watch_rule_get_by_name(context, watch_name)
         except Exception as ex:
             logger.warn('WatchRule.load (%s) db error %s' %
                         (watch_name, str(ex)))
     if watch == None:
         raise AttributeError('Unknown watch name %s' % watch_name)
     else:
         return cls(context=context,
                    watch_name=watch.name,
                    rule=watch.rule,
                    stack_id=watch.stack_id,
                    state=watch.state,
                    wid=watch.id,
                    watch_data=watch.watch_data,
                    last_evaluated=watch.last_evaluated)
Пример #15
0
    def test_create_watch_data(self):
        rule = {
            u'EvaluationPeriods': u'1',
            u'AlarmDescription': u'test alarm',
            u'Period': u'300',
            u'ComparisonOperator': u'GreaterThanThreshold',
            u'Statistic': u'SampleCount',
            u'Threshold': u'2',
            u'MetricName': u'CreateDataMetric'
        }
        wr = watchrule.WatchRule(context=self.ctx,
                                 watch_name='create_data_test',
                                 stack_id=self.stack_id,
                                 rule=rule)

        wr.store()

        data = {
            u'CreateDataMetric': {
                "Unit": "Counter",
                "Value": "1",
                "Dimensions": []
            }
        }
        wr.create_watch_data(data)

        dbwr = db_api.watch_rule_get_by_name(self.ctx, 'create_data_test')
        self.assertEqual(dbwr.watch_data[0].data, data)

        # Note, would be good to write another datapoint and check it
        # but sqlite seems to not interpret the backreference correctly
        # so dbwr.watch_data is always a list containing only the latest
        # datapoint.  In non-test use on mysql this is not the case, we
        # correctly get a list of all datapoints where watch_rule_id ==
        # watch_rule.id, so leave it as a single-datapoint test for now.

        # Cleanup
        db_api.watch_rule_delete(self.ctx, 'create_data_test')
Пример #16
0
 def get_by_name(cls, context, watch_rule_name):
     db_rule = db_api.watch_rule_get_by_name(context, watch_rule_name)
     return cls._from_db_object(context, cls(), db_rule)
Пример #17
0
 def get_by_name(cls, context, watch_rule_name):
     db_rule = db_api.watch_rule_get_by_name(context, watch_rule_name)
     return cls._from_db_object(context, cls(), db_rule)