def update_action(self, request): data = request.POST.dict() data['uic'] = ','.join(request.POST.getlist('uic[]')) data.pop('uic[]') f = Falcon(request.user.username) result = f.update_action(**data) return FalconResponse(result)
def query_cluster_status(request): #统计每个集群的可用性 try: cluster = request.POST['cluster'] range = request.POST['range'] end_timestamp = int(time.time()) start_timestamp = end_timestamp - int(range) #先统计有哪些集群名 if cluster == 'all': cluster_list = [i['fcluster'] for i in Service.objects.values('fcluster').distinct() if i['fcluster']] else: cluster_list = [cluster] counters = ['cluster.available.percent/clusterName=%s,project=oms'%i for i in cluster_list] endpoint = get_local_ip() f = Falcon() history_data = f.get_history_data(start_timestamp, end_timestamp, [endpoint], counters, step=24*60*60, CF='AVERAGE') data = [] for i in history_data: if i['counter']: tags = i['counter'].split('/')[1] tag_dict = gen_tags(tags) cluster = tag_dict.get('clusterName') ts_value = [] c_date = time.strftime("%Y-%m-%d") c_timestamp = int(time.mktime(time.strptime(c_date, '%Y-%m-%d'))) * 1000 for j in i['Values']: if (j['timestamp'] - 8*60*60)*1000 == c_timestamp: ts_value.append([c_timestamp, get_cluster_available(cluster, c_date)]) else: ts_value.append([(j['timestamp'] - 8*60*60)*1000, j['value']]) #这里要减去8个小时,是因为rrd里存的点的时刻是8点钟 data.append({'data':ts_value, 'name':cluster}) except: print traceback.format_exc() return JsonResponse({'code': 0, 'data': data, 'message': 'ok'})
def query_graph(self, request): fid = request.POST['fid'] range = request.POST['range'] end_ts = int(time.time()) start_ts = end_ts - int(range) container_obj = VirtualMachine.objects.get(fid=fid) endpoint = container_obj.fhostname #统计该组件对应的大metric有哪些 all_data = [] metric_list = [] f = Falcon() for metric,item_list in items['docker'].items(): metric_list.append(metric) counter_list = [i for i in item_list] history_data = f.get_history_data(start_ts, end_ts, [endpoint], counter_list, CF='AVERAGE') #print history_data[0] hdata = [] for i in history_data: hdata.append({'name':i['counter'], 'data':[[j['timestamp'], j['value'] ]for j in i['Values']]}) all_data.append({'metric':metric, 'hdata':hdata}) # 同时渲染文件 file = open('templates/service/chart_div_tmp.html') t = template.Template(file.read()) con = template.Context({'data': serialize_number(len(all_data), 2), 'metric_list': metric_list}) res = t.render(con) file.close() return JsonResponse({'code': 0, 'data': {'all_data': all_data, 'html_content': res}, 'message': 'ok'})
def authenticate(self, username, password): try: user_model = get_user_model() f = Falcon() ret = f.login(username, password) if not ret.has_key('error'): #获取falcon里该用户的信息详情,比如手机号、邮箱地址等,更新到OMS用户表 userinfo = f.get_userinfo(username) if not userinfo.has_key('error'): try: user = user_model._default_manager.get_by_natural_key( username) user.qq = userinfo.get('qq', '') user.cname = userinfo.get('cnname', '') user.phone = userinfo.get('phone', '') user.email = userinfo.get('email', '') user.role = userinfo.get('role', '') user.save() except user_model.DoesNotExist: user = user_model(username=username, cname=userinfo.get('cnname', ''), phone=userinfo.get('phone', ''), email=userinfo.get('email', ''), qq=userinfo.get('qq', ''), is_staff=1, role=userinfo.get('role', '')) user.save() return user except Exception, e: pass
def data(self, request): limit = int(request.GET['limit']) page = int(request.GET['page']) fhostname = request.GET['fhostname'] start_time = request.GET['start_time'] end_time = request.GET['end_time'] #将日期时间转换为时间戳 start_timestamp = '' end_timestamp = '' if start_time: start_timestamp = int( time.mktime(time.strptime(start_time, '%Y-%m-%d %H:%M:%S'))) if end_time: end_timestamp = int( time.mktime(time.strptime(end_time, '%Y-%m-%d %H:%M:%S'))) f = Falcon() #end_time = int(time.time()) # 必须要整形 #start_time = end_time - 5 * 86400 # 30分钟 if fhostname: endpoints = [fhostname] else: endpoints = [] data = f.get_eventcase(startTime=start_timestamp, endTime=end_timestamp, endpoints=endpoints) data = data[(page - 1) * limit:page * limit] return HttpResponse(json.dumps({'total': len(data), 'rows': data}))
def view(self, request): template_id = request.GET['id'] f = Falcon() hostgroups = f.get_hostgroups_of_template(template_id) #当前模板下已关联的机器组 current_hostgroup_ids = [i['id'] for i in hostgroups['hostgroups']] template_info = f.get_template_info(template_id) print template_info cur_uic = template_info['action']['uic'] return render(request, 'alarm/template_view.html', locals())
def queryevent(self, request): id = request.GET['id'] limit = int(request.GET['limit']) page = int(request.GET['page']) f = Falcon() data = f.get_events(event_id=id) total = len(data) data = data[(page - 1) * limit:page * limit] return HttpResponse(json.dumps({'total': total, 'rows': data}))
def templates(self, request): group_id = request.GET['id'] f = Falcon() data = f.get_templates_of_hostgroup(group_id) hostgroup_info = data['hostgroup'] data = data['templates'] all_template_list = f.get_all_template_list() all_template_list = all_template_list['templates'] return render(request, 'alarm/hostgroup_templates.html', locals())
def update(self, request): id = int(request.POST['id']) name = request.POST['resume'] resume = request.POST['resume'] user_id_list = request.POST.getlist('users') user_id_list = [int(i) for i in user_id_list] f = Falcon() print f.update_team(id, name, resume, user_id_list) return JsonResponse({'code': 0, 'data': '', 'message': '角色更新成功'})
def delete(self, request): try: id = request.POST['id'] print id f = Falcon() result = f.delete_expression(id) except: print traceback.format_exc() return FalconResponse(result)
def add(self, request): #创建/更新的页面 id = request.GET.get('id', 0) f = Falcon(request.user.username) expression_info = f.get_expression_info_by_id(id) print expression_info op_list = ['==', '!=', '<', '<=', '>', '>='] current_uic_list = expression_info['action']['uic'].split(',') all_uic = f.query_team() all_uic_list = [i['team']['name'] for i in all_uic] return render(request, 'alarm/expression_add.html', locals())
def add(self, request): name = request.POST['name'] resume = request.POST['resume'] user_list = request.POST.getlist('users') user_list = [int(i) for i in user_list] f = Falcon(request.user.username) print f.create_team(name, resume, user_list) return JsonResponse({ 'code': 0, 'data': '', 'message': '用户组【%s】创建成功' % name })
def query_service_top(self, request): try: cluster = request.POST['cluster'] date = request.POST['date'] topn = int(request.POST['topn']) start_ts = int(time.mktime(time.strptime(date, '%Y-%m-%d'))) end_ts = start_ts + 86400 #先插集群下有哪些ip和端口 endpoints = [] ports = [] for i in Service.objects.filter(fcluster=cluster): if i.fhost: endpoints.append(i.fhost) if i.fport: ports.append(i.fport) endpoints = list(set(endpoints)) ports = list(set(ports)) counters = ['%s/port=%s,project=oms' % (settings.port_listen_key, i) for i in ports] f = Falcon() history_data = f.get_history_data(start_ts, end_ts, endpoints, counters, step=60, CF='AVERAGE') print history_data names = [] values = [] data = {} for i in history_data: if i['Values']: host = i['endpoint'] tags = i['counter'].split('/')[1] tag_dict = gen_tags(tags) port = tag_dict['port'] if not Service.objects.filter(fhost=host,fport=port,fcluster=cluster): continue # 计算故障率 fail_count = 0 for j in i['Values']: if j['value'] == 0: fail_count += 1 fail_rate = '%.2f' % (float(fail_count) / len(i['Values']) * 100) fail_rate = float(fail_rate) data['%s:%s' % (host, port)] = fail_rate # 排序 if data: print data data = sorted(data.items(), key=lambda x: x[1], reverse=True)[:topn] for k, v in data: names.append(k) values.append(v) except: print traceback.format_exc() return JsonResponse({'code': 0, 'data': {'names': names, 'values': values}, 'message': 'ok'})
def update_template(self, request): print request.POST.dict() template_id = int(request.POST['template_id']) parent_id = int(request.POST['parent_id']) name = request.POST['name'] group_ids = request.POST.getlist('group_ids[]') #先更新模板基本信息 f = Falcon(request.user.username) result = f.update_template(template_id, parent_id, name) if result.has_key('error'): return JsonResponse({ 'code': 1, 'data': '', 'message': result['error'] }) #再更新机器组 hostgroups = f.get_hostgroups_of_template(template_id) # 当前模板下已关联的机器组 old_hostgroup_ids = [i['id'] for i in hostgroups['hostgroups']] current_hostgroup_ids = [int(i) for i in group_ids] need_add_hostgroup_ids = list( set(current_hostgroup_ids).difference(old_hostgroup_ids)) need_remove_hostgroup_ids = list( set(old_hostgroup_ids).difference(current_hostgroup_ids)) for i in need_add_hostgroup_ids: f.bindTemplate2HostGroup(template_id, i) for i in need_remove_hostgroup_ids: f.unbindTemplate2HostGroup(template_id, i) return JsonResponse({'code': 0, 'data': '', 'message': 'ok'})
def update(self, request): id = request.POST['id'] status = request.POST['status'] phone = request.POST['phone'] email = request.POST['email'] user_obj = OmsUser.objects.get(id=id) user_obj.is_active = int(status) user_obj.phone = phone user_obj.email = email user_obj.save() #同时更新falcon用户 f = Falcon() f.update_user(user_obj.cname, email, phone, qq='') return JsonResponse({'code': 0, 'data': '', 'message': '更新成功'})
def process_register(request): username = request.POST['username'] password = request.POST['password'] cname = request.POST['cname'] email = request.POST['email'] f = Falcon() ret = f.register(username, password, cname, email) if ret.has_key('error'): return JsonResponse({'code':1, 'data':'', 'message':ret['error']}) user = auth.authenticate(username=username, password=password) auth.login(request, user) return JsonResponse({'code':0, 'data':'', 'message':'用户注册成功!'})
def query_port(self, request): host = request.POST['host'] port = request.POST['port'] start_time = request.POST['start_time'] end_time = request.POST['end_time'] start_ts = int(time.mktime(time.strptime(start_time,'%Y-%m-%d %H:%M:%S'))) end_ts = int(time.mktime(time.strptime(end_time, '%Y-%m-%d %H:%M:%S'))) counter = '%s/port=%s,project=oms'%(settings.port_listen_key, port) f = Falcon() history_data = f.get_history_data(start_ts, end_ts, [host], [counter], CF='AVERAGE') hdata = [] for i in history_data: hdata.append({'name': i['endpoint'], 'data': [[j['timestamp'] * 1000, j['value']] for j in i['Values']]}) return JsonResponse({'code': 0, 'data': {'hdata': hdata}, 'message': 'ok'})
def main(self, request): # 统计集群数,服务数、机器数 cluster_count = Service.objects.values('fcluster').distinct().count() service_count = Service.objects.values('fhost', 'fname', 'fport').distinct().count() host_count = Service.objects.values('fhost').distinct().count() # 统计每个集群的健康度 status_info = [] cluster_list = [] success_rate_list = [] series = [] success_num_list = [] unknow_num_list = [] fail_num_list = [] for i in Service.objects.values('fcluster').distinct().order_by('fcluster'): fcluster = i['fcluster'] cluster_list.append(fcluster) success_num = int(Service.objects.filter(fcluster=fcluster, fstatus=1).count()) unknow_num = int(Service.objects.filter(fcluster=fcluster, fstatus=0).count()) fail_num = int(Service.objects.filter(fcluster=fcluster, fstatus=2).count()) total = success_num + unknow_num + fail_num success_num_list.append(success_num) unknow_num_list.append(unknow_num) fail_num_list.append(fail_num) if total == 0: success_rate = 100 else: success_rate = '%.2f' % (float(success_num) / total * 100) success_rate = float(success_rate) success_rate_list.append(success_rate) status_info.append({'fcluster': fcluster, 'success_num': success_num, 'unknow_num': unknow_num, 'fail_num': fail_num, 'total_num': success_num + unknow_num + fail_num}) # 统计最近10次的告警事件 host_list = [i['fhost'] for i in Service.objects.values('fhost').distinct()] for i in VirtualMachine.objects.all(): host_list.append(i.fhostname) host_list = list(set(host_list)) if not host_list: eventcase_list = [] else: f = Falcon() eventcase_list = f.get_eventcase(endpoints=host_list) eventcase_list = eventcase_list[0:10] # print eventcase_list return render(request, 'dashboard.html', locals())
def addhost(self, request): try: hostgroup_id = int(request.POST['hostgroup_id']) hostnames = request.POST['hostnames'] #先统计该机器组现有的机器列表 f = Falcon() result = f.get_hostgroup_info_by_id(hostgroup_id) hosts = result['hosts'] hostname_list = [i['hostname'] for i in hosts] hostname_list += hostnames.strip().split() hostname_list = list(set(hostname_list)) result = f.addHost2HhostGroup(hostgroup_id, hostname_list) except: print traceback.format_exc() return FalconResponse(result)
def query_graph2(self, request): fid = request.POST['fid'] counter = request.POST['counter'] range = request.POST['range'] end_ts = int(time.time()) start_ts = end_ts - int(range) service_obj = Service.objects.get(fid=fid) endpoint = service_obj.fhost f = Falcon() history_data = f.get_history_data(start_ts, end_ts, [endpoint], [counter], CF='AVERAGE') # print history_data[0] hdata = [] for i in history_data: hdata.append({'name': i['endpoint'], 'data': [[j['timestamp']*1000, j['value']] for j in i['Values']]}) return JsonResponse({'code': 0, 'data': {'hdata': hdata}, 'message': 'ok'})
def data(self, request): limit = int(request.GET['limit']) page = int(request.GET['page']) name = request.GET['name'] f = Falcon() result = f.get_all_expression_list() print result total = len(result) data = [] for i in result: if i['expression'].find(name) >= 0: data.append(i) data = data[(page - 1) * limit:page * limit] return HttpResponse(json.dumps({'total': total, 'rows': data}))
def get_role_info(self, request): try: id = request.POST['id'] f = Falcon() team_info = f.get_team_info_by_id(id) name = team_info['name'] resume = team_info['resume'] user_id_list = [int(i['id']) for i in team_info['users']] data = { 'name': name, 'resume': resume, 'user_id_list': user_id_list } except: print traceback.format_exc() return JsonResponse({'code': 0, 'data': data, 'message': 'ok'})
def data(self, request): limit = int(request.GET['limit']) page = int(request.GET['page']) name = request.GET['name'] f = Falcon() result = f.get_hostgroup_list(q=name) total = len(result) result = result[(page - 1) * limit:page * limit] data = [] for i in result: data.append({ 'id': i['id'], 'name': i['grp_name'], 'create_user': i['create_user'] }) return HttpResponse(json.dumps({'total': total, 'rows': data}))
def get_cluster_available(cluster, c_date): #获取某一天某个集群的可用率 start_ts = int(time.mktime(time.strptime(c_date, '%Y-%m-%d'))) end_ts = start_ts + 86400 host_list = [] port_list = [] weight_info = {} for j in Service.objects.filter(fcluster=cluster): if j.fhost: host_list.append(j.fhost) if j.fport: port_list.append(str(j.fport)) if j.fhost and j.fport: weight_info['%s_%s' % (j.fhost, j.fport)] = j.fweight host_list = list(set(host_list)) port_list = list(set(port_list)) counter_list = ['listen.port/port=%s,project=oms' % i for i in port_list] f = Falcon() data = f.get_history_data(start_ts, end_ts, host_list, counter_list, step=60) #print data success_count = 0 total = 0 for i in data: values = i['Values'] counter = i['counter'] endpoint = i['endpoint'] tags = counter.split('/')[1] tag_dict = gen_tags(tags) port = tag_dict['port'] if values: weight = weight_info.get('%s_%s' % (endpoint, port), 1) success_list = [j['value'] for j in values if j['value'] != 0] success_count += weight * len(success_list) total += weight * len(values) if success_count == 0: cluster_available_rate = 100 else: cluster_available_rate = float('%.2f' % (float(success_count) / total * 100)) return cluster_available_rate
def get_hosts(self, request): try: limit = int(request.GET['limit']) page = int(request.GET['page']) name = request.GET['name'] hostgroup_id = request.GET['hostgroup_id'] f = Falcon() result = f.get_hostgroup_info_by_id(hostgroup_id) data = [] for i in result['hosts']: if i['hostname'].find(name) >= 0: data.append({'id': i['id'], 'hostname': i['hostname']}) total = len(data) data = data[(page - 1) * limit:page * limit] except: print traceback.format_exc() return HttpResponse(json.dumps({'total': total, 'rows': data}))
def data(self, request): name = request.GET['name'] limit = int(request.GET['limit']) page = int(request.GET['page']) f = Falcon() data = f.query_team(q=name) print data total = len(data) data = data[(page - 1) * limit:page * limit] result = [] for i in data: result.append({ 'id': i['team']['id'], 'name': i['team']['name'], 'resume': i['team']['resume'], 'creator_name': i['creator_name'] }) return HttpResponse(json.dumps({'total': total, 'rows': result}))
def query_service_top(request): range = request.POST['range'] topn = int(request.POST['topn']) end_timestamp = int(time.time()) start_timestamp = end_timestamp - int(range) #先统计有endporints和couters endpoints = [] ports = [] for i in Service.objects.all(): if i.fhost: endpoints.append(i.fhost) if i.fport: ports.append(i.fport) endpoints = list(set(endpoints)) ports = list(set(ports)) counters = ['%s/port=%s,project=oms'%(settings.port_listen_key, i) for i in ports] f = Falcon() history_data = f.get_history_data(start_timestamp, end_timestamp, endpoints, counters, step=60, CF='AVERAGE') names = [] values = [] data = {} for i in history_data: if i['Values']: host = i['endpoint'] tags = i['counter'].split('/')[1] tag_dict = gen_tags(tags) port = tag_dict['port'] #计算故障率 fail_count = 0 for j in i['Values']: if j['value'] == 0: fail_count += 1 fail_rate = '%.2f'%(float(fail_count)/len(i['Values'])*100) fail_rate = float(fail_rate) data['%s:%s'%(host, port)] = fail_rate #排序 data = sorted(data.items(), key=lambda x: x[1], reverse=True)[:topn] for k, v in data: names.append(k) values.append(v) print values return JsonResponse({'code': 0, 'data': {'names':names, 'values':values}, 'message': 'ok'})
def update(self, request): template_id = request.GET['id'] f = Falcon() hostgroups = f.get_hostgroups_of_template(template_id) #当前模板下已关联的机器组 current_hostgroup_ids = [i['id'] for i in hostgroups['hostgroups']] template_info = f.get_template_info(template_id) print template_info all_template_list = f.get_all_template_list() all_template_list = all_template_list['templates'] print all_template_list all_hostgroup_list = f.get_all_hostgroup_list() op_list = ['==', '!=', '<', '<=', '>', '>='] cur_uic = template_info['action']['uic'] cur_uic_list = cur_uic.split(',') all_uic = f.query_team() all_uic_list = [i['team']['name'] for i in all_uic] return render(request, 'alarm/template_update.html', locals())
def delete(self, request): id = request.POST['id'] f = Falcon() print f.delete_team(id) return JsonResponse({'code': 0, 'data': '', 'message': '删除成功'})
def get(self, request): f = Falcon() user_list = f.get_user_list() return render(request, 'rbac/role.html', locals())