Skip to content
Snippets Groups Projects
Commit 58c4e050 authored by Erik Reid's avatar Erik Reid
Browse files

Finished feature cache-alarmsdb-tables.

parents ba05d954 30ae3abe
No related branches found
No related tags found
No related merge requests found
......@@ -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.
......@@ -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))
......@@ -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']
......
......@@ -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')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment