Exemple #1
0
def job_info():
    req = request.values
    id = int(req['id']) if ('id' in req and req['id']) else 0
    info = JobList.query.filter_by(id=id).first()
    if not info:
        return redirect(GlobalUrlService.buildHomeUrl("/job/index/index"))

    info = ModelHelper.model2Dict(info)

    server_info = JobServer.query.filter_by(id=info['server_id']).first()
    cate_info = JobCategory.query.filter_by(id=info['cate_id']).first()
    server_env_map = CommonConstant.server_env_map
    run_status_map = CommonConstant.run_status_map

    info['next_run_time'] = DateHelper.getDateOnTimestamps(
        info['next_run_time'], '%Y-%m-%d %H:%M')
    info['env_name'] = server_env_map.get(info['env_id'])
    info['run_status_desc'] = run_status_map.get(info['run_status'])
    info['job_status_desc'] = job_status_map.get(info['status'])
    info['server_name'] = server_info.name
    info['cate_name'] = cate_info.name if cate_info else ''
    info['run_interval_desc'] = DateHelper.formatBeautyTime(
        info['run_interval'] * 60)

    user_map = ModelHelper.getDictFilterField(
        User,
        select_field=User.id,
        id_list=[info['owner_uid'], info['relate_uid']])

    ##获取最近5天运行记录
    log_list = JobRunLog.query.filter_by(job_id=id).order_by(
        JobRunLog.id.desc())[0:5]
    log_data = []
    if log_list:
        for item in log_list:
            tmp_data = ModelHelper.model2Dict(item)
            tmp_data['status_desc'] = CommonConstant.job_log_status_map[
                tmp_data['status']]
            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'])
            log_data.append(tmp_data)

    return UtilHelper.renderView(
        "home/job/index/info.html", {
            "info": info,
            "log_list": log_data,
            "user_map": user_map,
            "job_level_map": CommonConstant.job_level_map,
        })
Exemple #2
0
    def alertRunTimeJudge(self, job_info, job_run_start_time):
        #常驻Job不用判断时间
        if job_info.job_type == CommonConstant.default_status_pos_2:
            return True
        job_run_total_time = int( time.time() - job_run_start_time)
        job_id = job_info.id

        alert_temp = "[运行时间异常]您的job(job_{0} {1})在{2}开始运行,预计时长{3} - {4}分钟,但是运行了{5}分钟"
        alert_content = ""
        if job_run_total_time < (job_info.threshold_down * 60 - 10 * 60):  # 运行时长低于下限值,报警(阀值后面-10分钟)
            alert_content = alert_temp.format(job_id, job_info.name,
                                              DateHelper.getDateOnTimestamps(job_info.next_run_time,format="%Y-%m-%d %H:%M"),
                                              job_info.threshold_down, job_info.threshold_up,DateHelper.formatBeautyTime( job_run_total_time ) )
        elif job_run_total_time > ( job_info.threshold_up * 60 + 15 * 60 ):
            alert_content = alert_temp.format(job_id, job_info.name,
                                              DateHelper.getDateOnTimestamps(job_info.next_run_time,format="%Y-%m-%d %H:%M"),
                                              job_info.threshold_down, job_info.threshold_up,DateHelper.formatBeautyTime( job_run_total_time ) )

        if alert_content:
            JobService.saveAlertLog( job_id , alert_content)

        return True
