async def get_networks(request): results = {"results": []} args = request.args filters = None if 'filter' in args: filters = args['filter'][0] networks = db.get_networks(ip_addr=filters) total = len(networks) if total > 0: for network in networks: network_id = network['id'] network['host_count'] = len(db.get_hosts(network_id=network_id)) results['results'].append(network) results['total'] = total return response.json(results)
async def get_host(request, host_id): results = {"host": {}} hosts = db.get_hosts(host_id=host_id) if len(hosts) > 0: host = hosts[0] host = count_ports(host) results['host'] = host results['host']['network'] = db.get_networks( network_id=host['network_id'])[0] return response.json(results)
async def get_network_hosts(request, network_id): results = {"network": {}, "results": []} hosts = db.get_hosts(network_id=network_id) for h in hosts: h = count_ports(h) results['results'].append(h) network = db.get_networks(network_id=network_id) results['network'] = network[0] results['total'] = len(hosts) return response.json(results)
async def get_metrics(request): hosts = db.get_hosts() ports_count = 0 for host in hosts: for service in host['services']: ports_count += 1 host_count = len(hosts) results = { "host_count": host_count, "network_count": len(db.get_networks()), "sensor_count": len(db.get_sensors()), "ports_count": ports_count } return response.json(results)
async def get_graphs(request, network_id=None): results = {"nodes": [], "edges": []} filters = {"network_id": network_id} for network in db.get_networks(**filters): gateway = {} hosts = db.get_hosts(network_id=network['id']) if len(hosts) > 0: for host in hosts: if is_gateway(host): gateway = host # Make host label label = "{}\n {}".format(last_digit(host['ip_addr']), host['dns_names']) # Make host id + network_id etc.. node = { "id": host['id'], "label": label, "group": host['network_id'] } results["nodes"].append(node) if gateway != {}: for host in hosts: if host != gateway: if host['network_id'] == gateway['network_id']: host_to_gw = edge(host['id'], gateway['id']) results['edges'].append(host_to_gw) return response.json(results)