Exemple #1
0
def login():
    try:
        db_sso = db_op.user_sso
        ticket = tools.http_args(request,'ticket')
        if ticket:
            try:
                cas_resp = cas_client.perform_service_validate(ticket=ticket,service_url=service_url)
                if cas_resp and cas_resp.success:
                    try:
                        infos = cas_resp.attributes
                        departmentName = infos['departmentName']
                        mail = infos['mail']
                        mobilePhone = infos['mobilePhone']
                        userName = infos['userName']
                        realName = infos['realName']
                        dingunionid = infos['dingunionid']
                        token = tools.Produce(24)
                        update_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
                    except Exception as e:
                        logging.error(e)
                    else:
                        try:
                            val = db_sso.query.filter(db_sso.dingunionid == dingunionid).all()
                            if val:
                                db_sso.query.filter(db_sso.dingunionid == dingunionid).update({db_sso.department:departmentName,
                                                                                               db_sso.mail:mail,
                                                                                               db_sso.mobilePhone:mobilePhone,
                                                                                               db_sso.userName: userName,
                                                                                               db_sso.ticket: ticket,
                                                                                               db_sso.realName: realName,
                                                                                               db_sso.update_time:update_time})
                                db_op.DB.session.commit()
                            else:
                                c =db_sso(userName=userName,realName=realName,mail=mail,mobilePhone=mobilePhone,department=departmentName,
                                          dingunionid=dingunionid,ticket=ticket,grade='9,10',update_time=update_time)
                                db_op.DB.session.add(c)
                                db_op.DB.session.commit()
                        except Exception as e:
                            logging.error(e)
                        else:
                            URL = url_for('main')
                            app_resp = make_response(redirect(URL))
                            app_resp.set_cookie('user', Md5.Md5_make(realName),path='/')
                            app_resp.set_cookie('dingId', Md5.Md5_make(dingunionid),path='/')
                            app_resp.set_cookie('ticket', Md5.Md5_make(ticket),path='/')
                            app_resp.set_cookie('token',token,path='/')
                            Redis.set('OP_dingId_%s' % Md5.Md5_make(dingunionid), dingunionid)
                            Redis.set('OP_user_%s' % Md5.Md5_make(realName), realName)
                            Redis.set('OP_token_%s' %dingunionid,token)
                            Redis.set('OP_ticket_%s' %Md5.Md5_make(ticket),ticket)
                            Redis.set('OP_logout_ticket_%s' %ticket, ticket)
                            Redis.set('OP_dingid_ticket_%s' %ticket, dingunionid)
                            return app_resp
            except Exception as e:
                logging.error(e)
    except Exception as e:
        logging.error(e)
        return redirect(url_for('error'))
    return redirect(cas_client.get_login_url(service_url=service_url))
Exemple #2
0
def approval():
    try:
        tools.Async_log(g.user, request.url)
        dt = time.strftime('%Y-%m-%d', time.localtime())
        db_approval = db_op.user_approval
        db_sso = db_op.user_sso
        db_permission = db_op.permission
        tables = ('申请人','部门','申请日期', '申请权限', '审批状态', '操作')
        action = tools.http_args(request,'action')
        id = tools.http_args(request,'id')
        status = {'allow': '审批通过', 'deny': '审批拒绝'}
        # 判断访问参数
        if action in ('allow', 'deny') and id:
            #验证操作人是否真实
            val = db_sso.query.filter(db_sso.dingunionid == g.dingId).all()
            if val:
                # 修改申请权限表状态
                db_approval.query.filter(and_(db_approval.id == id)).update({db_approval.status: status[action], db_approval.approval_time: dt,db_approval.approval_person: g.user})
                db_op.DB.session.commit()
                if action == 'allow':
                    # 写入授权列表
                    vals = db_approval.query.with_entities(db_approval.apply_grade).filter(db_approval.id == id).all()
                    grade = vals[0][0]
                    if int(grade) == 1:
                        db_sso.query.filter(db_sso.dingunionid==g.dingId).update({db_sso.grade:','.join([str(x) for x in range(1, 11)])})
                    else:
                        db_sso.query.filter(db_sso.dingunionid == g.dingId).update({db_sso.grade: '%s,9,10' % grade})
                    db_op.DB.session.commit()
    except  Exception as e:
        logging.error(e)
        db_op.DB.session.rollback()
    finally:
        # 获取权限列表
        auths = db_permission.query.with_entities(db_permission.authid, db_permission.auth).all()
        auths = {val[0]: val[1] for val in auths}
        users = db_sso.query.with_entities(db_sso.dingunionid,db_sso.realName,db_sso.department).all()
        users = {val[0]: val[1:] for val in users}
        values = db_approval.query.with_entities(db_approval.id, db_approval.dingId, db_approval.apply_time,db_approval.apply_grade, db_approval.status).filter(db_approval.status == '待审批').all()
    return render_template('approval.html', tables=tables, values=values, auths=auths,users=users)
Exemple #3
0
def assets_manage():
    form = MyForm.MyForm_assets_manage()
    db_idc_id = db_idc.idc_id
    db_server = db_idc.idc_servers
    db_network = db_idc.idc_networks
    db_store = db_idc.idc_store
    db_third = db_idc.third_resource
    db_project = db_op.project_list
    db_zabbix = db_idc.zabbix_info
    db_project_third = db_op.project_third
    change_infos = defaultdict()
    if tools.http_args(request,'mid') == 'assets_manage':
        if tools.http_args(request,'aid') and tools.http_args(request,'sid') and tools.http_args(request,'action'):
            if ':' in tools.http_args(request,'sid'):
                js_ip,js_ssh_port = tools.http_args(request,'sid').split(':')
                vals = db_server.query.with_entities(db_server.idc_id,db_server.idrac,db_server.purch_date,db_server.expird_date).filter(and_(db_server.ip==js_ip,db_server.ssh_port==js_ssh_port)).all()
                if vals:
                    idc_id,idrac,purch_date,expird_date = vals[0]
                    cids = db_idc_id.query.with_entities(db_idc_id.aid,db_idc_id.cid).filter(db_idc_id.id==idc_id).all()
                    if cids:
                        aid,cid = cids[0]
                        change_infos['aid'] = aid
                        change_infos['cid'] = cid
                        change_infos['idrac'] = idrac
                        change_infos['purch_date'] = purch_date
                        change_infos['expird_date'] = expird_date
                        change_infos['sid'] = tools.http_args(request,'sid')
            else:
                change_infos['aid'] = tools.http_args(request,'aid')
                change_infos['sid'] = tools.http_args(request,'sid')
            change_infos['action'] = tools.http_args(request,'action')
    if form.submit.data:
        try:
            aid = form.select_aid.data
            ips = form.text.data.strip()
            ips = set(ips.splitlines())
            rack = form.rack.data.strip()
            if rack:
                if not rack.endswith('机柜') and rack != 'KVM':
                    rack = '%s机柜' %rack
                rack.upper()
            action = form.select_action.data
            purch = form.purch.data
            expird = form.expird.data
            device = form.select_device.data
            idrac = form.idrac.data.strip()
            device_type = form.device_type.data.strip()
            fault = form.fault.data
            old_host = form.old_host.data
            #新增设备
            if action == 'add':
                if ips:
                    for info in ips:
                        if info:
                            if rack and purch and expird:
                                host_type = 'physical'
                                if rack == 'KVM':
                                    host_type = 'vm'
                                # 获取机房机柜信息
                                idc_id = db_idc_id.query.with_entities(db_idc_id.id).filter(and_(db_idc_id.aid == aid, db_idc_id.cid == rack)).all()
                                if not idc_id:
                                    c = db_idc_id(aid=aid, cid=rack)
                                    db_idc.DB.session.add(c)
                                    db_idc.DB.session.commit()
                                    idc_id = db_idc_id.query.with_entities(db_idc_id.id).filter(and_(db_idc_id.aid == aid, db_idc_id.cid == rack)).all()
                                idc_id = int(idc_id[0][0])
                                #判断是否为服务器
                                if device == 'server':
                                    if ':' in info:
                                        if len(info.split(':')) == 2:
                                            ip,ssh_port = info.split(':')
                                        else:
                                            raise flash('%s格式不符合要求!' %info)
                                    else:
                                        ip,ssh_port = (info.strip(),20443)
                                    #查询资产信息
                                    values = db_server.query.filter(and_(db_server.ip == ip,db_server.ssh_port == ssh_port)).all()
                                    if values:
                                        #获取自动发现信息
                                        discovery_values = db_server.query.filter(and_(db_server.ip == ip, db_server.ssh_port == ssh_port,db_server.status == '新发现')).all()
                                        #修改自动发现信息
                                        if discovery_values:
                                            if idc_id:
                                                db_server.query.filter(and_(db_server.ip == ip, db_server.ssh_port == ssh_port,db_server.status == '新发现')).update({db_server.idc_id:idc_id,db_server.status:'未使用',db_server.host_type:host_type,db_server.purch_date:purch,db_server.expird_date:expird,db_server.idrac:idrac})
                                                db_idc.DB.session.commit()
                                            flash('%s服务器上架成功!' % info)
                                        else:
                                            flash('%s服务器已存在!' % info)
                                    else:
                                        #添加资产信息
                                        if tcpping(host=ip, port=ssh_port, timeout=3):
                                            s = db_server(idc_id=idc_id,ip=ip,ssh_port=ssh_port,idrac=idrac,purch_date=purch,expird_date=expird,status='未使用',s_ip=None,host_type=host_type,hostname='',sn='',manufacturer='',productname='',system='',cpu_info='',cpu_core='',mem='',disk_count=0,disk_size='',comment='',)
                                            db_idc.DB.session.add(s)
                                            db_idc.DB.session.commit()
                                            flash('%s服务器上架成功!' %info)
                                        else:
                                            flash('运维平台尝试登录{0}的ssh端口失败,服务器不能上架!'.format(info))
                                #判断是否为网络设备
                                if device == 'network':
                                    if device_type and purch and expird:
                                        ip = info
                                        #查询资产信息
                                        values = db_network.query.filter(and_(db_network.ip == ip, db_network.idc_id == idc_id)).all()
                                        if values:
                                            raise flash('%s网络设备资产已存在!' % info)
                                        #添加资产信息
                                        v = db_network(idc_id=idc_id,type=device_type,ip=ip,redundance='是',purch_date=purch,expird_date=expird,status='使用中',comment='')
                                        db_idc.DB.session.add(v)
                                        db_idc.DB.session.commit()
                                        flash('%s网络设备上架成功!' % ip)
                                    else:
                                        raise flash('上架网络设备,设备型号不能为空!')
                                #判断是否为存储设备
                                if device == 'store':
                                    if device_type:
                                        ip = info
                                        #获取资产信息
                                        values = db_store.query.filter(and_(db_store.ip == ip,db_store.idc_id==idc_id)).all()
                                        if values:
                                            raise flash('%s存储设备资产已存在!' % info)
                                        #添加资产信息
                                        v = db_store(idc_id=idc_id,type=device_type,ip=ip,purch_date=purch,expird_date=expird,status='使用中',comment='')
                                        db_idc.DB.session.add(v)
                                        db_idc.DB.session.commit()
                                        flash('%s存储设备上架成功!' % ip)
                                    else:
                                        raise flash('上架存储设备,设备型号不能为空!')
                            else:
                                raise flash('必填项目不能为空!', 'error')
            #下架设备
            if action == 'down':
                if ips:
                    for info in ips:
                        if info:
                            idc_id =db_idc_id.query.with_entities(db_idc_id.id).filter(db_idc_id.aid == aid).all()
                            idc_id = tuple([id[0] for id in idc_id])
                            #判断为服务器删除
                            if device == 'server':
                                if ':' in info:
                                    if len(info.split(':')) == 2:
                                        ip, ssh_port = info.split(':')
                                    else:
                                        raise flash('%s格式不符合要求!' %info)
                                else:
                                    ip, ssh_port = (info, 20443)
                                server_values = db_server.query.filter(and_(db_server.ip == ip, db_server.ssh_port == ssh_port,db_server.idc_id.in_(idc_id))).all()
                                if server_values:
                                    #判断服务器是否在使用
                                    project_values = db_project.query.filter(and_(db_project.ip == ip,db_project.ssh_port == ssh_port,db_project.status=='使用中')).all()
                                    third_values = db_third.query.filter(and_(db_third.ip == ip, db_third.ssh_port == ssh_port,db_third.status=='使用中')).all()
                                    if project_values or third_values:
                                        raise flash('%s分配有应用资源,不能下架!' %info)
                                    #删除自有资源信息
                                    v = db_project.query.filter(and_(db_project.ip == ip, db_project.ssh_port == ssh_port)).all()
                                    for c in v:
                                        db_op.DB.session.delete(c)
                                        db_op.DB.session.commit()
                                    # 删除第三方资源信息
                                    v = db_third.query.filter(and_(db_third.ip == ip, db_third.ssh_port == ssh_port)).all()
                                    for c in v:
                                        db_idc.DB.session.delete(c)
                                        db_idc.DB.session.commit()
                                    # 删除zabbix信息
                                    v = db_zabbix.query.filter(and_(db_zabbix.ip == ip, db_zabbix.ssh_port == ssh_port)).all()
                                    for c in v:
                                        db_idc.DB.session.delete(c)
                                        db_idc.DB.session.commit()
                                    # 删除服务器信息
                                    v = db_server.query.filter(and_(db_server.ip == ip, db_server.ssh_port == ssh_port,db_server.idc_id.in_(idc_id))).all()
                                    for c in v:
                                        db_idc.DB.session.delete(c)
                                        db_idc.DB.session.commit()
                                    flash('%s服务器下架成功!' %info)
                                else:
                                    flash('%s资产没有找到!' %info)
                            else:
                                ip = info
                                #查询是否为网络设备
                                v_network = db_network.query.filter(and_(db_network.ip == ip,db_network.idc_id.in_(idc_id))).all()
                                v_store = db_store.query.filter(and_(db_store.ip == ip,db_store.idc_id.in_(idc_id))).all()
                                if v_network:
                                    for c in v_network:
                                        db_idc.DB.session.delete(c)
                                        db_idc.DB.session.commit()
                                    flash('%s网络设备下架成功!' %info)
                                #查询是否为存储设备
                                elif v_store:
                                    for c in v_store:
                                        db_idc.DB.session.delete(c)
                                        db_idc.DB.session.commit()
                                    flash('%s存储设备下架成功!' %info)
                                else:
                                    flash('%s资产没有找到!' %info)
            #修改设备信息
            if action == 'modify':
                if ips:
                    for info in ips:
                        if info:
                            #修改服务器信息
                            if device == 'server':
                                if ':' in info:
                                    if len(info.split(':')) == 2:
                                        ip, ssh_port = info.split(':')
                                    else:
                                        raise flash('%s格式不符合要求!' %info)
                                else:
                                    ip, ssh_port = (info, 20443)
                                #修改IP信息
                                if old_host:
                                    if ':' in old_host.strip():
                                        if len(old_host.split(':')) == 2:
                                            old_ip, old_ssh_port = old_host.split(':')
                                        else:
                                            raise flash('%s格式不符合要求!' % info)
                                    else:
                                        old_ip = old_host.strip()
                                        old_ssh_port = 20443
                                    if len(ips) == 1:
                                        if tcpping(host=ip, port=ssh_port, timeout=5):
                                            # 获取third_id
                                            third_id = db_third.query.with_entities(db_third.id).filter(and_(db_third.ip == ip, db_third.ssh_port == ssh_port)).all()
                                            # 清除项目资源表
                                            if third_id:
                                                v = db_project_third.query.filter(db_project_third.third_id == int(third_id[0][0])).all()
                                                if v:
                                                    for c in v:
                                                        db_op.DB.session.delete(c)
                                                        db_op.DB.session.commit()
                                            # 删除第三方资源信息
                                            v = db_third.query.filter(and_(db_third.ip == ip, db_third.ssh_port == ssh_port)).all()
                                            if v:
                                                for c in v:
                                                    db_idc.DB.session.delete(c)
                                                    db_idc.DB.session.commit()
                                            # 删除zabbix信息
                                            v = db_zabbix.query.filter(and_(db_zabbix.ip == ip, db_zabbix.ssh_port == ssh_port)).all()
                                            if v:
                                                for c in v:
                                                    db_idc.DB.session.delete(c)
                                                    db_idc.DB.session.commit()
                                            #修改自有资源信息
                                            db_project.query.filter(and_(db_project.ip == old_ip, db_project.ssh_port == old_ssh_port)).update({db_project.ip: ip, db_project.ssh_port: ssh_port})
                                            db_op.DB.session.commit()
                                            #删除新发现服务器信息
                                            v = db_server.query.filter(and_(db_server.ip == ip,db_server.ssh_port == ssh_port)).all()
                                            if v:
                                                for c in v:
                                                    db_idc.DB.session.delete(c)
                                                    db_idc.DB.session.commit()
                                            #修改服务器信息
                                            db_server.query.filter(and_(db_server.ip == old_ip,db_server.ssh_port == old_ssh_port)).update({db_server.ip: ip, db_server.ssh_port: ssh_port})
                                            db_idc.DB.session.commit()
                                        else:
                                            raise flash('运维平台尝试登录新IP{0}的ssh端口失败,服务器IP不能修改!'.format(ip))
                                    else:
                                        raise flash('修改服务器IP,新IP信息只能填写一行!', 'error')
                                values = db_server.query.filter(and_(db_server.ip == ip, db_server.ssh_port == ssh_port)).all()
                                if values:
                                    db_infos = {}
                                    if idrac:
                                        db_infos[db_server.idrac] = idrac
                                    if purch:
                                        db_infos[db_server.purch_date] = purch
                                    if expird:
                                        db_infos[db_server.expird_date] = expird
                                    if fault:
                                        db_infos[db_server.status] = '维护中'
                                    if db_infos:
                                        db_server.query.filter(and_(db_server.ip == ip, db_server.ssh_port == ssh_port)).update(db_infos)
                                    if rack:
                                        val = db_idc_id.query.filter(and_(db_idc_id.aid == aid,db_idc_id.cid == rack)).all()
                                        if not val:
                                            v = db_idc_id(aid=aid,cid=rack)
                                            db_idc.DB.session.add(v)
                                            db_idc.DB.session.commit()
                                        idc_id = db_idc_id.query.with_entities(db_idc_id.id).filter(and_(db_idc_id.aid == aid,db_idc_id.cid == rack)).all()
                                        idc_id =  int(idc_id[0][0])
                                        db_server.query.filter(and_(db_server.ip == ip, db_server.ssh_port == ssh_port)).update({db_server.idc_id:idc_id})

                                    try:
                                        db_idc.DB.session.commit()
                                    except Exception as e:
                                        flash(e,'error')
                                    else:
                                        flash('%s服务器信息修改成功!' % info)
                                else:
                                    flash('%s资产没有找到!' % info)
                            else:
                                #修改网络或者存储设备信息
                                ip = info
                                network_infos = {}
                                store_infos = {}
                                v_network = db_network.query.filter(and_(db_network.ip == ip)).all()
                                v_store = db_store.query.filter(and_(db_store.ip == ip)).all()
                                if v_network or v_store:
                                    if device_type:
                                        network_infos[db_network.type] = device_type
                                        store_infos[db_store.type] = device_type
                                    if purch:
                                        network_infos[db_network.purch_date] = purch
                                        store_infos[db_store.purch_date] = purch
                                    if expird:
                                        network_infos[db_network.expird_date] = expird
                                        store_infos[db_store.expird_date] = expird
                                    if network_infos and store_infos:
                                        db_network.query.filter(and_(db_network.aid == aid, db_network.ip == ip)).update(network_infos)
                                        db_store.query.filter(db_store.ip == ip).update(store_infos)
                                    if rack:
                                        val = db_idc_id.query.filter(and_(db_idc_id.aid == aid, db_idc_id.cid == rack)).all()
                                        if not val:
                                            v = db_idc_id(aid=aid, cid=rack)
                                            db_idc.DB.session.add(v)
                                            db_idc.DB.session.commit()
                                        idc_id = db_idc_id.query.with_entities(db_idc_id.id).filter(and_(db_idc_id.aid == aid, db_idc_id.cid == rack)).all()
                                        idc_id = int(idc_id[0][0])
                                        db_network.query.filter(and_(db_network.ip == ip)).update({db_network.idc_id:idc_id})
                                        db_store.query.filter(and_(db_store.ip == ip)).update({db_store.idc_id:idc_id})
                                    else:
                                        flash('%设备修改信息为空' %info)
                                    try:
                                        db_idc.DB.session.commit()
                                    except Exception as e:
                                        flash(e, 'error')
                                    else:
                                        flash('%s服务器信息修改成功!' %info)
                                else:
                                    raise flash('%s资产没有找到!' %info)
        except Exception as e:
            db_idc.DB.session.rollback()
            db_op.DB.session.rollback()
            if 'old' not in str(e):
                logging.error(e)
                flash(e)
        finally:
            return render_template('Message.html')
    return render_template('assets_manage.html',form=form,change_infos=change_infos)
