def decorator(*args, **kwargs): request = args[1] json = request.body_json request.graphite_patterns = [] for field, alt in [("targets", None), ("warn_value", "expression"), ("error_value", "expression")]: if json.get(field) is None and json.get(alt) is None: defer.returnValue( bad_request(request, "%s is required" % field)) try: request.body_json = trigger_reformat(json, json.get("id"), json.get("tags", [])) except Exception: log.err() defer.returnValue(bad_request(request, "Invalid trigger format")) expression_values = { 'warn_value': json.get('warn_value'), 'error_value': json.get('error_value') } try: yield resolve_patterns(request, expression_values) except Exception: log.err() defer.returnValue(bad_request(request, "Invalid graphite target")) try: getExpression(json.get("expression"), **expression_values) except Exception: log.err() defer.returnValue(bad_request(request, "Invalid expression")) yield f(*args, **kwargs)
def decorator(*args, **kwargs): request = args[1] json = request.body_json request.graphite_patterns = [] for field, alt in [("targets", None), ("warn_value", "expression"), ("error_value", "expression")]: if json.get(field) is None and json.get(alt) is None: defer.returnValue(bad_request(request, "%s is required" % field)) try: request.body_json = trigger_reformat(json, json.get("id"), json.get("tags", [])) except: log.err() defer.returnValue(bad_request(request, "Invalid trigger format")) expression_values = {'warn_value': json.get('warn_value'), 'error_value': json.get('error_value')} try: yield resolve_patterns(request, expression_values) except: log.err() defer.returnValue(bad_request(request, "Invalid graphite target")) try: getExpression(json.get("expression"), **expression_values) except: log.err() defer.returnValue(bad_request(request, "Invalid expression")) yield f(*args, **kwargs)
def getTriggersChecks(self): """ getTriggersChecks(self) - Returns all triggers with it check :rtype: json """ triggers_ids = list((yield self.getTriggers())) triggers = [] pipeline = yield self.rc.pipeline() for trigger_id in triggers_ids: pipeline.get(TRIGGER_PREFIX.format(trigger_id)) pipeline.smembers(TRIGGER_TAGS_PREFIX.format(trigger_id)) pipeline.get(LAST_CHECK_PREFIX.format(trigger_id)) pipeline.get(TRIGGER_NEXT_PREFIX.format(trigger_id)) results = yield pipeline.execute_pipeline() slices = [[triggers_ids[i / 4]] + results[i:i + 4] for i in range(0, len(results), 4)] for trigger_id, trigger_json, trigger_tags, last_check, throttling in slices: if trigger_json is None: continue trigger = anyjson.deserialize(trigger_json) trigger = trigger_reformat(trigger, trigger_id, trigger_tags) trigger[ "last_check"] = None if last_check is None else anyjson.deserialize( last_check) trigger["throttling"] = long( throttling ) if throttling and time.time() < long(throttling) else 0 triggers.append(trigger) defer.returnValue({"list": triggers})
def getTriggersChecks(self): """ getTriggersChecks(self) - Returns all triggers with it check :rtype: json """ triggers_ids = list((yield self.getTriggers())) triggers = [] pipeline = yield self.rc.pipeline() for trigger_id in triggers_ids: pipeline.get(TRIGGER_PREFIX.format(trigger_id)) pipeline.smembers(TRIGGER_TAGS_PREFIX.format(trigger_id)) pipeline.get(LAST_CHECK_PREFIX.format(trigger_id)) pipeline.get(TRIGGER_NEXT_PREFIX.format(trigger_id)) results = yield pipeline.execute_pipeline() slices = [[triggers_ids[i/4]] + results[i:i + 4] for i in range(0, len(results), 4)] for trigger_id, trigger_json, trigger_tags, last_check, throttling in slices: if trigger_json is None: continue trigger = anyjson.deserialize(trigger_json) trigger = trigger_reformat(trigger, trigger_id, trigger_tags) trigger["last_check"] = None if last_check is None else anyjson.deserialize(last_check) trigger["throttling"] = long(throttling) if throttling and time.time() < long(throttling) else 0 triggers.append(trigger) defer.returnValue({"list": triggers})
def getTrigger(self, trigger_id): """ getTrigger(self, trigger_id) - Read trigger by key {0} - Unpack trigger json :param tags: get with tags :type tags: boolean :param trigger_id: trigger identity :type trigger_id: string :rtype: tuple(json, trigger) """ pipeline = yield self.rc.pipeline() pipeline.get(TRIGGER_PREFIX.format(trigger_id)) pipeline.smembers(TRIGGER_TAGS_PREFIX.format(trigger_id)) trigger = {} json, trigger_tags = yield pipeline.execute_pipeline() if json is not None: trigger = anyjson.deserialize(json) trigger = trigger_reformat(trigger, trigger_id, trigger_tags) defer.returnValue((json, trigger))
def decorator(*args, **kwargs): request = args[1] json = request.body_json request.graphite_patterns = [] for field, alt in [("targets", None), ("warn_value", "expression"), ("error_value", "expression")]: if json.get(field) is None and json.get(alt) is None: defer.returnValue( bad_request(request, "%s is required" % field)) if type(json["targets"]) is not list: defer.returnValue(bad_request(request, "Invalid trigger targets")) try: request.body_json = trigger_reformat(json, json.get("id"), json.get("tags", [])) except Exception as e: log.error("Invalid trigger format [{json}]: {e}", json=json, e=e) defer.returnValue(bad_request(request, "Invalid trigger format")) expression_values = { 'warn_value': json.get('warn_value'), 'error_value': json.get('error_value'), 'PREV_STATE': state.NODATA } try: yield resolve_patterns(request, expression_values) except Exception as e: log.error("Invalid graphite targets [{targets}]: {e}", targets=request.body_json["targets"], e=e) defer.returnValue(bad_request(request, "Invalid graphite targets")) try: getExpression(json.get("expression"), **expression_values) except Exception as e: log.error("Invalid expression [{expression}]: {e}", expression=json.get("expression"), e=e) defer.returnValue(bad_request(request, "Invalid expression")) yield f(*args, **kwargs)