{
    "name"  : "全局配置不生效检查",
    "precondition" : ["isFix"],
    "problems" : {
        "upload_yes" : {
            "desc"      : "",
            "advice"    : "【建议】问题已经修复，可连接到设备上确认是否修复成功"
        },
        "upload_no" : {
            "desc"      : "【问题】存在全局配置不生效问题",
            "advice"    : "【建议】修复全局配置不生效问题"
        },
        "upload_skip" : {
            "desc"      : "",
            "advice"    : "【提示】该版本不存在问题"
        },
        "upload_fail" : {
            "desc"      : "【问题】主从管理板切换失败",
            "advice"    : "【建议】检查手动切换主从管理板能否成功，再重新修复"
        },
        "tftp_fail" : {
            "desc"      : "【问题】全局配置不生效检测-tftp从工具传输文件到设备失败",
            "advice"    : "【建议】请检查tftp端口(69)是否防火墙被禁用或者被其他tftp软件占用！或其他原因可手动tftp本地文件到设备查看具体失败原因"
        },
        "upgrade_fail" : {
            "desc"      : "【问题】全局配置不生效检测-热补丁升级失败",
            "advice"    : "【建议】请检查为何在5分钟内没升级完成！"
        },
        "runPatch_fail" : {
            "desc"      : "【问题】全局配置不生效检测-热补丁运行失败",
            "advice"    : "【建议】请检查为何在3分钟内热补丁运行不起来（patch running slot all）！"
        },
        "getpid_fail" : {
            "desc"      : "【问题】全局配置不生效检测-pgrep wba获取pid失败",
            "advice"    : "【建议】请检查pgrep wba的第二行是否包含非数字字符！"
        },
        "getversion_fail" : {
            "desc"      : "【问题】全局配置不生效检测-获取版本号失败",
            "advice"    : "【建议】手动show version detail查看版本号是否存在"
        }
    }
}
---------------------------code-------------------------------
pid_arr = []
patch_date = 'Jan 16'
precondition_result = 'pass'
precondition_message = ''
precondition_message_advice = ''

#执行log
log_version = cli('show version detail')
log_versionList = log_version.split('\r\n')
log_slot = cli('show version slot')
log_slotList = log_slot.split('\r\n')
log_debug = cli('debug scc dump err')
log_debugList = log_debug.split('<')

#获取要升级的热补丁文件名
key_version = get_bin_file(log_version)
bin_file = getHotfixFileName(key_version)

#用户不同意修复
isFixed = precondition('isFix')
if !isFixed || isFixed == undefined :
	precondition_result = 'fail'
	precondition_message = '用户只检查是否是全局配置不生效版本！'
	precondition_message_advice = '用户只检查是否是全局配置不生效版本！'
	set_share_value('precondition_result',precondition_result)
	set_share_value('precondition_message',precondition_message)
	set_share_value('precondition_message_advice',precondition_message_advice)
    #检测到是全局配置不生效版本
    if isConfigIssueVersion():
        add_problem('upload_no')
        change_title('检测出是全局配置不生效版本')
        return 'warn'
    else:
        add_problem('upload_skip')
        change_title('检测出非故障版本')
        return 'warn'
    end
end

#判断是否全局配置不生效版本
if !isConfigIssueVersion():
    precondition_result = 'fail'
	precondition_message = '检测出不是全局配置不生效版本'
	precondition_message_advice = '检测出不是全局配置不生效版本'
	set_share_value('precondition_result',precondition_result)
	set_share_value('precondition_message',precondition_message)
	set_share_value('precondition_message_advice',precondition_message_advice)
	add_problem('upload_skip')
    change_title('检测出非故障版本')
    return 'warn'
end

