
{
    "name"  : "热补丁检查-防火墙",
    "precondition" : ["fwPwd"],
    "problems" : {
        "patch_fw" : {
            "desc"      : "【问题】防火墙版本upgrade不成功。",
            "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')
#是否有防火墙卡

if log_slot.indexOf('WALL') == -1:
	add_reason('无防火墙卡，不进行检测')
	return 'skip'
end
pass_word = precondition('fwPwd')

key_version = get_bin_file(log_version)
bin_file_fw = getHotfixFileName(key_version)

fw_result = 'pass'
fw_message = ''

if bin_file_fw == null:
	fw_result = 'fail'
	fw_message = '防火墙-版本不匹配，没做升级动作，有疑问请联系后台处理！'
	set_share_value('fw_result',fw_result)
	set_share_value('fw_message',fw_message)
	return 'skip'
end

myTag = isPatched()
if myTag != 1:
	if myTag == 2:
		fw_result = 'fail'
		fw_message = '防火墙-防火墙密码错误，请手动确认密码后重试！'
		set_share_value('fw_result',fw_result)
		set_share_value('fw_message',fw_message)
		return 'skip'
	end
	if isPatchedEver():
		fw_result = 'fail'
		fw_message = '防火墙-检测到补丁残留，请联系后台进行处理！'
		set_share_value('fw_result',fw_result)
		set_share_value('fw_message',fw_message)
		return 'skip'
	end
	local_ip = getLocalIp()
	patch_path = getHotfixStoreFolder()
	if isMgmt():
		cmd_tftp_fw = 'copy oob_tftp://' + local_ip + patch_path + bin_file_fw + ' flash:' + bin_file_fw
	else:
		cmd_tftp_fw = 'copy tftp://' + local_ip + patch_path + bin_file_fw + ' flash:' + bin_file_fw
	end
	transferResult = 'pass'
	防火墙卡
	if !isTransfer(bin_file_fw):
		#tftp三次传输不成功，则返回失败
		cli(cmd_tftp_fw)
		times = 3
		trans_success = 'fail'
		while times>0:
			transferResult = 'pass'
			if !isTransfer(bin_file_fw):
				transferResult = 'fail'
				cli(cmd_tftp_fw)
			else:
				trans_success = 'success'
				break
			end
			times = times - 1
		end		
		if trans_success == 'fail':
			fw_result = 'fail'
			fw_message = '防火墙-文件传输失败，请检查tftp端口(69)是否防火墙被禁用或者已经被其他tftp软件占用！'
			set_share_value('fw_result',fw_result)
			set_share_value('fw_message',fw_message)
			return 'skip'
		end

	end
	cmd_upgrade_fw = format('upgrade flash:%s force',bin_file_fw)
	cli(cmd_upgrade_fw)
	#5秒检测一次lc是否upgrade成功，最多等待5分钟
	times = 59
	up_success = 'fail'
	while times>0:
		sleep(5000)
		cpuHandle()
		if isUpgreadSuccess('fw'):
			up_success = 'pass'
			break
		end
		times = times - 1
	end
	
	times = 3
	while times > 0:
		cpuHandle()
		sleep(5000)
		times = times - 1
	end
	
	if up_success == 'pass':
		cli('patch active slot all')
		cli('patch running slot all')
	else:
		fw_result = 'fail'
		fw_message = '防火墙-upgrade失败，若状态显示为upgrading，请等待upgrade状态为timeout后再做操作！'
		set_share_value('fw_result',fw_result)
		set_share_value('fw_message',fw_message)
		return 'skip'
	end
else:
	set_share_value('fw_result',fw_result)
	set_share_value('fw_message',fw_message)
	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':
	fw_result = 'fail'
	fw_message = '防火墙-检测到热补丁问题，尝试过修复，但修复不成功，请联系后台处理'
else:
	fw_result = 'fail'
	fw_message = '防火墙-检测到热补丁问题，已经修复成功'
end

set_share_value('fw_result',fw_result)
set_share_value('fw_message',fw_message)
return 'skip'

#判断补丁是不是成功打进
def isPatched():
	result = 1
		netType = isVsu(log_slotList)
		reg_fw = /(\d+)\s+(\d+)\s+\d+/
		foreach card in log_slotList:
			if card.indexOf('ok') != -1 && card.indexOf('WALL') != -1:
				cmd_session=''
				r = card.match(reg_fw)
				if r != null:
					dev = r[1]
					slot = r[2]
					if netType :
						cmd_session = format('session device %s slot lc %s',dev,slot)
					else:
						cmd_session = format('session slot lc %s',slot)
					end
				end
				str1 = cli(cmd_session, ['#'], ['>','stdio'])
				if str1.indexOf('>') != -1:
					cli('enable',':')
					log_pwd = cli(pass_word,['>','#',':'])
					if log_pwd.indexOf('#') == -1:
						log_pwd_2 = cli('fw',['>','#',':'])	
							#cli('enable',':')					
							if log_pwd_2.indexOf('#') == -1:
							result = 2
							break
						end
					end
				else:
					cli('\r\n')
					str_ve = cli('show version detail')
					if str_ve.indexOf('System software version') == -1:
						cli('@ctrl+^+x')
						cli(cmd_session,['>','#'],'stdio')
						cli('enable',':')
						log_pwd = cli(pass_word,['>','#',':'])
						if log_pwd.indexOf('#') == -1:
							log_pwd_2 = cli('fw',['>','#',':'])	
								#cli('enable',':')					
								if log_pwd_2.indexOf('#') == -1:
								result = 2
								break
							end
						end
					end
				end
				log_fw_version = cli('show version detail')
				log_fw_versionList = log_fw_version.split('\r\n')
				foreach data in log_fw_versionList:
					if data.indexOf('System patch number') != -1 && data.indexOf('SP') != -1:
						result = 1
						break
					end
				end
				cli('run-system-shell')
				log_lsmod = cli('lsmod')
				log_S999asid_patch = cli('ls /etc/rc.d/rc3.d/')
				cli('exit')
				cli('exit')
				if log_lsmod.indexOf('rg_hm_path') == -1 || log_lsmod.indexOf('test_asid') == -1 || log_lsmod.indexOf('mips_cpu_asid_patch') == -1:
					result = 0
					break
				end
				if log_S999asid_patch.indexOf('S999asid_patch') == -1:
					result = 0
					break
				end
			end
		end
	return result
end

#判断是否有做过打补丁操作
def isPatchedEver():
	result = 0
		netType = isVsu(log_slotList)
		reg_fw = /(\d+)\s+(\d+)\s+\d+/
		foreach card in log_slotList:
			if card.indexOf('ok') != -1 && card.indexOf('WALL') != -1:
				cmd_session=''
				r = card.match(reg_fw)
				if r != null:
					dev = r[1]
					slot = r[2]
					if netType :
						cmd_session = format('session device %s slot lc %s',dev,slot)
					else:
						cmd_session = format('session slot lc %s',slot)
					end
				end
				str1 = cli(cmd_session, ['#'], ['>','stdio'])
				#str1 = cli(cmd_session,['>','#'],'stdio')
				if str1.indexOf('>') != -1:
					cli('enable',':')
					log_pwd = cli(pass_word,['>','#',':'])
					if log_pwd.indexOf('#') == -1:
						log_pwd_2 = cli('fw',['>','#',':'])	
							#cli('enable',':')					
							if log_pwd_2.indexOf('#') == -1:
							result = 2
						end
					end
				else:
					cli('\r\n')
					str_ve = cli('show version detail')
					if str_ve.indexOf('System software version') == -1:
						cli('@ctrl+^+x')
						cli(cmd_session,['>','#'],'stdio')
						cli('enable',':')
						log_pwd = cli(pass_word,['>','#',':'])
						if log_pwd.indexOf('#') == -1:
							log_pwd_2 = cli('fw',['>','#',':'])	
								#cli('enable',':')					
								if log_pwd_2.indexOf('#') == -1:
								result = 2
							end
						end
					end
				end
				log_fw_version = cli('show version detail')
				log_fw_versionList = log_fw_version.split('\r\n')
				foreach data in log_fw_versionList:
					if data.indexOf('System patch number') != -1 && data.indexOf('SP') != -1:
						result = 1
						break
					end
				end
				cli('run-system-shell')
				log_lsmod = cli('lsmod')
				log_S999asid_patch = cli('ls /etc/rc.d/rc3.d/')
				cli('exit')
				cli('exit')
				if log_lsmod.indexOf('rg_hm_path') != -1 || log_lsmod.indexOf('test_asid') != -1 || log_lsmod.indexOf('mips_cpu_asid_patch') != -1:
					result = 1
					break
				end
				if log_S999asid_patch.indexOf('S999asid_patch') != -1:
					result = 1
					break
				end
			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
#判断tftp上传版本是否成功
def isTransfer(str):
	result = 0
	log_dir = cli('dir')
	if log_dir.indexOf(str) != -1:
		result = 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

#判断是否upgrade成功-fw
def isUpgreadSuccess(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 == 'fw':
				if slot.indexOf('fw')!= -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
#匹配补丁版本-线卡
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 = 'fw'
	date = get_date_version(str)
	if version != '':
		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

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




