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

add config input validation

parent 10e6c5cc
No related branches found
No related tags found
No related merge requests found
...@@ -3,10 +3,33 @@ import json ...@@ -3,10 +3,33 @@ import json
import re import re
import click import click
import jsonschema
import mysql.connector import mysql.connector
from pysnmp.hlapi import nextCmd, SnmpEngine, CommunityData, \ from pysnmp.hlapi import nextCmd, SnmpEngine, CommunityData, \
UdpTransportTarget, ContextData, ObjectType, ObjectIdentity 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): def walk(agent_hostname, community, base_oid):
""" """
...@@ -33,7 +56,7 @@ 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 # 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')
print("walking %s: %s" % (agent_hostname, base_oid)) # print("walking %s: %s" % (agent_hostname, base_oid))
for (engineErrorIndication, for (engineErrorIndication,
pduErrorIndication, pduErrorIndication,
errorIndex, errorIndex,
...@@ -68,7 +91,9 @@ def _validate_config(ctx, param, value): ...@@ -68,7 +91,9 @@ def _validate_config(ctx, param, value):
:param value: :param value:
:return: :return:
""" """
return json.loads(value.read()) config = json.loads(value.read())
jsonschema.validate(config, CONFIG_SCHEMA)
return config
def load_oids(config_file): def load_oids(config_file):
...@@ -109,7 +134,7 @@ def connection(alarmsdb): ...@@ -109,7 +134,7 @@ def connection(alarmsdb):
user=alarmsdb["username"], user=alarmsdb["username"],
passwd=alarmsdb["password"], passwd=alarmsdb["password"],
db=alarmsdb["dbname"]) db=alarmsdb["dbname"])
print(cx) # print(cx)
yield cx yield cx
finally: finally:
if cx: if cx:
...@@ -129,7 +154,7 @@ def cursor(cnx): ...@@ -129,7 +154,7 @@ def cursor(cnx):
def _db_test(db, router): def _db_test(db, router):
with cursor(db) as crs: with cursor(db) as crs:
print(router) # print(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:
...@@ -150,9 +175,9 @@ def get_router_interfaces(router): ...@@ -150,9 +175,9 @@ def get_router_interfaces(router):
details = {} details = {}
for name, oid in oid_map.items(): for name, oid in oid_map.items():
details[name] = walk(router["hostname"], router["community"], oid) details[name] = walk(router["hostname"], router["community"], oid)
print(name) # print(name)
details[name] = list(details[name]) details[name] = list(details[name])
print(details[name]) # print(details[name])
v4IfcNames = {} v4IfcNames = {}
for v4IfcName in details["v4InterfaceName"]: for v4IfcName in details["v4InterfaceName"]:
...@@ -207,9 +232,9 @@ def cli(config): ...@@ -207,9 +232,9 @@ def cli(config):
with connection(config["alarms-db"]) as c: with connection(config["alarms-db"]) as c:
_db_test(c, r) _db_test(c, r)
print(r["hostname"])
for i in get_router_interfaces(r): for i in get_router_interfaces(r):
print(i) print("\t%r" % i)
break
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -12,6 +12,8 @@ setup( ...@@ -12,6 +12,8 @@ setup(
install_requires=[ install_requires=[
'click', 'click',
'mysql-connector', 'mysql-connector',
'pysnmp' 'pysnmp',
'jsonschema',
'pika'
] ]
) )
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment