
{
    "name"  : "cpu误报专项",
    "problems" : {
        "patch_box" : {
            "desc"      : "【问题】当前版本未检测到使用补丁进行过修复，工具尝试修复，但修复失败，请再次尝试。",
            "advice"    : "【建议】对当前版本进行热补丁升级，升级补丁过程不断网。"
        }
    }
}

---------------------------code-------------------------------

result_precondition = get_share_value('precondition_result')
if result_precondition == 'fail':
	return 'skip'
end

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')

key_version = get_bin_file(log_version)
bin_file_box = getHotfixFileName(key_version)
box_result = 'pass'
box_message = ''
if bin_file_box == null:
	box_result = 'fail'
	box_message = '版本没匹配上，没做升级动作，有疑问请联系后台处理！'
	set_share_value('box_result',box_result)
	set_share_value('box_message',box_message)
	return 'skip'
end

if isNeedPatch():
	#if isPatchedEver():
	#	box_result = 'fail'
	#	box_message = '设备上有其他补丁信息，请联系后台处理！'
	#	set_share_value('box_result',box_result)
	#	set_share_value('box_message',box_message)
	#	return 'skip'
	#end
	local_ip = getLocalIp()
	patch_path = getHotfixStoreFolder()

	if isMgmt():
		cmd_tftp_box = 'copy oob_tftp://' + local_ip + patch_path + bin_file_box + ' flash:' + bin_file_box
	else:
		cmd_tftp_box = 'copy tftp://' + local_ip + patch_path + bin_file_box + ' flash:' + bin_file_box
	end
	transferResult = 'pass'
	if !isTransfer(bin_file_box):
		cli(cmd_tftp_box)
		times = 3
		trans_success = 'fail'
		while times>0:
			transferResult = 'pass'
			if !isTransfer(bin_file_box):
				transferResult = 'fail'
				cli(cmd_tftp_box)
			else:
				trans_success = 'success'
				break
			end
			times = times - 1
		end
		if trans_success == 'fail':
			box_result = 'fail'
			box_message = '文件传输失败，请检查tftp端口(69)是否防火墙被禁用或者已经被其他tftp软件占用！'
			set_share_value('box_result',box_result)
			set_share_value('box_message',box_message)
			return 'skip'
		end
	end
	if !isForceVersion(log_version):
	    cmd_upgrade_box = ""
		if isForce(log_version):
			cmd_upgrade_box = format('upgrade flash:%s force',bin_file_box)
		else:
			cmd_upgrade_box = format('upgrade flash:%s',bin_file_box)
		end
		cli(cmd_upgrade_box)
		#sleep(30000)
		#5秒检测一次是否upgrade成功，最多等待2分钟
		times = 23
		up_success = 'fail'
		while times>0:
			sleep(5000)
			cpuHandle()
			if isUpgreadSuccess():
				up_success = 'success'
				break
			end
			times = times - 1
		end
		times = 3
		while times > 0:
			cpuHandle()
			sleep(5000)
			times = times - 1
		end
		if up_success == 'success' :
			cli('patch active')
			cli('patch running')
			cli('patch active slot all')
			cli('patch running slot all')
		else:
			box_result = 'fail'
			box_message = 'upgrade失败，若状态显示为upgrading，请等待upgrade状态为timeout后再做操作！'
			set_share_value('box_result',box_result)
			set_share_value('box_message',box_message)
			return 'skip'
		end
	else:
		#判断是否需要强行打补丁(帝联版本)
		cli('run-system-shell')
		cmd_chmod = 'chmod 777 data/"'+ bin_file_box + '"'
		#cli('chmod 777 data/rg-sysmon')
		cli(cmd_chmod)
		cli('cd sbin')
		cli('cp rg-sysmon rg-sysmon.bak')
		cmd_cp = 'cp /data/"' + bin_file_box + '" rg-sysmon'
		#cli('cp /data/rg-sysmon rg-sysmon')
		cli(cmd_cp)
		cli('sync')
		cli('pkill -9 rg-sysmon')
		cli('mkdir sysmon.tag')
		cli('sync')
		cli('exit')
		
		if isVsu(log_slotList):
			cli('run-system-shell')
			log_tipc = cli('tipc-config -n')
			log_tipcList = log_tipc.split('\r\n')
			reg_tipc = /<\d+.\d+.(\d+)>: up/
			foreach data in log_tipcList:
				r = data.match(reg_tipc)
				if r != null:
					tipc_id = r[1]
					cmd_tipc = 'tftp_tipc_client_elf ' + tipc_id + ' /data/rg-sysmon  /data/"' + bin_file_box + '" 2'
					cli(cmd_tipc)
				end
			end
			cli('exit')
			reg_slot = /(\d+)\s+\d+\s+\d+/
			foreach data in log_slotList:
				r = data.match(reg_slot)
				if r != null && data.indexOf('backup') != -1:
					dev_id = r[1]
					cmd_slot = 'session device ' + dev_id
					cli(cmd_slot,'>')
					cli('enable')
					cli('run-system-shell')
					cli('chmod 777 data/rg-sysmon')
					cli('cd sbin')
					cli('cp rg-sysmon rg-sysmon.bak')
					cli('cp /data/rg-sysmon rg-sysmon')
					cli('sync')
					cli('pkill -9 rg-sysmon')
					cli('mkdir sysmon.tag')
					cli('sync')
					cli('exit')
					cli('exit')
				end
			end
		end
	end
