def check_letron_lg(): log = Log("tool","checklog") log.logger.info("[deploy lg]Start to get nodes info") letron_api = OpenAPI() node_dict = letron_api.sendRequest('GET', "") for k, v in node_dict.items(): server=v[0] ip = v[1] monitor_name = v[2] region = v[3] isp = v[4] url = "http://%s:5000/curl"%(ip) #my_data={'domain':'https://www.baidu.com/'} my_data={'host':'-ivk https://www.tma78.com/','domain':'','port':'','ip':''} try: r = requests.get(url, params = my_data,timeout=20) print() #r = requests.post(url, data = my_data, timeout=None) if r.status_code == 200: log.logger.info("[deploy lg]%s - %s finish to site check result : %s"%(server,ip,'Pass')) else: log.logger.info("[deploy lg]%s - %s finish to site check result : %s"%(server,ip,'Fail')) except Exception as e: log.logger.info("[deploy lg]%s - %s finish to site check result : %s"%(server,ip,'Fail'))
def task_add(self,*test_data): try: log = Log("task_addlog","task_addlog") channel_name = 'tools' print(test_data) channel_name = test_data[0] logger.info("channel_name:%s"%(channel_name)) if channel_name == 'tools': user_id = test_data[1] customer = test_data[2] tag_index = test_data[3] group_name = test_data[4] region_name = test_data[5] monitor_name = test_data[6] ip = test_data[7] command_site = test_data[8] msg_type = test_data[9] msg_times = int(test_data[10]) 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] for index in range(0, msg_times): 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) log.logger.info("[get_lg_node_result]{} : {}".format(status_code,testresult)) update_status(self.update_state,group_name,index,msg_type,100,"case completed",testresult) if res_data is not None and status_code!=408: history_id = create_lghistory(1,user_id,customer,monitor_name,command,host,testresult,res_data) logger.info("[task_add]: create lg history : {}".format(history_id)) elif channel_name == 'monitors': action = test_data[1] if action == 'query': customer_id = test_data[2] customer_name = test_data[3] region_id = test_data[4] group_name = test_data[5] monitor_id = test_data[6] monitor_ip = test_data[7] command = test_data[8] logger.info("customer_id:%s,customer_name:%s,region_id:%s,group_name:%s,monitor_id:%s,monitor_ip:%s,command:%s"%(customer_id,customer_name,region_id,group_name,monitor_id,monitor_ip,command)) msg_type = 'monitor_query_message' testresult = get_monitor_htconfig(region_id,customer_id,monitor_id,customer_name,monitor_ip) update_status(self.update_state,group_name,1,msg_type,100,"case completed",testresult) except Exception as exc: self.retry(countdown=1, exc=exc) print(exc)
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 Create_yml_file(self): log = Log("lg_operation_api","lg_operation_api_log") try: log.logger.info("Create_yml_file.{} ,{} , {}".format(self.config.id,self.customer_name ,self.instance.id)) ### get url string ### tmpurl='' for url in sorted(self.config.url_heartbeat, key=lambda k: k.get('id', 0),reverse=True): if url['enabled'] == True: tmpurl = tmpurl +'\"{}\"'.format(url['full_url'])+"," log.logger.info("tmpurl:{}".format(tmpurl)) ### get detail ## checkrequest = '' checkreponse='' log.logger.info("protocol:{}".format(self.protocol.name)) if self.protocol.name == 'http': log.logger.info("detail_heartbeat:{}".format(self.config.detail_heartbeat)) if self.config.detail_heartbeat: checkrequest = next((detail['value'] for detail in self.config.detail_heartbeat if detail['key'] =='check.request'), None) checkreponse= next((detail['value'] for detail in self.config.detail_heartbeat if detail['key'] =='check.response'), None) node_ht = nodes_heartbeat_api(self.instance.host_ip,self.is_asia) if node_ht.token: if self.customer_name == 'SSM': customer_id = 2 else: customer_id = self.customer_id customer_name = self.customer_name region = self.instance.region_name isp = self.instance.isp_name monitor = self.instance.ch_name schedule = self.config.schedule url = tmpurl ht_yml_file = self.config.hb_yml_name ht_type = self.protocol.name origin = self.config.origin node_config = Node_HTConfig(customer_id,customer_name,region,isp,monitor,schedule,url,ht_yml_file,ht_type,origin) return node_ht.New_HTConfig(node_config,checkrequest,checkreponse) else: return False except Exception as e: message = "Exception : "+ str(e) error_class = e.__class__.__name__ #取得錯誤類型 detail = e.args[0] #取得詳細內容 cl, exc, tb = sys.exc_info() #取得Call Stack lastCallStack = traceback.extract_tb(tb)[-1] #取得Call Stack的最後一筆資料 fileName = lastCallStack[0] #取得發生的檔案名稱 lineNum = lastCallStack[1] #取得發生的行號 funcName = lastCallStack[2] #取得發生的函數名稱 errMsg = "File \"{}\", line {}, in {}: [{}] {}".format(fileName, lineNum, funcName, error_class, detail) log.logger.info(errMsg) return False
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 Delete_yml_file(self): log = Log("lg_operation_api","lg_operation_api_log") try: #log.logger.info("Delete_yml_file") node_ht = nodes_heartbeat_api(self.instance.host_ip,self.is_asia) if node_ht.token: return node_ht.Delete_HTConfig( self.config.hb_yml_name) else: return False except Exception as e: message = "Exception : "+ str(e) log.logger.info(message) return False
def deploy_letron_lg(): log = Log("tool","deploylog") log.logger.info("[deploy lg]Start to get nodes info") letron_api = OpenAPI() node_dict = letron_api.sendRequest('GET', "") filelist = ["looking-glass-agent.tar.gz","looking_glass_agent_107.tar"] for k, v in node_dict.items(): server=v[0] ip = v[1] monitor_name = v[2] region = v[3] isp = v[4] log.logger.info("[deploy lg]%s - %s deploy_from_local_file"%(server,ip)) result = deploy_from_local_file(ip) log.logger.info("[deploy lg]%s - %s finish to deploy_from_local_file result : %s"%(server,ip,result))
def Check_yml_file(self): log = Log("lg_operation_api","lg_operation_api_log") result = True status_code = 0 status_code,yml_file = self.Get_yml_file_info() if status_code == 200 : url_content = ','.join(yml_file['content']) for url in self.config.url_heartbeat: if url['enabled']== True: if url['full_url'] not in url_content: result = False break else: result = False log.logger.info("[Check_yml_file]{} : {}".format(status_code,result)) return status_code,result
def create_lghistory(task_id,user_id,customer,monitor_name,lg_command,lg_host,test_data,res_data): try: ## create lg history log = Log("lg_history_api","lg_history_api_log") test_data = json.loads(test_data) status_code = test_data['status_code'] total_time = test_data['total_time'] down_link = test_data['downlink'] header = test_data['header'] body = test_data['body'] history =lg_history_api(customer,user_id,100) res = history.create_lg_history(task_id,monitor_name,lg_command,lg_host,status_code,total_time,down_link,header,body,res_data) log.logger.info("create lg history res:{}".format(res)) if res: resjson = res.json() log.logger.info("create lg history resjson:{}".format(resjson)) if 'successfully' in resjson["msg"] : return resjson['id'] else: return 0 except Exception as e: log.logger.error("create lg history error:"+ str(e)) return 0
# -*- coding: UTF-8 -*- import os import sys import timeit import datetime import shlex import json import io import requests from mlog import Log from worker.config import historyapihost, historyapiport log = Log("lg_history_api", "lg_history_api_log") class lg_history_api(object): def __init__(self, customer, user_id, query_count): self.customer = customer self.user_id = user_id self.query_count = query_count self.apihost = "http://{}:{}/lg".format(historyapihost, historyapiport) def create_lg_history(self, history_task_id, monitor_name, command, host, status_code, total_time, down_link, header, body, res_data): try: post_url = self.apihost headers = {'Content-Type': 'application/json'} if command == 'har': body = down_link
import sys import subprocess import timeit import datetime import shlex import paramiko import config as conf import socket import time import re from mlog import Log import requests from lgnodesapi import lg_nodes_api log = Log("tool","upgradefilelog") class sshtool(object): def __init__(self,host,port,username,passwd): self.output= None self.error= None self.client=None self.remote_conn = None self.host= conf.HOST self.username = conf.USERNAME self.password = conf.PASSWORD self.timeout = float(conf.TIMEOUT) self.commands = conf.COMMANDS self.pkey = conf.PKEY self.port = conf.PORT self.uploadremotefilepath = conf.UPLOADREMOTEFILEPATH
# -*- coding: UTF-8 -*- import os import sys import timeit import datetime import shlex import json import io import requests from mlog import Log log = Log("lg_api_log", "lg_api_log") class Instance(object): def __init__(self, id, nid, region, isp, ch_name, host_ip, status_name): self.id = id self.nid = nid self.region = region self.isp = isp self.ch_name = ch_name self.host_ip = host_ip self.status_name = status_name class Region(object): def __init__(self, id, en_name, ch_name): self.id = id self.en_name = en_name self.ch_name = ch_name
from django.shortcuts import render from django.shortcuts import render_to_response from django.views.decorators.csrf import csrf_exempt from django.contrib.auth.decorators import login_required from django.http import FileResponse from django.http import HttpResponse from worker.lgoperationapi import lg_operation_api from mlog import Log from worker.config import operapihost import json from config import expire log = Log("over", "lookglasslog") # Create your views here. def index(request): return render(request, 'index.html', locals()) 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
def task_deploy(self,*test_data,**kwargs): log = Log("task_deploylog","task_deploylog") log.logger.info("start to deploy") Action = test_data[0] token = test_data[1] config_instance_id = test_data[2] userpofile,protocol,config,instance = get_deploy_data(kwargs) htdeploy = HeatbeatDeploy(userpofile.customer_id,userpofile.customer_name,protocol,config,instance) username = userpofile.user_name yml_file_name = config.hb_yml_name protocol_name = protocol.name instance_name = instance.ch_name log.logger.info("[{}:{}]{}:{},{},{}".format(Action,config_instance_id,username,yml_file_name,protocol_name,instance_name)) ### set status to deploy configinstance = Update_ConfigInstance(token,config_instance_id,3,None) log.logger.info("[configinstance] {} status is {}, enable is {} ".format(configinstance.id,configinstance.status,configinstance.enabled)) if Action == 'New': username = userpofile.user_name yml_file_name = config.hb_yml_name protocol_name = protocol.name instance_name = instance.ch_name ## create yml file createtime = 1 status_code ,result = htdeploy.Create_yml_file() if status_code == 200 or status_code == 408: status_code, result = htdeploy.Check_yml_file() log.logger.info("[{}-{}:{}]{}:{},{},{}:{}-{}".format(createtime,Action,config_instance_id,username,yml_file_name,protocol_name,instance_name,result,status_code)) while result ==False or result == None: if createtime >3: break else: createtime +=1 status_code, result = htdeploy.Create_yml_file() if status_code == 200 or status_code == 408: status_code, result = htdeploy.Check_yml_file() log.logger.info("[{}-{}:{}]{}:{},{},{}:{}-{}".format(createtime,Action,config_instance_id,username,yml_file_name,protocol_name,instance_name,result,status_code)) if result == True: ### set status to ready configinstance = Update_ConfigInstance(token,config_instance_id,1,True) log.logger.info("[configinstance] {} status is {}, enable is {} ".format(configinstance.id,configinstance.status,configinstance.enabled)) log.logger.info("new deploy success:{}".format(config_instance_id)) else: log.logger.info("new deploy fail:{}".format(config_instance_id)) elif Action == 'Delete': deletedtime = 1 result = htdeploy.Delete_yml_file() if result != None: status_code, result = htdeploy.Check_yml_file() log.logger.info("[{}-{}:{}]{}:{},{},{}:{}-{}".format(deletedtime,Action,config_instance_id,username,yml_file_name,protocol_name,instance_name,result,status_code)) while status_code !=403: if deletedtime >3: break else: deletedtime +=1 result = htdeploy.Delete_yml_file() if result!= None: status_code, result = htdeploy.Check_yml_file() log.logger.info("[{}-{}:{}]{}:{},{},{}:{}-{}".format(deletedtime,Action,config_instance_id,username,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,config_instance_id,1,False) log.logger.info("[configinstance] {} status is {}, enable is {} ".format(configinstance.id,configinstance.status,configinstance.enabled)) log.logger.info("delete deploy success:{}".format(config_instance_id)) else: log.logger.info("delete deploy fail:{}".format(config_instance_id))
# -*- coding: UTF-8 -*- import os import sys import timeit import datetime import shlex import json import io import requests from mlog import Log log = Log("lg_message_api", "lg_message_api_log") class lg_message_api(object): def __init__(self): self.apihost = "http://18.162.81.56:8000/message" def send_message(self, message_type, receiverId, content): try: post_url = "{}/send".format(self.apihost) headers = {'Content-Type': 'application/json'} if message_type: my_data = { 'type': message_type, 'receiverId': receiverId, 'content': content } res = requests.post(post_url, headers=headers,