def get_timeseries(self, requestContext): targets = self.struct.get("targets", []) target_time_series = TargetTimeSeries() target_number = 1 for target in targets: time_series = yield evaluateTarget(requestContext, target) if target_number > 1: if len(time_series) == 1: target_time_series.other_targets_names[ "t%s" % target_number] = time_series[0].name elif not time_series: raise Exception("Target #%s has no timeseries" % target_number) else: raise Exception("Target #%s has more than one timeseries" % target_number) for time_serie in time_series: time_serie.last_state = self.last_check["metrics"].get( time_serie.name, { "state": state.NODATA, "timestamp": time_serie.start - 3600 }) target_time_series[target_number] = time_series target_number += 1 defer.returnValue(target_time_series)
def resolve_patterns(request, expression_values): now = int(time()) context = createRequestContext(str(now - 600), str(now), allowRealTimeAlerting=True) resolved = set() target_num = 1 context['time_series_names'] = set() is_simple_trigger = True if len(request.body_json["targets"]) > 1: is_simple_trigger = False for target in request.body_json["targets"]: time_series = yield evaluateTarget(context, target) if is_simple_trigger and not is_simple_target(context): is_simple_trigger = False target_name = "t%s" % target_num for ts in time_series: context['time_series_names'].add(ts.name) expression_values[target_name] = 42 target_num += 1 for pattern, resolve in context['graphite_patterns'].iteritems(): for r in resolve: if r != pattern: resolved.add(r) request.body_json["patterns"] = [ pattern for pattern in context['graphite_patterns'] if pattern not in resolved ] request.body_json["is_simple_trigger"] = is_simple_trigger request.context = context
def resolve_patterns(request, expression_values): now = int(time()) context = createRequestContext(str(now - 600), str(now), allowRealTimeAlerting=True) resolved = set() target_num = 1 context['time_series_names'] = set() is_simple_trigger = True if len(request.body_json["targets"]) > 1: is_simple_trigger = False for target in request.body_json["targets"]: time_series = yield evaluateTarget(context, target) if is_simple_trigger and not is_simple_target(context): is_simple_trigger = False target_name = "t%s" % target_num for ts in time_series: context['time_series_names'].add(ts.name) expression_values[target_name] = 42 target_num += 1 for pattern, resolve in context['graphite_patterns'].iteritems(): for r in resolve: if r != pattern: resolved.add(r) request.body_json["patterns"] = [pattern for pattern in context['graphite_patterns'] if pattern not in resolved] request.body_json["is_simple_trigger"] = is_simple_trigger request.context = context
def get_timeseries(self, requestContext): targets = self.struct.get("targets", []) target_time_series = TargetTimeSeries() target_number = 1 for target in targets: time_series = yield evaluateTarget(requestContext, target) if target_number > 1: if len(time_series) == 1: target_time_series.other_targets_names["t%s" % target_number] = time_series[0].name elif not time_series: raise Exception("Target #%s has no timeseries" % target_number) else: raise Exception("Target #%s has more than one timeseries" % target_number) for time_serie in time_series: time_serie.last_state = self.last_check["metrics"].get( time_serie.name, { "state": state.NODATA, "timestamp": time_serie.start - 3600}) target_time_series[target_number] = time_series target_number += 1 defer.returnValue(target_time_series)
def render_GET(self, request): json, trigger = yield self.db.getTrigger(self.trigger_id) if json is None: defer.returnValue(bad_request(request, "Trigger not found")) raise StopIteration context = createRequestContext(request.args.get('from')[0], request.args.get('to')[0]) result = {} for target in trigger.get("targets", [trigger.get("target")]): time_series = yield evaluateTarget(context, target) for time_serie in time_series: values = [(time_serie.start + time_serie.step * i, time_serie[i]) for i in range(0, len(time_serie))] result[time_serie.name] = [{"ts": ts, "value": value} for ts, value in values if value is not None] self.write_json(request, result)
def render_GET(self, request): json, trigger = yield self.db.getTrigger(self.trigger_id) if json is None: defer.returnValue(bad_request(request, "Trigger not found")) raise StopIteration fromTime = request.args.get('from')[0] endTime = request.args.get('to')[0] context = createRequestContext(fromTime, endTime, allowRealTimeAlerting=True) result = {} for target in trigger.get("targets", [trigger.get("target")]): time_series = yield evaluateTarget(context, target) for time_serie in time_series: values = [(time_serie.start + time_serie.step * i, time_serie[i]) for i in range(0, len(time_serie))] result[time_serie.name] = [{"ts": ts, "value": value} for ts, value in values if value is not None] self.write_json(request, result)
def resolve_patterns(request, expression_values): now = int(time()) context = createRequestContext(str(now - 10), str(now)) resolved = set() target_num = 1 for target in request.body_json["targets"]: yield evaluateTarget(context, target) target_name = "t%s" % target_num expression_values[target_name] = 42 target_num += 1 for pattern, resolve in context['graphite_patterns'].iteritems(): for r in resolve: if r != pattern: resolved.add(r) request.body_json["patterns"] = [pattern for pattern in context['graphite_patterns'] if pattern not in resolved]
def resolve_patterns(request, expression_values): now = int(time()) requestContext = createRequestContext(str(now - 10), str(now)) resolved = set() target_num = 1 for target in request.body_json["targets"]: target_time_series = yield evaluateTarget(requestContext, target) target_name = "t%s" % target_num expression_values[target_name] = 42 if len(target_time_series) > 0 and len(target_time_series[0]) > 0: expression_values[target_name] = target_time_series[0][-1] target_num += 1 for pattern, resolve in requestContext['graphite_patterns'].iteritems(): for r in resolve: if r != pattern: resolved.add(r) request.body_json["patterns"] = [pattern for pattern in requestContext['graphite_patterns'] if pattern not in resolved]
def migrate_triggers(db): now = int(time()) trigger_ids = yield db.getTriggers() logs.log.info("triggers count: %d" % len(trigger_ids)) converted_triggers_count = 0 simple_triggers_count = 0 complex_triggers_count = 0 failed_triggers_count = 0 for trigger_id in trigger_ids: try: json, _ = yield db.getTrigger(trigger_id) if json is None: continue trigger = anyjson.deserialize(json) if "is_simple_trigger" in trigger: continue logs.log.info("recalculating for trigger %s (%s)" % (trigger_id, trigger.get("name"))) context = createRequestContext(str(now - 600), str(now), allowRealTimeAlerting=True) if len(trigger["targets"]) != 1: is_simple_trigger = False else: yield evaluateTarget(context, trigger["targets"][0]) is_simple_trigger = is_simple_target(context) trigger["is_simple_trigger"] = is_simple_trigger logs.log.info(str(trigger["is_simple_trigger"])) yield db.saveTrigger(trigger_id, trigger) converted_triggers_count += 1 if is_simple_trigger: simple_triggers_count += 1 else: complex_triggers_count += 1 except Exception, e: failed_triggers_count += 1 logs.log.error("conversion failed for trigger: %s" % e)