def setUp(self):
        super(TestQueryAlarmsController, self).setUp()
        self.alarm_url = '/query/alarms'

        for state in ['ok', 'alarm', 'insufficient data']:
            for date in [
                    datetime.datetime(2013, 1, 1),
                    datetime.datetime(2013, 2, 2)
            ]:
                for id in [1, 2]:
                    alarm_id = "-".join([state, date.isoformat(), str(id)])
                    project_id = "project-id%d" % id
                    alarm = models.Alarm(name=alarm_id,
                                         type='threshold',
                                         enabled=True,
                                         alarm_id=alarm_id,
                                         description='a',
                                         state=state,
                                         state_timestamp=date,
                                         timestamp=date,
                                         ok_actions=[],
                                         insufficient_data_actions=[],
                                         alarm_actions=[],
                                         repeat_actions=True,
                                         user_id="user-id%d" % id,
                                         project_id=project_id,
                                         rule=dict(comparison_operator='gt',
                                                   threshold=2.0,
                                                   statistic='avg',
                                                   evaluation_periods=60,
                                                   period=1,
                                                   meter_name='meter.test',
                                                   query=[{
                                                       'field': 'project_id',
                                                       'op': 'eq',
                                                       'value': project_id
                                                   }]))
                    self.conn.update_alarm(alarm)
Beispiel #2
0
    def get_alarms(self,
                   name=None,
                   user=None,
                   project=None,
                   enabled=None,
                   alarm_id=None,
                   pagination=None):

        if pagination:
            raise NotImplementedError('Pagination not implemented')

        q = make_query(alarm_id=alarm_id,
                       name=name,
                       enabled=enabled,
                       user_id=user,
                       project_id=project)

        with self.conn_pool.connection() as conn:
            alarm_table = conn.table(self.ALARM_TABLE)
            gen = alarm_table.scan(filter=q)
            for ignored, data in gen:
                stored_alarm = deserialize_entry(data)[0]
                yield models.Alarm(**stored_alarm)
Beispiel #3
0
 def _make_alarm(uuid):
     return models.Alarm(name='instance_running_hot',
                         type='threshold',
                         user_id='foobar',
                         project_id='snafu',
                         enabled=True,
                         description='',
                         repeat_actions=False,
                         state='insufficient data',
                         state_timestamp=None,
                         timestamp=None,
                         ok_actions=[],
                         alarm_actions=[],
                         insufficient_data_actions=[],
                         alarm_id=uuid,
                         rule=dict(
                             statistic='avg',
                             comparison_operator='gt',
                             threshold=80.0,
                             evaluation_periods=5,
                             period=60,
                             query=[],
                         ))
Beispiel #4
0
    def get_alarms(self,
                   name=None,
                   user=None,
                   project=None,
                   enabled=True,
                   alarm_id=None,
                   pagination=None):
        """Yields a lists of alarms that match filters
        :param name: The Alarm name.
        :param user: Optional ID for user that owns the resource.
        :param project: Optional ID for project that owns the resource.
        :param enabled: Optional boolean to list disable alarm.
        :param alarm_id: Optional alarm_id to return one alarm.
        :param pagination: Optional pagination query.
        """
        if pagination:
            raise NotImplementedError(_('Pagination not implemented'))

        q = {}
        if user is not None:
            q['user_id'] = user
        if project is not None:
            q['project_id'] = project
        if name is not None:
            q['name'] = name
        if enabled is not None:
            q['enabled'] = enabled
        if alarm_id is not None:
            q['alarm_id'] = alarm_id

        for alarm in self.db.alarm.find(q):
            a = {}
            a.update(alarm)
            del a['_id']
            a['matching_metadata'] = \
                self._decode_matching_metadata(a['matching_metadata'])
            yield models.Alarm(**a)
