def delete_unit(request): """ 删除监控项 :param request: :return:result """ try: res = json.loads(request.body) # 根据前台传的来的id进行删除 unit_id = res['unit_id'] monitor_name = res['monitor_name'] Monitor.objects.filter(id=unit_id).delete() """ # 删除监控项需要删除该监控项的celery调度任务和子任务 # 否则子任务会一直执行到监控项的结束时间,占用系统资源 # 而且会出现监控项都已经删除了监控项的数据采集却还在继续 # 的灵异事件 """ co.delete_task(str(unit_id)) co.delete_task(str(unit_id) + "task") result = tools.success_result(None) # 修改获取用户的方式,直接从request中获取 info = make_log_info(u'删除监控项', u'业务日志', u'Monitor', sys._getframe().f_code.co_name, request.user.username, '成功', '无') add_log(info) except Exception as e: info = make_log_info(u'删除监控项', u'业务日志', u'Monitor', sys._getframe().f_code.co_name, request.user.username, '失败', repr(e)) result = tools.error_result(e) add_log(info) return result
def add_scene(res1): """ :param res1: :return: """ try: for i in res1: Scene_monitor.objects.create(**i) res_dic = tools.success_result(None) except Exception as e: res_dic = tools.error_result(e) return res_dic
def scene_data(id): """ :param id: :return: """ try: obj = Scene_monitor.objects.filter(scene_id=id) data_list = [] for scene_obj in obj: data_dic = model_to_dict(scene_obj) data_dic['scale'] = str(scene_obj.scale) monitor_data = Monitor.objects.filter(id=data_dic['item_id']) for monitor_item_obj in monitor_data: # 监控项采集表中只有一条记录 gather_data = TDGatherData.objects.filter( item_id=monitor_item_obj.id) for gather_data_obj in gather_data: data_dic['data_value'] = gather_data_obj.data_value data_dic['monitor_name'] = monitor_item_obj.monitor_name data_dic['monitor_type'] = monitor_item_obj.monitor_type data_dic['jion_id'] = monitor_item_obj.jion_id data_dic['gather_rule'] = monitor_item_obj.gather_rule data_dic['gather_params'] = monitor_item_obj.gather_params data_dic['params'] = monitor_item_obj.params data_dic['width'] = monitor_item_obj.width data_dic['height'] = monitor_item_obj.height data_dic['font_size'] = monitor_item_obj.font_size data_dic['period'] = monitor_item_obj.period data_dic['start_time'] = str(monitor_item_obj.start_time) data_dic['end_time'] = str(monitor_item_obj.end_time) data_dic['creator'] = monitor_item_obj.creator data_dic['editor'] = monitor_item_obj.editor data_dic['status'] = monitor_item_obj.status data_dic['contents'] = monitor_item_obj.contents data_dic['monitor_area'] = monitor_item_obj.monitor_area data_dic['source_type'] = monitor_item_obj.source_type data_dic['target_name'] = monitor_item_obj.target_name data_dic['measure_name'] = monitor_item_obj.measure_name data_dic['dimension'] = monitor_item_obj.dimension data_dic['display_type'] = monitor_item_obj.display_type data_dic['display_rule'] = monitor_item_obj.display_rule data_list.append(data_dic) res = tools.success_result(data_list) except Exception as e: res = tools.error_result(e) return res
def get_previous_day_ts(): """ 获取当前系统时间前一天的时间戳 :return: """ res = "" try: sql = "SELECT unix_timestamp(date_sub(now(), INTERVAL 1 DAY)) as timestamp;" db = get_db() cursor = db.cursor() cursor.execute(sql) res = cursor.fetchall() db.close() except Exception as e: return tools.error_result(e) # scene_list = list(res1) return res
def get_current_time(): """ 获取当前系统时间 :return: """ res = "" try: sql = "SELECT DATE_FORMAT(now(), '%H:%i:%S') AS cur_time;" db = get_db() cursor = db.cursor() cursor.execute(sql) res = cursor.fetchall() db.close() except Exception as e: return tools.error_result(e) # scene_list = list(res1) return res
def change_unit_status(req): """ 改变监控项的启用状态 :param req: :return: """ try: res = json.loads(req.body) flag = int(res['flag']) unit_id = res['id'] schename = str(unit_id) mon = Monitor.objects.get(id=unit_id) mon.status = flag mon.save() # 重新生效celery任务 if flag == 1: """ 注意: # 设置任务为可用同时(将挂起的任务重新生效)同时也要将子任务也设置为生效 # 否则主任务重新生效了,子任务却还在挂起状态 # 重启任务时还需要判断当前监控项的开始和结束时间,如果不在时间范围内就要挂起子任务 (就是子任务还是继续挂起) """ strnow = datetime.strftime(datetime.now(), '%H:%M') start_time = str(mon.start_time) end_time = str(mon.end_time) starttime = start_time.split(':')[0]+":"+start_time.split(':')[1] endtime = end_time.split(':')[0]+":"+end_time.split(':')[1] co.enable_task(schename) # 在时间范围内才将挂起子任务重启,否则不处理 if strnow <= endtime and strnow >= starttime: co.enable_task(schename + "task") else: # 挂起celery任务 """ 注意: # 设置任务为不可用同时(将celery任务暂时挂起)同时也要将子任务也设置为挂起 # 否则子任务还会一直执行 """ co.disable_task(schename) co.disable_task(schename+"task") res = tools.success_result(None) except Exception as e: res = tools.error_result(e) return res
def selectScenes_ById(request): """ 场景对比分析 :param request: :return: """ res = json.loads(request.body) m = Scene.objects.get(id=res['id']) rid = model_to_dict(m)['scene_area'] # 根据场景id查监控项个数 sm = Scene_monitor.objects.filter(scene_id=res['id']) item_len = sm.__len__() # 获取开始结束时间 b_time = res['dataTime'][0] e_time = res['dataTime'][1] Alldays = (datetime.strptime(e_time, "%Y-%m-%d %H:%M:%S") - datetime.strptime(b_time, "%Y-%m-%d %H:%M:%S")).days if Alldays > 15: return tools.success_result(Alldays) # 根据开始结束时间查询单个场景下所有监控项每一天最后一个采集到的数据 all_itemid = [] # 所有监控项,字符串拼接 str1 = "" for scen_monitor in sm: all_itemid.append(model_to_dict(scen_monitor)['item_id']) for i, index in enumerate(all_itemid): if (i + 1) < all_itemid.__len__(): str1 = str(index) + "," else: str1 += str(index) try: sql = "SELECT * from (select max(a.gather_time) AS mtime,a.item_id " \ "FROM (SELECT t.* FROM (SELECT DATE_FORMAT(tt.gather_time, '%Y-%m-%d') AS xx," \ "tt.gather_time,tt.gather_error_log,tt.item_id " \ "FROM td_gather_history tt WHERE item_id IN (" + str1 + ")) AS t WHERE " \ "gather_time BETWEEN '" + b_time + "' AND '" + e_time + "' ORDER BY item_id,gather_time) a group by " \ "a.item_id,a.xx) as m ORDER BY m.mtime" db = get_db() cursor = db.cursor() cursor.execute(sql) res1 = cursor.fetchall() except Exception as e: return tools.error_result(e) scene_list = list(res1) # 所有有错误的日期 d_data = [] h1_new = None for i in scene_list: h = TDGatherHistory.objects.filter(item_id=i[1]) for th in h: h1 = model_to_dict(th) h1['gather_time'] = th.gather_time if h1['gather_time'] == i[0]: h1_new = h1 if (h1_new['gather_error_log'] != '') and (h1_new['gather_error_log'] != None): d_data.append(i[0]) else: pass # 查到的总天数 try: sql1 = "select count(*) from (select DISTINCT DATE_FORMAT(tb.mtime,'%y-%m-%d') gather_time from (SELECT * from (select max(a.gather_time) AS mtime,a.item_id FROM (SELECT t.* FROM (SELECT DATE_FORMAT(tt.gather_time, '%Y-%m-%d') AS xx,tt.gather_time,tt.gather_error_log,tt.item_id FROM td_gather_history tt WHERE item_id IN (" + str1 + ")) AS t WHERE gather_time BETWEEN '" + b_time + "' AND '" + e_time + "' ORDER BY item_id,gather_time) a group by a.item_id,a.xx) as m ORDER BY m.mtime)as tb)as tf" db1 = get_db() cursor1 = db1.cursor() cursor1.execute(sql1) count = cursor1.fetchone() except Exception as e: return tools.error_result(e) count = count[0] # 时间间隔数 # 间隔天数小于3不查,大于3但是查到的有效天数小于3也不要 if Alldays < 3: return tools.success_result(Alldays) else: if count < 3: return tools.success_result(Alldays) # 有效天大于3天小于7天,取所有天数 else: return getPant_list(scene_list, d_data, all_itemid)
def add_unit(request): """ 新增监控项(1:基本监控项,2:图表监控项,3:作业监控兴,4:流程监控项) :param request:页面请求对象 :return:result """ try: # 添加事物控制防止异常时事物不回滚,这里事物必须放在try...catch里面 # 否则事物被try...catch捕获了就不起作用了 with transaction.atomic(): # 修改获取用户的方式,直接从request中获取 username = request.user.username res = json.loads(request.body) add_dic = res['data'] add_flow_dic = res['flow'] monitor_type = res['monitor_type'] # 根据前台来的单元类型进行分类 if res['monitor_type'] == 'first': monitor_type = 1 if res['monitor_type'] == 'second': monitor_type = 2 if res['monitor_type'] == 'third': monitor_type = 3 # 作业监控项的把作业id和name分别存放 add_dic['jion_id'] = res['data']['gather_rule'][0]['id'] add_dic['gather_rule'] = res['data']['gather_rule'][0]['name'] if res['monitor_type'] == 'fourth': monitor_type = 4 add_dic['jion_id'] = res['flow']['jion_id'] add_dic['gather_params'] = add_dic['node_name'] add_dic.pop('node_name') add_dic['gather_rule'] = res['data']['gather_rule'][0]['name'] add_dic['params'] = res['flow']['constants'] add_flow_dic['monitor_area']=res['monitor_area'] start_list = [] for i in res['flow']['node_times']: start_list.append(i['endtime']) start_list.append(i['starttime']) add_dic['start_time'] = min(start_list) add_dic['end_time'] = max(start_list) add_flow_dic['start_time']=add_dic['start_time'] add_flow_dic['end_time'] =add_dic['end_time'] # 修改后的基本监控项处理 if res['monitor_type'] == 'five': monitor_type = 5 add_dic['monitor_name'] = res['monitor_name'] # 新增一条数据时 开关状态默认为0 关闭 add_dic['status'] = 0 add_dic['monitor_type'] = monitor_type add_dic['creator'] = username add_dic['editor'] = username add_dic['monitor_area'] = res['monitor_area'] last_node = Monitor.objects.create(**add_dic) # 添加定时任务监控要求本地安装任务调度软件rabitmq # 正式环境服务器一般带有这个调度软件,如果没有就要安装 if res['monitor_type'] == 'fourth': function.add_unit_task(add_dicx=add_flow_dic) else: function.add_unit_task(add_dicx=add_dic) # 初始创建的监控项celery任务默认为挂起 co.disable_task(str(last_node.id)) result = tools.success_result(None) result['item_id'] = last_node.id # 修改获取用户的方式,直接从request中获取 info = make_log_info(u'增加监控项', u'业务日志', u'Monitor', sys._getframe().f_code.co_name, request.user.username, '成功', '无') add_log(info) except Exception as e: info = make_log_info(u'增加监控项', u'业务日志', u'Monitor', sys._getframe().f_code.co_name, request.user.username, '失败', repr(e)) result = tools.error_result(e) add_log(info) return result
def scene_show(res): """ 场景编排显示 :param res: :return: """ try: type = res['type'] limit = res['limit'] page = res['page'] if type == 0: # 四类监控项全部按id倒排序(基本监控项与一体化基本监控项放一起) base_unit = Monitor.objects.filter( Q(monitor_type=1) | Q(monitor_type=5)).order_by("-id") base_page_data, base_page_count = tools.page_paging( base_unit, limit, page) # 四类监控项全部按id倒排序(图表监控项) chart_unit = Monitor.objects.filter( monitor_type='2').order_by("-id") chart_page_data, chart_page_count = tools.page_paging( chart_unit, limit, page) # 四类监控项全部按id倒排序(作业监控项) job_unit = Monitor.objects.filter(monitor_type='3').order_by("-id") job_page_data, job_page_count = tools.page_paging( job_unit, limit, page) # 四类监控项全部按id倒排序(流程监控项) flow_unit = Monitor.objects.filter( monitor_type='4').order_by("-id") flow_page_data, flow_page_count = tools.page_paging( flow_unit, limit, page) base_list = tools.obt_dic(base_page_data, base_page_count) chart_list = tools.obt_dic(chart_page_data, chart_page_count) job_list = tools.obt_dic(job_page_data, job_page_count) flow_list = tools.obt_dic(flow_page_data, flow_page_count) res_dic = { 'base_list': base_list, 'chart_list': chart_list, 'job_list': job_list, 'flow_list': flow_list, } # 基本监控项与一体化基本监控项 elif type == 1: base_unit = Monitor.objects.filter( Q(monitor_type=1) | Q(monitor_type=5)).order_by("-id") base_page_data, base_page_count = tools.page_paging( base_unit, limit, page) base_list = tools.obt_dic(base_page_data, base_page_count) res_dic = { 'base_list': base_list, } # 图表监控项 elif type == 2: chart_unit = Monitor.objects.filter( monitor_type='2').order_by("-id") chart_page_data, chart_page_count = tools.page_paging( chart_unit, limit, page) chart_list = tools.obt_dic(chart_page_data, chart_page_count) res_dic = { 'chart_list': chart_list, } # 作业监控项 elif type == 3: job_unit = Monitor.objects.filter(monitor_type='3').order_by("-id") job_page_data, job_page_count = tools.page_paging( job_unit, limit, page) job_list = tools.obt_dic(job_page_data, job_page_count) job_status_list = [] for i in job_list: try: job_status = Job.objects.filter( job_id=i['jion_id']).last().status except Exception as e: job_status = 0 job_status_list.append(job_status) for i in range(0, len(job_status_list)): job_list[i]['job_status'] = job_status_list[i] res_dic = { 'job_list': job_list, } # 流程监控项 elif type == 4: flow_unit = Monitor.objects.filter( monitor_type='4').order_by("-id") flow_page_data, flow_page_count = tools.page_paging( flow_unit, limit, page) flow_list = tools.obt_dic(flow_page_data, flow_page_count) res_dic = { 'flow_list': flow_list, } result = tools.success_result(res_dic) except Exception as e: result = tools.error_result(e) return result