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

move config loading to its own module

parent fc212abd
Branches
Tags
No related merge requests found
import json
import logging
import re
import jsonschema
CONFIG_SCHEMA = {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"alarms-db": {
"type": "object",
"properties": {
"hostname": {"type": "string"},
"dbname": {"type": "string"},
"username": {"type": "string"},
"password": {"type": "string"}
},
"required": ["hostname", "dbname", "username", "password"],
"additionalProperties": False
},
"oid_list.conf": {"type": "string"},
"routers_community.conf": {"type": "string"},
"ssh": {
"type": "object",
"properties": {
"private-key": {"type": "string"},
"known-hosts": {"type": "string"}
},
"required": ["private-key", "known-hosts"],
"additionalProperties": False
}
},
"required": ["alarms-db", "oid_list.conf", "routers_community.conf"],
"additionalProperties": False
}
def _load_oids(config_file):
"""
:param config_file: file-like object
:return:
"""
result = {}
for line in config_file:
m = re.match(r'^([^=]+)=(.*)\s*$', line)
if m:
result[m.group(1)] = m.group(2)
return result
def _load_routers(config_file):
"""
:param config_file: file-like object
:return:
"""
for line in config_file:
m = re.match(r'^([a-z\d]+\.[a-z\d]{3,4}\.[a-z\d]{2}\.(geant|eumedconnect)\d*\.net)\s*=([^,]+)\s*,(.*)\s*$', line)
if not m:
logging.warning("malformed config file line: '%s'" % line.strip())
continue
yield {
"hostname": m.group(1),
"community": m.group(3),
"address": m.group(4)
}
def load(f):
"""
loads, validates and returns configuration parameters
:param f: file-like object that produces the config file
:return:
"""
config = json.loads(f.read())
jsonschema.validate(config, CONFIG_SCHEMA)
with open(config["oid_list.conf"]) as f:
config["oids"] = _load_oids(f)
with open(config["routers_community.conf"]) as f:
config["routers"] = list(_load_routers(f))
return config
......@@ -2,34 +2,3 @@ SNMP_LOGGER_NAME = "snmp-logger"
THREADING_LOGGER_NAME = "threading-logger"
JUNIPER_LOGGER_NAME = "juniper-logger"
DATABASE_LOGGER_NAME = "database-logger"
CONFIG_SCHEMA = {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"alarms-db": {
"type": "object",
"properties": {
"hostname": {"type": "string"},
"dbname": {"type": "string"},
"username": {"type": "string"},
"password": {"type": "string"}
},
"required": ["hostname", "dbname", "username", "password"],
"additionalProperties": False
},
"oid_list.conf": {"type": "string"},
"routers_community.conf": {"type": "string"},
"ssh": {
"type": "object",
"properties": {
"private-key": {"type": "string"},
"known-hosts": {"type": "string"}
},
"required": ["private-key", "known-hosts"],
"additionalProperties": False
}
},
"required": ["alarms-db", "oid_list.conf", "routers_community.conf"],
"additionalProperties": False
}
......@@ -9,68 +9,13 @@ import jsonschema
from inventory_provider import constants
from inventory_provider import snmp
from inventory_provider import juniper
from inventory_provider import config
def _load_oids(config_file):
"""
:param config_file: file-like object
:return:
"""
result = {}
for line in config_file:
m = re.match(r'^([^=]+)=(.*)\s*$', line)
if m:
result[m.group(1)] = m.group(2)
return result
def _load_routers(config_file):
"""
:param config_file: file-like object
:return:
"""
for line in config_file:
m = re.match(r'^([a-z\d]+\.[a-z\d]{3,4}\.[a-z\d]{2}\.(geant|eumedconnect)\d*\.net)\s*=([^,]+)\s*,(.*)\s*$', line)
if not m:
logging.warning("malformed config file line: '%s'" % line.strip())
continue
yield {
"hostname": m.group(1),
"community": m.group(3),
"address": m.group(4)
}
def _validate_config(ctx, param, value):
"""
loads, validates and returns configuration parameters
:param ctx:
:param param:
:param value:
:return:
"""
config = json.loads(value.read())
jsonschema.validate(config, constants.CONFIG_SCHEMA)
with open(config["oid_list.conf"]) as f:
config["oids"] = _load_oids(f)
with open(config["routers_community.conf"]) as f:
config["routers"] = list(_load_routers(f))
return config
def get_router_interfaces_q(router, config, q):
def get_router_interfaces_q(router, params, q):
threading_logger = logging.getLogger(constants.THREADING_LOGGER_NAME)
threading_logger.debug("[ENTER>>] get_router_interfaces_q: %r" % router)
q.put(list(snmp.get_router_interfaces(router, config)))
q.put(list(snmp.get_router_interfaces(router, params)))
threading_logger.debug("[<<EXIT] get_router_interfaces_q: %r" % router)
......@@ -122,14 +67,14 @@ def get_router_details(router, params, q):
threading_logger.debug("[<<EXIT]get_router_details: %r" % router)
def load_network_details(config):
def load_network_details(params):
threading_logger = logging.getLogger(constants.THREADING_LOGGER_NAME)
processes = []
for r in config["routers"]:
for r in params["routers"]:
q = Queue()
p = Process(target=get_router_details, args=(r, config, q))
p = Process(target=get_router_details, args=(r, params, q))
p.start()
processes.append({"router": r, "process": p, "queue": q})
......@@ -143,17 +88,20 @@ def load_network_details(config):
return result
def _validate_config(ctx, param, value):
return config.load(value)
@click.command()
@click.option(
"--config",
"--params",
# required=True,
type=click.File(),
help="Configuration filename",
default=open("config.json"),
callback=_validate_config)
def cli(config):
network_details = load_network_details(config)
def cli(params):
network_details = load_network_details(params)
filename = "/tmp/router-info.json"
logging.debug("writing output to: " + filename)
with open(filename, "w") as f:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment