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

Finished feature snmp-counter-helpers.

parents 9b0ab276 425a8a8c
No related branches found
No related tags found
No related merge requests found
......@@ -63,8 +63,8 @@ if __name__ == "__main__":
with open("config.json") as f:
params = config.load(f)
# update_network_details(params)
update_network_details(params)
network_info = load_network_details(params["redis"])
with open("./router-info.json", "w") as f:
f.write(json.dumps(network_info))
# network_info = load_network_details(params["redis"])
# with open("./router-info.json", "w") as f:
# f.write(json.dumps(network_info))
......@@ -66,14 +66,17 @@ def router_interfaces(hostname):
status=404,
mimetype="text/html")
def _interfaces(s):
for ifc in json.loads(s):
if 'v4InterfaceName' in ifc:
yield ifc['v4InterfaceName']
if 'v6InterfaceName' in ifc:
yield ifc['v6InterfaceName']
interfaces = list(_interfaces(ifc_data_string.decode('utf-8')))
def _interfaces(d):
for ii in d['interface-information']:
for ifc_list in ii['physical-interface'] + ii['logical-interface']:
for ifc in ifc_list:
yield {
'name': ifc['name'][0]['data'],
'description': ifc['description'][0]['data']
}
ifc_data = json.loads(ifc_data_string.decode('utf-8'))
interfaces = list(_interfaces(ifc_data))
if not interfaces:
return Response(
response="no interfaces found for '%s'" % hostname,
......@@ -85,6 +88,36 @@ def router_interfaces(hostname):
mimetype="application/json")
@routes.route("/snmp/<hostname>", methods=['GET', 'POST'])
@require_accepts_json
def snmp_ids(hostname):
redis_config = current_app.config["INVENTORY_PROVIDER_CONFIG"]["redis"]
r = redis.StrictRedis(
host=redis_config["hostname"],
port=redis_config["port"])
ifc_data_string = r.hget(hostname, 'snmp-interfaces')
if not ifc_data_string:
return Response(
response="no available info for '%s'" % hostname,
status=404,
mimetype="text/html")
def _ifc_name(ifc):
if 'v4InterfaceName' in ifc:
return ifc['v4InterfaceName']
if 'v6InterfaceName' in ifc:
return ifc['v6InterfaceName']
assert False, "sanity failure: no interface name found"
ifc_data = json.loads(ifc_data_string.decode('utf-8'))
result = [
{ 'index': i['index'], 'name': _ifc_name(i)}
for i in ifc_data]
return Response(
json.dumps(result),
mimetype="application/json")
@routes.route("/debug-dump/<hostname>", methods=['GET', 'POST'])
@require_accepts_json
def debug_dump_router_info(hostname):
......
......@@ -65,7 +65,9 @@ def walk(agent_hostname, community, base_oid):
# .resolveWithMib(mibViewController)
# for x in varBinds]
for oid, val in varBinds:
yield {"oid": "." + str(oid), "value": val.prettyPrint()}
result = {"oid": "." + str(oid), "value": val.prettyPrint()}
snmp_logger.debug(result)
yield result
def get_router_interfaces(hostname, community, config):
......@@ -89,7 +91,8 @@ def get_router_interfaces(hostname, community, config):
yield {
"v4Address": v4Address["value"],
"v4Mask": v4Mask["value"],
"v4InterfaceName": v4IfcNames[v4InterfaceOID["value"]]
"v4InterfaceName": v4IfcNames[v4InterfaceOID["value"]],
"index": v4InterfaceOID["value"]
}
v6IfcNames = {}
......@@ -109,5 +112,6 @@ def get_router_interfaces(hostname, community, config):
yield {
"v6Address": _v6address_oid2str(m.group(2)),
"v6Mask": v6AddressAndMask["value"],
"v6InterfaceName": v6IfcNames[m.group(1)]
"v6InterfaceName": v6IfcNames[m.group(1)],
"index": m.group(1)
}
import json
import logging
from celery import bootsteps, Task
import redis
......@@ -6,11 +7,19 @@ import redis
from inventory_provider.tasks.app import app
from inventory_provider import config
from inventory_provider import juniper, snmp
from inventory_provider import constants
logging.basicConfig(level=logging.WARNING)
logging.getLogger(constants.SNMP_LOGGER_NAME).setLevel(logging.INFO)
logging.getLogger(constants.TASK_LOGGER_NAME).setLevel(logging.INFO)
logging.getLogger(constants.JUNIPER_LOGGER_NAME).setLevel(logging.DEBUG)
logging.getLogger(constants.DATABASE_LOGGER_NAME).setLevel(logging.DEBUG)
class InventoryTask(Task):
config = None
logger = None
def __init__(self):
pass
......@@ -24,6 +33,8 @@ class InventoryTask(Task):
name=hostname,
key=key,
value=json.dumps(data))
InventoryTask.logger.debug(
"saved %s, key %s" % (hostname, key))
return "OK"
......@@ -31,6 +42,7 @@ class WorkerArgs(bootsteps.Step):
def __init__(self, worker, config_filename, **options):
with open(config_filename) as f:
InventoryTask.config = config.load(f)
InventoryTask.logger = logging.getLogger(constants.TASK_LOGGER_NAME)
def worker_args(parser):
......@@ -74,7 +86,7 @@ def juniper_refresh_interfaces(self, hostname):
def snmp_refresh_interfaces(self, hostname, community):
InventoryTask.save_key(
hostname,
"interfaces",
"snmp-interfaces",
list(snmp.get_router_interfaces(
hostname,
community,
......
This diff is collapsed.
......@@ -241,7 +241,15 @@ def test_router_interfaces(client_with_mocked_data):
interfaces_list_schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {"type": "string"}
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"description": {"type": "string"}
},
"required": ["name", "description"],
"additionalProperties": False
}
}
for router in _routers(client_with_mocked_data):
......@@ -254,6 +262,33 @@ def test_router_interfaces(client_with_mocked_data):
assert response # at least shouldn't be empty
def test_snmp_ids(client_with_mocked_data):
snmp_id_list_schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"index": {"type": "string"},
"name": {"type": "string"}
},
"required": ["index", "name"],
"additionalProperties": False
}
}
for hostname in _routers(client_with_mocked_data):
rv = client_with_mocked_data.post(
"/data/snmp/" + hostname,
headers=DEFAULT_REQUEST_HEADERS)
response = json.loads(rv.data.decode("utf-8"))
jsonschema.validate(response, snmp_id_list_schema)
assert response # at least shouldn't be empty
def test_router_bgp_route(client_with_mocked_data):
bgp_list_schema = {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment