def login(request): response_data = {} try: if request.method == "POST": api = lg_operation_api(operapihost) username = request.POST['username'] password = request.POST['password'] token = api.get_jwt_token(username, password) if token: response_data['expire'] = expire response_data['token'] = token response_data['message'] = "login success" response_data['status_code'] = 200 else: response_data['expire'] = 0 response_data['token'] = '' response_data[ 'message'] = "please login with correct username and password" response_data['status_code'] = 400 elif request.method == "GET": return render_to_response('registration/Login.html') else: response_data['token'] = '' response_data['message'] = "not support this url path" response_data['status_code'] = 403 return HttpResponse(json.dumps(response_data), content_type="application/json") except Exception as e: message = "Exception : " + str(e) response_data['message'] = message response_data['status_code'] = 408 return HttpResponse(json.dumps(response_data), content_type="application/json")
def index_v2(request): try: username = request.GET['username'] token = request.GET['token'] api = lg_operation_api(operapihost) userpofile = api.get_userprofile(token, username) customer = api.get_customerinfo(token, userpofile.customer_id) allregionlist = api.get_regions(token) monitorlist = api.get_instances(token, customer.instance) tmpregionlist = list( set([instance.region_name for instance in monitorlist])) regionlist = list(region for region in allregionlist if region.ch_name in tmpregionlist) uid = uuid.uuid1() context_dict = { 'username': username, 'ws_ip': WS_IP, 'ws_port': WS_PORT, 'uid': uid, 'regionlist': regionlist, 'monitorlist': monitorlist } response = render_to_response('lookglass/index_v2.html', context_dict) return response except Exception as e: message = "Exception : " + str(e) return HttpResponse(message)
def lg_task(request): try: username = request.GET['username'] token = request.GET['token'] if token: user_id = username api = lg_operation_api(operapihost) userpofile = api.get_userprofile(token, username) customer = api.get_customerinfo(token, userpofile.customer_id) user_id = userpofile.user_id tasklist = Task.objects.filter(uid=user_id, enabled=True) taskcount = len( tasklist.filter(enabled=True).exclude( status__name='Completed')) context_dict = { 'user_id': user_id, 'task_list': tasklist, 'task_count': taskcount } response = render_to_response('lookglass/task/index.html', context_dict) return response else: return ("No auth to access") except Exception as e: message = "Exception : " + str(e) return HttpResponse(message)
def lg_history(request): try: username = request.GET['username'] token = request.GET['token'] if token: user_id = username api = lg_operation_api(operapihost) userpofile = api.get_userprofile(token, username) customer = api.get_customerinfo(token, userpofile.customer_id) allregionlist = api.get_regions(token) monitorlist = api.get_instances(token, customer.instance) tmpregionlist = list( set([instance.region_name for instance in monitorlist])) regionlist = list(region for region in allregionlist if region.ch_name in tmpregionlist) context_dict = { 'user_id': user_id, 'regionlist': regionlist, 'monitorlist': monitorlist } response = render_to_response('lookglass/history/index.html', context_dict) return response else: return ("No auth to access") except Exception as e: message = "Exception : " + str(e) return HttpResponse(message)
def looking_glass_v2(request): try: response_data = {} if request.method == "GET": username = request.GET['username'] token = request.GET['token'] if token: group_name = 'lg_%s' % request.GET['uid'] #lg_name = request.GET['lg_name'] lg_id = request.GET['lg_id'] lg_command = request.GET['lg_command'] lg_host = request.GET['lg_host'] lg_origin = request.GET['lg_origin'] curl_domain = request.GET['curl_domain'] curl_port = request.GET['curl_port'] curl_ip = request.GET['curl_ip'] tcp_port = request.GET['tcp_port'] nslookup_type = request.GET['nslookup_type'] nslookup_dns = request.GET['nslookup_dns'] msg_type = request.GET['msg_type'] msg_times = request.GET['msg_times'] idlist = lg_id.split(";") api = lg_operation_api(operapihost) userpofile = api.get_userprofile(token, username) user_id = userpofile.user_id customer_name = userpofile.customer_name command_site = "%s;%s;%s;%s;%s;%s;%s;%s;%s" % ( lg_command, lg_host, lg_origin, curl_domain, curl_port, curl_ip, tcp_port, nslookup_type, nslookup_dns) index = 1 for key, value in enumerate(idlist): monitor_id = value.split("-")[0] monitor = api.get_instance(token, monitor_id) monitor_ip = monitor.host_ip monitor_name = monitor.ch_name region_name = monitor.region_name test_param = [ "tools", user_id, customer_name, monitor_id, group_name, region_name, monitor_name, monitor_ip, command_site, msg_type, msg_times ] WORKERS.send_task('worker.worker.task_add', test_param) response_data['message'] = 'Success' response_data['status_code'] = 200 return HttpResponse(json.dumps(response_data), content_type="application/json") except Exception as e: message = "Exception : " + str(e) response_data['message'] = message response_data['status_code'] = 408 return HttpResponse(json.dumps(response_data), content_type="application/json")
def task_lg(self,*test_data): log = Log("periodiclog","periodiclog") log.logger.info("[{}][task_lg]Start checking...{}".format(self.request.id,test_data)) task_id = int(test_data[0]) user_id = test_data[1] customer_name = test_data[2] nodes = test_data[3] times = int(test_data[4]) command_site = test_data[5] command = command_site.split(";")[0] host = command_site.split(";")[1].strip() origin = command_site.split(";")[2] domain = command_site.split(";")[3] port = command_site.split(";")[4] domain_ip = command_site.split(";")[5] tcp_port = command_site.split(";")[6] dns_type = command_site.split(";")[7] dns_server = command_site.split(";")[8] node_list = nodes.split(';') api = lg_operation_api(operapihost) token = api.get_jwt_token(admin,adminpw) task = Task.objects.get(pk = task_id) taskdetaillist = TaskDetail.objects.filter(task = task ,enabled =True) log.logger.info("[task_lg]taskdetaillist length : {} , times: {}".format(len(taskdetaillist),times)) if len(taskdetaillist) >= times: status = TaskStatus.objects.get(name = 'Completed') task.status = status task.save() periodtask = PeriodicTask.objects.get(pk = task.periodictask_id) periodtask.enabled = False periodtask.save() else: status = TaskStatus.objects.get(name = 'Running') task.status = status task.save() taskdetail = TaskDetail.objects.create(task = task, task_request_id= self.request.id) if taskdetail: for node in node_list: tag_index = node instance = api.get_instance(token,int(node)) region_name = instance.region_name monitor_name = instance.ch_name ip = instance.host_ip status_code,res_data,testresult = get_lg_node_result(region_name,monitor_name,ip,command,host,origin,domain,port,domain_ip,tcp_port,dns_type,dns_server,tag_index) if res_data is not None and status_code!=408: history_task_id = "{}-{}".format(task.name,task.id) history_id = create_lghistory(history_task_id,user_id,customer_name,monitor_name,command,host,testresult,res_data) log.logger.info("[task_lg]create lg history : {}".format(history_id)) if history_id!=0: taskreport = TaskReport.objects.create(detail = taskdetail,history_id = history_id,instaince_id =int(node)) log.logger.info("[task_lg]create taskreport : {}".format(taskreport))
def check_task(): log = Log("periodiclog","periodiclog") log.logger.info("Start checking...") api = lg_operation_api(operapihost) token = api.get_jwt_token(admin,adminpw) htconfiglist = api.get_HTInstances(token) deploystatus = 3 deployinstancelist = list([item for item in htconfiglist if item.status == deploystatus]) log.logger.info("[re-deploy]count:{}".format(len(deployinstancelist))) for item in deployinstancelist: config = api.get_HTConfig(token,item.heartbeat) customer = api.get_customerinfo(token,config.cid) protocol = api.get_protocol(token,config.hb_protocol) instance = api.get_instance(token,item.instance) htdeploy = HeatbeatDeploy(customer.id,customer.name,protocol,config,instance) customer_name = customer.name yml_file_name = config.hb_yml_name protocol_name = protocol.name instance_name = instance.ch_name log.logger.info("[re-deploy]info:{},{},{},{}-{}".format(customer_name,yml_file_name,protocol_name,instance_name,item.enabled)) if item.enabled == True: ## create yml file status_code,result = htdeploy.Create_yml_file() log.logger.info("[New-{}]{}:{},{},{}:{}-{}".format(item.heartbeat,customer_name,yml_file_name,protocol_name,instance_name,result,status_code)) if status_code == 200 or status_code == 408: status_code, result = htdeploy.Check_yml_file() log.logger.info("[New-{}]{}:{},{},{}:{}-{}".format(item.heartbeat,customer_name,yml_file_name,protocol_name,instance_name,result,status_code)) if result == True: ### set status to ready configinstance = Update_ConfigInstance(token,item.id,1,True) log.logger.info("[configinstance] {} status is {}, enable is {} ".format(item.id,item.status,item.enabled)) log.logger.info("new deploy success:{}".format(item.id)) else: log.logger.info("new deploy fail:{}".format(item.id)) elif item.enabled == False: result = htdeploy.Delete_yml_file() if result != None: status_code, result = htdeploy.Check_yml_file() log.logger.info("[Delete-{}]{}:{},{},{}:{}-{}".format(item.heartbeat,customer_name,yml_file_name,protocol_name,instance_name,result,status_code)) if status_code == 403: ### set status to ready and enable is false configinstance = Update_ConfigInstance(token,item.id,1,False) log.logger.info("[configinstance] {} status is {}, enable is {} ".format(item.id,item.status,item.enabled)) log.logger.info("delete deploy success:{}".format(item.id)) else: log.logger.info("delete deploy fail:{}".format(item.id))
def lg_history_detail(request): try: username = request.GET['username'] token = request.GET['token'] if token: api = lg_operation_api(operapihost) userpofile = api.get_userprofile(token, username) monitorlist = api.get_instances(token, None) res_data = [] count = 100 command = request.GET['command'] nodes_id = request.GET['nodes'] user_id = userpofile.user_id customer_name = userpofile.customer_name if nodes_id: nodes_id_list = list( [int(node_id) for node_id in nodes_id.split(",")]) monitor_value = ','.join( list(item.ch_name for item in monitorlist if item.id in nodes_id_list)) else: monitor_value = '' history = lg_history_api(customer_name, user_id, count) response_data = history.search_lg_history(command, monitor_value) data_list = response_data.json()["result"] for item in data_list: red_item = item del red_item["header"] del red_item["body"] del red_item["result"] tranfer_time = datetime.strptime( red_item["published_date"], '%a, %d %b %Y %H:%M:%S %Z') + timedelta(hours=8) red_item["published_date"] = tranfer_time.strftime( "%Y/%m/%d %H:%M:%S") red_item["status_code"] = red_item["status_code"].replace( "<br/>", "\n") red_item["total_time"] = red_item["total_time"].replace( "<br/>", "\n") res_data.append(red_item) context_dict = {'detaillist': res_data, 'supportfilter': 'False'} response = render_to_response( 'lookglass/history/history_detail.html', context_dict) return response else: return ("No auth to access") except Exception as e: message = "Exception : " + str(e) return HttpResponse(message)
def looking_glass(request): try: username = request.GET['username'] token = request.GET['token'] if token: lg_id = int(request.GET['lg_id']) lg_command = request.GET['lg_command'] lg_host = request.GET['lg_host'] lg_origin = request.GET['lg_origin'] curl_domain = request.GET['curl_domain'] curl_port = request.GET['curl_port'] curl_ip = request.GET['curl_ip'] tcp_port = request.GET['tcp_port'] nslookup_type = request.GET['nslookup_type'] nslookup_dns = request.GET['nslookup_dns'] api = lg_operation_api(operapihost) userpofile = api.get_userprofile(token, username) node = api.get_instance(token, lg_id) ## get user related information user_id = userpofile.user_id customer_name = userpofile.customer_name # send command to lg lgnodeapi = lg_nodes_api(node.region_name, node.ch_name, node.host_ip) response_data = lgnodeapi.get_nodes_api(lg_command, lg_host, lg_origin, curl_domain, curl_port, curl_ip, tcp_port, nslookup_type, nslookup_dns) #create lg history if response_data: create_history(userpofile.customer_name, userpofile.user_id, node.ch_name, lg_host, tcp_port, lg_command, response_data) return HttpResponse(response_data) else: return HttpResponse('please use the get for this url:' + request.method) except Exception as e: message = "Exception : " + str(e) return HttpResponse(message)
def lg_history_content(request): try: username = request.GET['username'] token = request.GET['token'] if token: api = lg_operation_api(operapihost) userpofile = api.get_userprofile(token, username) id = request.GET['id'] count = 100 history = lg_history_api(userpofile.customer_name, userpofile.user_id, count) response_data = history.get_lg_history(id) return HttpResponse(json.dumps(response_data.json()["result"][0]), content_type="application/json") except Exception as e: message = "Exception : " + str(e) return HttpResponse(message)
def lg_task_history(request): try: username = request.GET['username'] token = request.GET['token'] taskid = request.GET['taskid'] if token: api = lg_operation_api(operapihost) userpofile = api.get_userprofile(token, username) user_id = userpofile.user_id customer_name = userpofile.customer_name monitorlist = api.get_instances(token, None) res_data = [] count = 200 task = Task.objects.get(pk=int(taskid)) query_task_id = "{}-{}".format(task.name, taskid) history = lg_history_api(customer_name, user_id, count) response_data = history.search_lg_history_bytask(query_task_id) data_list = response_data.json()["result"] for item in data_list: red_item = item del red_item["header"] del red_item["body"] del red_item["result"] tranfer_time = datetime.strptime( red_item["published_date"], '%a, %d %b %Y %H:%M:%S %Z') + timedelta(hours=8) red_item["published_date"] = tranfer_time.strftime( "%Y/%m/%d %H:%M") red_item["status_code"] = red_item["status_code"].replace( "<br/>", "\n") red_item["total_time"] = red_item["total_time"].replace( "<br/>", "\n") res_data.append(red_item) context_dict = {'detaillist': res_data, 'supportfilter': 'True'} response = render_to_response( 'lookglass/history/history_detail.html', context_dict) return response else: return ("No auth to access") except Exception as e: message = "Exception : " + str(e) return HttpResponse(message)
def lg_task_instances(request): response_data = {} try: username = request.GET['username'] token = request.GET['token'] task_id = int(request.GET['id']) task = Task.objects.get(id=task_id) api = lg_operation_api(operapihost) nodelist = api.get_All_instances(token, None) if task: message = "" instance_list = task.nodes.split(';') for instance_id in instance_list: node = [ item for item in nodelist if item.id == int(instance_id) ] if node: #node = api.get_All_instances(token,instance_id) message = message + node[0].ch_name + "," response_data['command'] = task.command response_data['command_host'] = task.command_host response_data['message'] = message response_data['status_code'] = 200 else: response_data['command'] = "" response_data['command_host'] = "" response_data['message'] = 'not found' response_data['status_code'] = 403 return HttpResponse(json.dumps(response_data), content_type="application/json") except Exception as e: message = "Exception : " + str(e) response_data['status_code'] = 408 response_data['message'] = message response_data['command'] = "" response_data['command_host'] = "" return HttpResponse(json.dumps(response_data), content_type="application/json")
def task_config(request): try: response_data = {} api = lg_operation_api(operapihost) if request.method == 'GET': instancelist = list() username = request.GET['username'] token = request.GET['token'] taskid = request.GET['taskid'] userpofile = api.get_userprofile(token, username) customer = api.get_customerinfo(token, userpofile.customer_id) monitorlist = api.get_instances(token, instancelist) regionlist = list( set([instance.region_name for instance in monitorlist])) context_dict = { 'customer': customer, 'task_id': taskid, 'region_list': regionlist, 'monitor_list': monitorlist } return render_to_response('lookglass/task/taskconfig.html', context_dict) if request.method == 'POST': username = request.POST['username'] token = request.POST['token'] action = request.POST['action'] if action == 'delete': taskid = int(request.POST['taskid']) task = Task.objects.get(pk=taskid) if task: task.enabled = False temptask = task.save() periodictask = PeriodicTask.objects.get( pk=task.periodictask_id) if periodictask: periodictask.enabled = False periodictask.save() response_data['status_code'] = 200 response_data[ 'message'] = "period task disable success" else: response_data['status_code'] = 403 response_data['message'] = "period task is not existed" else: response_data['status_code'] = 403 response_data['message'] = "task is not existed" if action == 'new': taskstarttime = datetime.strptime( request.POST['taskstarttime'], '%Y/%m/%d %H:%M') taskevery = request.POST['taskevery'] taskperiod = request.POST['taskperiod'] tasktimes = request.POST['tasktimes'] taskcommand = request.POST['taskcommand'] taskhost = request.POST['taskhost'] tasknodes = request.POST['tasknodes'] curl_domain = request.POST['curl_domain'] curl_port = request.POST['curl_port'] curl_ip = request.POST['curl_ip'] tcp_port = request.POST['tcp_port'] digtype = request.POST['digtype'] digdns = request.POST['digdns'] msg_type = request.POST['msg_type'] msg_times = request.POST['msg_times'] lg_origin = "" userpofile = api.get_userprofile(token, username) user_id = userpofile.user_id customer_id = userpofile.customer_id customer_name = userpofile.customer_name taskname = "{}_{}_{}_{}_task-{}".format( taskcommand, taskevery, taskperiod, tasktimes, my_random_string(6)) ## get post data lgnodeapi = lg_nodes_api("", "", "") postdata = json.dumps( lgnodeapi.get_postdata(taskcommand, taskhost, lg_origin, curl_domain, curl_port, curl_ip, tcp_port, digtype, digdns)) ## worker task name task = Create_or_delete_Task(0, customer_id, user_id, 0, taskname, taskstarttime, taskevery, taskperiod, tasktimes, taskcommand, taskhost, postdata, tasknodes, "Ready", username, False) if task: ## get task arg command_site = "%s;%s;%s;%s;%s;%s;%s;%s;%s" % ( taskcommand, taskhost, lg_origin, curl_domain, curl_port, curl_ip, tcp_port, digtype, digdns) test_param = [ task.id, user_id, customer_name, tasknodes, tasktimes, command_site ] task_job_name = "worker.worker.task_lg" predictask = Create_or_delete_Predic_Task( 0, taskname, task_job_name, taskstarttime, taskevery, taskperiod, test_param, False) if predictask: savetask = Task.objects.get(pk=task.id) if savetask: savetask.periodictask_id = predictask.id savetask.save() if savetask.periodictask_id != 0: response_data[ 'message'] = serializers.serialize( 'json', [ task, ]) response_data['status_code'] = 200 else: status = TaskStatus.objects.get(name='Fail') savetask.status = status savetask.save() response_data['status_code'] = 403 response_data['message'] = "update task fail" else: response_data['status_code'] = 403 response_data['message'] = "task not existed" else: status = TaskStatus.objects.get(name='Fail') savetask.status = status savetask.save() response_data['status_code'] = 403 response_data['message'] = "Create predictask fail" else: response_data['status_code'] = 403 response_data['message'] = "Create task fail" return HttpResponse(json.dumps(response_data), content_type="application/json") except Exception as e: message = "Exception : " + str(e) response_data['message'] = message response_data['status_code'] = 408 log.logger.info('message:' + message) return HttpResponse(json.dumps(response_data), content_type="application/json")
def Update_ConfigInstance(token,config_instance_id,status,enabled): api = lg_operation_api(operapihost) return api.patch_HTConfigInstance(token,config_instance_id,status,enabled)