diff --git a/inventory_provider/opsdb.py b/inventory_provider/opsdb.py index 1d330354e8791a8e2eb2cc462a4192cb3e06ed93..7848f09de3407fd5c77a56f18b3ae0833e1bd6c1 100644 --- a/inventory_provider/opsdb.py +++ b/inventory_provider/opsdb.py @@ -166,6 +166,7 @@ retrieve_services_query = """SELECT * c.status != 'terminated' AND is_circuit = 1) AS inner_query + WHERE circuit_type IN ('path', 'service', 'l2circuit') ORDER BY FIELD(status, 'spare', @@ -211,6 +212,14 @@ def _juniper_field_update(record): return record +def _coriant_field_update(record): + record["interface_name"] = record["card_id"] + if record["port"] is not None and record["port"] != "": + separator = "/" if "-" in record["interface_name"] else "" + record["interface_name"] += separator + str(record["port"]) + return record + + def _update_fields(r): func = globals().get("_" + r["manufacturer"] + "_field_update") return func(r) if func else r diff --git a/inventory_provider/routes/data.py b/inventory_provider/routes/data.py index d96ce69c5b203f07aa238389f93dc30f8a78c42a..7b819151faf1a9ca1527cbe140f56072b8fd2ab8 100644 --- a/inventory_provider/routes/data.py +++ b/inventory_provider/routes/data.py @@ -177,3 +177,17 @@ def interface_statuses(hostname, interface): status=404, mimetype="text/html") return jsonify({"status": result.decode('utf-8')}) + + +@routes.route("/services/<hostname>/<path:interface>", + methods=['GET', 'POST']) +def services_for_interface(hostname, interface): + r = db.get_redis() + result = r.hget("interface_services", + "{}::{}".format(hostname, interface)) + if not result: + return Response( + response="no available info for {} {}".format(hostname, interface), + status=404, + mimetype="text/html") + return jsonify(json.loads(result.decode('utf-8'))) diff --git a/inventory_provider/routes/jobs.py b/inventory_provider/routes/jobs.py index 1f9bb4ebd123a6ad721eb256e210549640304fee..25954f2b93df1eef33bb70e53c72649f81ec400b 100644 --- a/inventory_provider/routes/jobs.py +++ b/inventory_provider/routes/jobs.py @@ -48,13 +48,6 @@ def startup_update(): return Response("OK") -@routes.route("update-services-to-monitor", methods=['GET']) -def update_services_to_monitor(): - app.send_task( - 'inventory_provider.tasks.worker.update_service_to_monitor') - return Response("OK") - - @routes.route("update-interfaces-to-services", methods=['GET']) def update_interfaces_to_services(): app.send_task( diff --git a/inventory_provider/routes/opsdb.py b/inventory_provider/routes/opsdb.py index 63e426244418c6fdcb31f6c8e40494db5c8ac7b9..c49fe1f269e04944aca61cb69e5cb158648185a7 100644 --- a/inventory_provider/routes/opsdb.py +++ b/inventory_provider/routes/opsdb.py @@ -6,7 +6,6 @@ from inventory_provider import db routes = Blueprint("inventory-opsdb-query-routes", __name__) -monitored_services_key = "monitored_services" interfaces_key = "interface_services" equipment_locations_key = "equipment_locations" service_child_to_parents_key = "child_to_parent_circuit_relations" diff --git a/inventory_provider/tasks/worker.py b/inventory_provider/tasks/worker.py index 4fec01f456c04654d4487bd1c22630f99e74abc3..2f48358a17bc2b5480ace087e8ab60391a4ea6af 100644 --- a/inventory_provider/tasks/worker.py +++ b/inventory_provider/tasks/worker.py @@ -83,7 +83,6 @@ class WorkerArgs(bootsteps.Step): InventoryTask.logger = logging.getLogger(constants.TASK_LOGGER_NAME) -monitored_services_key = "monitored_services" interfaces_key = "interface_services" equipment_locations_key = "equipment_locations" service_child_to_parents_key = "child_to_parent_circuit_relations" @@ -147,22 +146,6 @@ def update_alarmsdb_cache(self): logger.debug('FINISHED: update_alarmsdb_cache') -@app.task() -def update_service_to_monitor(): - # todo - factor this connection stuff out - r = redis.StrictRedis( - host=InventoryTask.config["redis"]["hostname"], - port=InventoryTask.config["redis"]["port"]) - r.delete(monitored_services_key) - - relevant_types = ("path", "service", "l2circuit") - with db.connection(InventoryTask.config["ops-db"]) as cx: - for circuit in opsdb.get_circuits(cx): - if circuit["circuit_type"].lower() in relevant_types: - r.hset( - monitored_services_key, circuit["id"], json.dumps(circuit)) - - @app.task() def update_interfaces_to_services(): # todo - factor this connection stuff out @@ -179,6 +162,14 @@ def update_interfaces_to_services(): service["interface_name"] ) mapped_interfaces[key].append(service) + # Puts lu services under the parent ae as well as their own interface + # eg. services on ae15.12 would be found under ae15 as well as ae15.12 + if "." in service["interface_name"]: + key = "{}::{}".format( + service["equipment"], + service["interface_name"].split(".")[0] + ) + mapped_interfaces[key].append(service) r.delete(interfaces_key) for key, value in mapped_interfaces.items(): diff --git a/test/test_opsdb.py b/test/test_opsdb.py index 3507bdc0bcb80e341586cd2d8d966a5591da94fc..7e6c1848378c14f31ea6ede92b04a1bc2a65f2e2 100644 --- a/test/test_opsdb.py +++ b/test/test_opsdb.py @@ -97,6 +97,24 @@ def test_juniper_field_update(): assert r["interface_name"] == "xe-2/0/0.123" +def test_coriant_update_fields(): + i = { + "equipment": "groove-1", + "card_id": "2-3", + "port": None + } + r = inventory_provider.opsdb._coriant_field_update(i) + assert r["interface_name"] == "2-3" + + i = { + "equipment": "groove-1", + "card_id": "2-3", + "port": "4" + } + r = inventory_provider.opsdb._coriant_field_update(i) + assert r["interface_name"] == "2-3/4" + + def test_get_circuits(mocker): mocker.patch("inventory_provider.opsdb.db.cursor") mocked_convert_to_dict = mocker.patch(