def host_unbind_get(): host_id = request.args.get('host_id', '').strip() if not host_id: return jsonify(msg='host_id is blank') group_id = request.args.get('group_id', '').strip() if not group_id: return jsonify(msg='group_id is blank') GroupHost.unbind(int(group_id), host_id) return jsonify(msg='')
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_add_post(): group_id = request.form['group_id'] if not group_id: return jsonify(msg='no group_id given') group_id = int(group_id) group = HostGroup.read('id = %s', [group_id]) if not group: return jsonify(msg='no such group') hosts = request.form['hosts'].strip() if not hosts: return jsonify(msg='hosts is blank') host_arr = hosts.splitlines() safe_host_arr = [h for h in host_arr if h] if not safe_host_arr: return jsonify(msg='hosts is blank') success = [] failure = [] for h in safe_host_arr: msg = GroupHost.bind(group_id, h) if not msg: success.append('%s<br>' % h) else: failure.append('%s %s<br>' % (h, msg)) data = '<div class="alert alert-danger" role="alert">failure:<hr>' + ''.join( failure ) + '</div><div class="alert alert-success" role="alert">success:<hr>' + ''.join( success) + '</div>' return jsonify(msg='', data=data)
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 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 host_remove_post(): group_id = int(request.form['grp_id'].strip()) host_ids = request.form['host_ids'].strip() GroupHost.unbind(group_id, host_ids) return jsonify(msg='')