else:
	set_share_value('box_result',box_result)
	return 'skip'
end
sleep(5000)
result='pass'
times = 3
while times>0:
	sleep(20000)
	result = 'pass'
	if !isPatched():
		result = 'fail'
	else:
		break
	end
	times = times - 1
end
if result == 'fail':
	box_result = 'fail'
	box_message = '检测到热补丁问题，尝试过修复，但修复不成功'
else:
	box_result = 'fail'
	box_message = '检测到热补丁问题，已经修复成功'
end
set_share_value('box_result',box_result)
set_share_value('box_message',box_message)

return 'skip'

def isForceVersion(str):
	result = 0
	version = get_version(str)
	date = get_date_version(str)
	release = get_release(str)
	#帝联
	if version=='S5700H_RGOS 11.4(1)B2' && release == '02200613':
		result = 1
	end
	if version=='S6000E_RGOS 11.4(1)B2' && release == '02200614':
		result = 1
	end
	#陌陌
	if version=='S5700H_RGOS 11.4(1)B2' && release == '02211117':
		result = 1
	end
	if version=='S6000E_RGOS 11.4(1)B2' && release == '02211117':
		result = 1
	end
	if version=='S5750_RGOS 11.4(1)B1P1' && release == '':
		result = 1
	end
	#xxx
	if version=='S2910_RGOS 11.4(1)B1P1' && release == '':
		result = 1
	end
	#xxx
	if version=='S2910_RGOS 11.4(1)B1' && release == '02160311':
		result = 1
	end
	#xxx
	if version=='NBS5600_RGOS 11.4(1)B1P1' && release == '':
		result = 1
	end
	#xxx
	if version=='S6000E_RGOS 11.4(1)B2' && release == '02191905':
		result = 1
	end
	#xxx
	if version=='S5700H_RGOS 11.4(1)B2' && release == '02191904':
		result = 1
	end
	#20160804添加
	if version=='NBS5600_RGOS 11.4(1)B1' && release == '02170513':
		result = 1
	end
	if version=='NBS5600_RGOS 11.4(1)B1' && release == '02161618':
		result = 1
	end
	if version=='NBS5600_RGOS 11.4(1)B1P1' && release == '03170619':
		result = 1
	end
	if version=='S2910_RGOS 11.4(1)B1P1' && release == '03170621':
		result = 1
	end
	if version=='S5750_RGOS 11.4(1)B1P1' && release == '03170621':
		result = 1
	end
	if version=='S2910_RGOS 11.4(1)B4' && release == '03180118':
		result = 1
	end
	return result
end

#判断是否需要打补丁
def isNeedPatch():
	result = 0
	if !isForceVersion(log_version):
		log_version = cli('show version detail')
		log_versionList = log_version.split('\r\n')
		foreach data in log_versionList:
			if data.indexOf('System patch number') != -1 && data.indexOf('SP') == -1:
				result = 1
				break
			end
		end
		if !result:
			log_proc = cli('show cpu | inc rg-sys')
			if log_proc.indexOf('rg-sysmon') == -1:
				result = 1
			end
		end
	else:
		log_proc = cli('show cpu | inc rg-sys')
		if log_proc.indexOf('rg-sysmon') == -1:
			result = 1
		end
		cli('run-system-shell')
		log_tag = cli('ls sbin | grep sys')
		cli('exit')
		if log_tag.indexOf('sysmon.tag') == -1:
			result = 1
		end
	end
	return result
end

#判断补丁是不是成功打进
def isPatched():
	result = 1
	if !isForceVersion(log_version):
		log_version = cli('show version detail')
		log_versionList = log_version.split('\r\n')
		foreach data in log_versionList:
			if data.indexOf('System patch number') != -1 && data.indexOf('SP') == -1:
				result = 0
				break
			end
		end
		if result:
			log_proc = cli('show cpu | inc rg-sys')
			if log_proc.indexOf('rg-sysmon') == -1:
				result = 0
			end
		end
	else:
		log_proc = cli('show cpu | inc rg-sys')
		if log_proc.indexOf('rg-sysmon') == -1:
			result = 0
		end
		cli('run-system-shell')
		log_tag = cli('ls sbin | grep sys')
		cli('exit')
		if log_tag.indexOf('sysmon.tag') == -1:
			result = 0
		end
		log_logging = cli('show logging | inc rg-sys')
		if log_logging.indexOf('times and new id rg-sysmon') == -1:
			result = 0
		end
	end
	return result
end