Exemple #3
0
    def handleItem(self, item, host):
        pid_file = self.getPidPath('job_%s.pid' % item['id'])
        pid = 0
        if self.checkPidExist(pid_file):
            # 文件存储的是python的进程id,找真真运行的需要使用关键词查找
            pid = self.findPidByKw(item['id'])

        app.logger.info("%s : %s" % (pid_file, pid))
        # 判断进程本身是否存在,但是由于删除pid在进程结束之后 会有延迟,所以需要临时增加删除功能
        run_flag = os.path.exists('/proc/%s' % pid) if pid > 0 else False
        '''
            1=>该结束未结束 
            2=>该运行未运行 
            3=>执行命令异常 
            4=>Job平台标识正在运行,但是在机器上没有发现job进程 
            5=>Job平台标识没有运行,但是在机器上发现了job进程 
            6=>运行时间过短
        '''
        alert_content = ''
        if item['run_status'] == CommonConstant.default_status_true and run_flag:
            # 数据库中标记没有运行,但是在机器上发现了进程
            alert_content = "您的job(job_{0} {1})运行状态异常,可能原因:{2}"\
                .format(item['id'], item['name'], "Job平台标识没有运行,但是在机器%s上发现了job进程" % host )
            os.system('ls -al /proc/%s' % pid)
            os.system('ps -ef |grep tmp_job_%s' % item['id'])
        elif item[
                'run_status'] == CommonConstant.default_status_pos_2 and not run_flag:
            # 重新检查job的状态,避免运行时间较短的job误报警
            time.sleep(2)
            job_info = JobList.query.filter_by(id=item['id']).first()

            if job_info.run_status == CommonConstant.default_status_pos_2:
                alert_content = "您的job(job_{0} {1})运行状态异常[Job平台会自动修复],可能原因:{2}" \
                    .format(item['id'], item['name'], "Job平台标识正在运行,但是在机器%s上没有发现job进程" % host )
        elif item['run_status'] == CommonConstant.default_status_true and item[
                'next_run_time'] < self.current_time:
            # 该运行未运行
            alert_content = "您的JOB(job_{0} {1})应该在{2}运行,但是现在还没有运行" \
                .format(item['id'], item['name'], DateHelper.getDateOnTimestamps(item['next_run_time'], '%Y-%m-%d %H:%M'))
        elif item['run_status'] == CommonConstant.default_status_pos_2 and item[
                'job_type'] != CommonConstant.default_status_pos_2:
            # 常驻Job不判断时间
            # 对于固定运行时长,如果运行时间大于原计划时间的120%就报警;动态运行时长,超出上限就报警
            run_duration_time = int(time.time() - item['next_run_time'])

            # 使用动态预估运行时间确定是否报警,加了15分钟的缓冲时间
            if run_duration_time > (item['threshold_up'] +
                                    15) * 60:  # 运行时长高于上限值,报警
                run_duration_min = round(run_duration_time / 60, 1)
                alert_content = "[运行超时]您的job(job_{0} {1})在{2}开始运行,预计时长{3}-{4}分钟,但是目前仍然在运行(运行了{5}分钟)"\
                    .format(item['id'], item['name'], DateHelper.getDateOnTimestamps(item['next_run_time'], '%Y-%m-%d %H:%M'),
                            item['threshold_down'], item['threshold_up'],run_duration_min )

        # 自动修复
        if 'Job平台标识正在运行' in alert_content and 'Job平台会自动修复' in alert_content:
            try:
                ##下一分钟
                # next_date = datetime.datetime.now() + datetime.timedelta(minutes=1)
                # next_date = next_date.replace(second=0)
                # next_run_time = int(time.mktime(next_date.timetuple()))
                # params = {
                #     "next_run_time": next_run_time,
                #     "run_status": CommonConstant.default_status_true,
                #     "status": CommonConstant.default_status_true
                # }
                # JobList.query.filter_by(id=item['id']).update(dict(params))
                # db.session.commit()
                app.logger.info("您的job(job_{0} {1}) 自动修复成功~~".format(
                    item['id'], item['name']))
            except:
                app.logger.info("您的job(job_{0} {1}) 自动修复失败,错误原因:{2}~~".format(
                    item['id'], item['name'], self.getErrMsg()))

        ##存储到报警日志表
        if alert_content:
            app.logger.info(alert_content)
            JobService.saveAlertLog(item['id'], alert_content)
        return True
