Exemple #1
0
def info(request, host_id):
    auth = ZabbixRestAPI.get_auth("Admin", "zabbix")
    zabbix = ZabbixRestAPI(auth)
    # 호스트 리스트
    host_result = zabbix.get_hosts(output=["hostid", "host", "name"])
    if host_result.get("result"):
        hosts = host_result.get("result")
    else:
        hosts = host_result

    # 호스트 정보가 없을 경우
    if '1' == host_id:
        return redirect("/dashboard/monitoring/" + hosts[0].get("hostid") + "/detail")

    # 이벤트 정보
    result = zabbix.get_host_events(hostid=host_id, output="extend", sortfield=["clock", "eventid"])
    if result.get("result"):
        events = result.get("result")
    else:
        events = result

    host_flag = True
    result_host_interfaces = zabbix.get_host_interfaces(output=["hostid", "ip", "port"])
    if result_host_interfaces.get("result"):
        host_interfaces = result_host_interfaces["result"]
        for host_interface in host_interfaces:
            # host와 interface 매칭
            if host_id == host_interface.get("hostid"):
                if "127.0.0.1" in host_interface.get("ip") or "localhost" in host_interface.get("ip"):
                    host_flag = False
    return render(request, "monitoring/info.html", {"hostid": host_id, "hosts": hosts, "events": events, "host_flag": host_flag})
Exemple #2
0
def synchronize_floating_server_host(request):
    if request.method == 'POST' and request.is_ajax():
        token = request.session.get("passToken")
        auth_url = request.session.get("auth_url")
        project_id = request.session.get("project_id")
        auth = ZabbixRestAPI.get_auth("Admin", "zabbix")
        zabbix = ZabbixRestAPI(auth)
        neutron = NeutronRestAPI(auth_url, token)
        err_msg_list = []
        # openstack의 floating_ip 목록 가져오기
        q = {"project_id": project_id}
        result_floating_ips = neutron.get_floating_ips(None, q)
        create_host_flag = False
        # host목록 가져오기
        result_hosts = zabbix.get_hosts(output=["hostid", "host", "status", "name"])
        if result_hosts.get("result") and result_floating_ips.get("success"):
            floating_ips = result_floating_ips["success"].get("floatingips")  # 중요1
            hosts = result_hosts.get("result")
            # floating_ips 목록이 Zabbix에 전부 등록되있는지 확인
            # TODO: zabbix에 등록된 host중 floatingip연결을 해제시키거나 삭제된 server가 있으면 제거
            recovery_list = []  # 중요3
            for floating_ip in floating_ips:
                if floating_ip.get("port_id"):
                    # floatingip - port 조회
                    result_port = neutron.getPort(floating_ip["port_id"])
                    if result_port.get("success"):
                        port = result_port["success"].get("port")
                        floating_ip["port_details"] = port
                        server_id = port.get("device_id")
                        host_names = [temp_host.get("host") for temp_host in hosts]
                        # 각 가상머신의 호스트가 등록되있지 않다면 등록 중요3
                        if server_id and "compute" in port.get("device_owner") and server_id not in host_names:
                            # floatingip - port - device(서버) 조회
                            recovery_info = {
                                "server": {
                                    "vm_id": server_id,
                                    "floating_ip_id": floating_ip.get("id"),
                                    "floating_ip_address": floating_ip.get("floating_ip_address"),
                                    "fixed_ips": port.get("fixed_ips")
                                },
                                "auth_url": auth_url,
                                "project_name": request.session.get("project_name")
                            }
                            recovery_list.append(recovery_info)
                    if result_port.get("error"):
                        err_msg_list.append(result_port["error"])

            if len(recovery_list) > 0:
                # 서비스 리스트 조회 -> 서비스 조회 -> vm_list에서 같은아이디 있는지 확인
                ctrl_header = request.session.get("ctrl_header")
                control = ControlEngine(ctrl_header)
                result_service_list = control.get_service_list()
                if result_service_list.get("success"):
                    for service in result_service_list["success"].get("service_list"):
                        service_id = service.get("service_id")
                        result_service = control.get_service(service_id)
                        if result_service.get("success"):
                            for recovery_info in recovery_list:
                                for vm in result_service["success"]["service_detail"].get("vm_list"):
                                    # 서비스내에 해당 server가 있는지 확인
                                    if vm.get("vm_id") == recovery_info["server"].get("vm_id"):
                                        recovery_info["server"]["vm_name"] = vm.get("vm_name")
                                        recovery_info["server"]["service_id"] = service_id
                        if result_service.get("error"):
                            err_msg_list.append(result_service["error"])
                if result_service_list.get("error"):
                    err_msg_list.append(result_service_list["error"])
                            # service_list 조회끝
            for recovery_info in recovery_list:
                logger.debug("\n\n\n\n" + json.dumps(recovery_info) + "\n\n\n\n")
                # hostgroup 조회
                result_hostgroup = zabbix.get_hostgroups({"name": "Linux servers"}, ["groupid"])
                if result_hostgroup.get("result"):
                    hostgroup_id = result_hostgroup["result"][0].get("groupid")
                    result_create_host = create_host(request, zabbix, recovery_info, hostgroup_id)
                    if len(result_create_host) < 1:
                        create_host_flag = True
                    else:
                        err_msg_list.append(result_create_host)
                if not result_hostgroup.get("result"):
                    err_msg_list.append(result_hostgroup)

            # 호스트 생성시 호스트 목록 다시조회
            if create_host_flag:
                result_hosts = zabbix.get_hosts(output=["hostid", "host", "status", "name"])
                if result_hosts.get("result"):
                    hosts = result_hosts["result"]
        if not result_hosts.get("result"):
            err_msg_list.append(result_hosts)
        if result_floating_ips.get("error"):
            err_msg_list.append(result_floating_ips["error"])
        return JsonResponse({"success": {"err_msg_list": err_msg_list}})