#开始升级
if isTftpCopySucc():
    record_pre_pid()
    cmd_upgrade = format('upgrade flash:%s',bin_file)
    cli(cmd_upgrade)
    #5秒检测一次CM是否upgrade成功，最多等待5分钟
    times = 60
    up_success = 'fail'
    while times>0:
        sleep(5000)
        #cpuHandle()
        if isUpgradeSuccess('cm'):
            up_success = 'success'
            break
        end
        times = times - 1
    end
    
    if up_success == 'success' :
        cli('patch active slot all')
        cli('patch running slot all')

        #检查补丁是否运行成功3分钟
        times = 60
        start_success = 'fail'
        while times>0:
            sleep(3000)
            if runPatchSucc():
                start_success = 'success'
                break
            end
            times = times - 1
        end
        if start_success == 'success':
            cli('run-system-shell')
            cli('pkill -9 sccd')
            tmp_file = bin_file.replace("(","\\(")
            tmp_file = tmp_file.replace(")","\\)")
            cmd_rm = format('rm -rf /data/%s',tmp_file)
            cli(cmd_rm)
            cli('sync')
            cli('exit')
            add_problem('upload_yes')
            change_title('问题已修复')
            return 'warn'
        else:
            add_problem('runPatch_fail')
            change_title('问题未修复')
            return 'warn'
        end
    else:
        cm_result = 'fail'
        cm_message = 'upgrade失败，若状态显示为upgrading，请等待upgrade状态为timeout后再做操作！'
        set_share_value('cm_result',cm_result)
        set_share_value('cm_message',cm_message)
        add_problem('upgrade_fail')
        change_title('问题未修复')
        return 'warn'
    end
else:
    precondition_result = 'fail'
    precondition_message = '文件传输失败'
    precondition_message_advice = '请检查tftp端口(69)是否防火墙被禁用或者已经被其他tftp软件占用！'
    set_share_value('precondition_result',precondition_result)
    set_share_value('precondition_message',precondition_message)
    set_share_value('precondition_message_advice',precondition_message_advice)
    add_problem('tftp_fail')
    change_title('问题未修复')
    return 'warn'
end

#匹配补丁版本-管理板
def get_bin_file(str):
    file_name = ''
    version = ''
    date = ''
    reg_ver = /\w+_RGOS\s+([^,^\r\n]+)/
    r = str.match(reg_ver)
    if r != null:
        version = r[1]
    end
    type = 'octeon'
    card = get_cm_version(str)
    date = get_date_version(str)
    if version != '' && type != '':
        file_name = version + '-' + type + '-' + card
    end
    #PR7阶段1(04272016)
    if date == '04272016' && version == '11.0(4)B1P2':
        file_name = file_name + '-SP2'
    end
    #PJ8-05102016
    if date == '05052016' && version == '11.0(4)B3':
        file_name = file_name + '-SP2'
    end
    return file_name
end
def get_date_version(str):
    date = ''
    strList = str.split('\r\n')
    reg = /System software number\s+:\s+\w{1}\d{6}(\d{8})/
    foreach data in strList:
        r = data.match(reg)
        if r != null:
            date = r[1]
            break
        end
    end
    return date
end
#判断是cm还是cmii
def get_cm_version(str):
    cm_version = ''
    strList = str.split('\r\n')
    foreach data in strList:
        if data.indexOf('Slot') != -1 && data.indexOf('CM') != -1:
            if data.indexOf('III') != -1:
                cm_version = 'cmiii'
                break
            else if data.indexOf('II') != -1:
                cm_version = 'cmii'
                break
            else:
                cm_version = 'cm'
                break
            end
        end
    end
    return cm_version
end

