def overview(request, **kwargs): my_auth = MyBasicAuthentication() is_login = my_auth.is_authenticated(request, **kwargs) if is_login: with_sg = check_with_sourcegroup(is_login) cf_per = check_with_permission(is_login) my_var = MyVariable() data_path = my_var.get_var('path', 'data_path') config_path = data_path + "custom" real_file = config_path + '/apps.json' try: with open(real_file, 'r') as f: config = json.load(f) apps = config["apps"] except Exception, e: print e apps = [] config = {} table_file = config_path + '/tables.json' try: with open(table_file, 'r') as f: config = json.load(f) tables = config["tables"] except Exception, e: print e tables = [] config = {}
def csv_download(self, request, **kwargs): self.method_check(request, allowed=['get']) data = [] dummy_data = {} my_auth = MyBasicAuthentication() es_check = my_auth.is_authenticated(request, **kwargs) if es_check: file_name = kwargs['fid'] root_path = os.getcwd() my_var = MyVariable() data_path = my_var.get_var('path', 'data_path') tmp_path = data_path + "yottaweb_tmp/" + es_check["d"] + "/" + es_check["u"] + "/" filename = tmp_path + file_name wrapper = FileWrapper(file(filename)) resp = HttpResponse(wrapper, content_type='text/plain') # resp = self.create_response(request, wrapper) resp['Content-Length'] = os.path.getsize(filename) resp['Content-Encoding'] = 'utf-8' resp['Content-Disposition'] = 'attachment;filename=%s' % file_name else: data = err_data.build_error({}, "auth error!") data["location"] = "/auth/login/" dummy_data = data bundle = self.build_bundle(obj=dummy_data, data=dummy_data, request=request) response_data = bundle resp = self.create_response(request, response_data) return resp
def steps(self, request, **kwargs): self.method_check(request, allowed=['get']) sid = kwargs.get('sid') dummy_data = {} my_auth = MyBasicAuthentication() es_check = my_auth.is_authenticated(request, **kwargs) if es_check: info = {} try: my_var = MyVariable() data_path = my_var.get_var('path', 'data_path') config_path = data_path + "custom" real_file = config_path + '/apps.json' with open(real_file, 'r') as f: config = json.load(f) apps = config["apps"] for item in apps: if item["id"] == sid: info = item except Exception, e: print e info = {} dummy_data["status"] = "1" dummy_data["name"] = info.get("name", "") dummy_data["auto_search"] = info.get("auto_search", "no") dummy_data["sumSection"] = info.get("sumSection", {}) dummy_data["steps"] = info.get("content", [])
def table_info(self, request, **kwargs): self.method_check(request, allowed=['get']) sid = kwargs.get('sid') dummy_data = {} my_auth = MyBasicAuthentication() es_check = my_auth.is_authenticated(request, **kwargs) if es_check: info = {} try: my_var = MyVariable() data_path = my_var.get_var('path', 'data_path') config_path = data_path + "custom" real_file = config_path + '/tables.json' with open(real_file, 'r') as f: config = json.load(f) tables = config["tables"] for item in tables: if item["id"] == sid: info = item except Exception, e: print e info = {} dummy_data["status"] = "1" dummy_data["table"] = info.get("content", {})
def tables_delete(self, request, **kwargs): self.method_check(request, allowed=['post']) dummy_data = {} id = kwargs.get("aid", "") my_auth = MyBasicAuthentication() es_check = my_auth.is_authenticated(request, **kwargs) if es_check: my_var = MyVariable() data_path = my_var.get_var('path', 'data_path') config_path = data_path + "custom" real_file = config_path + '/tables.json' try: with open(real_file, 'r') as f: config = json.load(f) f.close() tables = config.get("tables", []) except Exception, e: print e tables = [] cur_id = -1 for i in range(len(tables)): if tables[i]["id"] == id: cur_id = i try: if cur_id > -1: del(tables[cur_id]) except Exception, e: print e
def report_files(self, request, **kwargs): self.method_check(request, allowed=['get']) dummy_data = {} my_auth = MyBasicAuthentication() es_check = my_auth.is_authenticated(request, **kwargs) if es_check: domain = es_check["d"] owner = str(es_check["i"]) + "|" + str(es_check["u"]) + "|" + str( es_check["t"]) my_var = MyVariable() data_path = my_var.get_var('path', 'report_path') tmp_path = data_path + "yottaweb_reports/" + es_check[ "t"] + "/" + str(es_check["i"]) + "/" tmp_files = os.listdir(tmp_path) file_list = [] for one_file in tmp_files: file_list.append({ 'name': one_file, 'owner': es_check['u'], 'created_time': one_file.split('_')[-1] }) dummy_data["list"] = file_list dummy_data["status"] = "1" else: data = err_data.build_error({}, "auth error!") data["location"] = "/auth/login/" dummy_data = data bundle = self.build_bundle(obj=dummy_data, data=dummy_data, request=request) response_data = bundle resp = self.create_response(request, response_data) return resp
def table(request, cid, **kwargs): my_auth = MyBasicAuthentication() is_login = my_auth.is_authenticated(request, **kwargs) if is_login: try: my_var = MyVariable() data_path = my_var.get_var('path', 'data_path') config_path = data_path + "custom" real_file = config_path + '/tables.json' info = {} with open(real_file, 'r') as f: config = json.load(f) tables = config["tables"] for item in tables: if item["id"] == cid: info = item except Exception, e: print e info = {} page_data = { "active": "applications", "subnav": "overview", "user": is_login["u"], "email": is_login["e"], "token": is_login["t"], "userid": is_login["i"], "table_info": info, "role": is_login["r"] } return render(request, 'application/custom/table.html', {"page_data": json.dumps(page_data)})
def dashboard_group_login(request, username, password, sign, **kwargs): # cur_time = str(time.time()).split(".")[0] if not hashlib.md5(username + password).hexdigest() == sign: raise PermissionDenied auto_login_permit = MyVariable().get_var( "custom", "dashboard_login") if MyVariable().get_var( "custom", "dashboard_login") else "no" if not auto_login_permit == "yes": raise PermissionDenied referer = request.META.get('HTTP_REFERER') host = urlparse(referer).netloc domain = host.split('.')[0] logger = logging.getLogger("yottaweb.audit") param = {"domain": domain, "name": username, "passwd": password} # print password # user info for yottaD req = BackendRequest.login(param) es_check = req['result'] token = "" if es_check: token = req.get('token', "") request.session['user_name'] = username request.session['user_pwd'] = password request.session['user_tkn'] = token request.session['user_id'] = req.get('owner_id', "") to_log = { "timestamp": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "action": "login", "user_name": username, "user_id": req.get('owner_id', ""), "domain": domain, "result": "success" } cookie_string = hashlib.md5(username + ',' + domain + ',' + token).hexdigest() request.session['user_yottac'] = cookie_string request.session.set_expiry(259200) logger.info(json.dumps(to_log)) return HttpResponseRedirect('/dashboard/') else: #0: server error, 1:password or user wrong to_log = { "timestamp": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "action": "login", "user_name": username, "user_id": req.get('owner_id', ""), "domain": domain, "result": "error", "msg": req['error'] } logger.info(json.dumps(to_log)) return HttpResponseRedirect('/auth/login/')
def dashboards_update(self, request, **kwargs): self.method_check(request, allowed=['post', 'get']) dummy_data = {} id = kwargs.get("did", "") my_auth = MyBasicAuthentication() es_check = my_auth.is_authenticated(request, **kwargs) if es_check: my_var = MyVariable() data_path = my_var.get_var('path', 'data_path') config_path = data_path + "custom" real_file = config_path + '/dashboard.json' if request.method == "POST": post_data = json.loads(request.POST.get("data")) try: with open(real_file, 'r') as f: config = json.load(f) f.close() if not config: config = [] except Exception, e: print e config = [] cur_id = -1 for i in range(len(config)): if config[i]["custom_config_id"] == id: cur_id = i if cur_id > -1: config[cur_id] = post_data try: with open(real_file, 'w') as f: json.dump(config, f) f.close() dummy_data["status"] = "1" dummy_data["url"] = "/system/custom/dashboard/" except Exception, e: print e dummy_data["status"] = "0" dummy_data["msg"] = "update custom dashboard error!" else: dummy_data["status"] = "0" dummy_data["msg"] = "Can't find dashboard by id"+id+" !"
def tables_update(self, request, **kwargs): self.method_check(request, allowed=['post', 'get']) dummy_data = {} id = kwargs.get("aid", "") my_auth = MyBasicAuthentication() es_check = my_auth.is_authenticated(request, **kwargs) if es_check: my_var = MyVariable() data_path = my_var.get_var('path', 'data_path') config_path = data_path + "custom" real_file = config_path + '/tables.json' if request.method == "POST": post_data = json.loads(request.POST.get("data")) try: with open(real_file, 'r') as f: config = json.load(f) f.close() tables = config.get("tables", []) except Exception, e: print e tables = [] cur_id = -1 for i in range(len(tables)): if tables[i]["id"] == id: cur_id = i if cur_id > -1: tables[cur_id] = post_data app_content = { "tables": tables } try: with open(real_file, 'w') as f: json.dump(app_content, f) f.close() dummy_data["status"] = "1" dummy_data["url"] = "/system/custom/table/" except Exception, e: print e dummy_data["status"] = "0" dummy_data["msg"] = "update custom app error!"
def app_list(self, request, **kwargs): self.method_check(request, allowed=['get']) dummy_data = {} my_auth = MyBasicAuthentication() es_check = my_auth.is_authenticated(request, **kwargs) if es_check: my_var = MyVariable() data_path = my_var.get_var('path', 'data_path') config_path = data_path + "custom" real_file = config_path + '/apps.json' traces = [] tables = [] try: with open(real_file, 'r') as f: config = json.load(f) _tmp_traces = config["apps"] for item in _tmp_traces: print item traces.append({ "name": item.get("name", ""), "link": "/app/custom/" + item.get("id", "") + "/", "permission": "true" }) except Exception, e: print e table_file = config_path + '/tables.json' try: with open(table_file, 'r') as f: config = json.load(f) _tmp_tables = config["tables"] for item in _tmp_tables: tables.append({ "name": item.get("name", ""), "link": "/app/table/" + item.get("id", "") + "/", "permission": "true" }) except Exception, e: print e tables = []
def dashboards(self, request, **kwargs): self.method_check(request, allowed=['get']) my_auth = MyBasicAuthentication() es_check = my_auth.is_authenticated(request, **kwargs) dummy_data = {} if es_check: my_var = MyVariable() data_path = my_var.get_var('path', 'data_path') config_path = data_path + "custom" real_file = config_path + '/dashboard.json' try: with open(real_file, 'r') as f: config = json.load(f) f.close() except Exception, e: print e config = [] dummy_data["status"] = "1" dummy_data["dashboards"] = config
def tables(self, request, **kwargs): self.method_check(request, allowed=['get']) my_auth = MyBasicAuthentication() es_check = my_auth.is_authenticated(request, **kwargs) dummy_data = {} if es_check: my_var = MyVariable() data_path = my_var.get_var('path', 'data_path') config_path = data_path + "custom" try: real_path = config_path + '/tables.json' with open(real_path, 'r') as f: config = json.load(f) tables = config.get("tables", []) except Exception, e: print e tables = [] dummy_data["status"] = "1" dummy_data["tables"] = tables
def tables_new(self, request, **kwargs): self.method_check(request, allowed=['post']) dummy_data = {} my_auth = MyBasicAuthentication() es_check = my_auth.is_authenticated(request, **kwargs) if es_check: post_data = json.loads(request.POST.get("data")) my_var = MyVariable() data_path = my_var.get_var('path', 'data_path') config_path = data_path + "custom" real_file = config_path + '/tables.json' try: if not os.path.exists(config_path): os.makedirs(config_path) with open(real_file, 'rw') as f: config = json.load(f) f.close() tables = config.get("tables", []) except Exception, e: print e tables = [] tables.append(post_data) app_content = { "tables": tables } try: with open(real_file, 'w') as f: json.dump(app_content, f) f.close() dummy_data["status"] = "1" dummy_data["url"] = "/system/custom/table/" except Exception, e: print e dummy_data["status"] = "0" dummy_data["msg"] = "create custom table error!"
def report_files_download_directly(self, request, **kwargs): self.method_check(request, allowed=['get']) dummy_data = {} fid_encoded = kwargs['fid_encoded'] if fid_encoded: my_utils = MyUtils() _decrypt_path = my_utils.b64Decrypt(fid_encoded.encode("utf-8")) re_logger.info("report_files_download_directly decrypted_path: %s", _decrypt_path) file_path = base64.urlsafe_b64decode( _decrypt_path) if _decrypt_path else "" re_logger.info("report_files_download_directly: %s", file_path) my_var = MyVariable() data_path = my_var.get_var('path', 'report_path') tmp_path = data_path + "yottaweb_reports/" + file_path if file_path: file_name = file_path.split("/")[-1] wrapper = FileWrapper(file(file_path)) resp = HttpResponse(wrapper, content_type='text/plain') # resp = self.create_response(request, wrapper) resp['Content-Length'] = os.path.getsize(file_path) resp['Content-Encoding'] = 'utf-8' resp[ 'Content-Disposition'] = 'attachment;filename=%s' % file_name return resp else: data = err_data.build_error({}, "Download report file error!") dummy_data = data else: data = err_data.build_error({}, "auth error!") data["location"] = "/auth/login/" dummy_data = data bundle = self.build_bundle(obj=dummy_data, data=dummy_data, request=request) response_data = bundle resp = self.create_response(request, response_data) return resp
def dashboards_delete(self, request, **kwargs): self.method_check(request, allowed=['post']) dummy_data = {} id = kwargs.get("did", "") my_auth = MyBasicAuthentication() es_check = my_auth.is_authenticated(request, **kwargs) if es_check: my_var = MyVariable() data_path = my_var.get_var('path', 'data_path') config_path = data_path + "custom" real_file = config_path + '/dashboard.json' try: with open(real_file, 'r') as f: config = json.load(f) f.close() if not config: config = [] except Exception, e: print e config = [] cur_id = -1 for i in range(len(config)): if config[i]["custom_config_id"] == id: cur_id = i if cur_id > -1: del(config[cur_id]) try: with open(real_file, 'w') as f: json.dump(config, f) f.close() dummy_data["status"] = "1" dummy_data["dashboards"] = config except Exception, e: print e dummy_data["status"] = "0" dummy_data["msg"] = "delete custom dashboard error!"
def _build_csv(self, type, data, domain, user): if type == "sourcegroup": heads = ["sourcegroup", "appname", "hostname"] else: heads = ["appname", "sourcegroup", "hostname"] rows = [] for item in data: if item.get("infos", {}): for (k, v) in item["infos"].items(): if v: for hostname in v: rows.append([item["name"], k, hostname]) else: rows.append([item["name"], k, ""]) # todo else: rows.append([item["name"], "", ""]) root_path = os.getcwd() my_var = MyVariable() data_path = my_var.get_var('path', 'data_path') tmp_path = data_path + "yottaweb_tmp/" + domain + "/" + user + "/" if not os.path.exists(tmp_path): os.makedirs(tmp_path) # remove old file first name = 'log_resource_' + domain + "_" + user.encode("utf-8") file_name = str(name) + ".csv" file_path = tmp_path + file_name if os.path.isfile(file_path): os.remove(file_path) f = open(file_path, "wb+") # write bom to resolve unreadable chinese problem f.write("\xEF\xBB\xBF") writer = csv.writer(f) writer.writerow(heads) for line in rows: writer.writerow(line) return file_name
def dashboards_new(self, request, **kwargs): self.method_check(request, allowed=['post']) dummy_data = {} my_auth = MyBasicAuthentication() es_check = my_auth.is_authenticated(request, **kwargs) if es_check: post_data = json.loads(request.POST.get("data")) my_var = MyVariable() data_path = my_var.get_var('path', 'data_path') config_path = data_path + "custom" real_file = config_path + '/dashboard.json' try: if not os.path.exists(config_path): os.makedirs(config_path) with open(real_file, 'r') as f: config = json.load(f) f.close() if not config: config = [] except Exception, e: print e config = [] config.append(post_data) try: with open(real_file, 'w') as f: json.dump(config, f) f.close() dummy_data["status"] = "1" dummy_data["url"] = "/system/custom/dashboard/" except Exception, e: print e dummy_data["status"] = "0" dummy_data["msg"] = "create custom dashboard error!"
def report_files_download(self, request, **kwargs): self.method_check(request, allowed=['get']) dummy_data = {} my_auth = MyBasicAuthentication() es_check = my_auth.is_authenticated(request, **kwargs) if es_check: domain = es_check["d"] my_var = MyVariable() data_path = my_var.get_var('path', 'report_path') tmp_path = data_path + "yottaweb_reports/" + es_check[ "t"] + "/" + str(es_check["i"]) + "/" file_name = kwargs['fid'] file_path = tmp_path + kwargs['fid'] if file_name: wrapper = FileWrapper(file(file_path)) resp = HttpResponse(wrapper, content_type='text/plain') # resp = self.create_response(request, wrapper) resp['Content-Length'] = os.path.getsize(file_path) resp['Content-Encoding'] = 'utf-8' resp[ 'Content-Disposition'] = 'attachment;filename=%s' % file_name return resp else: data = err_data.build_error({}, "Delete report file error!") dummy_data = data else: data = err_data.build_error({}, "auth error!") data["location"] = "/auth/login/" dummy_data = data bundle = self.build_bundle(obj=dummy_data, data=dummy_data, request=request) response_data = bundle resp = self.create_response(request, response_data) return resp
es_url = cf.get('frontend', 'frontend_url') except Exception, e: print e es_url = ES_URL try: cf.read(conf_file) additional_params = cf.items('additional_params') except Exception, e: print e additional_params = [] URL = es_url log = logging.getLogger('django.request') local_language = MyVariable().get_var('i18n', 'local') if MyVariable().get_var('i18n', 'local') else "zhCN" try: local = "".join(local_language.split("_")) local = local[0:2]+'_'+local[2:4] path = os.path.dirname(os.path.realpath(__file__)) real_path = path + "/../../static/i18n/" + local.lower() + ".json" with open(real_path, 'r') as content_file: content = content_file.read() # content = content.split(";")[0].split("=")[1] obj = json.loads(content) error_code_obj = obj['ERRORCODE'] error_code_spl_obj = obj['SPLERRORCODE'] except Exception, e: print e error_code_obj = {}