Пример #1
0
def host_load():
    now = datetime.datetime.now()
    job_list = []
    client = get_client_by_user(user='******')

    hosts_ip = get_ip()
    for ip in hosts_ip:
        kwargs = {'bk_biz_id': BK_BIZ_ID, 'bk_job_id': LOAD_JOB_ID}
        resp = client.job.get_job_detail(**kwargs)
        ip_list = resp['data']['steps'][0]['ip_list']
        for _ip in ip_list:
            if _ip['ip'] == ip:
                resp['data']['steps'][0]['ip_list'] = [_ip]
                break
        else:
            continue
        steps = resp['data']['steps']
        kwargs.update({'steps': steps})
        job_instance = client.job.execute_job(**kwargs)
        job_list.append({
            'ip': ip,
            'job_id': job_instance['data']['job_instance_id']
        })

    for job in job_list:
        kws = {'bk_biz_id': BK_BIZ_ID, 'job_instance_id': job['job_id']}
        for i in range(10):
            status = client.job.get_job_instance_status(**kws)
            if status['data']['is_finished'] is True:
                resp = client.job.get_job_instance_log(**kws)
                break
            else:
                time.sleep(0.1)
        else:
            continue

        logs = resp['data'][0]['step_results'][0]['ip_logs'][0]['log_content']
        logs = logs.split('\n')
        logs = [_l.split(' ') for _l in logs]

        HostManager.add_host_load(ip=job['ip'], load=logs[1][1], now=now)
Пример #2
0
def home(request):
    hosts = HostManager.search_host()

    data = []
    for host in hosts:
        data.append(
            dict(
                bk_host_id=host.id,
                bk_host_name=host.bk_host_name,
                bk_host_innerip=host.bk_host_innerip,
                bk_biz_name=host.bk_biz_name,
                bk_os_name=host.bk_os_name,
                bk_inst_name=host.bk_inst_name,
                remark=host.remark,
            ))
    return render_mako_context(request, '/hosts/home.html/', {'hosts': data})
Пример #3
0
def search_host(request):
    ip = request.GET.get('ip', '')
    hosts = HostManager.search_host(ip=ip)

    data = []
    for host in hosts:
        data.append(
            dict(
                bk_host_id=host.id,
                bk_host_name=host.bk_host_name,
                bk_host_innerip=host.bk_host_innerip,
                bk_biz_name=host.bk_biz_name,
                bk_os_name=host.bk_os_name,
                bk_inst_name=host.bk_inst_name,
                remark=host.remark,
            ))

    result = {'result': True, 'data': data, 'code': 0}
    return render_json(result)
Пример #4
0
def host_load(request):
    host_ip = request.GET.get('host_ip')
    date_now = request.GET.get('to_time')
    hour_ago = request.GET.get('from_time')

    date_now = datetime.datetime.strptime(
        date_now.replace(" ", " ").replace("/", "-") +
        ':00', '%Y-%m-%d %H:%M:%S') if date_now else datetime.datetime.now()
    hour_ago = datetime.datetime.strptime(
        hour_ago.replace(" ", " ").replace("/", "-") + ':00',
        '%Y-%m-%d %H:%M:%S') if hour_ago else date_now - datetime.timedelta(
            hours=1)

    loads = HostManager.search_host_load(host_ip, date_now, hour_ago)
    is_true = True
    if len(loads) > 90:
        is_true = False
    if not loads:
        data = []
        categories = []
    else:
        data, categories = zip(*[(_load.load, str(_load.create_at))
                                 for _load in loads])

    result = {
        "code": 0,
        "result": is_true,
        "message": "success",
        "data": {
            "series": [{
                "color": "#f9ce1d",
                "name": "五分钟平均负载(近一小时)",
                "data": list(data),
            }],
            "categories":
            list(categories),
        }
    }
    return render_json(result)
Пример #5
0
def get_ip():
    hosts = HostManager.search_host()
    hosts_ip = [host.bk_host_innerip for host in hosts]
    return hosts_ip
Пример #6
0
def del_host(request):
    host_id = request.POST.get('host_id', '')
    result = HostManager.del_host(host_id=host_id)
    return render_json(result)
Пример #7
0
def update_host(request):
    ip = request.POST.get('ip', '')
    remark = request.POST.get('remark', '')
    result = HostManager.update_host(ip=ip, remark=remark)
    return render_json(result)