def get_fuzai(request): try: ip = request.GET.get("ip") biz_id = int(request.GET.get("biz_id")) yun_id = int(request.GET.get("yun_id")) ip_list = [{ "ip":ip, "bk_cloud_id":yun_id }] strip = """ #!/bin/bash cat /proc/loadavg """ result = run_fast_execute_script(biz_id,strip,ip_list) if result["result"]: job_id = int(result["data"]) log = get_job_instance_log(biz_id,job_id)[0] if log["is_success"]: logs = log["log_content"].split(" ")[1] else: logs = 0 return render_json({"result":True,"data":logs}) except Exception, e: return render_json({"result":False,"message":u"系统出错,请联系管理员","data":str(e)})
def get_dict(request): try: ip = request.GET.get("ip") biz_id = int(request.GET.get("biz_id")) yun_id = int(request.GET.get("yun_id")) ip_list = [{ "ip":ip, "bk_cloud_id":yun_id }] strip = """ #!/bin/bash df -h """ result = run_fast_execute_script(biz_id,strip,ip_list) data_list = [] if result["result"]: job_id = int(result["data"]) log = get_job_instance_log(biz_id,job_id)[0] if log["is_success"]: logs = log["log_content"].split("\n")[1:-1] for item in logs: datas = list(filter(None,item.split(" "))) dl = {} dl["filesystem"] = datas[0] dl["size"] = datas[1] dl["used"] = datas[2] dl["avail"] = datas[3] dl["use"] = datas[4] dl["mounton"] = datas[5] data_list.append(dl) return render_json({"result":True,"data":data_list}) except Exception, e: return render_json({"result":False,"message":u"系统出错,请联系管理员","data":str(e)})
def get_host_property(request): try: biz_id = int(request.GET.get("biz_id")) yun_id = int(request.GET.get("yun_id")) ip = request.GET.get("ip") ip_list = [ { "ip":ip, "bk_cloud_id":yun_id } ] script = """#!/bin/bash MEMORY=$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2 }') DISK=$(df -h | awk '$NF=="/"{printf "%s", $5}') CPU=$(top -bn1 | grep load | awk '{printf "%.2f%%", $(NF-2)}') DATE=$(date "+%Y-%m-%d %H:%M:%S") echo -e "$DATE|$MEMORY|$DISK|$CPU" """ proper = {} result = run_fast_execute_script(biz_id,script,ip_list) if result["result"]: job_id = int(result["data"]) log = get_job_instance_log(biz_id,job_id)[0] if log["is_success"]: logs = log["log_content"] proper = format_data(logs) return render_json({"reuslt":True,"data":proper}) except Exception,e: return render_json({"result":False,"message":u"系统出错,请联系管理员","data":str(e)})
def get_host_pro_data(): try: host_data = host.objects.filter() script = """#!/bin/bash MEMORY=$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2 }') DISK=$(df -h | awk '$NF=="/"{printf "%s", $5}') CPU=$(top -bn1 | grep load | awk '{printf "%.2f%%", $(NF-2)}') DATE=$(date "+%Y-%m-%d %H:%M:%S") echo -e "$DATE|$MEMORY|$DISK|$CPU" """ for item in host_data: biz_id = int(item.biz_id) ip = item.ip yun_id = int(item.yun_id) ip_list = [{"ip": ip, "bk_cloud_id": yun_id}] proper = {} result = run_fast_execute_script(biz_id, script, ip_list) if result["result"]: job_id = int(result["data"]) log = get_job_instance_log(biz_id, job_id)[0] if log["is_success"]: logs = log["log_content"] proper = format_data(logs) prop = HostData() prop.ip = ip prop.biz_id = biz_id prop.yun_id = yun_id prop.memory = proper["memory"] prop.cpu = proper["cpu"] prop.disk = proper["disk"] prop.save() logger.error("添加host性能") except Exception, e: logger.error("获取主机性能失败" + e)
def get_fuzai(): try: data_list = host.objects.filter() for item in data_list: ip = item.ip biz_id = int(item.biz_id) yun_id = int(item.yun_id) ip_list = [{"ip": ip, "bk_cloud_id": yun_id}] strip = """ #!/bin/bash cat /proc/loadavg """ result = run_fast_execute_script(biz_id, strip, ip_list) logs = 0 if result["result"]: job_id = int(result["data"]) log = get_job_instance_log(biz_id, job_id)[0] if log["is_success"]: logs = log["log_content"].split(" ")[1] fuzai = FuZai() fuzai.ip = ip fuzai.value = logs fuzai.save() except Exception, e: logger.error(e)
def get_log_content(request): """ :param request: 传递参数 :param 业务id biz_id, :param 作业实例id instance_id, :return: """ biz_id = request.GET.get('biz_id') job_instance_id = request.GET.get('instance_id') result = get_job_instance_log(biz_id, job_instance_id, request.user.username) data = {"data": result} return JsonResponse(data)
def get_disk_rate(request): try: ip = request.GET.get("ip") biz_id = int(request.GET.get("biz_id")) yun_id = int(request.GET.get("yun_id")) ip_list = [{ "ip":ip, "bk_cloud_id":yun_id }] strip = """ #!/bin/bash free -m """ result = run_fast_execute_script(biz_id,strip,ip_list) data = { "title": '', "series": [ { 'name': '已使用', 'value': 0 }, { 'name': '未使用', 'value': 0 } ] } if result["result"]: job_id = int(result["data"]) log = get_job_instance_log(biz_id,job_id)[0] if log["is_success"]: logs = log["log_content"].split("\n")[1] datas = list(filter(None,logs.split(" "))) total = int(datas[1]) used = int(datas[2]) data = { "title": '', "series": [ { 'name': '已使用', 'value': used }, { 'name': '未使用', 'value': total-used } ] } return render_json({"result":True,"data":data}) except Exception, e: return render_json({"result":False,"message":u"系统出错,请联系管理员","data":str(e)})
def exec_script(request): data = json.loads(request.body) scriptId = data['script'] script = script_list_get(int(scriptId)) script = script['data'] ipList = data['ip'] ipMap = [] for ip in ipList: ipData = {"ip": ip['ip'], "bk_cloud_id": ip['bk_cloud_id']} ipMap.append(ipData) execData = run_fast_execute_script(data['business'], script['content'], ipMap) logData = get_job_instance_log(data['business'], execData['data']) result = False logContent = [] for log in logData: result = log['is_success'] logContent.append({'ip': log['ip'], 'log_content': log['log_content']}) return JsonResponse({ 'result': result.__str__().lower(), 'logContent': logContent })