diff --git a/README.md b/README.md index 1f9360ce53b68f77c88348a50dc26a6c876bc512..142aacf6f32c6ee28f219f19618b3e77bdb31972 100644 --- a/README.md +++ b/README.md @@ -252,3 +252,7 @@ Any non-empty responses are JSON formatted messages. This resource updates the inventory network data for juniper devices. +* /jobs/update-startup + + This resource updates data that should only be refreshed + in case of system restart. diff --git a/inventory_provider/alarmsdb.py b/inventory_provider/alarmsdb.py index b3256e10bb1b60c3511d6942dff4f061ecd75ed1..89117314fbcf40a512a822189924824f0be1aef4 100644 --- a/inventory_provider/alarmsdb.py +++ b/inventory_provider/alarmsdb.py @@ -60,3 +60,17 @@ def get_last_known_interface_status(connection, equipment, interface): result = get_last_known_juniper_link_interface_status( connection, equipment, interface) return result + + +def _load_juniper_servers_table(connection): + with db.cursor(connection) as crs: + crs.execute('select ip_address, project_name from juniper_servers') + for row in crs.fetchall(): + yield { + 'ip_address': row[0], + 'project_name': row[1] + } + + +def load_cache(connection): + yield "juniper_servers", list(_load_juniper_servers_table(connection)) diff --git a/inventory_provider/routes/jobs.py b/inventory_provider/routes/jobs.py index b1a78c86065bc0a9446266c88aecd2a4e747f1f2..315c3e8d7c960228304a6a5020497cde0bbe1f42 100644 --- a/inventory_provider/routes/jobs.py +++ b/inventory_provider/routes/jobs.py @@ -38,6 +38,17 @@ def update(): return Response("OK") +@routes.route("/update-startup", methods=['GET', 'POST']) +def startup_update(): + task_logger = logging.getLogger(TASK_LOGGER_NAME) + task_logger.debug( + 'launching task: ' + 'inventory_provider.tasks.worker.update_alarmsdb_cache') + app.send_task( + 'inventory_provider.tasks.worker.update_alarmsdb_cache') + return Response("OK") + + @routes.route("update-services", methods=['GET']) def update_service(): config = current_app.config['INVENTORY_PROVIDER_CONFIG'] diff --git a/inventory_provider/tasks/worker.py b/inventory_provider/tasks/worker.py index 78f8ef788dd3b1f9a50b5b8c9e41126d24626fec..8f2efb84f0badfac81a87851d541f9297c6a1bd0 100644 --- a/inventory_provider/tasks/worker.py +++ b/inventory_provider/tasks/worker.py @@ -6,8 +6,10 @@ import redis from lxml import etree from inventory_provider.tasks.app import app +from inventory_provider import alarmsdb from inventory_provider import config from inventory_provider import constants +from inventory_provider import db from inventory_provider import environment from inventory_provider import snmp from inventory_provider import juniper @@ -38,6 +40,25 @@ class InventoryTask(Task): "saved %s, key %s" % (hostname, key)) return "OK" + @staticmethod + def save_value(key, value): + assert isinstance(value, str), \ + "sanity failure: expected string data as value" + r = redis.StrictRedis( + host=InventoryTask.config["redis"]["hostname"], + port=InventoryTask.config["redis"]["port"]) + r.set( + name=key, + value=value) + InventoryTask.logger.debug("saved %s" % key) + return "OK" + + @staticmethod + def save_value_json(key, data_obj): + InventoryTask.save_value( + key, + json.dumps(data_obj)) + @staticmethod def save_key_json(hostname, key, data_obj): InventoryTask.save_key( @@ -102,3 +123,15 @@ def netconf_refresh_config(self, hostname): juniper.load_config(hostname, InventoryTask.config["ssh"])) logger.debug('FINISHED: netconf_refresh_config(%r)' % hostname) + + +@app.task(bind=InventoryTask) +def update_alarmsdb_cache(self): + logger = logging.getLogger(constants.TASK_LOGGER_NAME) + logger.debug('STARTING: update_alarmsdb_cache') + + with db.connection(InventoryTask.config["alarms-db"]) as cx: + for table_name, data in alarmsdb.load_cache(cx): + InventoryTask.save_value_json('alarmsdb:%s' % table_name, data) + + logger.debug('FINISHED: update_alarmsdb_cache')