def run(self, kwargs): pid_path = self.getPidPath( 'monitor_system.pid' ) if self.checkPidExist(pid_path): app.logger.info("[monitor] system is running") return False pid = str( os.getpid() ) if not self.setPidFile( pid_path,pid): err_msg = self.getErrMsg() app.logger.info("Cann't get a lock file,err msg : " + err_msg) return False params = self.getEnvFile() server_id = params['id'] host = params['name'] ## 判断是否有僵尸进程 cmd = "ps -A -o stat,ppid,pid,cmd | grep -v grep |grep -e '^[Zz]'" status,output = subprocess.getstatusoutput( cmd ) if len( output ) < 1: return True output_arr = output.split( '\n' ) if len( output_arr ) > 3:#多余3个僵尸进程 staff_list = StaffService.getRootStaffList() staff_ids = ModelHelper.getFieldList( staff_list,"id") for tmp_staff_id in staff_ids: news_params = { "uid": tmp_staff_id, "title": "僵尸进程:" + host, "content": "<br/>".join( output_arr ) } NewsService.addNews(news_params) return True
def tools_alert(): req = request.values page = int(req.get("p", 1)) date_from = req.get("date_from", DateHelper.getCurrentTime(fmt="%Y-%m-%d")) date_to = req.get("date_to", DateHelper.getCurrentTime(fmt="%Y-%m-%d")) status = int(req.get("status", CommonConstant.default_status_neg_99)) query = JobAlertList.query.filter( JobAlertList.created_time.between(date_from, date_to + " 23:59:59")) if status > CommonConstant.default_status_neg_99: query = query.filter_by(status=status) page_params = { "total": query.count(), "page_size": CommonConstant.PAGE_SIZE, "page": page, "display": CommonConstant.PAGE_DISPLAY, } pages = UtilHelper.iPagination(page_params) offset = (page - 1) * CommonConstant.PAGE_SIZE limit = CommonConstant.PAGE_SIZE * page list = query.order_by(JobAlertList.id.desc())[offset:limit] data = [] if list: job_ids = ModelHelper.getFieldList(list, "job_id") job_map = ModelHelper.getDictFilterField(JobList, select_field=JobList.id, id_list=job_ids.sort()) for item in list: tmp_data = ModelHelper.model2Dict(item) tmp_job_info = ModelHelper.model2Dict( job_map.get(tmp_data['job_id'])) tmp_data['status_desc'] = CommonConstant.common_status_map4[ tmp_data['status']] tmp_data['job_name'] = tmp_job_info['name'] data.append(tmp_data) sc = {'date_from': date_from, 'date_to': date_to, 'status': status} return UtilHelper.renderView( "home/job/tools/alert.html", { "list": data, "pages": pages, "sc": sc, "status_map": CommonConstant.common_status_map4, "current": "alert" })
def run(self, params): list = JobAlertList.query.filter_by(status=CommonConstant.default_status_neg_2)\ .order_by( JobAlertList.id.asc() ).limit(10).all() if not list: app.logger.info("没有报警异常数据需要处理") return self.exitOK() job_ids = ModelHelper.getFieldList(list, "job_id") job_map = ModelHelper.getDictFilterField(JobList, select_field=JobList.id, id_list= job_ids ) staff_ids = [] for _key,_item in job_map.items(): staff_ids.append( _item.owner_uid ) staff_ids.append( _item.relate_uid ) staff_map = ModelHelper.getDictFilterField(User, select_field= User.id, id_list= staff_ids) done_ids = [] alert_content = [ "Job异常报警" ] cate_map = ModelHelper.getDictFilterField(JobCategory) for item in list: tmp_data = ModelHelper.model2Dict( item ) tmp_job_info = ModelHelper.model2Dict(job_map.get(tmp_data['job_id'])) #负责人和相关人用户信息 tmp_job_owner_info = ModelHelper.model2Dict( staff_map.get( tmp_job_info['owner_uid'] ) ) tmp_job_relate_info = ModelHelper.model2Dict( staff_map.get( tmp_job_info['relate_uid'] ) ) #分类 tmp_cate_info = ModelHelper.model2Dict( cate_map.get( tmp_job_info['cate_id'] ) ) done_ids.append(tmp_data['id']) tmp_msg = "Job Id: {0},名称:[{1}]{2},负责人:{3},相关人:{4},重要级别:{5},类型:{6},报警内容:{7}"\ .format( tmp_job_info['id'],tmp_cate_info['name'],tmp_job_info['name'] ,tmp_job_owner_info['name'],tmp_job_relate_info['name'] ,CommonConstant.job_level_map[ tmp_job_info['job_level'] ] ,CommonConstant.job_type_map[ tmp_job_info['job_type'] ] , tmp_data['content'] ) if 'Job平台标识没有运行' in tmp_msg or 'Job平台标识正在运行' in tmp_msg: continue self.handleItem(tmp_data, tmp_job_info) alert_content.append(tmp_msg) if done_ids: ''' synchronize_session用于query在进行delete or update操作时,对session的同步策略。 False - 不对session进行同步,直接进行delete or update操作 ''' JobAlertList.query.filter( JobAlertList.id.in_( done_ids ) )\ .update( dict( status = CommonConstant.default_status_true ) ,synchronize_session=False ) db.session.commit() if len( alert_content ) > 1: alert_content = "\r\n".join( alert_content ) self.dingdingAlert( alert_content ) self.wechatworkAlert( alert_content ) return self.exitOK()
def grant_assign(): if UtilHelper.isGet(): req = request.values role_id = int(req.get("role_id", 0)) role_pid = int(req.get("role_pid", 0)) ''' 取出来所有的一级部门 ''' p_role_list = Role.query.filter_by(pid=CommonConstant.default_status_false)\ .order_by( Role.id.asc() ).all() if not role_pid and p_role_list: #如果没有父部门,那就选择一个 role_pid = p_role_list[0].id sub_role_list = Role.query.filter_by( pid = role_pid)\ .order_by( Role.id.asc() ).all() if not role_id and sub_role_list: role_id = sub_role_list[0].id user_list = User.query.filter_by( status=CommonConstant.default_status_true, role_id=role_id).all() action_list = Action.query.filter_by( status = CommonConstant.default_status_true )\ .order_by( Action.level1_weight.desc(),Action.level2_weight.desc(),Action.weight.desc() ).all() action_data = {} if action_list: for item in action_list: tmp_level1_key = item.level1_name tmp_level2_key = item.level2_name if tmp_level1_key not in action_data: action_data[tmp_level1_key] = { "name": tmp_level1_key, "counter": 0, "sub": {} } if tmp_level2_key not in action_data[tmp_level1_key]['sub']: action_data[tmp_level1_key]['sub'][tmp_level2_key] = { "name": tmp_level2_key, "counter": 0, "act_list": [] } action_data[tmp_level1_key]['counter'] += 1 action_data[tmp_level1_key]['sub'][tmp_level2_key][ 'counter'] += 1 tmp_data = { "id": item.id, "name": item.name, "is_important": item.is_important } action_data[tmp_level1_key]['sub'][tmp_level2_key][ 'act_list'].append(tmp_data) owned_act = RoleAction.query.filter_by( role_id=role_id, status=CommonConstant.default_status_true).all() owned_act_ids = ModelHelper.getFieldList(owned_act, 'action_id') sc = { "role_id": role_id, "role_pid": role_pid, } return UtilHelper.renderView( "home/rbac/grant/assign.html", { "list": action_data, "p_role_list": p_role_list, "sub_role_list": sub_role_list, "user_list": user_list, "owned_act_ids": owned_act_ids, "sc": sc }) req = request.values role_id = int(req.get("role_id", 0)) action_ids = request.form.getlist("action_ids[]") action_ids = list(map(int, action_ids)) if not role_id or role_id < 1: return UtilHelper.renderErrJSON("请选择部门在分配权限~~") info = Role.query.filter_by(id=role_id).first() if not info: return UtilHelper.renderErrJSON("请选择部门在分配权限 -2~~") ###分配权限逻辑还是挺复杂的 ## 已有的权限 owned_act = RoleAction.query.filter_by(role_id=role_id).all() owned_act_ids = ModelHelper.getFieldList(owned_act, 'action_id') ''' 找出删除的权限(生产环境数据库没有删除权限) 假如已有的权限集合是A,界面传递过得权限集合是B 权限集合A当中的某个权限不在权限集合B当中,就应该删除 计算差集 ''' delete_act_ids = list(set(owned_act_ids) - set(action_ids)) if delete_act_ids: RoleAction.query.filter( RoleAction.role_id == role_id,RoleAction.action_id.in_( delete_act_ids ) )\ .update({ "status":CommonConstant.default_status_false },synchronize_session = False) db.session.commit() ''' 找出添加的权限 假如已有的权限集合是A,界面传递过得权限集合是B 权限集合B当中的某个权限不在权限集合A当中,就应该添加 计算差集 ''' add_act_ids = list(set(action_ids) - set(owned_act_ids)) if add_act_ids: for _action_id in add_act_ids: _model_role_action = RoleAction() _model_role_action.role_id = role_id _model_role_action.action_id = _action_id db.session.add(_model_role_action) db.session.commit() ''' 找出需要更新的权限(生产环境数据库没有删除权限) 假如已有的权限集合是A,界面传递过得权限集合是B 权限集合B当中的某个权限也在在权限集合A当中,就应该更新 计算补集 ''' update_act_ids = list(set(owned_act_ids).intersection(set(action_ids))) if update_act_ids: RoleAction.query.filter(RoleAction.role_id == role_id, RoleAction.action_id.in_(update_act_ids)) \ .update({"status": CommonConstant.default_status_true}, synchronize_session=False) db.session.commit() return UtilHelper.renderSucJSON({}, "权限分配成功~~")
def tools_log(): status_map = CommonConstant.job_log_status_map req = request.values page = int(req.get("p", 1)) date_from = req.get("date_from", DateHelper.getCurrentTime(fmt="%Y-%m-%d")) date_to = req.get("date_to", DateHelper.getCurrentTime(fmt="%Y-%m-%d")) status = int(req.get("status", CommonConstant.default_status_neg_99)) job_id = int(req.get("job_id", CommonConstant.default_status_false)) query = JobRunLog.query.filter( JobRunLog.created_time.between(date_from, date_to + " 23:59:59")) if job_id: query = query.filter_by(job_id=job_id) if status > CommonConstant.default_status_neg_99: query = query.filter_by(status=status) page_params = { "total": query.count(), "page_size": CommonConstant.PAGE_SIZE, "page": page, "display": CommonConstant.PAGE_DISPLAY, } pages = UtilHelper.iPagination(page_params) offset = (page - 1) * CommonConstant.PAGE_SIZE limit = CommonConstant.PAGE_SIZE * page list = query.order_by(JobRunLog.id.desc())[offset:limit] data = [] if list: job_ids = ModelHelper.getFieldList(list, "job_id") job_map = ModelHelper.getDictFilterField(JobList, select_field=JobList.id, id_list=job_ids.sort()) for item in list: tmp_data = ModelHelper.model2Dict(item) tmp_job_info = ModelHelper.model2Dict( job_map.get(tmp_data['job_id'])) tmp_data['status_desc'] = status_map[tmp_data['status']] tmp_data['job_name'] = tmp_job_info['name'] tmp_data['duration'] = "" if DateHelper.getCurrentTime(date=tmp_data['end_time'] ) == CommonConstant.DEFAULT_DATETIME: tmp_data['end_time'] = "未知" tmp_data['duration'] = time.time() - time.mktime( tmp_data['start_time'].timetuple()) else: tmp_data['duration'] = (tmp_data['end_time'].timestamp() - tmp_data['start_time'].timestamp()) tmp_data['duration'] = DateHelper.formatBeautyTime( tmp_data['duration']) data.append(tmp_data) sc = { 'date_from': date_from, 'date_to': date_to, 'status': status, 'job_id': job_id } return UtilHelper.renderView( "home/job/tools/log.html", { "list": data, "pages": pages, "sc": sc, "status_map": status_map, "current": "log" })