def submit_action(request): data = json.loads(request.POST.get("data")) auth = ZabbixRestAPI.get_auth("Admin", "zabbix") zabbix = ZabbixRestAPI(auth) if data.get("actionid"): result = zabbix.update_action(data) else: result = zabbix.create_action(data) return JsonResponse(result)
def trigger_list(request): auth = ZabbixRestAPI.get_auth("Admin", "zabbix") zabbix = ZabbixRestAPI(auth) result = zabbix.get_hostgroups(output="extend") if result.get("result"): hostgroups = result.get("result") else: hostgroups = result return render(request, "monitoring/action/trigger_list.html", {"hostgroups": hostgroups})
def action(request): auth = ZabbixRestAPI.get_auth("Admin", "zabbix") zabbix = ZabbixRestAPI(auth) result = zabbix.get_actions(filters={"eventsource": 0}, output="extend", selectOperations="extend", selectFilter="extend") if result.get("result"): actions = result.get("result") else: actions = result return render(request, "monitoring/action/index.html", {"actions": actions})
def chart(request, host_id): auth = ZabbixRestAPI.get_auth("Admin", "zabbix") zabbix = ZabbixRestAPI(auth) # 이벤트 정보 result = zabbix.get_host_events(hostid=host_id, output="extend", sortfield=["clock", "eventid"]) if result.get("result"): events = result.get("result") else: events = result return render(request, "monitoring/chart.html", {"events": events})
def get_triggers(request): auth = ZabbixRestAPI.get_auth("Admin", "zabbix") zabbix = ZabbixRestAPI(auth) templateids = request.POST.get("templateids") hostids = request.POST.get("hostids") if templateids: result = zabbix.get_triggers(output="extend", templateids=templateids) else: result = zabbix.get_triggers(output="extend", hostids=hostids) return JsonResponse(result)
def get_group_info(group_id): auth = ZabbixRestAPI.get_auth("Admin", "zabbix") zabbix = ZabbixRestAPI(auth) result = zabbix.get_group(usrgrpids=group_id) if result.get("result"): groups = result.get("result") group_name = "" for group in groups: group_name = group.get("name") group_info = group_name else: group_info = result return group_info
def network_interface(request, host_id, result_type=None): auth = ZabbixRestAPI.get_auth("Admin", "zabbix") zabbix = ZabbixRestAPI(auth) result = zabbix.get_item(output=["key_"], editable=True, hostids=host_id, search={"key_": "net.if"}) if result.get("result"): result["result"] = list(set([ item.get("key_").replace("net.if.in[", "").replace("net.if.out[", "").replace("]", "") for item in result["result"] ])) if result_type == "dict": return result else: return JsonResponse(result)
def get_mediatype_info(mediatype_id): auth = ZabbixRestAPI.get_auth("Admin", "zabbix") zabbix = ZabbixRestAPI(auth) result = zabbix.get_mediatype(mediatypeids=mediatype_id) if result.get("result"): mediatypes = result.get("result") mediatype_name = "" for mediatype in mediatypes: mediatype_name = mediatype.get("description") mediatype_info = mediatype_name else: mediatype_info = result return mediatype_info
def action_modal(request): modal_title = request.GET.get("modal_title") auth = ZabbixRestAPI.get_auth("Admin", "zabbix") zabbix = ZabbixRestAPI(auth) result = {"modal_title": modal_title} if request.GET.get("action_id"): action_id = request.GET.get("action_id") action_result = zabbix.get_actions(filters={"eventsource": 0}, output="extend", selectOperations="extend", selectFilter="extend", actionids=action_id) if action_result.get("result") and len(action_result.get("result")) > 0: result["action"] = action_result.get("result")[0] else: result["action"] = action_result return render(request, "monitoring/action/modal.html", result)
def get_trigger_info(trigger_id): auth = ZabbixRestAPI.get_auth("Admin", "zabbix") zabbix = ZabbixRestAPI(auth) result = zabbix.get_triggers(triggerids=trigger_id, selectHosts="extend") if result.get("result"): triggers = result.get("result") for trigger in triggers: hosts = trigger.get("hosts") trigger["host_name"] = "" for host in hosts: trigger["host_name"] += host.get("host") trigger["description"] = trigger.get("description").replace( "{HOST.NAME}", trigger["host_name"]) trigger = triggers[0] trigger_info = trigger.get("description") else: trigger_info = result return trigger_info
def get_operation(request): data = {} if request.GET.get("operation"): data["operation"] = json.loads(request.GET.get("operation")) else: data["operation"] = { "esc_step_from": 1, "esc_step_to": 1, "esc_period": 0 } auth = ZabbixRestAPI.get_auth("Admin", "zabbix") zabbix = ZabbixRestAPI(auth) user_result = zabbix.get_user(filters={"alias": "Admin"}) if user_result.get("result"): data["user"] = user_result["result"] media_result = zabbix.get_mediatype() if media_result.get("result"): data["mediatype"] = media_result["result"] return render(request, "monitoring/action/loadpage/operation.html", data)
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})
def chart_data_detail(request, host_id): data = json.loads(request.POST.get("data")) date_format = "%Y-%m-%dT%H:%M:%S" if data.get("time_from"): time_from = datetime.datetime.strptime(data["time_from"], date_format) else: time_from = datetime.datetime.now() - datetime.timedelta(days=1) if data.get("time_till"): time_till = datetime.datetime.strptime(data["time_till"], date_format) else: time_till = datetime.datetime.now() time_from_sec = (time_from - datetime.timedelta(hours=9) - datetime.datetime(1970, 1, 1)).total_seconds() time_till_sec = (time_till - datetime.timedelta(hours=9) - datetime.datetime(1970, 1, 1)).total_seconds() auth = ZabbixRestAPI.get_auth("Admin", "zabbix") zabbix = ZabbixRestAPI(auth) host_item_result = zabbix.get_item(output="extend", hostids=host_id, search={"key_": data.get("key")}) if host_item_result.get("result") and len(host_item_result["result"]) == 1: host_item = host_item_result["result"][0] sortorder = data.get("sortorder") if data.get("sortorder") else "DESC" if time_from < (datetime.datetime.now() - datetime.timedelta(days=7, hours=9)): result_history = zabbix.get_trend(ouput=["itemid", "clock", "num", "value_avg"], itemids=host_item.get("itemid"), time_from=time_from_sec, time_till=time_till_sec) else: result_history = zabbix.get_history(itemids=host_item.get("itemid"), sortfield="clock", sortorder=sortorder, history=host_item.get("value_type"), time_from=time_from_sec, time_till=time_till_sec) result = {"history": result_history} else: result = host_item_result return JsonResponse(result)
def get_user_info(user_id): auth = ZabbixRestAPI.get_auth("Admin", "zabbix") zabbix = ZabbixRestAPI(auth) result = zabbix.get_user(userids=user_id) if result.get("result"): users = result.get("result") user_name = "" for user in users: user_name = user.get("alias") if user.get("name") or user.get("surname"): # first_name + family_name user_name += " (" if user.get("name"): user_name += user["name"] if user.get("surname"): user_name += " " if user.get("surname"): user_name += user["surname"] user_name += ")" user_info = user_name else: user_info = result return user_info
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)
def chart_data(request, host_id): auth = ZabbixRestAPI.get_auth("Admin", "zabbix") zabbix = ZabbixRestAPI(auth) app_type = request.POST.get("type") if app_type == "icmpping": chart_list = {"ping": {}} result = zabbix.get_chart_data(host_id, "icmpping") if result.get("result"): chart_list["ping"]["icmpping"] = result.get("result") else: chart_list["ping"]["icmpping"] = [] else: item_group = { "ping": {"agent.ping": []}, "memory": { "vm.memory.size": ["available", "total"], "system.swap.size": [",free", ",pfree", ",total"] }, "cpu": { "system.cpu.load": ["percpu,avg1", "percpu,avg5", "percpu,avg15", ], "system.cpu.util": [",idle", ",interrupt", ",iowait", ",nice", ",softirq", ",steal", ",system", ",user"], "system.cpu.switches": [], "system.cpu.intr": [] }, "filesystems": { "vfs.fs.inode": ["/,pfree"], "vfs.fs.size": ["/,free", "/,pfree", "/,total", "/,used"] }, "network": { "net.if.in": [], "net.if.out": [] } } interfaces = network_interface(request, host_id, "dict") if interfaces.get("result"): item_group["network"]["net.if.in"] = interfaces["result"] item_group["network"]["net.if.out"] = interfaces["result"] chart_list = {} item_dic = item_group[app_type] chart_list[app_type] = {} for item_main_key, item_sub_key_list in item_dic.items(): # item key 조합 item_key = item_main_key if len(item_sub_key_list) > 0: # subkey 있을때 for item_sub_key in item_sub_key_list: item_key = item_main_key + "[" + item_sub_key + "]" result = zabbix.get_chart_data(host_id, item_key) if result.get("result"): chart_list[app_type][item_key] = result.get("result") else: chart_list[app_type][item_key] = [] else: # subkey 없을 때 result = zabbix.get_chart_data(host_id, item_key) if result.get("result"): chart_list[app_type][item_key] = result.get("result") else: chart_list[app_type][item_key] = [] return JsonResponse(chart_list)
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}})