Exemplo n.º 1
0
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}))
Exemplo n.º 2
0
def bp_access(request, cc_biz_id):
    """
    基础性能接入
    """
    args = request.POST
    hostid_list = args.get("hostid_list", "")
    ip_list = None
    if hostid_list:
        hostid_list = hostid_list.split(",")
        # 置顶ip列表接入
        ip_list = []
        for hostid in hostid_list:
            ip, plat_id = parse_host_id(hostid)
            ip_list.append({
                "ip": ip,
                "plat_id": plat_id,
            })
    try:
        result = access_performance(cc_biz_id, ip_list)
        if result.get("result"):
            return render_json(ok())
        else:
            return render_json(failed(u"基础性能接入失败:%s" % result.get("message")))
    except Exception as e:
        logger.exception(u"基础性能接入失败:%s" % e)
        return render_json(failed(u"基础性能接入失败,请联系管理员"))
Exemplo n.º 3
0
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"监控不存在", {}))
Exemplo n.º 4
0
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'
            }))
Exemplo n.º 5
0
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'
            }))
Exemplo n.º 6
0
def get_agent_status(request, cc_biz_id):
    """
    获取主机agent状态,
    """
    host_id = request.GET.get("host_id")
    if not host_id:
        return render_json(failed(u"无效请求"))
    agent_status = Host.get_agent_status_by_hostid(cc_biz_id,
                                                   host_id).get(host_id)
    return render_json(ok_data({'status': agent_status}))
Exemplo n.º 7
0
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())
Exemplo n.º 8
0
def get_alarm_count(request, cc_biz_id):
    """
    获取一串主机某日的告警事件数
    """
    args = request.GET
    alarm_date = args.get("alarm_date")
    host_id_list = args.get("host_id_list")
    if not host_id_list:
        return render_json(failed(u"无效的请求"))
    host_id_list = host_id_list.split(',')
    alarm_count_info = AlarmInstance.get_alarm_count_by_host_id_list(
        host_id_list, cc_biz_id, alarm_date)
    return render_json(ok_data({"alarm_count_info": alarm_count_info}))
Exemplo n.º 9
0
def get_alarm_list(request, cc_biz_id):
    """
    获取ip关联的告警事件列表
    """
    args = request.GET
    alarm_date = args.get("alarm_date")
    host_id = args.get("host_id")
    if not host_id:
        return render_json(failed(u"无效的请求"))
    alarm_list = AlarmInstance.get_alarm_list_by_host_id(
        host_id, cc_biz_id, alarm_date)
    alarm_info_list = filter(lambda alarm: alarm,
                             map(lambda a: a.detail, alarm_list))
    return render_json(ok_data({"alarm_info_list": alarm_info_list}))
Exemplo n.º 10
0
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))
Exemplo n.º 11
0
def get_alarm_strategy_list(request, cc_biz_id):
    """
    获取ip关联的告警策略列表
    """

    strategy_info_list = list()
    args = request.GET
    host_id = args.get("host_id")
    if not host_id:
        return render_json(failed(u"无效的请求"))
    alarm_strategys = AlarmStrategy.objects.get_strategy_by_host_id(
        host_id, cc_biz_id)
    for strategy in alarm_strategys:
        item = AlarmStrategy.objects.get_alarm_strategy_info(strategy)
        if item is not None:
            strategy_info_list.append(item)
    return render_json(ok_data(strategy_info_list))
Exemplo n.º 12
0
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())
Exemplo n.º 13
0
def stick_host(request, cc_biz_id):
    """
    主机置顶/取消置顶
    """
    args = request.POST
    host_id = args.get("host_id")
    if not (host_id and "|" in host_id):
        return render_json(failed(u"无效请求"))
    ip, plat_id = parse_host_id(host_id)
    MonitorHostSticky.objects.filter(host=ip,
                                     plat_id=plat_id,
                                     cc_biz_id=cc_biz_id).delete()
    if args.get("action") == "add":
        hs = MonitorHostSticky(host=ip, plat_id=plat_id, cc_biz_id=cc_biz_id)
        hs.save()
        return render_json(ok_data(hs.id))
    return render_json(ok_data(0))
