服务器上架检测(代码)

概述

该项目支持所有服务器上架管理,规模从一台到数量无限制。确保服务器满足自身要求后顺利交付给用户。

详细

概述

服务器上架过程中会有各种各样的问题,如果前期能提前识别出来,那么后面我们在交付的过程中就能很高效的完成,因此结合某大厂的场景,实现了几十万台服务器的上架检查,如果是其他硬件设备也可以参照此项目进行扩展,具有很强的可扩展性。

详细

一、运行效果

二、实现过程

①、创建开始安装检查
@bmsCheckApi.route("/bms/v1/install/bms_check", methods=["POST"])
def bms_reserve_install_check():
    """
    上架质量检查
    :return:
     """
    message = Message()
    detail = request.get_json().get("detail")
    status, info = BmsCheck.hardware_progress_check(detail)
    print(status, info)
    if status:
        message.sucess()
        message.set_data(info)
    else:
        message.fail()
        message.set_message(info)
    return jsonify(message.return_base_data())
②、相关检查信息记录
class BmsCheck(object):
    @classmethod
    def hardware_progress_check(cls, detail):
        mylock = redis_distribute_lock()
        user_id = detail.get("userId")
        status = True
        user_time = str(user_id) + str(datetime.datetime.now())
        with mylock:
            transation = Transation()
            try:
                #######生产一条检查记录
                info_={
                 "user_id":user_id,
                    "status":"开始检查",
                    "reg":"0",
                    "user_time":user_time
                }
                print(info_)
                transation.bmsHardwareCheckProgress.insert(**info_)
                transation.commit()
                start_track_task(1, cls.hardware_progressing, args=(detail,user_time))
                msg = SearchTable.bmsHardwareCheckProgress.search(user_time=user_time)
            except Exception as e:
                traceback.print_exc()
                status, msg = False, str(e)
        return status, msg
③、异步执行各个模块的检测。

主要代码如下:

@classmethod
def hardware_progressing(cls,detail,user_time):
    try:
        serial_num1, serial_num2, serial_num3 = [], [], []
        mess, info, serial_ = {}, [], []
        cloud_type = detail.get("cloud_type").split(",")
        user_id = detail.get("userId")
        sn = detail.get("sn").split(",")
        for i in range(0, len(sn)):
            serial_number = {"serial_number": sn[i]}
            if cloud_type[i] == "idc":
                table1 = "bms_tradition_server"
                table2 = "bms_tradition_host"
            else:
                table1 = "bare_metal_server"
                table2 = "bare_metal_host"
                status, result1 = HardwareManager.bms_hardware_get(table1, serial_number)
            status, result2 = HardwareManager.bms_hardware_get(table2, serial_number)
            if not result1:
                serial_num1.append(serial_number["serial_number"])
                mess[1] = json.loads(HardwareCheckInfo.status.get("1") % (",".join(serial_num1),
                                                                          CheckResultBelong.status.get("0")))
                continue
            if result1 and result1.get("bms_server_status") != '0':
                serial_num2.append(serial_number["serial_number"])
                mess[2] = json.loads(HardwareCheckInfo.status.get("2") % (",".join(serial_num2),
                                                                          CheckResultBelong.status.get("0")))
                continue
            if result2:
                serial_num3.append(serial_number["serial_number"])
                mess[3] = json.loads(HardwareCheckInfo.status.get("3") % (",".join(serial_num3),
                                                                          CheckResultBelong.status.get("0")))
                continue
            serial_num = [item for item in sn if item not in (serial_num2 + serial_num3)]
        if serial_num:
            try:
                HardwareManager.bms_hardware_check_update({"status": "线标相关信息检查中","reg":"1"},"hardware_check",
                                                          {"user_time": user_time})
                status, mess[4] = hardware_cmdb_get(serial_num, detail)  # 0.8s
                HardwareManager.bms_hardware_check_update({"status": "交换机相关信息检查中","reg":"2"}, "hardware_check",
                                                          {"user_time": user_time})
                status, mess[5] = hardware_switch_get(serial_num, detail)  # 6.75s
                HardwareManager.bms_hardware_check_update({"status": "ipmitool信息检查中","reg":"3"}, "hardware_check",
                                                          {"user_time": user_time})
                status, mess[6] = hardware_kvmip_check(serial_num, detail, serial_num1)  # 35s
            except Exception as e:
                return False, str(e)
        for key, values in mess.items():
            if isinstance(values, dict):
                if key == 1:
                    info.append(values)
                    continue
                status = False
                serial_.append(values.get("sn"))
                info.append(values)
            if isinstance(values, list):
                for i in range(0, len(values)):
                    status = False
                serial_.append(values[i].get("sn"))
                info.append(values[i])
        #####计算结果0.0003s
        serial_o = [item for item in sn if item not in list(set(",".join(serial_).split(",")))]
        if serial_o:
            try:
                info.append(json.loads(
                    HardwareCheckInfo.status.get("30") % (",".join(serial_o), CheckResultBelong.status.get("0"))))
                for serial in serial_o:
                    table_ = "bms_tradition_server" if cloud_type[
                                                           sn.index(serial)] == "idc" else "bare_metal_server"
                    # 正常的时候需要将结果bms_server_status修改为8,测试过程中为0
                    HardwareManager.bms_hardware_check_update({"bms_server_status": "8", "send_to_cmdb": "3"},
                                                              table_, {"serial_number": serial})
                    # 收集硬件信息并更新到数据库
                    HardwareHandles.data_info_get(serial, table_, serial_num1)
                    _, RESULT = HardwareManager.bms_hardware_getordey("hardware_checklog",
                                                                      {"serial_number": serial},
                                                                      order=['id'], desc=['id'], rows=1)
                    RESULT_REG = int(RESULT.get("reg")) + 1 if RESULT and RESULT.get("reg") is not None else 0
                    HardwareHandles.data_mess_preserve({0: [{"sn": serial}]}, user_id, reg=RESULT_REG,
                                                       stime=str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())),
                                                       activity="1")
                    print(RESULT_REG, [serial], cloud_type[sn.index(serial)])
                    #status, mess_pool = InstallManager.pooling_auto_approve([serial], cloud_type[sn.index(serial)])
                    if not status:
                        info.append(
                            json.loads(HardwareCheckInfo.status.get("33") % (
                                serial, CheckResultBelong.status.get("0"))))
            except Exception as e:
                info.append(
                    json.loads(HardwareCheckInfo.status.get("31") % (serial, CheckResultBelong.status.get("2"))))
        # 数据库结果写入9.7s
        HardwareHandles.data_mess_preserve(info, user_id, "reg")
        HardwareManager.bms_hardware_check_update({"status": "上架质量检查完成","result":str(info),"reg":"0"}, "hardware_check",
                                                  {"user_time": user_time})
    except Exception as e:
        traceback.print_exc()
        status, mess = False, str(e)
    return status, mess
