def add(cls, pid, name): d = {"pid": pid, "name": name} r = corelib.auth_requests("POST", API_ADDR + "/dashboard/screen", data = d) if r.status_code != 200: raise Exception(r.text) j = r.json() return cls(*[j["id"], j["pid"], j["name"]])
def api_delete_counters(): ret = { "ok": False, "msg": "", } endpoints = request.form.getlist("endpoints[]") or [] counters = request.form.getlist("counters[]") or [] if len(endpoints) == 0 or len(counters) == 0: ret['msg'] = "no endpoint and counter" return json.dumps(ret) h = {"Content-type": "application/json"} d = { "endpoints": endpoints, "counters": counters, } r = corelib.auth_requests("DELETE", config.API_ADDR + "/graph/counter", headers=h, data=json.dumps(d)) if r.status_code != 200: abort(r.status_code, r.text) j = r.json() ret["ok"] = True ret["data"] = "%s counters affected" %j.get("affected_counter") return json.dumps(ret)
def get_users(cls, query_term, limit=20, page=1): users = [] if not query_term: query_term = '.' d = { "q": query_term, "limit": limit, "page": page, } h = {"Content-type":"application/json"} r = corelib.auth_requests("GET", "%s/user/users" \ %(config.API_ADDR,), params=d, headers=h) log.debug("%s:%s" %(r.status_code, r.text)) if r.status_code != 200: raise Exception("%s %s" %(r.status_code, r.text)) j = r.json() or [] for x in j: u = cls(x["id"], x["name"], x["cnname"], x["email"], x["phone"], x["im"], x["qq"], x["role"]) users.append(u) return users
def api_get_counters(): ret = { "ok": False, "msg": "", "data": [], } q = request.form.get("q") or "" limit = int(request.form.get("limit") or 50) page = int(request.form.get("page") or 1) eids = request.form.get("eids") or "" eids = eids and json.loads(eids) or [] if not (eids or q): ret['msg'] = "no endpoints or counter given" return json.dumps(ret) h = {"Content-type": "application/json"} r = corelib.auth_requests("GET", config.API_ADDR + "/graph/endpoint_counter?eid=%s&metricQuery=%s&limit=%d&page=%d" %(",".join(eids), q, limit, page), headers=h) if r.status_code != 200: abort(400, r.text) 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['data'] = sorted_values ret['ok'] = True return json.dumps(ret)
def get(cls, id): h = {"Content-type": "application/json"} r = corelib.auth_requests("GET", API_ADDR + "/dashboard/tmpgraph/%s" %(id,), headers=h) if r.status_code != 200: raise Exception(r.text) j = r.json() return j and cls(*[id, j["endpoints"], j["counters"]])
def get(cls, id): r = corelib.auth_requests("GET", API_ADDR + "/dashboard/screen/%s" %(id,)) if r.status_code != 200: raise Exception(r.text) j = r.json() if j: row = [j["id"], j["pid"], j["name"]] return cls(*row)
def logout_user(user_token): if not user_token: return r = corelib.auth_requests("GET", "%s/user/logout" %config.API_ADDR) if r.status_code != 200: raise Exception("%s:%s" %(r.status_code, r.text)) clear_user_cookie(session)
def get_by_name(cls, name): h = {"Content-type": "application/json"} r = corelib.auth_requests("GET", "%s/user/name/%s" %(config.API_ADDR, name), headers=h) log.debug("%s:%s" %(r.status_code, r.text)) if r.status_code != 200: raise Exception("%s %s" %(r.status_code, r.text)) j = r.json() return j and cls(j['id'], j['name'], j['cnname'], j['email'], j['phone'], j['im'], j['qq'], j['role'])
def admin_update_user_profile(cls, data={}): h = {"Content-type": "application/json"} r = corelib.auth_requests("PUT", "%s/admin/change_user_profile" %(config.API_ADDR,), \ data=json.dumps(data), headers=h) log.debug("%s:%s" %(r.status_code, r.text)) if r.status_code != 200: raise Exception("%s %s" %(r.status_code, r.text)) return r.text
def delete_team(cls, team_id): h = {"Content-type": "application/json"} r = corelib.auth_requests("DELETE", "%s/team/%s" \ %(config.API_ADDR, team_id), headers=h) log.debug("%s:%s" %(r.status_code, r.text)) if r.status_code != 200: raise Exception("%s %s" %(r.status_code, r.text)) return r.text
def get_team_users_by_name(cls, team_name): h = {"Content-type": "application/json"} r = corelib.auth_requests("GET", "%s/team/name/%s" \ %(config.API_ADDR, team_name), headers=h) log.debug("%s:%s" %(r.status_code, r.text)) if r.status_code != 200: raise Exception("%s %s" %(r.status_code, r.text)) return r.json()
def admin_delete_user(cls, user_id): h = {"Content-type":"application/json"} d = {"user_id": int(user_id)} r = corelib.auth_requests("DELETE", "%s/admin/delete_user" \ %(config.API_ADDR,), data=json.dumps(d), headers=h) log.debug("%s:%s" %(r.status_code, r.text)) if r.status_code != 200: raise Exception("%s %s" %(r.status_code, r.text)) return r.text
def in_teams(self, groups=[]): if not groups: return False r = corelib.auth_requests("GET", '%s/user/u/%s/in_teams?team_names=%s' \ % (config.API_ADDR, self.id, ','.join(groups))) log.debug("%s:%s" %(r.status_code, r.text)) if r.status_code != 200: return False j = r.json() return j["message"] == "true"
def admin_change_user_role(cls, user_id, admin): h = {"Content-type":"application/json"} d = {"admin": admin, "user_id": user_id} r = corelib.auth_requests("PUT", "%s/admin/change_user_role" \ %(config.API_ADDR,), data=json.dumps(d), headers=h) log.debug("%s:%s" %(r.status_code, r.text)) if r.status_code != 200: raise Exception("%s %s" %(r.status_code, r.text)) return r.text
def update(self, pid=None, name=None): d = {} if pid: d["pid"] = pid if name: d["name"] = name r = corelib.auth_requests("PUT", API_ADDR + "/dashboard/screen/%s" %self.id, data = d) if r.status_code != 200: raise Exception(r.text) return r.json()
def get_current_user_profile(user_token): if not user_token: return h = {"Content-type": "application/json"} r = corelib.auth_requests("GET", "%s/user/current" %config.API_ADDR, headers=h) if r.status_code != 200: return j = r.json() return User(j["id"], j["name"], j["cnname"], j["email"], j["phone"], j["im"], j["qq"], j["role"])
def search_in_endpoint_ids(cls, qs, endpoint_ids, limit=100): if not endpoint_ids: return [] eid_str = ",".join(endpoint_ids) r = corelib.auth_requests("GET", API_ADDR + "/graph/endpoint_counter?eid=%s&metricQuery=%s&limit=%d" %(eid_str, " ".join(qs), limit)) if r.status_code != 200: raise Exception(r.text) j = r.json() or [] return [cls(*[x["endpoint_id"], x["counter"], x["step"], x["type"]]) for x in j]
def add(cls, endpoints, counters): d = { "endpoints": endpoints, "counters": counters, } h = {'Content-type': 'application/json'} r = corelib.auth_requests("POST", API_ADDR + "/dashboard/tmpgraph", headers=h, data=json.dumps(d)) if r.status_code != 200: raise Exception(r.text) j = r.json() return j and j.get('id')
def create_team(cls, name, resume, user_ids=[]): h = {"Content-type": "application/json"} d = { "team_name": name, "resume": resume, "users": user_ids, } r = corelib.auth_requests("POST", "%s/team" %(config.API_ADDR,), \ data=json.dumps(d), headers=h) log.debug("%s:%s" %(r.status_code, r.text)) if r.status_code != 200: raise Exception("%s %s" %(r.status_code, r.text)) return r.text
def get_api_endpoints(q, tags, page=1, limit=100): if not q and not tags: raise Exception("no query params given") h = {"Content-type": "application/json"} r = corelib.auth_requests("GET", config.API_ADDR + "/graph/endpoint?q=%s&limit=%d&page=%d&tags=%s" %(q, limit, page, tags), headers=h) if r.status_code != 200: raise Exception(r.text) j = sorted(r.json(), key=lambda x:x["endpoint"]) return j
def create_user(cls, name, cnname, password, email, phone="", im="", qq=""): h = {"Content-type": "application/json"} d = { "name": name, "cnname": cnname, "password": password, "email": email, "phone": phone, "im": im, "qq": qq, } r = corelib.auth_requests("POST", "%s/user/create" %(config.API_ADDR,), \ data=json.dumps(d), headers=h) log.debug("%s:%s" %(r.status_code, r.text)) if r.status_code != 200: raise Exception("%s %s" %(r.status_code, r.text)) return r.json()
def admin_change_user_passwd(cls, user_id, password): h = {"Content-type": "application/json"} d = { "user_id": user_id, "password": password, } r = corelib.auth_requests("PUT", "%s/admin/change_user_passwd" %(config.API_ADDR,), \ data=json.dumps(d), headers=h) log.debug("%s:%s" %(r.status_code, r.text)) if r.status_code != 200: raise Exception("%s %s" %(r.status_code, r.text)) return r.text
def gets_by_endpoint(cls, endpoints, deadline=0): if not endpoints: return [] h = {"Content-type": "application/json"} qs = "deadline=%d" %deadline for x in endpoints: qs += "&endpoints=%s" %x r = corelib.auth_requests("GET", API_ADDR + "/graph/endpointobj?%s" %qs, headers=h) if r.status_code != 200: raise Exception(r.text) j = r.json() or [] return [cls(*[x["id"], x["endpoint"], x["ts"]]) for x in j]
def get_current_user_profile(user_token): if not user_token: return h = {"Content-type": "application/json"} r = corelib.auth_requests("GET", "%s/user/current" % config.API_ADDR, headers=h) if r.status_code != 200: return j = r.json() return User(j["id"], j["name"], j["cnname"], j["email"], j["phone"], j["im"], j["qq"], j["role"])
def change_user_passwd(cls, old_password, new_password): h = {"Content-type": "application/json"} d = { "old_password": old_password, "new_password": new_password, } r = corelib.auth_requests("PUT", "%s/user/cgpasswd" %(config.API_ADDR,), \ data=json.dumps(d), headers=h) log.debug("%s:%s" % (r.status_code, r.text)) if r.status_code != 200: raise Exception("%s %s" % (r.status_code, r.text)) return r.text
def gets_by_endpoint(cls, endpoints, deadline=0): if not endpoints: return [] h = {"Content-type": "application/json"} qs = "deadline=%d" % deadline for x in endpoints: qs += "&endpoints=%s" % x r = corelib.auth_requests("GET", API_ADDR + "/graph/endpointobj?%s" % qs, headers=h) if r.status_code != 200: raise Exception(r.text) j = r.json() or [] return [cls(*[x["id"], x["endpoint"], x["ts"]]) for x in j]
def graph_history(endpoints, counters, cf, start, end): #TODO:step params = { "start_time": start, "end_time": end, "consol_fun": cf, "hostnames": endpoints, "counters": counters, } h = {"Content-type": "application/json"} r = corelib.auth_requests("POST", "%s/graph/history" %API_ADDR, headers=h, data=json.dumps(params)) if r.status_code != 200: raise Exception("%s : %s" %(r.status_code, r.text)) return r.json()
def add(cls, endpoints, counters): d = { "endpoints": endpoints, "counters": counters, } h = {'Content-type': 'application/json'} r = corelib.auth_requests("POST", API_ADDR + "/dashboard/tmpgraph", headers=h, data=json.dumps(d)) if r.status_code != 200: raise Exception(r.text) j = r.json() return j and j.get('id')
def api_metric_query(): q = request.args.get('query', '').strip() limit = int(request.args.get('limit', '10')) h = {"Content-type": "application/json"} r = corelib.auth_requests("GET", "%s/metric/default_list" \ %(config.API_ADDR,), headers=h) if r.status_code != 200: log.error("%s:%s" %(r.status_code, r.text)) return [] metrics = r.json() or [] metrics = [q,] + metrics return jsonify(data=[{'name': name} for name in metrics])
def gets_by_screen_id(cls, screen_id): h = {"Content-type": "application/json"} r = corelib.auth_requests("GET", API_ADDR + "/dashboard/graphs/screen/%s" % (screen_id, ), headers=h) if r.status_code != 200: raise Exception(r.text) j = r.json() return [ cls(*[ x["graph_id"], x["title"], x["endpoints"], x["counters"], x["screen_id"], x["timespan"], x["graph_type"], x["method"], x["position"] ]) for x in j ]
def create_team(cls, name, resume, user_ids=[]): h = {"Content-type": "application/json"} d = { "team_name": name, "resume": resume, "users": user_ids, } r = corelib.auth_requests("POST", "%s/team" % (config.API_ADDR, ), data=json.dumps(d), headers=h) log.debug("%s:%s" % (r.status_code, r.text)) if r.status_code != 200: raise Exception("%s %s" % (r.status_code, r.text)) return r.text
def api_metric_query(): q = request.args.get('query', '').strip() limit = int(request.args.get('limit', '10')) h = {"Content-type": "application/json"} r = corelib.auth_requests("GET", "%s/metric/default_list" \ %(config.API_ADDR,), headers=h) if r.status_code != 200: log.error("%s:%s" %(r.status_code, r.text)) return [] metrics = r.json() or [] matched_metrics = [x for x in metrics if q in x] ret_data = [q,] + matched_metrics[:limit] return jsonify(data=[{'name': name} for name in ret_data])
def get_api_endpoints(q, tags, page=1, limit=100): if not q and not tags: raise Exception("no query params given") h = {"Content-type": "application/json"} r = corelib.auth_requests("GET", config.API_ADDR + "/graph/endpoint?q=%s&limit=%d&page=%d&tags=%s" % (q, limit, page, tags), headers=h) if r.status_code != 200: raise Exception(r.text) j = sorted(r.json(), key=lambda x: x["endpoint"]) return j
def search_in_endpoint_ids(cls, qs, endpoint_ids, limit=100): if not endpoint_ids: return [] eid_str = ",".join(endpoint_ids) r = corelib.auth_requests( "GET", API_ADDR + "/graph/endpoint_counter?eid=%s&metricQuery=%s&limit=%d" % (eid_str, " ".join(qs), limit)) if r.status_code != 200: raise Exception(r.text) j = r.json() or [] return [ cls(*[x["endpoint_id"], x["counter"], x["step"], x["type"]]) for x in j ]
def api_metric_query(): q = request.args.get('query', '').strip() limit = int(request.args.get('limit', '10')) h = {"Content-type": "application/json"} r = corelib.auth_requests("GET", "%s/metric/default_list?q=%s&limit=%d" \ %(config.API_ADDR, q, limit), headers=h) if r.status_code != 200: log.error("%s:%s" % (r.status_code, r.text)) return jsonify(data=[{'name': q}]) metrics = r.json() or [] matched_metrics = [x for x in metrics if q in x] ret_data = [ q, ] + matched_metrics[:limit] return jsonify(data=[{'name': name} for name in ret_data])
def update(self, title=None, hosts=None, counters=None, screen_id=None, timespan=None, graph_type=None, method=None, position=None): title = self.title if title is None else title hosts = self.hosts if hosts is None else hosts counters = self.counters if counters is None else counters screen_id = screen_id or self.screen_id timespan = timespan or self.timespan graph_type = graph_type or self.graph_type method = method if method is not None else self.method position = position or self.position d = { "screen_id": int(screen_id), "title": title, "endpoints": hosts, "counters": counters, "timespan": int(timespan), "graph_type": graph_type, "method": method, "position": int(position), "falcon_tags": "", } h = {"Content-type": "application/json"} r = corelib.auth_requests("PUT", API_ADDR + "/dashboard/graph/%s" % (self.id, ), data=json.dumps(d), headers=h) if r.status_code != 200: raise Exception(r.text) j = r.json() graph_id = j and j.get("id") return graph_id and DashboardGraph.get(graph_id)
def graph_history(endpoints, counters, cf, start, end): #TODO:step params = { "start_time": start, "end_time": end, "consol_fun": cf, "hostnames": endpoints, "counters": counters, } h = {"Content-type": "application/json"} #log.debug("graph_history_params_%s"%str(params)) print today_date_str(),"call_graph_history",endpoints,counters,cf, start, end r = corelib.auth_requests("POST", "%s/graph/history" %API_ADDR, headers=h, data=json.dumps(params)) print today_date_str(), "call_graph_history_r",r.request if r.status_code != 200: raise Exception("%s : %s" %(r.status_code, r.text)) print r.content print r.status_code print r.json() return r.json()
def api_get_counters(): ret = { "ok": False, "msg": "", "data": [], } q = request.form.get("q") or "" limit = int(request.form.get("limit") or 50) page = int(request.form.get("page") or 1) eids = request.form.get("eids") or "" eids = eids and json.loads(eids) or [] if not (eids or q): ret['msg'] = "no endpoints or counter given" return json.dumps(ret) h = {"Content-type": "application/json"} r = corelib.auth_requests( "GET", config.API_ADDR + "/graph/endpoint_counter?eid=%s&metricQuery=%s&limit=%d&page=%d" % (",".join(eids), q, limit, page), headers=h) if r.status_code != 200: abort(400, r.text) 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['data'] = sorted_values ret['ok'] = True return json.dumps(ret)
def get_teams(cls, query_term, limit=20, page=1): if not query_term: query_term = "." d = { "q": query_term, "limit": limit, "page": page, } h = {"Content-type": "application/json"} r = corelib.auth_requests("GET", "%s/team" \ %(config.API_ADDR,), params=d, headers=h) log.debug("%s:%s" %(r.status_code, r.text)) if r.status_code != 200: raise Exception("%s %s" %(r.status_code, r.text)) teams = [] for j in r.json(): users = [User(x["id"], x["name"], x["cnname"], x["email"], x["phone"], x["im"], x["qq"], x["role"]) for x in j['users']] t = Team(j["team"]["id"], j["team"]["name"], j["team"]["resume"], j["team"]["creator"], j['creator_name'], users) teams.append(t) return teams
def gets_by_pid(cls, pid): r = corelib.auth_requests("GET", API_ADDR + "/dashboard/screens/pid/%s" %(pid,)) if r.status_code != 200: raise Exception(r.text) j = r.json() or [] return [cls(*[x["id"], x["pid"], x["name"]]) for x in j]
def gets_all(cls, limit=500): r = corelib.auth_requests("GET", API_ADDR + "/dashboard/screens?limit=%s" %(limit,)) if r.status_code != 200: raise Exception(r.text) j = r.json() or [] return [cls(*[x["id"], x["pid"], x["name"]]) for x in j]
def remove(cls, id): h = {"Content-type": "application/json"} r = corelib.auth_requests("DELETE", API_ADDR + "/dashboard/screen/%s" %(id,), headers=h) if r.status_code != 200: raise Exception(r.text) return r.json()