diff --git a/inventory_provider/router_interfaces.py b/inventory_provider/router_interfaces.py index 0384fc7c9799b2b357d7fffcc1d590a904e45524..21d6eb11dd1ffe4514b737ef447312b5d9e5ba3b 100644 --- a/inventory_provider/router_interfaces.py +++ b/inventory_provider/router_interfaces.py @@ -3,10 +3,33 @@ import json import re import click +import jsonschema import mysql.connector from pysnmp.hlapi import nextCmd, SnmpEngine, CommunityData, \ UdpTransportTarget, ContextData, ObjectType, ObjectIdentity +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"} + }, + "required": ["alarms-db", "oid_list.conf", "routers_community.conf"], + "additionalProperties": False +} + def walk(agent_hostname, community, base_oid): """ @@ -33,7 +56,7 @@ def walk(agent_hostname, community, base_oid): # Pre-load MIB modules we expect to work with mibBuilder.loadModules('SNMPv2-MIB', 'SNMP-COMMUNITY-MIB', 'RFC1213-MIB') - print("walking %s: %s" % (agent_hostname, base_oid)) + # print("walking %s: %s" % (agent_hostname, base_oid)) for (engineErrorIndication, pduErrorIndication, errorIndex, @@ -68,7 +91,9 @@ def _validate_config(ctx, param, value): :param value: :return: """ - return json.loads(value.read()) + config = json.loads(value.read()) + jsonschema.validate(config, CONFIG_SCHEMA) + return config def load_oids(config_file): @@ -109,7 +134,7 @@ def connection(alarmsdb): user=alarmsdb["username"], passwd=alarmsdb["password"], db=alarmsdb["dbname"]) - print(cx) + # print(cx) yield cx finally: if cx: @@ -129,7 +154,7 @@ def cursor(cnx): def _db_test(db, router): with cursor(db) as crs: - print(router) + # print(router) query = "SELECT absid FROM routers WHERE hostname = %s" crs.execute(query, (router['hostname'],)) for (absid,) in crs: @@ -150,9 +175,9 @@ def get_router_interfaces(router): details = {} for name, oid in oid_map.items(): details[name] = walk(router["hostname"], router["community"], oid) - print(name) + # print(name) details[name] = list(details[name]) - print(details[name]) + # print(details[name]) v4IfcNames = {} for v4IfcName in details["v4InterfaceName"]: @@ -207,9 +232,9 @@ def cli(config): with connection(config["alarms-db"]) as c: _db_test(c, r) + print(r["hostname"]) for i in get_router_interfaces(r): - print(i) - break + print("\t%r" % i) if __name__ == "__main__": diff --git a/requirements.txt b/requirements.txt index 0a3a0947653dc3002d9805b85f74fde6ebf89386..e38b2fd17eb4fb56c50aafac45cf27c580a1f6b2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ click mysql-connector pysnmp +jsonschema +pika diff --git a/setup.py b/setup.py index d5038a02b3299b23fd4ebbd14b1e3ff1568b1816..1bdb77dd8f0c7fe38fdaa529f58fbdd89935711a 100644 --- a/setup.py +++ b/setup.py @@ -12,6 +12,8 @@ setup( install_requires=[ 'click', 'mysql-connector', - 'pysnmp' + 'pysnmp', + 'jsonschema', + 'pika' ] )