def chart(): endpoints = request.form.getlist("endpoints[]") or [] counters = request.form.getlist("counters[]") or [] graph_type = request.form.get("graph_type") or GRAPH_TYPE_HOST endpoints = sorted(set(endpoints)) group_objs = Group.gets_by_group(endpoints) if len(group_objs) > 0: 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) endpoint_names = [x.name for x in host_objs] id_ = TmpGraph.add(endpoint_names, counters) else: id_ = TmpGraph.add(endpoints, counters) ret = { "ok": False, "id": id_, "params": { "graph_type": graph_type, }, } if id_: ret['ok'] = True return json.dumps(ret)
def host_maintain_post(): begin = int(request.form['begin'].strip()) end = int(request.form['end'].strip()) host_ids = request.form['host_ids'].strip() if begin <= 0 or end <= 0: return jsonify(msg='begin or end is invalid') return jsonify(msg=Host.maintain(begin, end, host_ids))
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 host_groups_get(host_id): host_id = int(host_id) h = Host.read('id = %s', params=[host_id]) if not h: return jsonify(msg='no such host') group_ids = GroupHost.group_ids(h.id) groups = [HostGroup.read('id = %s', [group_id]) for group_id in group_ids] return render_template('host/groups.html', groups=groups, host=h)
def api_group_hosts_json(grp_name): group = HostGroup.read(where='id = %s', params=[grp_name]) if not group: group = HostGroup.read(where='grp_name = %s', params=[grp_name]) if not group: return jsonify(msg='no such group %s' % grp_name) vs, _ = Host.query(1, 10000000, '', '0', group.id) names = [v.hostname for v in vs] return jsonify(msg='', data=names)
def group_hosts_export(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) vs, _ = Host.query(1, 10000000, '', '0', group_id) names = [v.hostname for v in vs] response = make_response('\n'.join(names)) response.headers["content-type"] = "text/plain" return response
def host_templates_get(host_id): host_id = int(host_id) h = Host.read('id = %s', params=[host_id]) if not h: return jsonify(msg='no such host') group_ids = GroupHost.group_ids(h.id) templates = GrpTpl.tpl_set(group_ids) for v in templates: v.parent = Template.get(v.parent_id) return render_template('host/templates.html', **locals())
def api_host_variables(hostname): host = Host.read(where='hostname = %s', params=[hostname]) if not host: return jsonify(msg='no such host %s' % hostname) groups_host = GroupHost.select_vs(where='host_id = %s' % host.id) if not groups_host: return jsonify(msg='the host %s has no group attribute' % hostname) data = [] for gh in groups_host: variables = Variable.select_vs(where='grp_id = %s' % gh.grp_id) for v in variables: data.append({'key': v.name, 'value': v.content, 'note': v.note}) return jsonify(msg='ok', data=data)
def graph_list_get(group_id): group_id = int(group_id) ret = {"ok": False, "msg": "", "endpoints": [], "counters": []} return render_template("graph/counter.html", data=ret) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: ret['msg'] = 'no such group %s' % group_id return render_template("graph/counter.html", data=ret) vs, _ = Host.query(1, 10000000, '', '0', group_id) names = [v.hostname for v in vs] h = {"Content-type": "application/json"} r = corelib.auth_requests( "GET", config.API_ADDR + "/graph/endpoints?endpoints=%s" % ','.join(names), headers=h) if r.status_code != 200: abort(400, r.text) ret['msg'] = r.text return render_template("graph/counter.html", data=ret) endpoint_ids = json.loads(r.text) # return json.dumps(endpoint_ids) if len(endpoint_ids) == 0: ret['msg'] = 'No any endpoint selected' return render_template("graph/counter.html", data=ret) r = corelib.auth_requests( "GET", config.API_ADDR + "/graph/endpoint_counter?eid=%s" % ",".join('%s' % e['id'] for e in endpoint_ids), headers=h) if r.status_code != 200: ret['msg'] = r.text return render_template("graph/counter.html", data=ret) j = r.json() counters_map = {} for x in j: 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['ok'] = True ret['endpoints'] = endpoint_ids ret['counters'] = sorted_values return render_template("graph/counter.html", data=ret)
def group_hosts_list(group_id): g.xbox = request.args.get('xbox', '') group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) page = int(request.args.get('p', 1)) limit = int(request.args.get('limit', 10)) query = request.args.get('q', '') maintaining = request.args.get('maintaining', '0') vs, total = Host.query(page, limit, query, maintaining, group_id) return render_template('host/index.html', data={ 'vs': vs, 'total': total, 'query': query, 'limit': limit, 'page': page, 'maintaining': maintaining, 'group': group, })
def host_reset_post(): host_ids = request.form['host_ids'].strip() return jsonify(msg=Host.no_maintain(host_ids))