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 get_task_list(request, cc_biz_id):
    solution_type = request.GET.get("solution_type")
    solution_model = SolutionConf.solution_model(solution_type)
    if solution_model is None:
        return render_json(failed_data(u"无效的请求", []))
    task_list = solution_model.task_list(cc_biz_id)
    return render_json(ok_data(task_list))
Exemplo n.º 3
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.º 4
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.º 5
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.º 6
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.º 7
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.º 8
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.º 9
0
def host_index_list(request, cc_biz_id):
    """
    获取单个主机性能列表
    """
    def index_to_dict(host_index_obj, dimension_field_value=""):
        return {
            "description": host_index_obj.desc,
            "category": host_index_obj.get_category_display(),
            "dimension_field": host_index_obj.dimension_field,
            "dimension_field_value": dimension_field_value,
            "index_id": host_index_obj.id,
            "unit_display": host_index_obj.unit_display,
        }

    all_graphs = list()
    index_info = HostIndex.get_bp_graph_index()
    bp_index_list = [
        _index for index_list in index_info.values() for _index in index_list
    ]
    dimension_values_cache = {}
    for bp_index in bp_index_list:
        # 是否维度细分
        if bp_index.dimension_field and bp_index.dimension_field == "name":
            from utils.trt import trans_bkcloud_rt_bizid
            rt_id = trans_bkcloud_rt_bizid(
                "%s_%s" % (cc_biz_id, bp_index.result_table_id))
            cache_key = "%s:%s" % (rt_id, bp_index.dimension_field)
            if cache_key not in dimension_values_cache:
                dimension_values = get_field_results(rt_id,
                                                     bp_index.dimension_field)
                dimension_values_cache[cache_key] = dimension_values
            else:
                dimension_values = dimension_values_cache[cache_key]
            # 网卡拆分 这里列出了业务下所有主机的eth网卡,在前端需要根据ip展示。
            eth_values = filter(lambda eth: str(eth).strip().startswith("eth"),
                                dimension_values)
            if eth_values:
                for v in eth_values:
                    all_graphs.append(index_to_dict(bp_index, v))
                continue
        all_graphs.append(index_to_dict(bp_index))
    categories_sorted_list = [i[1] for i in HostIndex.CATEGORY_CHOICES]
    all_graphs = sorted(
        all_graphs,
        key=lambda x: categories_sorted_list.index(x["category"])
        if x["category"] in categories_sorted_list else 10000)
    return render_json(ok_data(all_graphs))
Exemplo n.º 10
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.º 11
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.º 12
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.º 13
0
def host_index_option_list(request, cc_biz_id):
    """
    主机性能列表,select2展示参数
    :param request:
    :param cc_biz_id:
        ("cpu", u"CPU"),
        ("net", u"网卡"),
        ("mem", u"内存"),
        ("disk", u"磁盘"),
        ("process", u"进程"),
    """

    info_list = []
    fields = {
        'cpu': u"CPU",
        'net': u"网卡",
        'mem': u"内存",
        'disk': u"磁盘",
        # 'process': u"进程",
    }
    for _f in fields:
        # 只过滤需要展示的内容
        hosts = HostIndex.objects.filter(graph_show=True, category=_f)
        data = []
        for _h in hosts:
            info = dict(id=_h.id,
                        item=_h.item,
                        text=_h.desc,
                        unit=_h.unit_display)
            data.append(info)
        info_list.append({"text": fields[_f], "children": data})
    categories_sorted_list = [i[1] for i in HostIndex.CATEGORY_CHOICES]
    info_list = sorted(info_list,
                       key=lambda x: categories_sorted_list.index(x["text"])
                       if x["text"] in categories_sorted_list else 10000)
    info_list.append(BaseHostIndex.get_base_hostindex_options())
    return render_json(ok_data(info_list))
Exemplo n.º 14
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.º 15
0
def index(request, cc_biz_id):
    """
    request.method:
    get:    基础性能页面首页
    post:   主机性能信息,包括主机属性,主机各指标信息。
    """
    def get_access_status(cc_biz_id):
        """
        获取数据平台基础性能接入进展
        :return: {
            "accessed": True,
            "div_message": u"正在接入中",
            "btn_message": u"确定"
        }
        """
        btn_message = u"确定"
        div_message = u"您的性能指标采集任务已下发正在进行中,请稍候再试!"
        # step 1 获取该业务下基础性能接入的data_id
        data_id_exist = any(get_bp_data_id(cc_biz_id))
        # step 2 如果data_id 不存在,则直接返回未接入
        if not data_id_exist:
            div_message = (u"检测到您的业务尚未开启监控,"
                           u"点击下面按钮开启主机指标采集,\n"
                           u"10-20分钟后刷新此页面即可查看到主机数据,请耐心等待!\n"
                           u"(未安装agent的主机,"
                           u"在主机详情页中按照指引完成部署并开启数据采集。)")
            btn_message = u"开始采集"
        return {
            "accessed": data_id_exist,
            "div_message": div_message,
            "btn_message": btn_message
        }

    data = {
        "hosts": [],
        "update_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
    }
    if request.method == "GET":
        count_per_page = 10
        # 获取云区域信息
        plat_info = CCBiz.plat_info(cc_biz_id)
        checkhost_id = request.GET.get("alarm_strategy_id")
        return render_mako_context(request, '/monitor/performanceV2/home.html',
                                   locals())
    ts = TimeStats(u"get_host_info_list: %s" % cc_biz_id)
    # 获取所有主机信息(前台分页)
    ts.split(u"获取主机列表")
    hosts = CCBiz.hosts(cc_biz_id).get("data") or []
    host_ids = set([host_key(h) for h in hosts])
    ts.split(u"获取主机agent状态")
    hosts_agent_status = CCBiz.agent_status(cc_biz_id, host_ids)
    default_status_info = {
        'alarm': {
            'level': [0, 0, 0],
        },
        'component': [],
    }
    try:
        # 主机性能数据结果表字典,当性能获取需要查询之前查过的结果表的时候,
        # 直接使用已有的client,节省查询时间。
        # cpu 总使用率
        ts.split(u"获取主机cpu 总使用率")
        cpu_usage_info = HostIndex.get_host_performance(
            cc_biz_id,
            item="cpuusage",
            category="cpu",
            result_table_id="ja_gse_cpu_cpuusage")
        # CPU 5分钟负载
        ts.split(u"获取主机cpu 5分钟负载")
        cpu_load_info = HostIndex.get_host_performance(
            cc_biz_id,
            item="locdavg",
            category="cpu",
            result_table_id="ja_gse_cpu")
        # cpu 单核使用率
        ts.split(u"获取主机cpu 单核使用率")
        cpu_single_usage_info = HostIndex.get_host_performance(
            cc_biz_id,
            item="cpuusage",
            category="cpu",
            result_table_id="ja_gse_cpu_core_cpuusage")
        # 磁盘使用量 (暂时隐藏)
        # disk_used_info = get_host_performance(cc_biz_id, item="used_percent",
        # category="disk", result_table_id="ja_gse_disk_used")
        # IO等待
        ts.split(u"获取主机IO等待")
        io_util_info = HostIndex.get_host_performance(
            cc_biz_id,
            item="util",
            category="disk",
            result_table_id="ja_gse_disk_iostats")
        host_list = list()
        ts.split(u"处理整合所有数据")
        for h in hosts:
            host = Host(h)
            for k, v in default_status_info.iteritems():
                setattr(host, k, v)
            host.is_stickied = MonitorHostSticky.objects.host_is_stickied(
                host.id)
            # 基础性能数据
            host.cpu_usage = cpu_usage_info.get(host.id)
            host.cpu_single_usage = cpu_single_usage_info.get(host.id)
            host.cpu_load = cpu_load_info.get(host.id)
            host.io_util = io_util_info.get(host.id)
            if host.io_util and isinstance(host.io_util.get("val"), list):
                io_util = dict()
                for _io_util in host.io_util["val"]:
                    for k, v in _io_util.iteritems():
                        io_util[k] = v
                host.io_util["val"] = max(io_util.values())
            host.status = hosts_agent_status.get(host.id, AGENT_STATUS.UNKNOWN)
            index_list = [
                host.cpu_usage, host.cpu_single_usage, host.cpu_load,
                host.io_util
            ]
            if host.status == AGENT_STATUS.ON and not any(index_list):
                host.status = AGENT_STATUS.NO_DATA
            # 控制前端页面展示的参数
            # 是否被选中
            host.checked = False
            # 是否显示(按属性分组时)
            host._is_show = True
            host_list.append(host)
        host_list.sort(key=lambda x: x.status, reverse=True)
        data["hosts"] = host_list
        return render_json(ok_data(data))
    except (TableNotExistException, SqlQueryException) as e:
        # 前端引导用户接入
        if not hosts:
            data["need_access"] = False
            data["access_div_message"] = (
                u"检测到当前业务没有主机,请前往 %s 快速部署主机!" %
                href_link(u"Agent安装", AGENT_SETUP_URL))
            data["access_btn_message"] = u"确定"
        else:
            host_list = list()
            for h in hosts:
                host = Host(h)
                for k, v in default_status_info.iteritems():
                    setattr(host, k, v)
                host.is_stickied = MonitorHostSticky.objects.host_is_stickied(
                    host.id)
                # 基础性能数据
                host.cpu_usage = None
                host.cpu_single_usage = None
                host.cpu_load = None
                host.io_util = None
                host.status = hosts_agent_status.get(host.id,
                                                     AGENT_STATUS.UNKNOWN)
                if host.status == AGENT_STATUS.ON:
                    host.status = AGENT_STATUS.NO_DATA
                # 控制前端页面展示的参数
                # 是否被选中
                host.checked = False
                # 是否显示(按属性分组时)
                host._is_show = True
                host_list.append(host)
            host_list.sort(key=lambda x: x.status, reverse=True)
            data["hosts"] = host_list
            access_status = get_access_status(cc_biz_id)
            data["need_access"] = not access_status.get("accessed")
            if isinstance(e, SqlQueryException):
                logger.exception(u"数据平台查询失败:%s" % e)
                data["access_div_message"] = u"数据查询异常,请联系管理员"
            else:
                data["access_div_message"] = access_status.get("div_message")
            data["access_btn_message"] = access_status.get("btn_message")
        return render_json(failed_data(u"", data))
    # except SqlQueryException as e:
    #     logger.exception(u"数据平台查询失败:%s" % e)
    #     return render_json(failed_data(u"数据查询异常", data))
    except Exception as e:
        logger.exception(u"拉取主机性能信息失败: %s" % e)
        return render_json(failed_data(u"系统错误", data))
    finally:
        ts.stop()
        time_stats_info = ts.display()
        logger.warning(time_stats_info)
Exemplo n.º 16
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.º 17
0
def get_eth_info(request, cc_biz_id):
    """
    获取各主机对应的网卡信息, rt_id: ja_gse_net_detail
    """
    return render_json(ok_data(HostIndex.get_eth_info_by_biz(cc_biz_id)))
Exemplo n.º 18
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.º 19
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}))
Exemplo n.º 20
0
def get_plat_list(request, cc_biz_id):
    """
    获取云平台信息列表
    """
    return render_json(ok_data(get_plat(cc_biz_id)))