④、结果保存和下载
@classmethod
def data_mess_preserve(cls, info, useid, reg="0", stime="", activity=""):
    step_msg = "hardware server check: check log info save mysql ... "
    ilog = InstallLogger()
    mess, what = {}, {}
    userid = useid
    what["reg"] = reg
    case_id = "bms_check" + dt_
    try:
        if info and isinstance(info, list):
            for i in range(0, len(info)):
                for sn_ in info[i].get("sn").split(","):
                    what["serial_number"] = sn_
                    if reg == "reg":
                        _, RESULT = HardwareManager.bms_hardware_getordey("hardware_checklog",
                                                                          {"serial_number": sn_},
                                                                          order=['id'], desc=['id'], rows=1)
                        what["reg"] = RESULT.get("reg") if RESULT else 0
                        what["case_id"] = case_id
                    what["case_info"] = info[i].get("info")
                    what["create_time"] = dt
                    what["user_id"] = userid
                    what["stime"] = stime
                    what["activity"] = activity
                    what["owner"] = info[i].get("belong")
                    status, msg = HardwareManager.bms_hardware_add(what, "hardware_checklog")
                    if not status:
                        ilog.step_info(step_msg=step_msg, status=status, info=msg)
        elif info and isinstance(info, dict):
            what["serial_number"] = info[0][0].get('sn')
            what["case_info"] = "上架质量检查完成,更新记录时间"
            what["create_time"] = dt
            what["user_id"] = userid
            what["stime"] = stime
            what["activity"] = activity
            what["case_id"] = case_id
            what["reg"] = reg
            status, msg = HardwareManager.bms_hardware_add(what, "hardware_checklog")
            if not status:
                ilog.step_info(step_msg=step_msg, status=status, info=msg)
    except Exception as e:
        print("data insert hardware_checklog failed...", e)
    ilog.step_info(step_msg=step_msg, status="True", info=info)

三、项目结构图

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西安未央

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值