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

use multiple loggers

parent b5f76565
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,12 @@ import paramiko ...@@ -11,6 +11,12 @@ import paramiko
from pysnmp.hlapi import nextCmd, SnmpEngine, CommunityData, \ from pysnmp.hlapi import nextCmd, SnmpEngine, CommunityData, \
UdpTransportTarget, ContextData, ObjectType, ObjectIdentity UdpTransportTarget, ContextData, ObjectType, ObjectIdentity
SNMP_LOGGER_NAME = "snmp-logger"
THREADING_LOGGER_NAME = "threading-logger"
JUNIPER_LOGGER_NAME = "juniper-logger"
DATABASE_LOGGER_NAME = "database-logger"
CONFIG_SCHEMA = { CONFIG_SCHEMA = {
"$schema": "http://json-schema.org/draft-07/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
"type": "object", "type": "object",
...@@ -57,6 +63,8 @@ def walk(agent_hostname, community, base_oid): ...@@ -57,6 +63,8 @@ def walk(agent_hostname, community, base_oid):
:return: :return:
""" """
snmp_logger = logging.getLogger(SNMP_LOGGER_NAME)
from pysnmp.smi import builder, view, compiler, rfc1902 from pysnmp.smi import builder, view, compiler, rfc1902
mibBuilder = builder.MibBuilder() mibBuilder = builder.MibBuilder()
mibViewController = view.MibViewController(mibBuilder) mibViewController = view.MibViewController(mibBuilder)
...@@ -64,7 +72,8 @@ def walk(agent_hostname, community, base_oid): ...@@ -64,7 +72,8 @@ def walk(agent_hostname, community, base_oid):
# Pre-load MIB modules we expect to work with # Pre-load MIB modules we expect to work with
mibBuilder.loadModules('SNMPv2-MIB', 'SNMP-COMMUNITY-MIB', 'RFC1213-MIB') mibBuilder.loadModules('SNMPv2-MIB', 'SNMP-COMMUNITY-MIB', 'RFC1213-MIB')
# logging.debug("walking %s: %s" % (agent_hostname, base_oid)) snmp_logger.debug("walking %s: %s" % (agent_hostname, base_oid))
for (engineErrorIndication, for (engineErrorIndication,
pduErrorIndication, pduErrorIndication,
errorIndex, errorIndex,
...@@ -159,12 +168,13 @@ def cursor(cnx): ...@@ -159,12 +168,13 @@ def cursor(cnx):
def _db_test(db, router): def _db_test(db, router):
database_logger = logging.getLogger(DATABASE_LOGGER_NAME)
with cursor(db) as crs: with cursor(db) as crs:
logging.debug("_db_test: %r" % router) database_logger.debug("_db_test: %r" % router)
query = "SELECT absid FROM routers WHERE hostname = %s" query = "SELECT absid FROM routers WHERE hostname = %s"
crs.execute(query, (router['hostname'],)) crs.execute(query, (router['hostname'],))
for (absid,) in crs: for (absid,) in crs:
logging.debug("absid: %r" % absid) database_logger.debug("absid: %r" % absid)
def _v6address_oid2str(dotted_decimal): def _v6address_oid2str(dotted_decimal):
...@@ -239,40 +249,45 @@ def ssh_connection(router, ssh_params): ...@@ -239,40 +249,45 @@ def ssh_connection(router, ssh_params):
def exec_router_commands_json(router, ssh_params, commands): def exec_router_commands_json(router, ssh_params, commands):
juniper_logger = logging.getLogger(JUNIPER_LOGGER_NAME)
with ssh_connection(router, ssh_params) as ssh: with ssh_connection(router, ssh_params) as ssh:
_, stdout, _ = ssh.exec_command("set cli screen-length 0") _, stdout, _ = ssh.exec_command("set cli screen-length 0")
assert stdout.channel.recv_exit_status() == 0 assert stdout.channel.recv_exit_status() == 0
for c in commands: for c in commands:
logging.debug("command: '%s'" % (c + " | display json")) juniper_logger.debug("command: '%s'" % (c + " | display json"))
_, stdout, _ = ssh.exec_command(c + " | display json") _, stdout, _ = ssh.exec_command(c + " | display json")
assert stdout.channel.recv_exit_status() == 0 assert stdout.channel.recv_exit_status() == 0
# TODO: error handling # TODO: error handling
output = stdout.read() output = stdout.read()
if output: if output:
logging.debug("%r output: [%d] %r" % (router, len(output), output[:20])) juniper_logger.debug("%r output: [%d] %r" % (router, len(output), output[:20]))
yield json.loads(output) yield json.loads(output)
else: else:
logging.debug("%r output empty") juniper_logger.debug("%r output empty")
yield {} yield {}
def get_router_interfaces_q(router, q): def get_router_interfaces_q(router, q):
logging.debug("[ENTER>>] get_router_interfaces_q: %r" % router) threading_logger = logging.getLogger(THREADING_LOGGER_NAME)
threading_logger.debug("[ENTER>>] get_router_interfaces_q: %r" % router)
q.put(list(get_router_interfaces(router))) q.put(list(get_router_interfaces(router)))
logging.debug("[<<EXIT] get_router_interfaces_q: %r" % router) threading_logger.debug("[<<EXIT] get_router_interfaces_q: %r" % router)
def exec_router_commands_json_q(router, ssh_params, commands, q): def exec_router_commands_json_q(router, ssh_params, commands, q):
logging.debug("[ENTER>>] exec_router_commands_q: %r" % router) threading_logger = logging.getLogger(THREADING_LOGGER_NAME)
threading_logger.debug("[ENTER>>] exec_router_commands_q: %r" % router)
q.put(list(exec_router_commands_json(router, ssh_params, commands))) q.put(list(exec_router_commands_json(router, ssh_params, commands)))
logging.debug("[<<EXIT] exec_router_commands_q: %r" % router) threading_logger.debug("[<<EXIT] exec_router_commands_q: %r" % router)
def get_router_details(router, params, q): def get_router_details(router, params, q):
logging.debug("get_router_details: %r" % router) threading_logger = logging.getLogger(THREADING_LOGGER_NAME)
threading_logger.debug("[ENTER>>]get_router_details: %r" % router)
commands = [ commands = [
'show configuration routing-instances IAS protocols bgp', 'show configuration routing-instances IAS protocols bgp',
...@@ -292,22 +307,27 @@ def get_router_details(router, params, q): ...@@ -292,22 +307,27 @@ def get_router_details(router, params, q):
commands_proc = Process(target=exec_router_commands_json_q, args=(router, params["ssh"], commands, commands_proc_queue)) commands_proc = Process(target=exec_router_commands_json_q, args=(router, params["ssh"], commands, commands_proc_queue))
commands_proc.start() commands_proc.start()
logging.debug("waiting for commands result: %r" % router) threading_logger.debug("waiting for commands result: %r" % router)
command_output = commands_proc_queue.get() command_output = commands_proc_queue.get()
assert len(command_output) == len(commands) assert len(command_output) == len(commands)
result = dict(zip(["bgp", "vrr", "interfaces"], command_output)) result = dict(zip(["bgp", "vrr", "interfaces"], command_output))
commands_proc.join() commands_proc.join()
logging.debug("... got commands result & joined: %r" % router) threading_logger.debug("... got commands result & joined: %r" % router)
logging.debug("waiting for snmp ifc results: %r" % router) threading_logger.debug("waiting for snmp ifc results: %r" % router)
result["snmp-interfaces"] = snmpifc_proc_queue.get() result["snmp-interfaces"] = snmpifc_proc_queue.get()
snmpifc_proc.join() snmpifc_proc.join()
logging.debug("... got snmp ifc result & joined: %r" % router) threading_logger.debug("... got snmp ifc result & joined: %r" % router)
q.put(result) q.put(result)
threading_logger.debug("[<<EXIT]get_router_details: %r" % router)
def load_network_details(config): def load_network_details(config):
threading_logger = logging.getLogger(THREADING_LOGGER_NAME)
with open("routers_community.conf") as f: with open("routers_community.conf") as f:
routers = list(load_routers(f)) routers = list(load_routers(f))
...@@ -320,10 +340,10 @@ def load_network_details(config): ...@@ -320,10 +340,10 @@ def load_network_details(config):
result = {} result = {}
for p in processes: for p in processes:
logging.debug("waiting for get_router_details result: %r" % p["router"]) threading_logger.debug("waiting for get_router_details result: %r" % p["router"])
result[p["router"]["hostname"]] = p["queue"].get() result[p["router"]["hostname"]] = p["queue"].get()
p["process"].join() p["process"].join()
logging.debug("got result and joined get_router_details proc: %r" % p["router"]) threading_logger.debug("got result and joined get_router_details proc: %r" % p["router"])
return result return result
...@@ -346,6 +366,10 @@ def cli(config): ...@@ -346,6 +366,10 @@ def cli(config):
if __name__ == "__main__": if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.WARNING)
logging.getLogger(SNMP_LOGGER_NAME).setLevel(logging.DEBUG)
logging.getLogger(THREADING_LOGGER_NAME).setLevel(logging.INFO)
logging.getLogger(JUNIPER_LOGGER_NAME).setLevel(logging.DEBUG)
logging.getLogger(DATABASE_LOGGER_NAME).setLevel(logging.DEBUG)
cli() cli()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment