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))
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 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_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' }))
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)
# echo_sql = cache.get(cache_key, u"未能成功生成查询sql") # 判断是否是新接入的图表 5分钟 if ((datetime.datetime.now() - arrow.get(m.create_time).naive) < datetime.timedelta(seconds=60 * 10)): try: interval = m.result_table.count_freq / 60 except: interval = 5 e = (u"请稍等,数据正在接入中…(请%s分钟后刷新图表)" % (interval + 1)) else: e = u"数据查询异常,请联系管理员!(数据表不存在)" echo_sql = "" return render_json( failed_data( u"%s" % e, { 'echo_sql': echo_sql, 'error_class': "info", 'need_access': "need_access" })) except Exception, e: logger.exception(u"后台异常: %s" % e) # echo_sql = cache.get(cache_key, u"未能成功生成查询sql") return render_json( failed_data(u"生成图表异常", { 'echo_sql': "", 'error_class': 'info' })) @decorators.check_perm def get_operation_monitor_alert_list(request, cc_biz_id): # 获取告警列表