#判断是否配置不生效版本
def isConfigIssueVersion():
    result = 0
    #库函数
    version = get_device_softvesion()
    if version == null || version == '':
        add_problem('getversion_fail')
        change_title('获取版本号失败')
        return 'warn'
    end
    version = version+'*'
    com_version1 = '_RGOS 11.0(1)B3*'
    com_version2 = '_RGOS 11.0(1)B3 *'
    com_version3 = '_RGOS 11.0(1)B3  *'
    if version.indexOf(com_version1) != -1 || version.indexOf(com_version2) != -1 || version.indexOf(com_version3) != -1:
        cmd_reg1 = /flag\s*:\s*1/
        cmd_reg2 = /ret\s*:\s*-1/
        foreach data in log_debugList:
            r1 = data.match(cmd_reg1)
            r2 = data.match(cmd_reg2)
            if r1 != null && r2 != null:
                result = 1
                break
            end
        end
    end
    return result
end

#tftp传输文件从工具到设备
def isTftpCopySucc():
    result = 0
    local_ip = getLocalIp()
    patch_path = getHotfixStoreFolder()
    if isMgmt():
        cmd_tftp_cm = 'copy oob_tftp://' + local_ip + patch_path + bin_file + ' flash:' + bin_file
    else:
        cmd_tftp_cm = 'copy tftp://' + local_ip + patch_path + bin_file + ' flash:' + bin_file
    end
    tftp_log = cli(cmd_tftp_cm)
    print '======tftp_log:'+tftp_log
    #给3次机会传输
    times = 3
    while times>0:
        if !isTransfer(bin_file):
            tftp_log = cli(cmd_tftp_cm)
            print '======tftp_log:'+tftp_log
        else:
            result = 1
            break
        end
        times = times - 1
    end
    return result
end
#判断是否mgmt口
def isMgmt():
    result = 0
    ip = getDeviceIp()
    cmd = 'show ip interface brief | include ' + ip
    log_ip_intf = cli(cmd)
    log_ip_intfList = log_ip_intf.split('\r\n')
    foreach data in log_ip_intfList:
        if data.indexOf('Mgmt') != -1 && data.indexOf('up') != -1:
            result = 1
        end
    end
    return result
end
#判断tftp上传版本是否成功
def isTransfer(str):
    result = 0
    log_dir = cli('dir')
    if log_dir.indexOf(str) != -1:
        result = 1
    end
    return result
end

def cpuHandle():
    log_cpu = cli('show cpu | inc rg-upgr')
    log_cpuList = log_cpu.split('\r\n')
    reg =  /\d+\s+([^ ]+)%\s+[^ ]+%\s+[^ ]+%\s+rg-upgrade/
    foreach data in log_cpuList:
        r = data.match(reg)
        if r != null:
            cpu = r[1]
            if cpu > 90:
                cli('run-system-shell')
                cli('pkill -9 rg-upgrade')
                cli('exit')
            end
        end
    end
end

#判断是否upgrade成功
def isUpgradeSuccess(cardType):
    isSuccess = 1
    log_upgrade_sta = cli('show upgrade status')
    log_upgrade_staList = log_upgrade_sta.split('\r\n')
    reg = /(\[Slot\s+[^ ]+\])/
    foreach data in log_upgrade_staList:
        r = data.match(reg)
        if r != null:
            slot = r[1]
            if cardType == 'cm':
                if slot.indexOf('M1')!= -1 || slot.indexOf('M2') != -1:
                    logstr = getStaLog(data,log_upgrade_staList)
                    if logstr.indexOf('success') == -1 && logstr.indexOf('skipped') == -1:
                        isSuccess = 0
                    end
                end
            else if cardType == 'lc':
                if slot.indexOf('M1')== -1 && slot.indexOf('M2') == -1:
                    logstr = getStaLog(data,log_upgrade_staList)
                    if logstr.indexOf('success') == -1 && logstr.indexOf('skipped') == -1:
                        isSuccess = 0
                    end
                end
            end
        end
    end
    return isSuccess
end

#截取管理板的升级状态信息
def getStaLog(slot,strList):
    str = ""
    isAdd = 0
    foreach data in strList:
        if data == slot:
            isAdd = 1
        end
        if data.indexOf('Slot') != -1 && data != slot:
            isAdd = 0
        end
        if isAdd:
            str = str + data + '\r\n'
        end
    end
    return str