Exemple #4
0
def resource_pool(action=None, id=None):
    importlib.reload(MyForm)
    form = MyForm.MyFromResourcePool()
    form_third = MyForm.MyFromThirdResource()
    db_project = db_op.project_list
    db_server = db_idc.idc_servers
    db_third = db_idc.third_resource
    db_project_third = db_op.project_third
    source_type = 'self'

    def recyle_resource(ip, ssh_port, app_port):
        try:
            project_id = db_project.query.with_entities(db_project.id).filter(
                and_(db_project.ip == ip, db_project.ssh_port == ssh_port,
                     db_project.app_port == app_port)).all()
            third_id = db_third.query.with_entities(db_third.id).filter(
                and_(db_third.ip == ip, db_third.ssh_port == ssh_port,
                     db_third.app_port == app_port)).all()
            if project_id or third_id:
                # 删除自有资源表信息
                v = db_project.query.filter(
                    and_(db_project.ip == ip, db_project.ssh_port == ssh_port,
                         db_project.app_port == app_port)).all()
                for c in v:
                    db_op.DB.session.delete(c)
                    db_op.DB.session.commit()
                # 删除第三方资源表信息
                v = db_third.query.filter(
                    and_(db_third.ip == ip, db_third.ssh_port == ssh_port,
                         db_third.app_port == app_port)).all()
                for c in v:
                    db_idc.DB.session.delete(c)
                    db_idc.DB.session.commit()
                # 删除项目资源表信息
                if third_id:
                    third_id = third_id[0][0]
                    v = db_project_third.query.filter(
                        db_project_third.third_id == third_id).all()
                    for c in v:
                        db_op.DB.session.delete(c)
                        db_op.DB.session.commit()
                if project_id:
                    project_id = project_id[0][0]
                    v = db_project_third.query.filter(
                        db_project_third.project_id == project_id).all()
                    for c in v:
                        db_op.DB.session.delete(c)
                        db_op.DB.session.commit()
            # 判断该服务器资源不再被使用
            project_vals = db_project.query.filter(
                and_(db_project.ip == ip,
                     db_project.ssh_port == ssh_port)).all()
            third_vals = db_third.query.filter(
                and_(db_third.ip == ip, db_third.ssh_port == ssh_port)).all()
            if not project_vals and not third_vals:
                # 资源自动回收
                db_server.query.filter(db_server.ip == ip,
                                       db_server.ssh_port == ssh_port).update(
                                           {db_server.status: "未使用"})
                db_idc.DB.session.commit()
        except Exception as e:
            logging.error(e)

    tables = ['项目', '域名', '主机名', '应用服务', '应用端口', '开发语言', '部署环境', '操作']
    third_tables = ['项目', '应用服务', '集群类型', '主机名', '应用端口', '操作']
    #默认项目列表展示
    project = db_project.query.with_entities(distinct(
        db_project.project)).limit(1)
    project = project[0][0]
    values = db_project.query.with_entities(
        db_project.id, db_project.project, db_project.domain, db_project.ip,
        db_project.ssh_port, db_project.resource, db_project.app_port,
        db_project.sys_args,
        db_project.env).filter(db_project.project == project).all()
    third_values = []
    third_indexs = []
    # 按照项目查找
    if tools.http_args(request, 'project'):
        project = tools.http_args(request, 'project')
        servers = db_server.query.with_entities(db_server.ip,
                                                db_server.ssh_port,
                                                db_server.hostname).all()
        servers = {'%s:%s' % (info[0], info[1]): info[2] for info in servers}
        values = db_project.query.with_entities(
            db_project.id, db_project.ip, db_project.ssh_port,
            db_project.resource, db_project.app_port, db_project.sys_args,
            db_project.env).filter(db_project.project == project).order_by(
                db_project.ip).all()
        if values:
            values = [
                list(val) for val in values
                if '%s:%s' % (val[1], val[2]) in servers
            ]
            for val in values:
                val.insert(3, servers['%s:%s' % (val[1], val[2])])
            values.insert(0, tables[1:])
        third_id = db_project_third.query.with_entities(
            db_project_third.third_id).filter(
                db_project_third.project == project).all()
        if third_id:
            third_id = tuple([val[0] for val in third_id])
            third_values = db_third.query.with_entities(
                db_third.id, db_third.resource_type, db_third.cluster_type,
                db_third.ip, db_third.ssh_port,
                db_third.app_port).filter(db_third.id.in_(third_id)).order_by(
                    db_third.resource_type).all()
            if third_values:
                third_values = [list(val) for val in third_values]
                for val in third_values:
                    val.insert(5, servers['%s:%s' % (val[3], val[4])])
                third_values.insert(0, third_tables[1:])
        return render_template('resource_info.html',
                               values=values,
                               third_values=third_values,
                               project=project,
                               third_indexs=third_indexs,
                               form=form)
    # 按照应用服务查找
    if tools.http_args(request, 'application'):
        app = tools.http_args(request, 'application')
        app_id = tools.http_args(request, 'application-id')
        tables = ['应用服务', '集群类型', '主机名', '应用端口']
        host_info = None
        values = []
        servers = db_server.query.with_entities(db_server.ip,
                                                db_server.ssh_port,
                                                db_server.hostname).all()
        servers = {'%s:%s' % (info[0], info[1]): info[2] for info in servers}
        if app_id:
            if app in ('tomcat', 'python', 'php'):
                host_info = db_project.query.with_entities(
                    db_project.ip, db_project.ssh_port,
                    db_project.app_port).filter(db_project.id == app_id).all()
                if host_info:
                    host_info = [
                        servers['%s:%s' % (host_info[0][0], host_info[0][1])],
                        host_info[0][-1]
                    ]
                    third_id = db_project_third.query.with_entities(
                        db_project_third.third_id).filter(
                            and_(db_project_third.project_id == app_id)).all()
                    if third_id:
                        third_id = [val[0] for val in third_id]
                        values = db_third.query.with_entities(
                            db_third.id, db_third.resource_type,
                            db_third.cluster_type, db_third.ip,
                            db_third.ssh_port, db_third.app_port).filter(
                                db_third.id.in_(tuple(third_id))).order_by(
                                    db_third.resource_type).all()
                        if values:
                            values = [list(val) for val in values]
            else:
                host_info = db_third.query.with_entities(
                    db_third.ip, db_third.ssh_port,
                    db_third.app_port).filter(db_third.id == app_id).all()
                if host_info:
                    host_info = [
                        servers['%s:%s' % (host_info[0][0], host_info[0][1])],
                        host_info[0][-1]
                    ]
                    project_id = db_project_third.query.with_entities(
                        db_project_third.project_id).filter(
                            and_(db_project_third.third_id == app_id)).all()
                    if project_id:
                        project_id = [val[0] for val in project_id]
                        values = db_project.query.with_entities(
                            db_project.id, db_project.resource, db_project.ip,
                            db_project.ssh_port, db_project.app_port).filter(
                                db_project.id.in_(tuple(project_id))).order_by(
                                    db_project.resource).all()
                        if values:
                            values = [list(val) for val in values]
                            for i, vals in enumerate(values):
                                vals.insert(2, '非集群')
                                values[i] = vals
        else:
            if app in ('php', 'tomcat', 'python'):
                values = db_project.query.with_entities(
                    db_project.id, db_project.resource, db_project.ip,
                    db_project.ssh_port, db_project.app_port).filter(
                        db_project.resource == app).order_by(
                            db_project.ip).all()
                if values:
                    values = [list(val) for val in values]
                    for i, vals in enumerate(values):
                        vals.insert(2, '非集群')
                        values[i] = vals
            else:
                values = db_third.query.with_entities(
                    db_third.id, db_third.resource_type, db_third.cluster_type,
                    db_third.ip, db_third.ssh_port, db_third.app_port).filter(
                        db_third.resource_type == app).order_by(
                            db_third.ip).all()
                if values:
                    values = [list(val) for val in values]
        if values:
            for val in values:
                val.insert(5, servers['%s:%s' % (val[3], val[4])])
        return render_template('application_list.html',
                               values=values,
                               app=app,
                               tables=tables,
                               host_info=host_info,
                               form=form)
    # 第三方资源回收接口
    if tools.http_args(request, 'action') == 'recyle' and tools.http_args(
            request, 'ip') and tools.http_args(
                request, 'ssh_port') and tools.http_args(request, 'app_port'):
        ip = tools.http_args(request, 'ip')
        ssh_port = tools.http_args(request, 'ssh_port')
        app_port = tools.http_args(request, 'app_port')
        result = recyle_resource(ip, ssh_port, app_port)
        if result:
            flash(result)
        else:
            flash('%s %s %s 资源回收完成!' % (ip, ssh_port, app_port))
        return render_template('Message.html')
    #修改项目列表
    if action == 'del' and id:
        try:
            source_type = tools.http_args(request, 'resource_type')
            if tools.http_args(request, 'resource_type') == 'self':
                #修改自有资源列表
                db_project.query.filter(db_project.id == id).update({
                    db_project.project:
                    '',
                    db_project.domain:
                    '',
                    db_project.sys_args:
                    '',
                    db_project.env:
                    '',
                    db_project.gray:
                    '',
                    db_project.status:
                    '未分配',
                    db_project.update_date:
                    time.strftime('%Y-%m-%d', time.localtime())
                })
                db_op.DB.session.commit()
                # 删除项目资源表信息
                v = db_project_third.query.filter(
                    db_project_third.project_id == id).all()
                for c in v:
                    db_op.DB.session.delete(c)
                    db_op.DB.session.commit()
                project = db_project.query.with_entities(
                    db_project.project).filter(db_project.id == id).all()
                project = project[0][0]
            if tools.http_args(request, 'resource_type') == 'third':
                #删除项目资源表信息
                v = db_project_third.query.filter(
                    db_project_third.third_id == id).all()
                for c in v:
                    db_op.DB.session.delete(c)
                    db_op.DB.session.commit()
                project = db_project_third.query.with_entities(
                    db_project_third.project).filter(
                        db_project_third.third_id == id).all()
                project = project[0][0]
        except Exception as e:
            logging.error(e)
    #资源池预分配
    if form.submit_allot.data:
        servers = form.servers.data
        ip, ssh_port = servers.split(":")
        resource = form.resource.data
        app_port = form.app_port.data.strip()
        if app_port:
            #写入资源表
            if resource in ('php', 'tomcat', 'python', 'java'):
                c = db_project(resource=resource,
                               project='',
                               domain='',
                               ip=ip,
                               ssh_port=ssh_port,
                               app_port=app_port,
                               business_id='',
                               sys_args='',
                               env='',
                               gray='',
                               status='未分配',
                               update_date=time.strftime(
                                   '%Y-%m-%d', time.localtime()))
                db_op.DB.session.add(c)
                db_op.DB.session.commit()
            else:
                c = db_third(resource_type=resource,
                             cluster_type='',
                             ip=ip,
                             ssh_port=ssh_port,
                             app_port=app_port,
                             busi_id=0,
                             department='',
                             person='',
                             contact='',
                             status='未分配',
                             update_date=time.strftime('%Y-%m-%d',
                                                       time.localtime()))
                db_idc.DB.session.add(c)
                db_idc.DB.session.commit()
            flash('%s应用资源预配成功!' % servers)
    #资源池资源加锁
    if form.submit_lcok.data:
        try:
            servers = form.servers.data
            ip, ssh_port = servers.split(":")
            project_vals = db_project.query.filter(
                and_(db_project.ip == ip,
                     db_project.ssh_port == ssh_port)).all()
            third_vals = db_third.query.filter(
                and_(db_third.ip == ip, db_third.ssh_port == ssh_port)).all()
            if project_vals or third_vals:
                db_server.query.filter(
                    and_(db_server.ip == ip,
                         db_server.ssh_port == ssh_port)).update(
                             {db_server.status: '使用中'})
                db_idc.DB.session.commit()
            else:
                raise flash('%s该资源还未分配应用资源,不能锁定!' % servers)
        except Exception as e:
            logging.error(e)
        else:
            flash('%s资源将不能在分配应用服务!' % servers)
    #第三方资源回收
    if form_third.submit_recucle.data:
        hosts = form_third.hosts.data
        if hosts:
            hosts = hosts.splitlines()
            for host in hosts:
                if len(host.split(':')) == 3:
                    ip, ssh_port, app_port = host.split(':')
                    result = recyle_resource(ip, ssh_port, app_port)
                    if result:
                        flash(result)
                    else:
                        flash('%s %s %s 资源回收完成!' % (ip, ssh_port, app_port))
                else:
                    flash('%s格式不符合要求!' % host)
    #项目列表增加主机
    if form.submit_add.data:
        try:
            resource_val = form.hosts_add.data
            project = form.Project.data
            resource_val = resource_val.split(':')
            resource, app_port, ip, ssh_port = resource_val
            vals = db_project.query.with_entities(
                db_project.domain, db_project.sys_args).filter(
                    and_(db_project.project == project,
                         db_project.env == '生产')).limit(1)
            domain, sys_args = vals[0]
            #写入自有资源表
            db_project.query.filter(
                and_(db_project.resource == resource, db_project.ip == ip,
                     db_project.ssh_port == ssh_port,
                     db_project.app_port == app_port).update({
                         db_project.project:
                         project,
                         db_project.domain:
                         domain,
                         db_project.sys_args:
                         sys_args,
                         db_project.env:
                         '生产',
                         db_project.gray:
                         '0',
                         db_project.status:
                         '使用中',
                         db_project.update_date:
                         time.strftime('%Y-%m-%d', time.localtime())
                     }))
            db_op.DB.session.commit()
            #重新加载数据
            values = db_project.query.with_entities(
                db_project.id, db_project.project, db_project.domain,
                db_project.ip, db_project.ssh_port, db_project.resource,
                db_project.app_port, db_project.sys_args,
                db_project.env).filter(db_project.project == project).all()
        except Exception as e:
            logging.error(e)
    #项目列表查询
    if form.submit_query.data:
        source_type = form.source_type.data
        project = form.Project.data
        #数据初始化
        values = []
        try:
            #判断是否需要条件查找
            if source_type == 'self':
                values = db_project.query.with_entities(
                    db_project.id, db_project.project, db_project.domain,
                    db_project.ip, db_project.ssh_port, db_project.resource,
                    db_project.app_port, db_project.sys_args,
                    db_project.env).filter(
                        db_project.project == project).all()
            if source_type == 'third':
                third_id = db_project_third.query.with_entities(
                    db_project_third.third_id).filter(
                        db_project_third.project == project).all()
                if third_id:
                    third_id = [id[0] for id in third_id]
                    third_values = db_third.query.with_entities(
                        db_third.id, db_third.resource_type,
                        db_third.cluster_type, db_third.ip, db_third.ssh_port,
                        db_third.app_port).filter(
                            db_third.id.in_(tuple(third_id))).order_by(
                                db_third.resource_type).all()
            if third_values:
                for i, t_val in enumerate(third_values):
                    t_val = list(t_val)
                    t_val.insert(1, project)
                    third_values[i] = t_val
        except Exception as e:
            flash(e)
    return render_template('resource_pool.html',
                           form=form,
                           form_third=form_third,
                           tables=tables,
                           third_tables=third_tables,
                           values=values,
                           third_values=third_values,
                           source_type=source_type,
                           project=project)
