def get_alarm_detail_chart_list(request): """新监控系统告警详情页-获取关联视图以及每个视图内有哪些图表""" result = {'result': True, 'message': u'', 'err_code': 0, 'data': {"views": [], 'main_chart': {}}} base_info = get_alarm_detail_base_info(request) if not base_info.get('result', False): result['result'] = False result['message'] = base_info.get('message', '') return render_json(result) # 告警详情与监控配置 alarm = base_info['alarm'] monitor = base_info['monitor'] cc_biz_id = request.GET.get("biz_id") if not (check_permission(alarm, cc_biz_id) or check_permission(monitor, cc_biz_id)): result['result'] = False result['message'] = u"无权限查看" return render_json(result) if monitor.scenario in ["performance", "custom"]: result['data']['main_chart'] = { "chart_title": monitor.monitor_name, "alarm_id": alarm.id, "monitor_id": monitor.backend_id } # 关联场景 if monitor.scenario == "performance": monitor_views = MonitorLocation.objects.\ get_scenario_menu_list_by_monitor_id(monitor.id) for view_info in monitor_views: menu_info = view_info menu_charts = MonitorLocation.objects.\ get_monitor_list_by_scenario_menu(menu_info.id) charts = [] # 每个关联场景的图表 for chart in menu_charts: if chart.id == monitor.id: # 当前monitor_id是大图, 不加入列表 continue chart_config = { "chart_title": chart.monitor_name, "alarm_id": alarm.id, "monitor_id": chart.backend_id } charts.append(chart_config) view_config = { 'title': menu_info.name, 'id': menu_info.id, 'charts': charts } result['data']['views'].append(view_config) return render_json(result)
def favorite_toggle(request, cc_biz_id): # 收藏 or 取消收藏 args = request.POST monitor_id = args.get("monitor_id") try: monitor = Monitor.objects.get(pk=monitor_id) except JAItemDoseNotExists: return render_json(failed(u"监控不存在")) if monitor.is_deleted: return render_json(failed(u"监控不存在")) if not check_permission(monitor, cc_biz_id): return render_json(failed(u"无权限")) try: sm = ScenarioMenu.objects.get(is_deleted=False, system_menu=u"favorite", biz_id=cc_biz_id) except: return render_json(failed(u"收藏夹未创建")) m = MonitorLocation.objects.filter(is_deleted=False, monitor_id=monitor_id, menu_id=sm.id, biz_id=cc_biz_id) if m.exists(): m.update(is_deleted=True) return render_json(ok_data({'status': False})) else: MonitorLocation(biz_id=cc_biz_id, monitor_id=monitor_id, menu_id=sm.id).save() return render_json(ok_data({'status': True}))
def operation_graph_panel(request, cc_biz_id): # 渲染右侧图表面板 menu_id = request.GET.get("menu_id") try: if not menu_id: # 拿收藏 menu = ScenarioMenu.objects.get(biz_id=cc_biz_id, system_menu="favorite") else: menu = ScenarioMenu.objects.get(pk=menu_id) except: return HttpResponse(u"无效的请求") if not check_permission(menu, cc_biz_id): return HttpResponseForbidden(u"无权限") # 获取对应的监控视图 locations = MonitorLocation.objects.filter( menu_id=menu.id, biz_id=cc_biz_id).order_by("graph_index") invalid_location_ids = list() for l in locations: # if not l.monitor or l.monitor.is_deleted or not l.monitor.is_enabled: if not l.monitor or l.monitor.is_deleted: invalid_location_ids.append(l.pk) l.is_deleted = True l.save() locations = locations.exclude(pk__in=invalid_location_ids) menu_id = menu.id value_method_desc = VALUE_METHOD_DESC return render_mako_context(request, '/monitor/dataview/operation/right_panel.html', locals())
def alarm_detail(request, cc_biz_id, alarm_id): """ 单页的告警流程页面 2015.10.28 graph = [ ({1: ['~success']}, u'155'), ({2: ['~success'], 3: ['failure']}, u'1134'), ({5: ['success']}, u'1961'), ({4: ['success']}, u'1958'), ({5: ['success']}, u'1962'), ({6: ['~success']}, u'807'), ({7: ['~success']}, u'433'), ({8: ['success']}, u'367'), ({9: ['success']}, u'1958'), ({10: ['~success']}, u'1968'), ({11: ['success']}, u'480'), ({12: ['success']}, u'361'), ({13: ['success']}, u'1965'), ({}, u'360') ] """ try: alarm = AlarmInstance.objects.get(id=alarm_id) except: return HttpResponseForbidden(u"告警不存在") if not check_permission(alarm, cc_biz_id): return HttpResponseForbidden(u"无权限查看") related_alarms = [alarm] return render_mako_context( request, '/monitor/event_center/alarm_detail_new.html', locals())
def get_alarm_detail_sub_chart_data(request): """新监控系统告警详情页-获取关联视图内的其他图型数据""" result = {'result': True, 'message': u'', 'err_code': 0, 'data': {}} base_info = get_alarm_detail_base_info(request) if not base_info.get('result', False): result['result'] = False result['message'] = base_info.get('message', '') return render_json(result) # 告警详情与监控配置 alarm = base_info['alarm'] monitor = base_info['monitor'] cc_biz_id = request.GET.get("biz_id") if not (check_permission(alarm, cc_biz_id) or check_permission(monitor, cc_biz_id)): result['result'] = False result['message'] = u"无权限查看" return render_json(result) try: monitor_dimensions = [d.field for d in monitor.dimensions] alarm_dimensions = alarm.dimensions common_dimensions = {} for dimension_key in monitor_dimensions: if dimension_key in alarm_dimensions.keys(): common_dimensions[dimension_key] = alarm_dimensions[dimension_key] result_table_id = monitor.monitor_result_table_id monitor_field = monitor.monitor_field alarm_source_time_date = arrow.get( alarm.source_time ).format("YYYYMMDD") series_name = get_dimensions_display( common_dimensions, alarm, result_table_id ) step = get_time_step(result_table_id, monitor.generate_config_id) * 1000 result['data'] = gen_highchart_data_config( get_data( result_table_id, monitor_field, common_dimensions, date=alarm_source_time_date ), series_name, step) except Exception, e: logger.exception(u'查询监控数据失败:%s' % e) result['result'] = False result['message'] = u'查询监控数据失败'
def monitor_info(request, cc_biz_id): monitor_id = request.GET.get("monitor_id") if monitor_id: monitor = Monitor.objects.filter(pk=monitor_id) if monitor: if monitor[0].is_deleted: return render_json(failed(u"监控不存在")) if not check_permission(monitor[0], cc_biz_id): return render_json(failed(u"无权限")) monitor_info_dict = dict() for k in monitor[0].__dict__.keys(): if not k.startswith("_"): monitor_info_dict[k] = getattr(monitor[0], k, "") return render_json(ok_data(monitor_info_dict)) return render_json(failed_data(u"监控不存在", {}))
def graph_detail_point(request, cc_biz_id): start = time.time() args = request.POST monitor_id = args.get("monitor_id", "") try: m = Monitor.objects.get(pk=monitor_id) except JAItemDoseNotExists: return render_json(failed(u"监控不存在")) if m.is_deleted: return render_json(failed(u"监控不存在")) if not check_permission(m, cc_biz_id): return render_json(failed(u"无权限")) rt_id = m.monitor_result_table_id value_method = args.get("value_method", m.count_method) value_field = "%s(%s)" % (value_method.lower(), m.monitor_field) if value_method == "count": value_field = "count(*)" group_field = ",".join([d.field for d in m.dimensions]) datestr = args.get("date_str") filter_str = args.get("filter_str") h = HTMLParser.HTMLParser() filter_str = unicodedata.normalize("NFKD", h.unescape(filter_str).strip()) kwargs = {} if not datestr: kwargs["time_range"] = get_time_range(datetime.datetime.now()) else: kwargs["time_range"] = get_time_range_by_datestr(datestr) try: data = DataProcessor.make_multiple_graph_point(rt_id, value_field, group_field, params=kwargs, filter_str=filter_str, monitor_id=m.id) data = {'data': data, 'echo_sql': data['echo_sql']} del data["data"]["echo_sql"] spend_time = str(round(time.time() - start, 2)) data["spend_time"] = spend_time data["update_time"] = datetime.datetime.now() return render_json(ok_data(data)) except SqlQueryException, e: logger.exception(u"查询失败,原因: %s" % e) return render_json( failed_data(u"数据查询异常,请联系管理员!", { 'echo_sql': "", 'error_class': 'info' }))
def delete_monitor_location(request, cc_biz_id): # 删除监控视图 args = request.POST location_id = args.get("location_id") del_monitor = args.get("del_monitor") location = MonitorLocation.objects.get(pk=location_id) if not check_permission(location, cc_biz_id): return render_json(failed(u"无权限查看")) location.is_deleted = True location.save() if del_monitor: MonitorLocation.objects.filter( biz_id=cc_biz_id, is_deleted=False, monitor_id=location.monitor_id).update(is_deleted=True) # close monitor Monitor.objects.get(pk=location.monitor_id).delete() return render_json(ok())
def get_operation_monitor_point(request, cc_biz_id): start = time.time() kwargs = {} config_key_words = ["monitor_id", "value_method"] monitor_id = request.GET.get("monitor_id", "") try: m = Monitor.objects.get(pk=monitor_id) except JAItemDoseNotExists: return render_json(failed(u"监控不存在")) if m.is_deleted: return render_json(failed(u"监控不存在")) if not check_permission(m, cc_biz_id): return render_json(failed(u"无权限查看")) rt_id = m.monitor_result_table_id value_method = request.GET.get("value_method", "sum") value_field = "%s(%s)" % (value_method.lower(), m.monitor_field) if value_method == "count": value_field = "count(*)" h = HTMLParser.HTMLParser() for k, v in request.GET.items(): if k not in config_key_words: v = unicodedata.normalize("NFKD", h.unescape(v).strip()) # v = v.replace(' ', " ") if v: kwargs[k] = v if "time_range" not in kwargs: kwargs["time_range"] = get_time_range(datetime.datetime.now()) try: data = DataProcessor.operation_monitor_data(rt_id, value_field, kwargs, monitor_id=m.id) data = {'data': data, 'echo_sql': data['echo_sql']} # del data["data"]["echo_sql"] spend_time = str(round(time.time() - start, 2)) data["spend_time"] = spend_time data["update_time"] = datetime.datetime.now() return render_json(ok_data(data)) except SqlQueryException, e: logger.exception(u"查询失败,原因: %s" % e) return render_json( failed_data(u"数据查询异常,请联系管理员!", { 'echo_sql': "", 'error_class': 'info' }))
def graph_detail(request, cc_biz_id): # 展示图表详情弹层 monitor_id = request.GET.get("monitor_id", "") if not monitor_id: return HttpResponse(u"无效请求") try: m = Monitor.objects.get(pk=monitor_id) except JAItemDoseNotExists: return render_json(failed(u"监控不存在")) if m.is_deleted: return render_json(failed(u"监控不存在")) if not check_permission(m, cc_biz_id): return render_json(failed(u"无权限")) fields = m.dimensions rt_id = m.monitor_result_table_id value_method = request.GET.get("value_method", m.count_method) field_id_list = [f.field for f in fields] field_values = get_field_results(rt_id, ",".join(field_id_list)) if field_values and len(field_id_list) == 1: field_values = [[i] for i in field_values] return render_mako_context( request, "/monitor/dataview/operation/graph_detail.html", locals())
def get_filter_field_results_by_monitor(request, cc_biz_id): """ 获取菜单上,过滤参数的所有取值范围, """ args = request.POST monitor_id = args.get("monitor_id") field = args.get("field") time_range = args.get("time_range") m = Monitor.objects.get(pk=monitor_id) if not check_permission(m, cc_biz_id): return render_json(failed(u"无权限")) result_table_id = m.monitor_result_table_id # 过滤条件 ext_filter = args.get("ext_filter") or "{}" ext_filter_dict = { k: v for k, v in (json.loads(ext_filter) or {}).items() if v } data = get_field_values_options(result_table_id, field, ext_filter=ext_filter_dict, time_range=time_range) return render_json(ok_data(data))
def get_all_field_values(request, cc_biz_id): # 获取所有维度对应的值 monitor_id = request.GET.get("monitor_id", "") if not monitor_id: return HttpResponse(u"无效请求") try: m = Monitor.objects.get(pk=monitor_id) except JAItemDoseNotExists: return render_json(failed(u"监控不存在")) if m.is_deleted: return render_json(failed(u"监控不存在")) if not check_permission(m, cc_biz_id): return render_json(failed(u"无权限")) fields = m.dimensions rt_id = m.monitor_result_table_id field_id_list = [f.field for f in fields] date_str = request.GET.get("date_str") field_values = get_field_results( rt_id, ",".join(field_id_list), time_range=get_time_range_by_datestr(date_str)) if field_values and len(field_id_list) == 1: field_values = [[i] for i in field_values] return render_json(ok_data(field_values))
def add_monitor_loaction(request, cc_biz_id): if request.method == "GET": # 新增视图modal弹框 redirect_url = "" menu_id = request.GET.get("menu_id") is_overview = request.GET.get("overview") # 列出在此菜单下的所有视图 menu_locations = MonitorLocation.objects.filter(menu_id=menu_id, is_deleted=False, biz_id=cc_biz_id) exists_monitor_ids = set([l.monitor_id for l in menu_locations]) monitors = Monitor.objects.filter(is_deleted=False, biz_id=cc_biz_id, scenario="custom") monitors_ids = set( [m.id for m in monitors if m.id not in exists_monitor_ids]) group_monitors = [] # 按分组列出监控视图 menus = ScenarioMenu.objects.filter(biz_id=cc_biz_id, is_deleted=False) for menu in menus: if menu.system_menu != u"favorite": # logger.info("menu_id: %s, biz_id: %s" % (menu.id, cc_biz_id)) this_menu_locations = MonitorLocation.objects.filter( menu_id=menu.id, biz_id=cc_biz_id) this_monitors = [] for l in this_menu_locations: if (l.monitor and not l.monitor.is_deleted and l.monitor.id not in exists_monitor_ids): this_monitors.append(l.monitor) if l.monitor.id in monitors_ids: monitors_ids.remove(l.monitor.id) group_monitors.append([menu.name, this_monitors]) none_menu_monitors = Monitor.objects.filter( id__in=",".join(map(str, monitors_ids))) if monitors_ids else [] group_monitors.append([u"未分组", none_menu_monitors]) return render_mako_context( request, '/monitor/dataview/operation/modal_add_location.html.part', locals()) else: # 新增视图 menu_id = request.POST.get("menu_id") monitor_id = request.POST.get("monitor_id") try: monitor = Monitor.objects.get(pk=monitor_id) except JAItemDoseNotExists: return render_json(failed(u"监控不存在")) if monitor.is_deleted: return render_json(failed(u"监控不存在")) if not check_permission(monitor, cc_biz_id): return render_json(failed(u"无权限")) if MonitorLocation.objects.filter(biz_id=cc_biz_id, menu_id=menu_id, monitor_id=monitor_id).exists(): return render_json(failed(u"监控已经在分组中了")) new_location = MonitorLocation(biz_id=cc_biz_id, menu_id=menu_id, monitor_id=monitor_id) new_location.save() value_method_desc = VALUE_METHOD_DESC html = render_mako_context( request, '/monitor/dataview/operation/one_panel.html.part', locals()).content return render_json(ok_data({"html": html}))
def get_operation_monitor_alert_list(request, cc_biz_id): # 获取告警列表 args = request.GET time_range = args.get("time_range") time_range = time_range.replace(' ', " ") start_time, end_time = get_date_range(time_range) monitor_id = args.get("monitor_id") try: monitor = Monitor.objects.get(pk=monitor_id) except JAItemDoseNotExists: return render_json(failed(u"监控不存在")) if monitor.is_deleted: return render_json(failed(u"监控不存在")) if not check_permission(monitor, cc_biz_id): return render_json(failed(u"无权限")) alerts = AlarmInstance.get_alarm_by_monitor_id(start_time, end_time, monitor_id) filter_dict = dict() for k, v in request.GET.items(): if k not in ["time_range", "monitor_id", "value_method"]: filter_dict[k] = v if filter_dict.keys(): alerts = filter_alarms(alerts, filter_dict) alert_group_dict = {} # 聚合 for alert in alerts: hit = True for k, v in filter_dict.items(): if k in alert.dimensions: if v and v != str(alert.dimensions[k]): hit = False else: if v: hit = False if not hit: continue alert_time_point = datetime.datetime.strptime( alert.source_time.strftime('%Y-%m-%d %H:%M:00'), '%Y-%m-%d %H:%M:%S') alert.source_time = alert_time_point if alert_time_point not in alert_group_dict: setattr(alert, "alert_count", 1) setattr(alert, "alert_count_info", {1: 0, 2: 0, 3: 0}) setattr(alert, "alert_ids", [alert.id]) alert.alert_count_info[alert.level] += 1 alert_group_dict[alert_time_point] = alert else: old_alert = alert_group_dict[alert_time_point] new_alert = alert if old_alert.level > alert.level else old_alert setattr(new_alert, "alert_count", old_alert.alert_count) setattr(new_alert, "alert_ids", old_alert.alert_ids) setattr(new_alert, "alert_count_info", old_alert.alert_count_info) new_alert.alert_count += 1 new_alert.alert_ids.append(alert.id) new_alert.alert_count_info[alert.level] += 1 alert_group_dict[alert_time_point] = new_alert # 影响范围 data = get_group_of_alarm(alert_group_dict.values()) data["interval"] = monitor.result_table.count_freq * 1000 return render_json(ok_data(data))
def get_alarm_detail_main_chart_data(request): """新监控系统告警详情页-获取关联视图内的大图数据""" result = {'result': True, 'message': u'', 'err_code': 0, 'data': {}} base_info = get_alarm_detail_base_info(request) if not base_info.get('result', False): result['result'] = False result['message'] = base_info.get('message', '') return render_json(result) # 告警详情与监控配置 alarm = base_info['alarm'] monitor = base_info['monitor'] cc_biz_id = request.GET.get("biz_id") if not (check_permission(alarm, cc_biz_id) or check_permission(monitor, cc_biz_id)): result['result'] = False result['message'] = u"无权限查看" return render_json(result) try: result_table_id = monitor.monitor_result_table_id monitor_field = monitor.monitor_field dimensions = alarm.dimensions alarm_source_time_date = arrow.get( alarm.source_time ).format("YYYYMMDD") if monitor.scenario == "custom": series_name = alarm.get_dimensions_display() step = get_time_step( result_table_id, monitor.generate_config_id ) * 1000 result['data'] = gen_highchart_data_config( get_data( result_table_id, monitor_field, dimensions, method=monitor.count_method, date=alarm_source_time_date ), series_name, step ) else: # 基础性能监控 key_alias = get_key_alias(result_table_id) hostindex = HostIndex.objects.get(id=monitor.monitor_target) host_dimensions = { "ip": alarm.ip } if hostindex.dimension_field and hostindex.dimension_field in dimensions: host_dimensions[hostindex.dimension_field] = dimensions[hostindex.dimension_field] series_name = " | ".join( ["%s:%s" % (JUNGLE_SUBJECT_TYPE.get(k, key_alias.get(k, k)), v) for k, v in host_dimensions.items()] ) result['data'] = gen_highchart_data_config( get_data( result_table_id, hostindex.item, host_dimensions, method="avg", date=alarm_source_time_date, conversion=hostindex.conversion) , series_name, 60 * 1000) result['data']["unit"] = hostindex.conversion_unit.replace( u"个", u"" ) # 设置 告警信息 try: setattr(alarm, "alert_count", 1) setattr(alarm, "alert_ids", [alarm.id]) result['alarm_info'] = get_group_of_alarm([alarm]) except Exception, e: result['alarm_info'] = {} logger.exception(u'告警详情-告警分组异常 %s' % e) except Exception, e: logger.exception(u'查询监控数据失败:%s' % e) result['result'] = False result['message'] = u'查询监控数据失败'