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__": ...@@ -63,8 +63,8 @@ if __name__ == "__main__":
with open("config.json") as f: with open("config.json") as f:
params = config.load(f) params = config.load(f)
# update_network_details(params) update_network_details(params)
network_info = load_network_details(params["redis"]) # network_info = load_network_details(params["redis"])
with open("./router-info.json", "w") as f: # with open("./router-info.json", "w") as f:
f.write(json.dumps(network_info)) # f.write(json.dumps(network_info))
...@@ -66,14 +66,17 @@ def router_interfaces(hostname): ...@@ -66,14 +66,17 @@ def router_interfaces(hostname):
status=404, status=404,
mimetype="text/html") mimetype="text/html")
def _interfaces(s): def _interfaces(d):
for ifc in json.loads(s): for ii in d['interface-information']:
if 'v4InterfaceName' in ifc: for ifc_list in ii['physical-interface'] + ii['logical-interface']:
yield ifc['v4InterfaceName'] for ifc in ifc_list:
if 'v6InterfaceName' in ifc: yield {
yield ifc['v6InterfaceName'] 'name': ifc['name'][0]['data'],
'description': ifc['description'][0]['data']
interfaces = list(_interfaces(ifc_data_string.decode('utf-8'))) }
ifc_data = json.loads(ifc_data_string.decode('utf-8'))
interfaces = list(_interfaces(ifc_data))
if not interfaces: if not interfaces:
return Response( return Response(
response="no interfaces found for '%s'" % hostname, response="no interfaces found for '%s'" % hostname,
...@@ -85,6 +88,36 @@ def router_interfaces(hostname): ...@@ -85,6 +88,36 @@ def router_interfaces(hostname):
mimetype="application/json") 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']) @routes.route("/debug-dump/<hostname>", methods=['GET', 'POST'])
@require_accepts_json @require_accepts_json
def debug_dump_router_info(hostname): def debug_dump_router_info(hostname):
......
...@@ -65,7 +65,9 @@ def walk(agent_hostname, community, base_oid): ...@@ -65,7 +65,9 @@ def walk(agent_hostname, community, base_oid):
# .resolveWithMib(mibViewController) # .resolveWithMib(mibViewController)
# for x in varBinds] # for x in varBinds]
for oid, val 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): def get_router_interfaces(hostname, community, config):
...@@ -89,7 +91,8 @@ def get_router_interfaces(hostname, community, config): ...@@ -89,7 +91,8 @@ def get_router_interfaces(hostname, community, config):
yield { yield {
"v4Address": v4Address["value"], "v4Address": v4Address["value"],
"v4Mask": v4Mask["value"], "v4Mask": v4Mask["value"],
"v4InterfaceName": v4IfcNames[v4InterfaceOID["value"]] "v4InterfaceName": v4IfcNames[v4InterfaceOID["value"]],
"index": v4InterfaceOID["value"]
} }
v6IfcNames = {} v6IfcNames = {}
...@@ -109,5 +112,6 @@ def get_router_interfaces(hostname, community, config): ...@@ -109,5 +112,6 @@ def get_router_interfaces(hostname, community, config):
yield { yield {
"v6Address": _v6address_oid2str(m.group(2)), "v6Address": _v6address_oid2str(m.group(2)),
"v6Mask": v6AddressAndMask["value"], "v6Mask": v6AddressAndMask["value"],
"v6InterfaceName": v6IfcNames[m.group(1)] "v6InterfaceName": v6IfcNames[m.group(1)],
"index": m.group(1)
} }
import json import json
import logging
from celery import bootsteps, Task from celery import bootsteps, Task
import redis import redis
...@@ -6,11 +7,19 @@ import redis ...@@ -6,11 +7,19 @@ import redis
from inventory_provider.tasks.app import app from inventory_provider.tasks.app import app
from inventory_provider import config from inventory_provider import config
from inventory_provider import juniper, snmp 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): class InventoryTask(Task):
config = None config = None
logger = None
def __init__(self): def __init__(self):
pass pass
...@@ -24,6 +33,8 @@ class InventoryTask(Task): ...@@ -24,6 +33,8 @@ class InventoryTask(Task):
name=hostname, name=hostname,
key=key, key=key,
value=json.dumps(data)) value=json.dumps(data))
InventoryTask.logger.debug(
"saved %s, key %s" % (hostname, key))
return "OK" return "OK"
...@@ -31,6 +42,7 @@ class WorkerArgs(bootsteps.Step): ...@@ -31,6 +42,7 @@ class WorkerArgs(bootsteps.Step):
def __init__(self, worker, config_filename, **options): def __init__(self, worker, config_filename, **options):
with open(config_filename) as f: with open(config_filename) as f:
InventoryTask.config = config.load(f) InventoryTask.config = config.load(f)
InventoryTask.logger = logging.getLogger(constants.TASK_LOGGER_NAME)
def worker_args(parser): def worker_args(parser):
...@@ -74,7 +86,7 @@ def juniper_refresh_interfaces(self, hostname): ...@@ -74,7 +86,7 @@ def juniper_refresh_interfaces(self, hostname):
def snmp_refresh_interfaces(self, hostname, community): def snmp_refresh_interfaces(self, hostname, community):
InventoryTask.save_key( InventoryTask.save_key(
hostname, hostname,
"interfaces", "snmp-interfaces",
list(snmp.get_router_interfaces( list(snmp.get_router_interfaces(
hostname, hostname,
community, community,
......
This diff is collapsed.
...@@ -241,7 +241,15 @@ def test_router_interfaces(client_with_mocked_data): ...@@ -241,7 +241,15 @@ def test_router_interfaces(client_with_mocked_data):
interfaces_list_schema = { interfaces_list_schema = {
"$schema": "http://json-schema.org/draft-07/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
"type": "array", "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): for router in _routers(client_with_mocked_data):
...@@ -254,6 +262,33 @@ def test_router_interfaces(client_with_mocked_data): ...@@ -254,6 +262,33 @@ def test_router_interfaces(client_with_mocked_data):
assert response # at least shouldn't be empty 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): def test_router_bgp_route(client_with_mocked_data):
bgp_list_schema = { 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