Exemple #5
0
def ensure_application():
    try:
        db_work_order = db_op.work_order
        db_publish_application = db_op.publish_application
        db_sso = db_op.user_sso
        msg = None
        source = 'ensure_application'
        Key = 'new_application_work_number_%s' % dt
        #验证票据
        actions = {'complete': '已完成', 'deny': '已拒绝', 'rollback': '已回滚'}
        ticket = tools.http_args(request, 'ticket')
        action = tools.http_args(request, 'action')
        work_number = tools.http_args(request, 'work_number')
        if ticket or (action == 'activate' and work_number):
            if ticket:
                work_number = Redis.get('work_order_ticket_%s' % ticket)
            if work_number:
                val = db_work_order.query.filter(
                    db_work_order.work_number == int(work_number)).all()
                if val:
                    val = db_work_order.query.filter(
                        and_(db_work_order.work_number == int(work_number),
                             db_work_order.source == source,
                             db_work_order.status == '未受理')).all()
                    if val:
                        db_work_order.query.filter(db_work_order.work_number ==
                                                   int(work_number)).update({
                                                       db_work_order.dingid:
                                                       g.dingId,
                                                       db_work_order.status:
                                                       '受理中'
                                                   })
                        db_op.DB.session.commit()
                        Redis.sadd(Key, work_number)
                        Redis.delete('work_order_ticket_%s' % ticket)
                        msg = "%s工单已受理!" % work_number
                    else:
                        msg = "工单暂时无法受理!"
                else:
                    msg = "无效的请求验证地址!"
            else:
                msg = "无效的请求验证地址!"
        #验证执行状态
        if action and work_number:
            moment = None
            if action in actions:
                if action == 'deny':
                    moment = tools.http_args(request, 'moment')
                val = db_work_order.query.filter(
                    and_(db_work_order.work_number == int(work_number),
                         db_work_order.source == source,
                         db_work_order.status == '受理中')).all()
                if val:
                    db_work_order.query.filter(
                        db_work_order.work_number == int(work_number)).update(
                            {db_work_order.status: actions[action]})
                    db_op.DB.session.commit()
                    Redis.srem(Key, work_number)
                    if Redis.exists('op_send_mail_html_%s' % work_number):
                        dingid = db_publish_application.query.with_entities(
                            db_publish_application.dingid).filter(
                                db_publish_application.work_number ==
                                work_number).all()
                        mailer = db_sso.query.with_entities(
                            db_sso.mail).filter(
                                db_sso.dingunionid == dingid[0][0]).all()
                        if mailer:
                            Msg = Message("%s工单进度通知" % work_number,
                                          sender=sender,
                                          recipients=[mailer[0][0]],
                                          cc=[receiver],
                                          charset='utf-8')
                            mail_html = Redis.get('op_send_mail_html_%s' %
                                                  work_number)
                            alarm_html = '<p style="color:red">工单当前进度:%s</p>' % actions[
                                action]
                            if action == 'deny':
                                if moment:
                                    alarm_html = "%s<p>拒绝说明:%s</p>" % (
                                        alarm_html, moment)
                            Msg.html = '%s%s' % (mail_html, alarm_html)
                            with mapp.app_context():
                                mail.send(Msg)
                    msg = "%s工单当前状态:%s!" % (work_number, actions[action])
                else:
                    msg = "无效操作!"
    except Exception as e:
        logging.error(e)
        if not msg:
            msg = "未知异常错误!"
    finally:
        if action == 'deny':
            return jsonify({'status': 'ok'})
        #获取最新数据
        tables = ('工单号', '日期', '项目名称', '版本', '描述', '申请人', '执行人', '详情', '状态',
                  '操作')
        users = db_sso.query.with_entities(db_sso.dingunionid,
                                           db_sso.realName).all()
        users = {info[0]: info[1:] for info in users}
        projects = db_publish_application.query.with_entities(
            db_publish_application.work_number, db_publish_application.date,
            db_publish_application.project, db_publish_application.version,
            db_publish_application.describe,
            db_publish_application.dingid).all()
        projects = {info[0]: info[1:] for info in projects}
        work_orders = db_work_order.query.with_entities(
            db_work_order.work_number, db_work_order.dingid,
            db_work_order.status).filter(
                db_work_order.source == source).order_by(desc(
                    db_work_order.id)).all()
        if action and work_number:
            if action == 'query':
                work_orders = db_work_order.query.with_entities(
                    db_work_order.work_number, db_work_order.dingid,
                    db_work_order.status).filter(
                        and_(db_work_order.source == source,
                             db_work_order.work_number == work_number)).all()
        if work_orders:
            work_orders = [list(info) for info in work_orders]
            for info in work_orders:
                info.extend(projects[info[0]][:-1])
                info.extend(users[projects[info[0]][-1]])
                if info[1]:
                    info.append(users[info[1]][0])
                else:
                    info.append('')
        new_work_number = Redis.smembers(Key)
    return render_template('ensure_work_order.html',
                           tables=tables,
                           work_orders=work_orders,
                           msg=msg,
                           new_work_number=new_work_number,
                           total='代码上线工单管理')
