def api_get_counters(): ret = { "ok": False, "msg": "", "data": [], } endpoints = request.form.get("endpoints") or "" endpoints = endpoints and json.loads(endpoints) q = request.form.get("q") or "" limit = int(request.form.get("limit") or 100) nethelp = request.form.get("nethelp") or "" if not (endpoints or q): ret['msg'] = "no endpoints or counter given" return json.dumps(ret) endpoint_objs = Endpoint.gets_by_endpoint(endpoints) endpoint_ids = [x.id for x in endpoint_objs] group_ids = [] if not endpoint_ids: group_objs = Group.gets_by_group(endpoints) group_ids = [x.id for x in group_objs] grouphost_objs = GroupHost.search(group_ids) host_ids = [x.hostId for x in grouphost_objs] host_objs = Host.search(host_ids) host_names = [x.name for x in host_objs] endpoint_objs = Endpoint.gets_by_endpoint(host_names) endpoint_ids = [x.id for x in endpoint_objs] if not endpoint_ids: ret['msg'] = "no endpoints in graph" return json.dumps(ret) qs = q.split() if nethelp == "true": ecs = EndpointCounter.search_in_endpoint_ids(qs, endpoint_ids, limit=100) elif len(group_ids) > 0: limit = 5000 if len(qs) > 0: ecs = EndpointCounter.search_in_endpoint_ids(qs, endpoint_ids, limit=limit) else: ecs = EndpointCounter.gets_by_endpoint_ids(endpoint_ids, limit=limit) if not ecs: ret["msg"] = "no counters in graph" return json.dumps(ret) counters_map = {} for x in ecs: counters_map[x.counter] = [x.counter, x.type_, x.step] sorted_counters = sorted(counters_map.keys()) sorted_values = [counters_map[x] for x in sorted_counters] ret['data'] = sorted_values ret['ok'] = True return json.dumps(ret)
def api_get_counters(): ret = { "ok": False, "msg": "", "data": [], "tags":[], } endpoints = request.form.get("endpoints") or "" endpoints = endpoints and json.loads(endpoints) q = request.form.get("q") or "" limit = int(request.form.get("limit") or 100) if not (endpoints or q): ret['msg'] = "no endpoints or counter given" return json.dumps(ret) endpoint_objs = Endpoint.gets_by_endpoint(endpoints) endpoint_ids = [x.id for x in endpoint_objs] if not endpoint_ids: ret['msg'] = "no endpoints in graph" return json.dumps(ret) if q: qs = q.split() ecs = EndpointCounter.search_in_endpoint_ids(qs, endpoint_ids, limit=limit) else: ecs = EndpointCounter.gets_by_endpoint_ids(endpoint_ids, limit=limit) if not ecs: ret["msg"] = "no counters in graph" return json.dumps(ret) counters_map = {} tags = ["notag"] for x in ecs: if x.counter.find("=") > 0: end = x.counter.rfind("/", 0,x.counter.find("=")) metric = x.counter[0:end] tag = x.counter[end+1:] tags.append(tag) else: tag = "" tags.append(tag) metric = x.counter if counters_map.has_key(metric): if tag not in counters_map[metric]: counters_map[metric] += [tag] else: counters_map[metric] = [metric,x.type_,x.step,tag] sorted_counters = sorted(counters_map.keys()) sorted_values = [counters_map[x] for x in sorted_counters] ret['data'] = sorted_values ret['ok'] = True ret['tags'] = filter(None,list(set(tags))) return json.dumps(ret)
def generate_graph_urls(graph, start, end): counters = graph.counters or [] if not counters: return [] endpoint_list = graph.hosts or [] if not endpoint_list: return [] endpoint_objs = Endpoint.gets_by_endpoint(endpoint_list) if not endpoint_objs: return [] endpoint_ids = [x.id for x in endpoint_objs] counters = [] for c in graph.counters: if c.find("metric=") == -1: counters.append(c) else: metric = "" tags = [] qs = [] c = c.strip() for q in c.split(): q = q.strip() if q.startswith("metric="): metric = q.replace("metric=", "", 1) qs.append(metric) else: qs.append(q) tags.append(q) counter_objs = EndpointCounter.search_in_endpoint_ids( qs, endpoint_ids[:], limit=100) if not counter_objs: continue for co in counter_objs: if not re.search('^%s(/|$)' % metric, co.counter): continue matched = True for tag in tags: if not re.search('(/|,)%s(,|$)' % tag, co.counter): matched = False break if not matched: continue counters.append(co.counter) if not counters: return [] counters = sorted(list(set(counters))) return _generate_graph_urls(graph, counters, endpoint_list, start, end)
def generate_graph_urls(graph, start, end): counters = graph.counters or [] if not counters: return [] endpoint_list = graph.hosts or [] if not endpoint_list: return [] endpoint_objs = Endpoint.gets_by_endpoint(endpoint_list) if not endpoint_objs: return [] endpoint_ids = [x.id for x in endpoint_objs] counters = [] for c in graph.counters: if c.find("metric=") == -1: counters.append(c) else: metric="" tags = [] qs = [] c = c.strip() for q in c.split(): q = q.strip() if q.startswith("metric="): metric = q.replace("metric=", "", 1) qs.append(metric) else: qs.append(q) tags.append(q) counter_objs = EndpointCounter.search_in_endpoint_ids(qs, endpoint_ids[:], limit=100) if not counter_objs: continue for co in counter_objs: if not re.search('^%s(/|$)' %metric, co.counter): continue matched = True for tag in tags: if not re.search('(/|,)%s(,|$)' %tag, co.counter): matched = False break if not matched: continue counters.append(co.counter) if not counters: return [] counters = sorted(list(set(counters))) return _generate_graph_urls(graph, counters, endpoint_list, start, end)
def api_get_counters(): ret = { "ok": False, "msg": "", "data": [], } endpoints = request.form.get("endpoints") or "" endpoints = endpoints and json.loads(endpoints) q = request.form.get("q") or "" limit = int(request.form.get("limit") or 100) if not (endpoints or q): ret['msg'] = "no endpoints or counter given" return json.dumps(ret) endpoint_objs = Endpoint.gets_by_endpoint(endpoints) endpoint_ids = [x.id for x in endpoint_objs] if not endpoint_ids: ret['msg'] = "no endpoints in graph" return json.dumps(ret) if q: qs = q.split() ecs = EndpointCounter.search_in_endpoint_ids(qs, endpoint_ids, limit=limit) else: ecs = EndpointCounter.gets_by_endpoint_ids(endpoint_ids, limit=limit) if not ecs: ret["msg"] = "no counters in graph" return json.dumps(ret) counters_map = {} for x in ecs: counters_map[x.counter] = [x.counter, x.type_, x.step] sorted_counters = sorted(counters_map.keys()) sorted_values = [counters_map[x] for x in sorted_counters] ret['data'] = sorted_values ret['ok'] = True return json.dumps(ret)
def api_get_counters(): RefreshCounterDesp() ret = { "ok": False, "msg": "", "data": [], } endpoints = request.form.get("endpoints") or "" endpoints = endpoints and json.loads(endpoints) q = request.form.get("q") or "" limit = int(request.form.get("limit") or 100) if not (endpoints or q): ret['msg'] = "no endpoints or counter given" return json.dumps(ret) endpoint_objs = Endpoint.gets_by_endpoint(endpoints) endpoint_ids = [x.id for x in endpoint_objs] if not endpoint_ids: ret['msg'] = "no endpoints in graph" return json.dumps(ret) if q: qs = q.split() ecs = EndpointCounter.search_in_endpoint_ids(qs, endpoint_ids, limit=limit) else: ecs = EndpointCounter.gets_by_endpoint_ids(endpoint_ids, limit=limit) if not ecs: ret["msg"] = "no counters in graph" return json.dumps(ret) counters_map = {} for x in ecs: counters_map[x.counter] = [x.counter, x.type_, x.step, x.desp] sorted_counters = sorted(counters_map.keys()) sorted_values = [counters_map[x] for x in sorted_counters] ret['data'] = sorted_values ret['ok'] = True return json.dumps(ret)
def api_get_counters(): ret = { "ok": False, "msg": "", "data": [], } data = request.json or "" type = data['type'] endpoints = [] filter = "" containers = [] if not type: ret['msg'] = "no type given" return json.dumps(ret) if type == "node": endpoints = data['data'] filter = data['filter'] elif type == "pod": filter = data['filter'] for pod in data['data']: for container in pod['containers']: node = container['hostname'] containers.append(container['containerId']) if len(endpoints) == 0: endpoints.append(node) else: p = 0 for endpoint in endpoints: if endpoint == node: break else: p = p + 1 if p != len(endpoints): endpoints.append(node) elif type == "container": filter = data['filter'] for container in data['data']: node = container['hostname'] containers.append(container['containerId']) if len(endpoints) == 0: endpoints.append(node) else: p = 0 for endpoint in endpoints: if endpoint == node: break else: p = p + 1 if p != len(endpoints): endpoints.append(node) endpoint_objs = Endpoint.gets_by_endpoint(endpoints) endpoint_ids = [x.id for x in endpoint_objs] if not endpoint_ids: ret['msg'] = "no endpoints in graph" return json.dumps(ret) if filter: filters = filter.split() ecs = EndpointCounter.search_in_endpoint_ids(filters, endpoint_ids) else: ecs = EndpointCounter.gets_by_endpoint_ids(endpoint_ids) if not ecs: ret["msg"] = "no counters in graph" return json.dumps(ret) counters_map = {} for x in ecs: if type == 'node': if 'id=' not in x.counter: counters_map[x.counter] = [x.counter, x.type_, x.step] elif type == 'pod' or type == 'container': if 'id=' in x.counter: if x.counter.split('/')[1].split('=')[1] in containers: counters_map[x.counter] = [x.counter, x.type_, x.step] sorted_counters = sorted(counters_map.keys()) sorted_values = [counters_map[x] for x in sorted_counters] ret['data'] = sorted_values ret['ok'] = True return json.dumps(ret)