def _check_config_from_context(cls, config_context, team_id): # To avoid multiple identical calls to the database already_checked_rules = set() for label_name, meta in config_context["all_labels"].items(): # This is not possible to schedule an operation or a rule after an aggregation # The only task authorized after an aggregation is also another aggregation try: dstream_label_name, dstream_compute_type = config_context[ "labels_with_downstream" ][label_name] if ( config_context["all_labels"][label_name]["compute_type"] == "aggregation" and dstream_compute_type != "aggregation" ): raise IntegrityError( 'Label "{dstream_label_name}" could not be executed after label "{label_name}"'.format( dstream_label_name=dstream_label_name, label_name=label_name ) ) except KeyError: # It happens when the current label has no downstream compute task # (not present into config_context['labels_with_downstream']) pass if ( meta["compute_type"] == "rule" and config_context["all_labels"][label_name]["rule_name"] not in already_checked_rules ): # Search if the rule exists rule_name = config_context["all_labels"][label_name]["rule_name"] try: RuleController.get( filters={"Rule": {"name": rule_name, "team_id": team_id}} ) except NotFoundError: raise NotFoundError( 'Rule "{rule}" defined in label "{label}" does not exist'.format( rule=rule_name, label=label_name ) ) already_checked_rules.add(rule_name) # This is an operation or an aggregation elif ( meta["compute_type"] == "operation" or meta["compute_type"] == "aggregation" ): for dep in meta["dependencies"]: if dep not in config_context["all_labels"].keys(): msg = ( 'Dependency "{dep}" declared in label "{label}" has not ' "been declared in the configuration" ) raise NotFoundError(msg.format(dep=dep, label=label_name))
def get_label_config(cls, team_id, label): config = cls.get_current_config(team_id) label_config = config["data"][label] if not label_config: raise NotFoundError("No config found.") return label_config
def check_info(source_name, check_name): """Return a check from a source. .. :quickref: GET; Return a check from a source. **Example request**: .. sourcecode:: http GET /sources/OpenTSDB/checks/Threshold HTTP/1.1 Host: example.com Accept: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK { "name": "Threshold", "description": "This check executes a query on an OpenTSDB : every datapoints which is above a critical threshold lower the QOS.", "required": ["query", "threshold"], "type": "object", "form": [{ "key": "query", "type": "codemirror" }, { "key": "threshold", "placeholder": "Ex: 500" }], "schema": { "additionalProperties": false, "properties": { "query": { "description": "Query must return 1 or more timeserie(s).", "title": "OpenTSDB query", "type": "string" }, "threshold": { "description": "The QOS will be lowered for every values strictly superior to this threshold.", "title": "Threshold", "type": "string" } } } } :resheader Content-Type: application/json :status 200: the check """ try: checks = BaseSource.check_information(source_name, check_name) except NotImplementedError as e: raise NotFoundError(str(e)) return jsonify(checks), 200
def get_source_info(source_name): """Return a specific source. .. :quickref: GET; Return a specific source. **Example request**: .. sourcecode:: http GET /sources/OpenTSDB HTTP/1.1 Host: example.com Accept: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK { "name": "OpenTSDB", "description": "Use an OpenTSDB database to launch your queries.", "type": "object", "required": ["url", "credentials"], "schema": { "properties": { "credentials": { "description": "The credentials used authenticate the queries.", "title": "Credentials", "type": "string" }, "url": { "description": "The url used to query the database.", "title": "Url", "type": "string" } }, }, "form": [{ "key": "url", "placeholder": "http://127.0.0.1" }, { "key": "credentials", "placeholder": "foo:bar" }] } :resheader Content-Type: application/json :status 200: the source """ try: checks = BaseSource.source_information(source_name) except NotImplementedError as e: raise NotFoundError(str(e)) return jsonify(checks), 200
def before_data_load(cls, data): """Ensure that all checks exist.""" if "checks" in data: for check_id in data["checks"]: try: CheckController.get(filters={"Check": {"id": check_id}}) except NotFoundError: raise NotFoundError("Check {} not found".format(check_id)) # Transform ID into objects data["checks"] = CheckController._list({"Check": {"id": data["checks"]}})
def get_current_config(cls, team_id): configs = ConfigController.list( filters={"Config": {"team_id": team_id}}, order_by="updated_at", reverse=True, limit=1, ) config = next(iter(configs), None) if not config: raise NotFoundError("No config found.") return config
def get_label_node(cls, team_id, label, node): team = TeamController._get({"Team": {"id": team_id}}) neo = Neo4jClient() query = "MATCH(n:{0}_{1}{{name: '{2}'}}) RETURN n".format( team.kafka_topic, label, node) result = neo.query(query) try: data = list(result)[0][0]["data"] except IndexError: raise NotFoundError("Node {} does not exist".format(node)) return data
def before_data_load(cls, data): """Ensure that the source and checks exist.""" if "source_id" in data: try: source = SourceController.get( filters={"Source": { "id": data["source_id"] }}) except NotFoundError: raise NotFoundError("Source {} not found".format( data["source_id"])) plugin = BaseSource.load_source(source["plugin"], {}) plugin.validate_check_parameters(data["type"], data["parameters"])
def _not_found(cls): raise NotFoundError("Could not find resource")
def before_data_load(cls, data): if "source_id" in data: try: SourceController.get(filters={"Source": {"id": data["source_id"]}}) except NotFoundError: raise NotFoundError("Source {} not found".format(data["source_id"]))