#判断是否有做过打补丁操作
def isPatchedEver():
	result = 0
	if isForceVersion(log_version):
		foreach data in log_versionList:
			if data.indexOf('System patch number') != -1 && data.indexOf('SP') != -1:
				result = 1
				break
			end
		end
	else:
		cli('run-system-shell')
		log_tag = cli('ls sbin | grep sys')
		cli('exit')
		if log_tag.indexOf('sysmon.tag') != -1:
			result = 1
		end
	end
	return result
end

#获取槽位号
def getSlot(str,netType):
	slot = ''
	reg = /(\d+)\s+(\w+)\s+\d+/
	r = str.match(reg)
	if r != null:
		dev = r[1]
		slotID = r[2]
		if netType == 1:
			slot = dev + '/' + slotID
		else:
			slot = slotID
		end
	end
	return slot
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

#匹配补丁版本
def get_bin_file(str):
	result = ''
	version = get_version(str)
	date = get_date_version(str)
	release = get_release(str)
	if version == "NBS5600_RGOS 11.4(1)B1" && date == "06012015" :
		result = 'NBS5600_RGOS 11.4(1)B1-06012015'
	else if version == "S5750_RGOS 11.4(1)B1" && date == "06012015" :
		result = 'S5750_RGOS 11.4(1)B1-06012015'
	else:
		if release != '':
			result = version + '-' + release
		else:
			result = version
		end
	end
	return result
end

def get_version(str):
	version = ''
	#System software version : S5700H_RGOS 11.4(1)B2, Release(02191904)
	reg_ver = /System software version :\s+([^,^\r\n]+)/
	r = str.match(reg_ver)
	if r != null:
		version = r[1]
	end
	return version
end

def get_release(str):
	release = ''
	strList = str.split('\r\n')
	#System software version : S5700H_RGOS 11.4(1)B2, Release(02211117)
	reg = /System software version\s+:\s+[^\r\n]+Release\((\d+)\)/
	foreach data in strList:
		r = data.match(reg)
		if r != null:
			release = r[1]
			break
		end
	end
	return release
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


#判断tftp上传版本是否成功
def isTransfer(str):
	result = 0
	log_dir = cli('dir')
	if isForceVersion(log_version):
		log_dirList = log_dir.split('\r\n')
		foreach data in log_dirList:
			if data.indexOf(str) != -1 && data.indexOf('.bin') == -1:
				result = 1
				break
			end
		end
	else:
		if log_dir.indexOf(str) != -1:
			result = 1
		end
	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

#判断是否需要force参数
def isForce(str):
	result = 0
	version = get_version(log_version)
	release = get_release(log_version)
	if release != "":
		if version == "S2910_RGOS 11.4(1)B1" && release == "02163021":
			result = 1
		end
		if version == "S2910_RGOS 11.4(1)B1" && release == "02172216":
			result = 1
		end
		if version == "S5750_RGOS 11.4(1)B1" && release == "02172813":
			result = 1
		end
		if version == "S5750_RGOS 11.4(1)B1" && release == "02180121":
			result = 1
		end
		if version == "S2910_RGOS 11.4(1)B1" && release == "02180121":
			result = 1
		end
		if version == "S5700L_RGOS 11.4(1)B1" && release == "02180121":
			result = 1
		end
		if version == "NBS5600_RGOS 11.4(1)B1" && release == "02180121":
			result = 1
		end
		if version == "S5750_RGOS 11.4(1)B1" && release == "02172216":
			result = 1
		end
		if version == "S2910_RGOS 11.4(1)B1" && release == "02172216":
			result = 1
		end
		if version == "S5700L_RGOS 11.4(1)B1" && release == "02172216":
			result = 1
		end
		if version == "NBS5600_RGOS 11.4(1)B1" && release == "02172216":
			result = 1
		end
		if version == "S5750_RGOS 11.4(1)B1" && release == "02172813":
			result = 1
		end
		if version == "S2910_RGOS 11.4(1)B1" && release == "02172813":
			result = 1
		end
		if version == "S5700L_RGOS 11.4(1)B1" && release == "02172813":
			result = 1
		end
		if version == "NBS5600_RGOS 11.4(1)B1" && release == "02172813":
			result = 1
		end
		if version == "S2910_RGOS 11.4(1)B1" && release == "02180318":
			result = 1
		end
	else:
		if version == "S2910_RGOS 11.4(1)B1":
			result = 1
		end
		if version == "S5750_RGOS 11.4(1)B3P1":
			result =1
		end
		if version == "S5750_RGOS 11.4(1)B1":
			result =1
		end
		if version == "NBS5600_RGOS 11.4(1)B1":
			result = 1
		end
		if version == "NBS5500_RGOS 11.4(1)B3P4":
			result = 1
		end
		if version == "S5700H_RGOS 11.4(1)B2":
			result = 1
		end
	end
	#return result
	return 1
end

#判断是否upgrade成功
def isUpgreadSuccess():
	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]
			logstr = getStaLog(data,log_upgrade_staList)
			if logstr.indexOf('success') == -1 && logstr.indexOf('skipped') == -1:
				isSuccess = 0
			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

def str_to_num(str):
	num = ''
	strList = str.split(",")
	foreach data in strList:
		num = num+data
	end
	return num
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