Exemple #6
0
def ensure_server_auth():
    try:
        db_work_order = db_op.work_order
        db_server_auth = db_op.server_auth
        db_sso = db_op.user_sso
        msg = None
        source = 'ensure_server_auth'
        Key = 'new_server_auth_work_number_%s' % dt
        actions = {'complete': '已完成', 'deny': '审批拒绝', 'agree': '审批通过'}
        action = tools.http_args(request, 'action')
        work_number = tools.http_args(request, 'work_number')
        #验证票据
        ticket = tools.http_args(request, 'ticket')
        if ticket or (action == 'activate' and work_number):
            if ticket:
                work_number = Redis.get('work_order_ticket_%s' % ticket)
            if work_number:
                val = db_work_order.query.filter(
                    db_work_order.work_number == int(work_number)).all()
                if val:
                    val = db_work_order.query.filter(
                        and_(db_work_order.work_number == int(work_number),
                             db_work_order.source == source,
                             db_work_order.status == '审批通过')).all()
                    if val:
                        db_work_order.query.filter(
                            and_(db_work_order.work_number == int(work_number),
                                 db_work_order.source == source)).update({
                                     db_work_order.dingid:
                                     g.dingId,
                                     db_work_order.status:
                                     '受理中'
                                 })
                        db_op.DB.session.commit()
                        Redis.sadd(Key, work_number)
                        Redis.delete('work_order_ticket_%s' % ticket)
                        msg = "%s工单已受理!" % work_number
                    else:
                        msg = "工单暂时无法受理!"
                else:
                    msg = "无效的请求验证地址!"
            else:
                msg = "无效的请求验证地址!"
        #验证执行状态
        if action and work_number:
            if action in actions:
                if action in ('deny', 'agree'):
                    if g.grade[0] != '0':
                        msg = '当前用户无审批权限!'
                        raise AssertionError
                val = db_work_order.query.filter(
                    and_(db_work_order.work_number == int(work_number),
                         db_work_order.source == source,
                         db_work_order.status.in_(('待审批', '受理中')))).all()
                if val:
                    db_work_order.query.filter(
                        and_(db_work_order.work_number == int(work_number),
                             db_work_order.source == source)).update(
                                 {db_work_order.status: actions[action]})
                    db_op.DB.session.commit()
                    Redis.srem(Key, work_number)
                    #完成发送邮件
                    if Redis.exists('op_send_mail_html_%s' % work_number):
                        dingid = db_server_auth.query.with_entities(
                            db_server_auth.dingid).filter(
                                db_server_auth.work_number ==
                                work_number).all()
                        mailer = db_sso.query.with_entities(
                            db_sso.mail).filter(
                                db_sso.dingunionid == dingid[0][0]).all()
                        if mailer:
                            Msg = Message("%s工单进度通知" % work_number,
                                          sender=sender,
                                          recipients=[mailer[0][0]],
                                          cc=[receiver],
                                          charset='utf-8')
                            mail_html = Redis.get('op_send_mail_html_%s' %
                                                  work_number)
                            alarm_html = '<p style="color:red">工单当前进度:%s</p>' % actions[
                                action]
                            Msg.html = '%s%s' % (mail_html, alarm_html)
                            if action == 'agree':
                                Msg.html = '%s%s%s' % (
                                    mail_html, alarm_html,
                                    Redis.get(
                                        'op_send_mail_url_%s' % work_number))
                            with mapp.app_context():
                                mail.send(Msg)
                    if Redis.exists(
                            'op_send_dingding_msg_%s' %
                            work_number) and action in ('deny', 'agree'):
                        text = eval(
                            Redis.get('op_send_dingding_msg_%s' % work_number))
                        text.append("##### 审批结果:%s" % actions[action])
                        if action == 'agree':
                            text.append("##### %s" % Redis.get(
                                'op_send_dingding_url_%s' % work_number))
                        tools.dingding_msg(text, token=work_token)
                    msg = "%s工单当前状态:%s!" % (work_number, actions[action])
                else:
                    msg = "无效操作!"
    except Exception as e:
        logging.error(e)
        if not msg:
            msg = "未知异常错误!"
    finally:
        #获取最新数据
        tables = ('工单号', '日期', '申请人', '部门', '系统账号', '服务器列表', '申请权限', '所属用途',
                  '执行人', '详情', '状态', '操作')
        users = db_sso.query.with_entities(db_sso.dingunionid, db_sso.realName,
                                           db_sso.department).all()
        users = {info[0]: info[1:] for info in users}
        servers = db_server_auth.query.with_entities(
            db_server_auth.work_number, db_server_auth.date,
            db_server_auth.account, db_server_auth.servers,
            db_server_auth.auth_level, db_server_auth.purpose,
            db_server_auth.dingid).all()
        servers = {info[0]: info[1:] for info in servers}
        work_orders = db_work_order.query.with_entities(
            db_work_order.work_number, db_work_order.dingid,
            db_work_order.status).filter(
                db_work_order.source == source).order_by(desc(
                    db_work_order.id)).all()
        if action and work_number:
            if action == 'query':
                work_orders = db_work_order.query.with_entities(
                    db_work_order.work_number, db_work_order.dingid,
                    db_work_order.status).filter(
                        and_(db_work_order.source == source,
                             db_work_order.work_number == work_number)).all()
        if work_orders:
            work_orders = [list(info) for info in work_orders]
            for info in work_orders:
                info.extend(servers[info[0]][:-1])
                info.insert(4, users[servers[info[0]][-1]][0])
                info.insert(5, users[servers[info[0]][-1]][-1])
                if info[1]:
                    info.append(users[info[1]][0])
                else:
                    info.append('')
        new_work_number = Redis.smembers(Key)
    return render_template('ensure_server_auth.html',
                           tables=tables,
                           work_orders=work_orders,
                           msg=msg,
                           new_work_number=new_work_number,
                           total='服务器权限工单管理')
