def ensure_check(cls, obj): """Ensure check-source compatibility and validate configuration""" source = SourceController.get( filters={"Source": { "id": obj.source_id }}) plugin = BaseSource.load_source(source["plugin"], {}) plugin.validate_check_parameters(obj.type, obj.parameters)
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 list_available_checks(source_name): """Return the checks of a source. .. :quickref: GET; Return the checks of a source. **Example request**: .. sourcecode:: http GET /sources/OpenTSDB/checks HTTP/1.1 Host: example.com Accept: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK { "checks": [{ "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 checks of the source """ checks = BaseSource.available_checks(source_name) return jsonify({"checks": checks}), 200
def list_available_sources(): """Return the available sources. .. :quickref: GET; Return the available sources. **Example request**: .. sourcecode:: http GET /sources HTTP/1.1 Host: example.com Accept: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK { "sources": [{ "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: list of available sources """ sources = BaseSource.available_sources() return jsonify({"sources": sources}), 200
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 ensure_plugin(cls, obj): BaseSource.validate_source_config(obj.plugin, obj.configuration)
def execute_check(logger, check_id, result_key, variables, name, start, end): check = db.session.query(Check).get(check_id) if not check: raise UnrecoverableError("Check {} not found".format(check_id)) logger.info( "[{0}] Executing check ({1})...".format(check.name, check.id), extra={"result_key": result_key}, ) source = check.source source_plugin = BaseSource.load_source(source.plugin, source.configuration) # Render every values in parameters logger.debug( "[{0}] Raw parameters : {1}".format(check.name, check.parameters), extra={"result_key": result_key}, ) template = Template( check=check, context={ "name": name, "start": start, "end": end, "variables": variables }, ) start_time = time.time() error = None check_result = None try: parameters = template.render() except TemplateError as e: parameters = {} error = e logger.critical("[{0}] {1}".format(check.name, str(error)), extra={"result_key": result_key}) else: logger.debug( "[{0}] Rendered parameters : {1}".format(check.name, parameters), extra={"result_key": result_key}, ) # Load the check check_plugin = source_plugin.load_check( check_name=check.type, parameters=parameters, name=name, start=start, end=end, ) # Execute the check and compute the elapsed time try: check_result = check_plugin.execute() # There is no data returned by the check except UnknownStateException as e: error = e logger.warning( "[{0}] {1}".format(check.name, str(error)), extra={"result_key": result_key}, ) # Do not stop the chain if this check fails except (BadConfigurationException, Exception) as e: error = e logger.critical( "[{0}] {1}".format(check.name, str(error)), extra={"result_key": result_key}, ) # Display check duration duration = time.time() - start_time logger.debug( "[{0}] Check duration : {1}s".format(check.name, duration), extra={"result_key": result_key}, ) result = { "id": check.id, "name": check.name, "type": check.type, "parameters": parameters, "duration": duration, "qos": None, # No QOS by default } if error or not check_result: result.update({"error": str(error)}) else: result.update(check_result) if result["qos"]: logger.info( "[{0}] Check returned {1}%".format(check.name, check_result["qos"]), extra={"result_key": result_key}, ) else: logger.warning( "[{0}] No QOS returned by the check".format( check.name, check_result["qos"]), extra={"result_key": result_key}, ) return result
async def execute_asyncio_check(check, name, start, end, key, variables, auto_fill): logs = [] logs = write_log( logs, "[{0}] Executing check ({1})...".format(check.name, check.id), "INFO") source = check.source source_plugin = BaseSource.load_source(source.plugin, source.configuration) logs = write_log( logs, "[{0}] Raw parameters : {1}".format(check.name, check.parameters), "INFO") template = Template( check=check, context={ "name": name, "start": start, "end": end, "variables": variables }, ) start_time = time.time() error = None check_result = None try: parameters = template.render() except TemplateError as e: parameters = {} error = e logs = write_log(logs, "[{0}] {1}".format(check.name, str(error)), "ERROR") else: logs = write_log( logs, "[{0}] Rendered parameters : {1}".format(check.name, parameters), "DEBUG", ) try: check_result = await source_plugin.run( parameters=parameters, name=name, start=start, end=end, auto_fill=auto_fill, ) except UnknownStateException as e: error = e logs = write_log(logs, "[{0}] {1}".format(check.name, str(error)), "ERROR") except (BadConfigurationException, Exception) as e: error = e logs = write_log(logs, "[{0}] {1}".format(check.name, str(error)), "ERROR") # Display check duration duration = time.time() - start_time logs = write_log( logs, "[{0}] Check duration : {1}s".format(check.name, duration), "INFO") result = { "id": check.id, "name": check.name, "type": check.type, "parameters": parameters, "duration": duration, "qos": None, } if error or not check_result: result.update({"error": str(error)}) else: result.update(check_result) if result["qos"]: logs = write_log( logs, "[{0}] Check returned {1}%".format(check.name, check_result["qos"]), "INFO", ) else: logs = write_log( logs, "[{0}] No QOS returned by the check".format( check.name, check_result["qos"]), "INFO", ) return {"logs": logs, "result": result}