defevaluate(self, alarm): #判断是否在包含的时间内 ifnot self.within_time_constraint(alarm): LOG.debug(_(‘Attempted to evaluate alarm %s, but it is not ‘ ‘within its time constraint.’) % alarm.alarm_id) return
@classmethod def_bound_duration(cls, alarm, constraints): “””Bound the duration of the statistics query.””” now = timeutils.utcnow() # when exclusion of weak datapoints is enabled, we extend # the look-back period so as to allow a clearer sample count # trend to be established look_back = (cls.look_back ifnot alarm.rule.get(‘exclude_outliers’) else alarm.rule[‘evaluation_periods’])
window = (alarm.rule[‘period’] * (alarm.rule[‘evaluation_periods’] + look_back)) start = now - datetime.timedelta(seconds=window) LOG.debug(_(‘query stats from %(start)s to ‘ ‘%(now)s’) % {‘start’: start, ‘now’: now}) after = dict(field=‘timestamp’, op=‘ge’, value=start.isoformat()) before = dict(field=‘timestamp’, op=‘le’, value=now.isoformat()) constraints.extend([before, after]) return constraints
deflist(self, meter_name, q=None, period=None, groupby=None, aggregates=None): groupby = groupby or [] aggregates = aggregates or [] p = [‘period=%s’ % period] if period else [] if isinstance(groupby, six.string_types): groupby = [groupby] p.extend([‘groupby=%s’ % g for g in groupby] if groupby else []) p.extend(self._build_aggregates(aggregates)) return self._list(options.build_url( ‘/v2/meters/‘ + meter_name + ‘/statistics’, q, p))
def _list(self, url, response_key=None, obj_class=None, body=None, expect_single=False): try: resp = self.api.get(url) except exceptions.NotFound: raise exc.HTTPNotFound if not resp.content: raise exc.HTTPNotFound body = resp.json()
if obj_class is None: obj_class = self.resource_class
if response_key: try: data = body[response_key] except KeyError: return [] else: data = body if expect_single: data = [data] return [obj_class(self, res, loaded=True)for res in data if res]
这个使用GET去查到了数据返回; 最后回到evaluate方法_sufficient
1 2 3 4 5 6 7 8 9 10 11 12 13
if self._sufficient(alarm, statistics): def_compare(stat): op = COMPARATORS[alarm.rule[‘comparison_operator’]] value = getattr(stat, alarm.rule[‘statistic’]) limit = alarm.rule[‘threshold’] LOG.debug(_(‘comparing value %(value)s against threshold’ ‘ %(limit)s’) % {‘value’: value, ‘limit’: limit}) return op(value, limit)
self._client.alarms.set_state(alarm.alarm_id, state=state) alarm.state = state if self.notifier: self.notifier.notify(alarm, previous, reason, reason_data) except Exception: # retry will occur naturally on the next evaluation # cycle (unless alarm state reverts in the meantime) LOG.exception(_(‘alarm state update failed’))