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 search(request): query = {} text_query = request.args.get('q') if text_query: query['text_query'] = text_query hosts = db.get_hosts(**query) results = {"total": len(hosts), "results": hosts} 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)
app = Flask(__name__, static_url_path="/static") CORS(app, **config.get("apiserver.cors")) Compress(app) log = config.logger(__file__) log.info("################ API Server initializing #####################") app.config["SECRET_KEY"] = config.get("secure.http.session_secret.apiserver") app.config["JSONIFY_PRETTYPRINT_REGULAR"] = config.get("apiserver.pretty_json") database.initialize() # build a key that uniquely identifies specific mongo instance hosts_string = ";".join(sorted(database.get_hosts())) key = "db_init_" + md5(hosts_string.encode()).hexdigest() with distributed_lock(key, timeout=config.get("apiserver.db_init_timout", 120)): upgrade_monitoring = config.get( "apiserver.elastic.upgrade_monitoring.v16_migration_verification", True) try: empty_es = check_elastic_empty() except ElasticConnectionError as err: if not upgrade_monitoring: raise log.error(err) info.es_connection_error = True empty_db = check_mongo_empty()