Exemplo n.º 14
0
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))
Exemplo n.º 15
0
def graph_point(request, cc_biz_id):
    """
    获取图表数据
    """
    args = request.GET
    time_range = None
    single_graph_date = args.get("graph_date")
    host_id = request.GET.get("host_id", "")
    index_id = args.get("index_id", "")
    ip = plat_id = index_obj = ""
    with ignored(Exception):
        # 主机信息
        ip, plat_id = parse_host_id(host_id)
        # 需要转换平台id(gse的0 等于 cc的1)........................
        plat_id = "0" if plat_id == "1" else plat_id
        index_obj = HostIndex.get(id=index_id)
        # 时间解析
        if single_graph_date:
            start = datetime.datetime.strptime(single_graph_date, "%Y-%m-%d")
            end = start + datetime.timedelta(days=1)
            time_range = "%s -- %s" % (start.strftime("%Y-%m-%d %H:%M"),
                                       end.strftime("%Y-%m-%d %H:%M"))
    if not all([ip, plat_id, index_obj]):
        return render_json(failed(u"无效的请求"))
    try:
        unit = index_obj.unit_display
        conversion = float(index_obj.conversion)
        ext_kw = {
            "unit": unit,
            "conversion": conversion,
            "series_label_show": index_obj.desc
        }
        kwargs = {"time_range": time_range, "ip": ip, "cloud_id": str(plat_id)}
        result_table_id = index_obj.result_table_id
        value_field = index_obj.item
        group_field = index_obj.dimension_field
        rt_id = "%s_%s" % (cc_biz_id, result_table_id)
        if group_field:
            ext_kw["group_field"] = group_field
        series_suffix = ""
        dimension_field = args.get("dimension_field",
                                   index_obj.dimension_field)
        dimension_value = args.get("dimension_field_value", "")
        if dimension_field and dimension_value:
            kwargs[dimension_field] = dimension_value
        series_name_list = []
        series_list = []
        from utils.trt import trans_bkcloud_rt_bizid
        rt_id = trans_bkcloud_rt_bizid(rt_id)
        data = DataProcessor.base_performance_data(rt_id, value_field, kwargs,
                                                   **ext_kw)
        for series_name in data["series_name_list"]:
            series_name_list.append(series_name + series_suffix)
        for series in data["series"]:
            if series["name"] in [u"本周期"]:
                series["zIndex"] = 5
            series["name"] += series_suffix
            series_list.append(series)
        data["series"] = series_list
        data["series_name_list"] = series_name_list
        data["min_y"] = min(data["min_y"], 0)
        data["max_y"] = max(data["max_y"], 0)
        if data["min_y"] == data["max_y"]:
            if data["max_y"] == 0:
                data["yaxis_range"] = "0:1"
            if data["max_y"] > 0:
                data["yaxis_range"] = "0:"
            if data["max_y"] < 0:
                data["yaxis_range"] = ":0"
        data = {'data': data, 'echo_sql': data['echo_sql']}
        del data["data"]["echo_sql"]
        data["update_time"] = datetime.datetime.now()
        return render_json(ok_data(data))
    except EmptyQueryException:
        return render_json(
            failed_data(u"查询无数据,请确认该主机(%s)的数据上报是否正常!" % ip, {
                'echo_sql': "",
                'error_class': 'info'
            }))
    except SqlQueryException as e:
        logger.exception(u"查询失败,原因: %s" % e)
        return render_json(
            failed_data(u"查询失败,原因: %s" % e, {
                'echo_sql': "",
                'error_class': 'warning'
            }))
    except TableNotExistException as e:
        logger.exception(e)
        return render_json(
            failed_data(
                u"查询无数据!", {
                    'echo_sql': "",
                    'error_class': 'info',
                    'need_access': "need_access"
                }))
    except Exception as e:
        logger.exception(u"后台异常: %s" % e)
        return render_json(
            failed_data(u"生成图表异常", {
                'echo_sql': "",
                'error_class': 'critical'
            }))
Exemplo n.º 16
0
def get_operation_monitor_alert_list(request, cc_biz_id):
    # 获取告警列表
    args = request.GET
    time_range = args.get("time_range")
    time_range = time_range.replace('&nbsp;', " ")
    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))
Exemplo n.º 17
0
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}))