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 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())