Exemple #7
0
def ensure_other_work():
    db_work_order = db_op.work_order
    db_other_work = db_op.other_work
    db_sso = db_op.user_sso
    msg = None
    source = 'ensure_other_work'
    Key = 'new_other_work_work_number_%s' % dt
    # 验证票据
    actions = {
        'complete': '已完成',
        'deny': '已拒绝',
        'refuse': '审批拒绝',
        'agree': '审批通过'
    }
    ticket = tools.http_args(request, 'ticket')
    action = tools.http_args(request, 'action')
    work_number = tools.http_args(request, 'work_number')
    try:
        if ticket or (action == 'activate' and work_number):
            if ticket:
                work_number = Redis.get('work_order_ticket_%s' % ticket)
            if work_number:
                val = db_work_order.query.filter(
                    db_work_order.work_number == int(work_number)).all()
                if val:
                    val = db_work_order.query.filter(
                        and_(db_work_order.work_number == int(work_number),
                             db_work_order.source == source,
                             db_work_order.status.in_(('审批通过', '未受理')))).all()
                    if val:
                        db_work_order.query.filter(
                            and_(db_work_order.work_number == int(work_number),
                                 db_work_order.source == source)).update({
                                     db_work_order.dingid:
                                     g.dingId,
                                     db_work_order.status:
                                     '受理中'
                                 })
                        db_op.DB.session.commit()
                        Redis.sadd(Key, work_number)
                        Redis.delete('work_order_ticket_%s' % ticket)
                        msg = "%s工单已受理!" % work_number
                    else:
                        msg = "工单暂时无法受理!"
                else:
                    msg = "无效的请求验证地址!"
            else:
                msg = "无效的请求验证地址!"
        #验证执行状态
        if action and work_number:
            moment = None
            if action in actions:
                if action in ('refuse', 'agree'):
                    if g.grade[0] != '0':
                        msg = '当前用户无审批权限!'
                        raise AssertionError
                if action == 'deny':
                    moment = tools.http_args(request, 'moment')
                val = db_work_order.query.filter(
                    and_(db_work_order.work_number == int(work_number),
                         db_work_order.source == source,
                         db_work_order.status.in_(('待审批', '受理中')))).all()
                if val:
                    db_work_order.query.filter(
                        and_(db_work_order.work_number == int(work_number),
                             db_work_order.source == source)).update(
                                 {db_work_order.status: actions[action]})
                    db_op.DB.session.commit()
                    Redis.srem(Key, work_number)
                    if Redis.exists('op_send_mail_html_%s' % work_number):
                        try:
                            dingid = db_other_work.query.with_entities(
                                db_other_work.dingid).filter(
                                    db_other_work.work_number ==
                                    work_number).all()
                            mailer = db_sso.query.with_entities(
                                db_sso.mail).filter(
                                    db_sso.dingunionid == dingid[0][0]).all()
                            if mailer:
                                receiver = Redis.get(
                                    'op_other_work_receiver_%s' % work_number)
                                Msg = Message("%s工单进度通知" % work_number,
                                              sender=sender,
                                              recipients=[mailer[0][0]],
                                              cc=[receiver],
                                              charset='utf-8')
                                mail_html = Redis.get('op_send_mail_html_%s' %
                                                      work_number)
                                alarm_html = '<p style="color:red">工单当前进度:%s</p>' % actions[
                                    action]
                                if action == 'deny':
                                    if moment:
                                        alarm_html = "%s<p>拒绝说明:%s</p>" % (
                                            alarm_html, moment)
                                Msg.html = '%s%s' % (mail_html, alarm_html)
                                if action == 'agree':
                                    Msg.html = '%s%s%s' % (
                                        mail_html, alarm_html,
                                        Redis.get('op_send_ensure_url_%s' %
                                                  work_number))
                                with app.app_context():
                                    mail.send(Msg)
                        except Exception as e:
                            logging.error(e)
                    msg = "%s工单当前状态:%s!" % (work_number, actions[action])
                else:
                    msg = "无效操作!"
    except Exception as e:
        logging.error(e)
        if not msg:
            msg = "未知异常错误!"
    finally:
        if action == 'deny':
            return jsonify({'status': 'ok'})
        #获取最新数据
        tables = ('工单号', '日期', '事项标题', '事项描述', '申请人', '详情', '问题备注', '操作')
        users = db_sso.query.with_entities(db_sso.dingunionid,
                                           db_sso.realName).all()
        users = {info[0]: info[1:] for info in users}
        other_works = db_other_work.query.with_entities(
            db_other_work.work_number, db_other_work.date, db_other_work.title,
            db_other_work.describe, db_other_work.dingid).all()
        other_works = {info[0]: info[1:] for info in other_works}
        work_orders = db_work_order.query.with_entities(
            db_work_order.work_number, db_work_order.dingid,
            db_work_order.status).filter(
                db_work_order.source == source).order_by(desc(
                    db_work_order.id)).limit(500).all()
        if action and work_number:
            if action == 'query':
                work_orders = db_work_order.query.with_entities(
                    db_work_order.work_number, db_work_order.dingid,
                    db_work_order.status).filter(
                        and_(db_work_order.source == source,
                             db_work_order.work_number == work_number)).all()
        if work_orders:
            work_orders = [list(info) for info in work_orders]
            for info in work_orders:
                info.extend(other_works[info[0]][:-1])
                info.extend(users[other_works[info[0]][-1]])
        new_work_number = Redis.smembers(Key)
    return render_template('ensure_other_work.html',
                           tables=tables,
                           work_orders=work_orders,
                           msg=msg,
                           new_work_number=new_work_number,
                           total='运维其它事项工单管理')
