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

add config input validation

parent 10e6c5cc
Branches
Tags
No related merge requests found
......@@ -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__":
......
......@@ -12,6 +12,8 @@ setup(
install_requires=[
'click',
'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