Exemple #4
0
def job_index():
    req = request.values
    cate_id = int(req.get("cate_id", CommonConstant.default_status_false))
    owner_uid = int(req.get("owner_uid", CurrentUserService.getUid()))
    env_id = int(req.get("env_id", CommonConstant.default_status_false))
    server_id = int(req.get("server_id", CommonConstant.default_status_false))
    status = int(req.get("status", CommonConstant.default_status_neg_99))
    display_status = int(
        req.get("display_status", CommonConstant.default_status_neg_99))
    job_type = int(req.get("job_type", CommonConstant.default_status_neg_99))
    job_level = int(req.get("job_level", CommonConstant.default_status_neg_99))

    kw = req.get("kw", "").strip()
    page = int(req.get("p", 1))

    query = JobList.query

    if RBACService.checkDataPrivilege("all"):
        pass
    else:
        query = query.filter_by(owner_uid=CurrentUserService.getUid())

    if cate_id:
        query = query.filter_by(cate_id=cate_id)

    if env_id:
        query = query.filter_by(env_id=env_id)

    if owner_uid:
        query = query.filter_by(owner_uid=owner_uid)

    if server_id:
        query = query.filter_by(server_id=server_id)

    if status > CommonConstant.default_status_neg_99:
        query = query.filter_by(status=status)

    if display_status > CommonConstant.default_status_neg_99:
        query = query.filter_by(is_del=display_status)

    if job_type > CommonConstant.default_status_neg_99:
        query = query.filter_by(job_type=job_type)

    if job_level > CommonConstant.default_status_neg_99:
        query = query.filter_by(job_level=job_level)

    if kw:
        if kw.isdigit():
            query = query.filter_by(id=int(kw))
        else:
            query = query.filter(
                or_(JobList.name.ilike('%{}%'.format(kw)),
                    JobList.command.ilike('%{}%'.format(kw))))

    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(JobList.id.desc())[offset:limit]
    data = []

    server_map = ModelHelper.getDictFilterField(JobServer)
    cate_map = ModelHelper.getDictFilterField(JobCategory)
    staff_map = ModelHelper.getDictFilterField(User)
    server_env_map = CommonConstant.server_env_map
    run_status_map = CommonConstant.run_status_map
    job_type_map = CommonConstant.job_type_map
    job_level_map = CommonConstant.job_level_map
    if list:
        for item in list:
            tmp_data = ModelHelper.model2Dict(item)
            tmp_server_info = ModelHelper.model2Dict(
                server_map.get(tmp_data['server_id']))
            tmp_cate_info = ModelHelper.model2Dict(
                cate_map.get(tmp_data['cate_id']))
            tmp_owner_staff_info = ModelHelper.model2Dict(
                staff_map.get(tmp_data['owner_uid']))
            tmp_relate_staff_info = ModelHelper.model2Dict(
                staff_map.get(tmp_data['relate_uid']))
            tmp_data['next_run_time'] = DateHelper.getDateOnTimestamps(
                tmp_data['next_run_time'], '%Y-%m-%d %H:%M')
            tmp_data['env_name'] = server_env_map.get(tmp_data['env_id'])
            tmp_data['run_status_desc'] = run_status_map.get(
                tmp_data['run_status'])
            tmp_data['job_status_desc'] = job_status_map.get(
                tmp_data['status'])
            tmp_data['server_name'] = tmp_server_info.get("name")
            tmp_data['cate_name'] = tmp_cate_info.get("name", '')
            tmp_data['owner_name'] = tmp_owner_staff_info.get("name", '')
            tmp_data['relate_name'] = tmp_relate_staff_info.get("name", '')
            tmp_data['run_interval_desc'] = DateHelper.formatBeautyTime(
                tmp_data['run_interval'] * 60)
            data.append(tmp_data)
    sc = {
        'kw': kw,
        'cate_id': cate_id,
        'owner_uid': owner_uid,
        'env_id': env_id,
        'server_id': server_id,
        'status': status,
        'display_status': display_status,
        'job_type': job_type,
        'job_level': job_level,
    }

    set_flag = RBACService.checkPageRelatePrivilege("set")
    ops_flag = RBACService.checkPageRelatePrivilege("ops")
    return UtilHelper.renderView(
        "home/job/index/index.html", {
            "list": data,
            "pages": pages,
            "job_status_map": job_status_map,
            "server_env_map": server_env_map,
            "server_map": server_map,
            "staff_map": staff_map,
            "cate_map": cate_map,
            "display_status_map": display_status_map,
            "job_type_map": job_type_map,
            "job_level_map": job_level_map,
            "sc": sc,
            "set_flag": set_flag,
            "ops_flag": ops_flag,
        })