Exemple #8
0
def assets_get(action=None):
    #公共参数
    Args = {
        info: tools.http_args(request, info)
        for info in ('aid', 'ip', 'port', 'type', 'host_type', 'action',
                     'page', 'hostname')
    }
    search_key = 'search_results_%s' % g.token
    form = MyForm.MyFormServer()
    db = db_idc.idc_servers
    db_idc_id = db_idc.idc_id
    db_zabbix = db_idc.zabbix_info
    db_project = db_op.project_list
    db_busi = db_op.business
    db_project_third = db_op.project_third
    idc_vals = db_idc_id.query.with_entities(db_idc_id.id, db_idc_id.aid,
                                             db_idc_id.cid).all()
    idc_val = {val[0]: val[1] for val in idc_vals}
    cid_val = {val[0]: val[-1] for val in idc_vals}
    values = []
    tables = [
        '机房', '机柜', 'IP', 'ssh端口', '主机名', '服务器型号', '操作系统', 'CPU', '内存', '磁盘数',
        '磁盘总量', '远程管理IP', '购买日期', '状态'
    ]
    try:
        # 导出数据功能
        if action:
            if action == 'export':
                try:
                    file_name = "/tmp/export_assets.xlsx"
                    values = [list(val) for val in eval(Redis.get(search_key))]
                    if os.path.exists(file_name):
                        os.remove(file_name)
                    pyexcel.save_as(array=values,
                                    dest_file_name=file_name,
                                    sheet_name='export_assets')
                except Exception as e:
                    logging.error(e)
                else:
                    return send_file(file_name, as_attachment=True)
        # 判断是否查询
        if form.submit.data:
            ts = form.text.data.strip()
            Infos = {
                'ip': db.ip,
                'sn': db.sn,
                'hostname': db.hostname,
                'status': db.status
            }
            if ts:
                try:
                    if form.select.data == 'cid':
                        cid = ts.strip()
                        if not ts.endswith('机柜') and cid != 'KVM':
                            cid = '%s机柜' % ts.strip()
                        # 优先进行精确匹配
                        idc_id = db_idc_id.query.with_entities(
                            db_idc_id.id).filter(db_idc_id.cid == cid).all()
                        if not idc_id:
                            # 精确匹配不到结果后进行模糊匹配
                            idc_id = db_idc_id.query.with_entities(
                                db_idc_id.id).filter(
                                    db_idc_id.cid.like(
                                        '%{0}%'.format(cid))).all()
                        idc_id = tuple([id[0] for id in idc_id])
                        values = db.query.with_entities(
                            db.idc_id, db.ip, db.ssh_port, db.hostname,
                            db.productname, db.system, db.cpu_core, db.mem,
                            db.disk_count, db.disk_size, db.idrac,
                            db.purch_date,
                            db.status).filter(db.idc_id.in_(idc_id)).all()
                    if form.select.data == 'buy_date':
                        start_time = ts.split('to')[0].strip()
                        end_time = ts.split('to')[-1].strip()
                        values = db.query.with_entities(
                            db.idc_id, db.ip, db.ssh_port, db.hostname,
                            db.productname, db.system, db.cpu_core, db.mem,
                            db.disk_count, db.disk_size, db.idrac,
                            db.purch_date, db.status).filter(
                                and_(db.purch_date >= start_time,
                                     db.purch_date <= end_time,
                                     db.host_type == 'physical')).all()
                    if form.select.data in ('sn', 'hostname', 'ip', 'status'):
                        val = db.query.with_entities(
                            db.ip, db.ssh_port).filter(
                                Infos[form.select.data] == ts).all()
                        #优先进行精确匹配
                        if val and len(val) == 1:
                            ip, ssh_port = val[0]
                            return redirect(
                                "assets_get?type=server&ip=%s&port=%s" %
                                (ip, ssh_port))
                        else:
                            #精确匹配不到结果后进行模糊匹配
                            values = db.query.with_entities(
                                db.idc_id, db.ip, db.ssh_port, db.hostname,
                                db.productname, db.system, db.cpu_core, db.mem,
                                db.disk_count, db.disk_size, db.idrac,
                                db.purch_date,
                                db.status).filter(Infos[form.select.data].like(
                                    '%{0}%'.format(ts))).all()
                except Exception as e:
                    logging.error(e)
                else:
                    try:
                        #获取服务器信息
                        if values:
                            values = [list(val) for val in values]
                            for val in values:
                                id = val[0]
                                val[0] = idc_val[id]
                                val.insert(1, cid_val[id])
                            export_values = [val for val in values]
                            export_values.insert(0, tables)
                            Redis.set(search_key, export_values)
                    except Exception as e:
                        logging.error(e)
                    return render_template('server_list.html',
                                           values=values,
                                           tables=tables,
                                           form=form,
                                           export=True,
                                           assets_type='server')
        #获取API接口参数
        if Args:
            idc_ids = ()
            try:
                if Args['aid']:
                    aid = Args['aid']
                    idc_ids = db_idc_id.query.with_entities(
                        db_idc_id.id).filter(db_idc_id.aid == aid).all()
                    if idc_ids:
                        idc_ids = tuple([val[0] for val in idc_ids])
                else:
                    idc_ids = db_idc_id.query.with_entities(db_idc_id.id).all()
                    if idc_ids:
                        idc_ids = tuple([val[0] for val in idc_ids])
            except Exception as e:
                logging.error(e)
            #判断是否为服务器
            if Args['type'] == 'server':
                if Args['action']:
                    try:
                        action = Args['action']
                        if action == 'all_list':
                            if Args['host_type']:
                                host_type = Args['host_type']
                                if idc_ids:
                                    values = db.query.with_entities(
                                        db.idc_id, db.ip, db.ssh_port,
                                        db.hostname, db.productname, db.system,
                                        db.cpu_core, db.mem, db.disk_count,
                                        db.disk_size, db.idrac, db.purch_date,
                                        db.status).filter(
                                            and_(db.idc_id.in_(idc_ids),
                                                 db.host_type ==
                                                 host_type)).all()
                        if action == 'expire':
                            values = db.query.with_entities(
                                db.idc_id, db.ip, db.ssh_port, db.hostname,
                                db.productname, db.system, db.cpu_core, db.mem,
                                db.disk_count, db.disk_size, db.idrac,
                                db.purch_date, db.status).filter(
                                    and_(db.host_type == 'physical',
                                         db.expird_date < tt,
                                         db.idc_id != 0)).order_by(
                                             db.idc_id).all()
                        if action == 'about_to':
                            values = db.query.with_entities(
                                db.idc_id, db.ip, db.ssh_port, db.hostname,
                                db.productname, db.system, db.cpu_core, db.mem,
                                db.disk_count, db.disk_size, db.idrac,
                                db.purch_date, db.status).filter(
                                    and_(db.host_type == 'physical',
                                         db.expird_date >= tt,
                                         db.expird_date <= dt,
                                         db.idc_id != 0)).order_by(
                                             db.idc_id).all()
                        if action == 'server_off':
                            values = db_zabbix.query.with_entities(
                                db_zabbix.hostname).filter(
                                    db_zabbix.icmpping == 0).all()
                            if values:
                                values = [value[0] for value in values]
                                values = db.query.with_entities(
                                    db.idc_id, db.ip, db.ssh_port, db.hostname,
                                    db.productname, db.system, db.cpu_core,
                                    db.mem, db.disk_count, db.disk_size,
                                    db.idrac, db.purch_date, db.status).filter(
                                        db.hostname.in_(
                                            tuple(values))).order_by(
                                                db.idc_id).all()
                        if action == 'search':
                            if Redis.exists(search_key):
                                values = eval(Redis.get(search_key))
                        if values:
                            Redis.set(search_key, values)
                            values = [list(val) for val in values]
                            for val in values:
                                id = val[0]
                                val[0] = idc_val[id]
                                val.insert(1, cid_val[id])
                    except Exception as e:
                        logging.error(e)
                    return render_template('server_list.html',
                                           values=values,
                                           tables=tables,
                                           form=form,
                                           export=True,
                                           assets_type=Args['type'])
                if (Args['ip'] and Args['port']) or Args['hostname']:
                    total_infos = defaultdict()
                    try:
                        ip = Args['ip']
                        ssh_port = Args['port']
                        hostname = Args['hostname']
                        total_infos['ssh_port'] = ssh_port
                        server_info = None
                        #获取服务器硬件信息
                        db_server = db_idc.idc_servers
                        if ip and ssh_port:
                            total_infos['ip'] = ip
                            server_info = db_server.query.with_entities(
                                db_server.idc_id, db_server.ip,
                                db_server.ssh_port, db_server.s_ip,
                                db_server.host_type, db_server.hostname,
                                db_server.sn, db_server.manufacturer,
                                db_server.productname, db_server.system,
                                db_server.cpu_info, db_server.cpu_core,
                                db_server.mem, db_server.disk_count,
                                db_server.disk_size, db_server.idrac,
                                db_server.purch_date, db_server.expird_date,
                                db_server.status, db_server.comment).filter(
                                    and_(
                                        db_server.ip == ip,
                                        db_server.ssh_port == ssh_port)).all()
                        if hostname:
                            total_infos['ip'] = hostname
                            server_info = db_server.query.with_entities(
                                db_server.idc_id, db_server.ip,
                                db_server.ssh_port, db_server.s_ip,
                                db_server.host_type, db_server.hostname,
                                db_server.sn, db_server.manufacturer,
                                db_server.productname, db_server.system,
                                db_server.cpu_info, db_server.cpu_core,
                                db_server.mem, db_server.disk_count,
                                db_server.disk_size, db_server.idrac,
                                db_server.purch_date, db_server.expird_date,
                                db_server.status, db_server.comment).filter(
                                    db_server.hostname == hostname).all()
                            ip = server_info[0][1]
                            ssh_port = int(server_info[0][2])
                    except Exception as e:
                        logging.error(e)
                    else:
                        if server_info:
                            try:
                                server_info = list(server_info[0])
                                # 获取服务器机房机柜信息
                                idc_info = db_idc_id.query.with_entities(
                                    db_idc_id.aid,
                                    db_idc_id.cid).filter(db_idc_id.id == int(
                                        server_info[0])).all()
                                server_info.pop(0)
                                if idc_info:
                                    server_info.insert(0, idc_info[0][1])
                                    server_info.insert(0, idc_info[0][0])
                                else:
                                    server_info.insert(0, None)
                                    server_info.insert(0, None)
                                table_info = [
                                    '机房', '机柜', 'IP', 'SSH_PORT', '附属ip',
                                    '主机类型', 'hostname', 'sn', '生产厂家', '服务器型号',
                                    '操作系统', 'cpu信息', 'cpu核数', '内存', '磁盘数',
                                    '磁盘总量', 'idrac', '采购日期', '过保日期', '状态',
                                    '管理', '备注'
                                ]
                                total_infos['server_info'] = [
                                    table_info, server_info
                                ]
                            except Exception as e:
                                logging.error(e)
                            try:
                                stt = datetime.datetime.now(
                                ) - datetime.timedelta(minutes=15)
                                stt = stt.strftime('%Y-%m-%d %H:%M:%S')
                                zabbix_infos = [0, 0, 0, 0, 0]
                                vals = db_zabbix.query.with_entities(
                                    db_zabbix.icmpping, db_zabbix.cpu_load,
                                    db_zabbix.mem_use, db_zabbix.disk_io,
                                    db_zabbix.openfile
                                ).filter(
                                    and_(db_zabbix.ip == server_info[2],
                                         db_zabbix.ssh_port == server_info[3],
                                         db_zabbix.update_time > stt)).all()
                                if vals:
                                    zabbix_infos = [
                                        float(val) for val in list(vals[0])
                                    ]
                                total_infos['zabbix_infos'] = zabbix_infos
                            except Exception as e:
                                logging.error(e)
                            third_table = [
                                '应用服务', '应用端口', '所属项目', '集群类型', '业务使用', '所属部门',
                                '负责人', '联系方式'
                            ]
                            project_table = [
                                '应用服务', '应用端口', '所属项目', '域名', '开发语言', '环境',
                                '状态', '所属业务'
                            ]
                            total_infos['pool_project'] = True
                            try:
                                # 获取第三方资源信息
                                db_third = db_idc.third_resource
                                busis = db_busi.query.with_entities(
                                    db_busi.id, db_busi.business).all()
                                busis = {
                                    int(busi[0]): busi[1]
                                    for busi in busis
                                }
                                busis[0] = '未知业务'
                                project_third = db_project_third.query.with_entities(
                                    db_project_third.third_id,
                                    db_project_third.project).all()
                                if project_third:
                                    project_third = {
                                        info[0]: info[1]
                                        for info in project_third
                                    }
                                third_info = db_third.query.with_entities(
                                    db_third.id, db_third.resource_type,
                                    db_third.app_port, db_third.cluster_type,
                                    db_third.busi_id, db_third.department,
                                    db_third.person, db_third.contact).filter(
                                        and_(
                                            db_third.ip == ip,
                                            db_third.ssh_port == ssh_port,
                                        )).all()
                                if third_info:
                                    third_info = [
                                        list(info) for info in third_info
                                    ]
                                    third_id = [info[0] for info in third_info]
                                    for i, info in enumerate(third_info):
                                        info = list(info)
                                        info[4] = busis[int(info[4])]
                                        if project_third:
                                            if third_id[
                                                    i] in project_third.keys():
                                                info.insert(
                                                    3,
                                                    project_third[third_id[i]])
                                            else:
                                                info.insert(3, '')
                                        else:
                                            info.insert(3, '')
                                        third_info[i] = info
                                    third_info.insert(0, third_table)
                                    total_infos['third_info'] = third_info
                            except Exception as e:
                                logging.error(e)
                            try:
                                #获取自有资源信息
                                project_info = db_project.query.with_entities(
                                    db_project.id, db_project.resource,
                                    db_project.app_port, db_project.project,
                                    db_project.domain, db_project.sys_args,
                                    db_project.env, db_project.status,
                                    db_project.business_id).filter(
                                        and_(db_project.ip == ip,
                                             db_project.ssh_port ==
                                             ssh_port)).all()
                                project_info = [
                                    list(info) for info in project_info
                                ]
                                if project_info:
                                    for info in project_info:
                                        business = db_busi.query.with_entities(
                                            db_busi.business).filter(
                                                db_busi.id == int(
                                                    info[-1])).all()
                                        info[-1] = '%s:%s' % (info[-1],
                                                              business[0][0])
                                    project_info.insert(0, project_table)
                                    total_infos['project_info'] = project_info
                            except Exception as e:
                                logging.error(e)
                    return render_template('server_infos.html',
                                           total_infos=total_infos)
            #判断是否是存储设备
            try:
                if Args['type'] == 'store' and Args['action']:
                    db_store = db_idc.idc_store
                    tables = ('机房', '机柜', '设备型号', 'ip', '购买日期', '过保日期', '状态',
                              '备注')
                    if idc_ids:
                        val = db_store.query.with_entities(
                            db_store.idc_id, db_store.type, db_store.ip,
                            db_store.purch_date, db_store.expird_date,
                            db_store.status, db_store.comment).filter(
                                db_store.idc_id.in_(idc_ids)).all()
                        values = [list(va) for va in val]
                        for value in values:
                            idc_id = int(value[0])
                            cid = db_idc_id.query.with_entities(
                                db_idc_id.aid, db_idc_id.cid).filter(
                                    db_idc_id.id == idc_id).all()
                            value.pop(0)
                            value.insert(0, cid[0][1])
                            value.insert(0, cid[0][0])
            except Exception as e:
                logging.error(e)
            #判断是否是网络设备
            try:
                if Args['type'] == 'network' and Args['action']:
                    db_network = db_idc.idc_networks
                    tables = ('机房', '机柜', '设备型号', 'ip', '冗余', '购买日期', '过保日期',
                              '状态', '备注')
                    if idc_ids:
                        val = db_network.query.with_entities(
                            db_network.idc_id, db_network.type, db_network.ip,
                            db_network.redundance, db_network.purch_date,
                            db_network.expird_date, db_network.status,
                            db_network.comment).filter(
                                db_network.idc_id.in_(idc_ids)).all()
                        values = [list(va) for va in val]
                        for value in values:
                            idc_id = int(value[0])
                            cid = db_idc_id.query.with_entities(
                                db_idc_id.aid, db_idc_id.cid).filter(
                                    db_idc_id.id == idc_id).all()
                            value.pop(0)
                            value.insert(0, cid[0][1])
                            value.insert(0, cid[0][0])
            except Exception as e:
                logging.error(e)
    except Exception as e:
        logging.error(e)
        flash('获取数据错误!', "error")
        return render_template('Message.html')
    return render_template('server_list.html',
                           values=values,
                           tables=tables,
                           form=form,
                           export=False,
                           assets_type=Args['type'])