Beispiel #5
0
 def prepare_alarms(self):
     self.alarms = [
         models.Alarm(name='instance_running_hot',
                      description='instance_running_hot',
                      type='threshold',
                      enabled=True,
                      user_id='foobar',
                      project_id='snafu',
                      alarm_id=str(uuid.uuid4()),
                      state='insufficient data',
                      state_timestamp=None,
                      timestamp=None,
                      insufficient_data_actions=[],
                      ok_actions=[],
                      alarm_actions=[],
                      repeat_actions=False,
                      time_constraints=[],
                      rule=dict(comparison_operator='gt',
                                threshold=80.0,
                                evaluation_periods=5,
                                statistic='avg',
                                period=60,
                                meter_name='cpu_util',
                                query=[{
                                    'field': 'meter',
                                    'op': 'eq',
                                    'value': 'cpu_util'
                                }, {
                                    'field': 'resource_id',
                                    'op': 'eq',
                                    'value': 'my_instance'
                                }])),
         models.Alarm(name='group_running_idle',
                      description='group_running_idle',
                      type='threshold',
                      enabled=True,
                      user_id='foobar',
                      project_id='snafu',
                      state='insufficient data',
                      state_timestamp=None,
                      timestamp=None,
                      insufficient_data_actions=[],
                      ok_actions=[],
                      alarm_actions=[],
                      repeat_actions=False,
                      alarm_id=str(uuid.uuid4()),
                      time_constraints=[],
                      rule=dict(comparison_operator='le',
                                threshold=10.0,
                                evaluation_periods=4,
                                statistic='max',
                                period=300,
                                meter_name='cpu_util',
                                query=[{
                                    'field': 'meter',
                                    'op': 'eq',
                                    'value': 'cpu_util'
                                }, {
                                    'field': 'metadata.user_metadata.AS',
                                    'op': 'eq',
                                    'value': 'my_group'
                                }])),
     ]
    def prepare_data(self):
        def old_record_metering_data(self, data):
            self.db.user.update(
                {'_id': data['user_id']},
                {
                    '$addToSet': {
                        'source': data['source'],
                    },
                },
                upsert=True,
            )
            self.db.project.update(
                {'_id': data['project_id']},
                {
                    '$addToSet': {
                        'source': data['source'],
                    },
                },
                upsert=True,
            )
            received_timestamp = datetime.datetime.utcnow()
            self.db.resource.update(
                {'_id': data['resource_id']},
                {
                    '$set': {
                        'project_id': data['project_id'],
                        'user_id': data['user_id'],
                        # Current metadata being used and when it was
                        # last updated.
                        'timestamp': data['timestamp'],
                        'received_timestamp': received_timestamp,
                        'metadata': data['resource_metadata'],
                        'source': data['source'],
                    },
                    '$addToSet': {
                        'meter': {
                            'counter_name': data['counter_name'],
                            'counter_type': data['counter_type'],
                        },
                    },
                },
                upsert=True,
            )

            record = copy.copy(data)
            self.db.meter.insert(record)
            return

        # Stubout with the old version DB schema, the one w/o 'counter_unit'
        self.stubs.Set(self.conn, 'record_metering_data',
                       old_record_metering_data)
        self.counters = []
        c = sample.Sample(
            'volume.size',
            'gauge',
            'GiB',
            5,
            'user-id',
            'project1',
            'resource-id',
            timestamp=datetime.datetime(2012, 9, 25, 10, 30),
            resource_metadata={
                'display_name': 'test-volume',
                'tag': 'self.counter',
            },
            source='test',
        )
        self.counters.append(c)
        msg = rpc.meter_message_from_counter(c, secret='not-so-secret')
        self.conn.record_metering_data(self.conn, msg)

        # Create the old format alarm with a dict instead of a
        # array for matching_metadata
        alarm = models.Alarm('0ld-4l3rt',
                             'old-alert',
                             'test.one',
                             'eq',
                             36,
                             'count',
                             'me',
                             'and-da-boys',
                             evaluation_periods=1,
                             period=60,
                             alarm_actions=['http://nowhere/alarms'],
                             matching_metadata={'key': 'value'})
        alarm.alarm_id = str(uuid.uuid1())
        data = alarm.as_dict()
        self.conn.db.alarm.update({'alarm_id': alarm.alarm_id}, {'$set': data},
                                  upsert=True)