Exemple #3
0
def index(request):
    token = request.session.get("passToken")
    auth_url = request.session.get("auth_url")
    project_id = request.session.get("project_id")
    auth = ZabbixRestAPI.get_auth("Admin", "zabbix")
    zabbix = ZabbixRestAPI(auth)
    if request.method == "POST" and request.is_ajax():
        groupids = request.POST.get("groupids")
        groupname = request.POST.get("groupname")
        hostids = request.POST.get("hosts")
        if groupname or groupids:
            if "Template" in groupname:
                if groupname == "Templates":
                    result_hosts = zabbix.get_template(output="extend")
                else:
                    result_hosts = zabbix.get_template(output="extend", groupids=groupids)
            else:
                result_hosts = zabbix.get_hosts(output=["hostid", "host"], groupids=groupids)
        else:
            host_ping_list = []
            for hostid in json.loads(hostids):
                result_item = zabbix.get_item({"hostid": hostid, "name": "Agent ping"}, output=["lastvalue"])
                if result_item.get("result"):
                    host_ping_list.append({"hostid": hostid, "ping": result_item["result"][0].get("lastvalue")})
            result_hosts = {"host_ping_list": host_ping_list}
        return JsonResponse(result_hosts)
    else:
        """
         1. openstack - floating_ip 목록 조회
         2. zabbix - host 목록 조회
         floating_ip 목록 중 등록되지 않은 vm이 있으면 host 생성(floating_ip-port조회)
         3. openstack - vm조회
         4. zabbix - hostgroup조회
         5. zabbix - host 생성
         6. zabbix - hostinterface 조회
         7. zabbix - application 생성
         8. zabbix - item 생성
         9. zabbix - trigger 생성
        10. zabbix - mediatype 생성
        11. zabbix - action 생성
        12. zabbix - host 목록 조회
        13. zabbix - hostinterface 조회
        floating_ip 목록 중 등록되지 않은 vm이 없거나 생성 완료후
         1. host목록, hostinterface목록, vm목록 매칭
         2. host목록 반환
        
        """
        neutron = NeutronRestAPI(auth_url, token)
        # openstack의 floating_ip 목록 가져오기
        response_data = {"hosts": [], "vm_list": []}
        q = {"project_id": project_id}
        result_floating_ips = neutron.get_floating_ips(None, q)
        create_host_flag = False
        # host목록 가져오기
        result_hosts = zabbix.get_hosts(output=["hostid", "host", "status", "name"])
        if result_hosts.get("result") and result_floating_ips.get("success"):
            floating_ips = result_floating_ips["success"].get("floatingips")  # 중요1
            hosts = result_hosts.get("result")
            # floating_ips 목록이 Zabbix에 전부 등록되있는지 확인
            # TODO: javascript에서 서버 상태(ICMP Ping)를 실시간으로 변경
            recovery_list = []  # 중요3
            for floating_ip in floating_ips:
                if floating_ip.get("port_id"):
                    # floatingip - port 조회
                    result_port = neutron.getPort(floating_ip["port_id"])
                    if result_port.get("success"):
                        port = result_port["success"].get("port")
                        floating_ip["port_details"] = port
                        server_id = port.get("device_id")
                        host_names = [temp_host.get("host") for temp_host in hosts]
                        # 각 가상머신의 호스트가 등록되있지 않다면 등록
                        if server_id and "compute" in port.get("device_owner") and server_id not in host_names:
                            # floatingip - port - device(서버) 조회
                            recovery_info = {
                                "server": {
                                    "vm_id": server_id,
                                    "floating_ip_id": floating_ip.get("id"),
                                    "floating_ip_address": floating_ip.get("floating_ip_address")
                                },
                                "auth_url": auth_url,
                                "project_name": request.session.get("project_name")
                            }
                            recovery_list.append(recovery_info)

            # host의 interface 목록 가져오기
            result_host_interfaces = zabbix.get_host_interfaces(output=["hostid", "ip", "port"])
            if result_host_interfaces.get("result"):
                host_interfaces = result_host_interfaces["result"]
                for host in hosts:
                    for host_interface in host_interfaces:
                        # host와 interface 매칭
                        if host.get("hostid") == host_interface.get("hostid"):
                            host["interface"] = host_interface
                            # host중 interface의 ip가 127.0.0.1이거나 localhost인 경우
                            if "127.0.0.1" in host_interface.get("ip") or "localhost" in host_interface.get("ip"):
                                for floating_ip in floating_ips:
                                    #  floating_ip의 device_id와 host의 host(hostname)이 같을때만 리스트에 담아 보여주기
                                    if floating_ip.get("port_details"):
                                        result_item = zabbix.get_item({"hostid": host.get("hostid"), "name": "ICMP ping"})
                                        if result_item.get("result"):
                                            item = result_item["result"][0]
                                            if host.get("host") == floating_ip["port_details"]["device_id"]:
                                                host["floatingip_info"] = floating_ip
                                                host["ping"] = item.get("lastvalue")
                                                response_data["vm_list"].append(host)
                                                # logger.debug("\n\n\n\nfloating_ip: {}\nctrl_header: {}\n\n\n\n".format(floating_ip, request.session.get("ctrl_header")))
                                            else:
                                                check_recovered_vm_host(zabbix, host, item, floating_ip)
                            elif "admin" in request.session["roles"]:
                                result_item = zabbix.get_item({"hostid": host.get("hostid"), "name": "Agent ping"})
                                if result_item.get("result"):
                                    host["ping"] = result_item["result"][0].get("lastvalue")
                                response_data["hosts"].append(host)

            if len(recovery_list) > 0:
                response_data["synchronize_flag"] = False
            else:
                response_data["synchronize_flag"] = True

        else:
            response_data["error"] = {}
            if result_floating_ips.get("error"):
                response_data["error"]["openstack"] = result_floating_ips["error"]
            if not result_hosts.get("result"):
                response_data["error"]["zabbix"] = result_hosts
        return render(request, "monitoring/index.html", response_data)