Exemple #9
0
def resource_report():
    try:
        form = MyForm.Form_resource_report()
        days = tools.http_args(request, 'days')
        date = datetime.datetime.now()
        db_server = db_idc.idc_servers
        if days:
            days = int(days)
            update_date = date - datetime.timedelta(days=days)
        else:
            days = 30
            update_date = date - datetime.timedelta(days=30)
        datazoom = True
        if days == 30:
            datazoom = False
        Bars = {}
        titles = {30: '近一个月', 90: '近三个月', 180: '近六个月', 360: '近一年内'}
        db_third = db_idc.third_resource
        db_project = db_op.project_list
        vals = db_project.query.with_entities(
            db_project.update_date, func.count(db_project.update_date)).filter(
                and_(db_project.resource == 'tomcat',
                     db_project.update_date != '',
                     db_project.update_date >= update_date)).group_by(
                         db_project.update_date).all()
        if vals:
            attrs = [val[0] for val in vals]
            datas = [val[-1] for val in vals]
            tomcat_bar = Bar("tomcat(%s)" % titles[days],
                             width='110%',
                             height='100%',
                             title_pos='center',
                             title_text_size=14)
            tomcat_bar.add("",
                           attrs,
                           datas,
                           is_label_show=True,
                           is_toolbox_show=False,
                           legend_orient='vertical',
                           legend_pos='right',
                           xaxis_interval=0,
                           is_random=True,
                           xaxis_rotate=15,
                           is_datazoom_show=datazoom,
                           datazoom_type='both')
            Bars['tomcat'] = tomcat_bar
        vals = db_third.query.with_entities(
            db_third.update_date, func.count(db_third.update_date)).filter(
                and_(db_third.resource_type == 'redis',
                     db_third.update_date != '',
                     db_third.update_date >= update_date)).group_by(
                         db_third.update_date).all()
        if vals:
            attrs = [val[0] for val in vals]
            datas = [val[-1] for val in vals]
            redis_bar = Bar("redis(%s)" % titles[days],
                            width='110%',
                            height='100%',
                            title_pos='center',
                            title_text_size=14)
            redis_bar.add("",
                          attrs,
                          datas,
                          is_label_show=True,
                          is_toolbox_show=False,
                          legend_orient='vertical',
                          legend_pos='right',
                          xaxis_interval=0,
                          is_random=True,
                          xaxis_rotate=15,
                          is_datazoom_show=datazoom,
                          datazoom_type='both')
            Bars['redis'] = redis_bar
        vals = db_third.query.with_entities(
            db_third.update_date, func.count(db_third.update_date)).filter(
                and_(db_third.resource_type == 'redis',
                     db_third.update_date != '')).group_by(
                         db_third.update_date).all()
        if vals:
            attrs = [val[0] for val in vals]
            datas = [val[-1] for val in vals]
            mysql_bar = Bar("mysql(%s)" % titles[days],
                            width='110%',
                            height='100%',
                            title_pos='center',
                            title_text_size=14)
            mysql_bar.add("",
                          attrs,
                          datas,
                          is_label_show=True,
                          is_toolbox_show=False,
                          legend_orient='vertical',
                          legend_pos='right',
                          xaxis_interval=0,
                          is_random=True,
                          xaxis_rotate=15,
                          is_datazoom_show=datazoom,
                          datazoom_type='both')
            Bars['mysql'] = mysql_bar
        vals = db_third.query.with_entities(
            db_third.update_date, func.count(db_third.update_date)).filter(
                and_(db_third.resource_type == 'redis',
                     db_third.update_date != '')).group_by(
                         db_third.update_date).all()
        if vals:
            attrs = [val[0] for val in vals]
            datas = [val[-1] for val in vals]
            nginx_bar = Bar("nginx(%s)" % titles[days],
                            width='110%',
                            height='100%',
                            title_pos='center',
                            title_text_size=14)
            nginx_bar.add("",
                          attrs,
                          datas,
                          is_label_show=True,
                          is_toolbox_show=False,
                          legend_orient='vertical',
                          legend_pos='right',
                          xaxis_interval=0,
                          is_random=True,
                          xaxis_rotate=15,
                          is_datazoom_show=datazoom,
                          datazoom_type='both')
            Bars['nginx'] = nginx_bar
        try:
            td = time.strftime("%Y-%m-%d", time.localtime())
            std = datetime.datetime.now() - datetime.timedelta(days=180)
            std = std.strftime("%Y-%m-%d")
            if RC.exists('op_web_vm_vales_%s' % td) and RC.exists(
                    'op_web_py_vales_%s' % td):
                vm_vals = eval(RC.get('op_web_vm_vales_%s' % td))
                py_vals = eval(RC.get('op_web_py_vales_%s' % td))
            else:
                vm_vals = db_server.query.with_entities(
                    db_server.purch_date,
                    func.count(db_server.purch_date)).filter(
                        and_(db_server.host_type == 'vm',
                             db_server.purch_date > std)).group_by(
                                 db_server.purch_date).all()
                RC.set('op_web_vm_vales_%s' % td, vm_vals)
                RC.expire('op_web_vm_vales_%s', 86400)
                py_vals = db_server.query.with_entities(
                    db_server.purch_date,
                    func.count(db_server.purch_date)).filter(
                        and_(db_server.host_type == 'physical',
                             db_server.purch_date > std)).group_by(
                                 db_server.purch_date).all()
                RC.set('op_web_py_vales_%s' % td, py_vals)
                RC.expire('op_web_py_vales_%s', 86400)
        except Exception as e:
            logging.error(e)
        try:
            server_bar = Bar("近6个月新增服务器数量",
                             title_pos='center',
                             title_text_size=12,
                             width='110%',
                             height='230px')
            attrs = sorted(
                set([val[0] for val in vm_vals if val] +
                    [val[0] for val in py_vals if val]))
            vm_vals = {val[0]: val[1] for val in vm_vals}
            py_vals = {val[0]: val[1] for val in py_vals}
            for attr in attrs:
                if attr not in vm_vals:
                    vm_vals[attr] = 0
                if attr not in py_vals:
                    py_vals[attr] = 0
            vm_vals = sorted(vm_vals.items(), key=lambda item: item[0])
            vm_vals = [val[1] for val in vm_vals]
            py_vals = sorted(py_vals.items(), key=lambda item: item[0])
            py_vals = [val[1] for val in py_vals]
            attrs = ['-'.join(val.split('-')[1:]) for val in attrs]
            vm_counts = reduce(lambda x, y: x + y, vm_vals)
            server_bar.add('虚拟机%s台' % vm_counts,
                           attrs,
                           vm_vals,
                           is_label_show=True,
                           is_toolbox_show=False,
                           xaxis_interval=0,
                           xaxis_rotate=25,
                           legend_pos='70%')
            py_counts = reduce(lambda x, y: x + y, py_vals)
            server_bar.add('物理机%s台' % py_counts,
                           attrs,
                           py_vals,
                           is_label_show=True,
                           is_toolbox_show=False,
                           xaxis_interval=0,
                           xaxis_rotate=25,
                           legend_pos='70%')
        except Exception as e:
            logging.error(e)
    except Exception as e:
        logging.error(e)
        return redirect(url_for('error'))
    return render_template('resource_report.html',
                           Bars=Bars,
                           form=form,
                           days=days,
                           server_bar=server_bar)