end

#记录run前的pid
def record_pre_pid():
    reg_cm = /(\d+)\s+(M\d+)\s+\d+/
    foreach card in log_slotList:
        if card.indexOf('master') != -1 || card.indexOf('candidate') != -1 || card.indexOf('backup') != -1:
            r = card.match(reg_cm)
            if r != null:
                dev = r[1]
                slot = r[2]
                #进入从管理板
                if card.indexOf('master') == -1:
                    cmd_cm = ''
                    if isVsu(log_slotList):
                        cmd_cm = 'session device ' + dev + ' slot ' + slot
                    else:
                        cmd_cm = 'session slot ' + slot
                    end
                    session_res = cli(cmd_cm,'>')
                    if !isSessionSucc(session_res):
                        add_problem('upload_fail')
                        change_title('问题未修复')
                        return 'warn'
                    end
                    cli('enable')
                end
                #获取pid
                cli('run-system-shell')
                ls_log = cli('pgrep wba')
                ls_log = ls_log.split(/\r?\n/)
                ls_log = ls_log[1]
                #判断到第二行包含非数字
                if ls_log.indexOf('#') != -1:
                    add_problem('getpid_fail')
                    change_title('问题未修复')
                    return 'warn'
                end
                ls_log = ls_log + '_' + dev + '-' + slot
                pid_arr.push(ls_log)
                cli('exit')
                if card.indexOf('master') == -1:
                    cli('exit')
                end
            end
        end
    end
end
#判断运行热补丁是否成功
def runPatchSucc():
    result = 1
    reg_cm = /(\d+)\s+(M\d+)\s+\d+/
    foreach card in log_slotList:
        if card.indexOf('master') != -1 || card.indexOf('candidate') != -1 || card.indexOf('backup') != -1:
            r = card.match(reg_cm)
            if r != null:
                dev = r[1]
                slot = r[2]
                #进入从管理板
                if card.indexOf('master') == -1:
                    cmd_cm = ''
                    if isVsu(log_slotList):
                        cmd_cm = 'session device ' + dev + ' slot ' + slot
                    else:
                        cmd_cm = 'session slot ' + slot
                    end
                    session_res = cli(cmd_cm,'>')
                    if !isSessionSucc(session_res):
                        add_problem('upload_fail')
                        change_title('问题未修复')
                        return 'warn'
                    end
                    cli('enable')
                end
                #判断进程pid是否改变
                if !isProcessOK(dev, slot):
                    result = 0
                    if card.indexOf('master') == -1:
                        cli('exit')
                    end
                    break
                end
                if card.indexOf('master') == -1:
                    cli('exit')
                end
            end
        end
    end
    return result
end

#判断进去从管理板是否成功
def isSessionSucc(session_res):
    result = 1
    if session_res.indexOf('Destination unreachable') != -1 || session_res.indexOf('Unrecognized host or address') != -1:
        result = 0
    end
    return result
end

#判断进程pid是否改变
def isProcessOK(dev, slot):
    result = 0
    #获取pid
    cli('run-system-shell')
    dev_slot = dev + '-' + slot
    ls_log = cli('pgrep wba')
    ls_log = ls_log.split(/\r?\n/)
    ls_log = ls_log[1]
    #说明有捕捉到pid
    if ls_log.indexOf('#') == -1:
        foreach x in pid_arr:
            if x.indexOf(dev_slot) != -1 && x.indexOf(ls_log) == -1:
                result = 1
                break
            end
        end
    end
    cli('exit')
    return result
end

#判断是否VSU
def isVsu(strList):
	result = 0
	reg = /(\d+)\s+\w+\s+\d+/
	foreach data in strList:
		r = data.match(reg)
		if r != null:
			dev = r[1]
			if dev == 2:
				result = 1
				break
			end
		end
	end
	return result
end