Exemple #5
0
def job_set():
    if UtilHelper.isGet():
        req = request.values
        id = int(req['id']) if ('id' in req and req['id']) else 0
        act = req.get("act", "").strip()
        info = {'owner_uid': CurrentUserService.getUid()}
        if id > 0:
            info = JobList.query.filter_by(id=id).first()
            info = ModelHelper.model2Dict(info)
            info['next_run_time'] = DateHelper.getDateOnTimestamps(
                info['next_run_time'], '%Y-%m-%d %H:%M')

        if act == "copy":
            info['id'] = 0
            info['status'] = 0
            info['name'] = "【复制】" + info['name']

        server_list = JobServer.query.order_by(JobServer.id.desc()).all()
        user_list = User.query.order_by(User.id.desc()).all()
        cate_list = JobCategory.query.order_by(JobCategory.id.desc()).all()

        return UtilHelper.renderView(
            "home/job/index/set.html", {
                "info": info,
                "server_list": server_list,
                "user_list": user_list,
                "cate_list": cate_list,
                "server_env_map": CommonConstant.server_env_map,
                "job_type_map": CommonConstant.job_type_map,
                "job_status_map": job_status_map,
                "job_level_map": CommonConstant.job_level_map,
            })

    req = request.values
    id = int(req['id']) if ('id' in req and req['id']) else 0
    cate_id = int(req.get("cate_id", "0").strip())
    name = req.get("name", "").strip()
    env_id = int(req.get("env_id", "0").strip())
    server_id = int(req.get("server_id", "0").strip())
    owner_uid = int(req.get("owner_uid", "0").strip())
    relate_uid = int(req.get("relate_uid", "0").strip())
    command = req.get("command", "").strip()
    command_kill = req.get("command_kill", "").strip()
    job_type = int(req.get("job_type", "0").strip())
    job_level = int(req.get("job_level", "1").strip())
    status = int(req.get("status", "0").strip())
    next_run_time = req.get("next_run_time", "").strip()
    run_interval = int(req.get("run_interval", "0").strip())
    threshold_down = int(req.get("threshold_down", "0").strip())
    threshold_up = int(req.get("threshold_up", "0").strip())
    note = req.get("note", "").strip()

    if cate_id < 1:
        return UtilHelper.renderErrJSON("请选择所属分类~~")

    if not ValidateHelper.validLength(name, 1, 15):
        return UtilHelper.renderErrJSON("请输入符合规范的名称~~")

    if env_id < 1:
        return UtilHelper.renderErrJSON("请选择运行环境~~")

    if server_id < 1:
        return UtilHelper.renderErrJSON("请选择运行服务器~~")

    if owner_uid < 1:
        return UtilHelper.renderErrJSON("请选择Job负责人~~")

    if relate_uid < 1:
        return UtilHelper.renderErrJSON("请选择Job相关人~~")

    if not ValidateHelper.validLength(command, 5):
        return UtilHelper.renderErrJSON("请输入Job命令~~")

    if job_type < 1:
        return UtilHelper.renderErrJSON("请选择Job类型~~")

    if not ValidateHelper.validDate(next_run_time,
                                    r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$'):
        return UtilHelper.renderErrJSON("请选择调度时间~~")

    if job_type != CommonConstant.default_status_pos_2:
        if run_interval < 1:
            return UtilHelper.renderErrJSON("请输入运行间隔~~")

        if threshold_down < 0:
            return UtilHelper.renderErrJSON("请输入预估最短运行时长~~")

        if threshold_up < 1:
            return UtilHelper.renderErrJSON("请输入预估最长运行时长~~")
    else:
        run_interval = threshold_down = threshold_up = 0

    info = JobList.query.filter_by(id=id).first()

    if info:
        model_job = info
        ##正在运行的Job不能编辑运行时间
        if info.run_status == CommonConstant.default_status_pos_2:
            if info.command != command:
                return UtilHelper.renderErrJSON("正在运行的Job不能修改运行命令~~")

            if info.job_type != job_type:
                return UtilHelper.renderErrJSON("正在运行的Job不能修改类型~~")

            if info.status != status:
                return UtilHelper.renderErrJSON("正在运行的Job不能修改调度状态~~")

            if info.next_run_time != DateHelper.getTimestamps(next_run_time +
                                                              ":00"):
                return UtilHelper.renderErrJSON("正在运行的Job不能修改调度时间~~")

    else:
        model_job = JobList()

    ##只有需要调度的才需要判断时间
    if model_job.run_status != CommonConstant.default_status_pos_2 and status \
            and DateHelper.getTimestamps(next_run_time + ":00") < time.time():
        return UtilHelper.renderErrJSON("调度时间不能小于当前时间~~")

    model_job.name = name
    model_job.env_id = env_id
    model_job.server_id = server_id
    model_job.owner_uid = owner_uid
    model_job.relate_uid = relate_uid
    model_job.job_type = job_type
    model_job.job_level = job_level
    model_job.cate_id = cate_id
    model_job.command = command
    model_job.command_kill = command_kill
    model_job.next_run_time = DateHelper.getTimestamps(next_run_time + ":00")
    model_job.run_interval = run_interval
    model_job.threshold_up = threshold_up
    model_job.threshold_down = threshold_down
    model_job.note = note
    model_job.status = status
    if status and (not info or not info.run_status):
        model_job.run_status = CommonConstant.default_status_true

    db.session.add(model_job)
    db.session.commit()

    return UtilHelper.renderSucJSON({"id": model_job.id})