
{
    "name"  : "cpu误报专项-升级后CPU状态检查",
    "problems" : {
        "patch_cpu" : {
            "desc"      : "【问题】设备板卡${0}出现CPU高于${1}%,建议现场观察一段时间！。",
            "advice"    : "【建议】建议现场观察一段时间！。"
        },
		"patch_memory" : {
            "desc"      : "【问题】设备系统内存可用不足${0}%,建议现场观察一段时间！",
            "advice"    : "【建议】建议现场观察一段时间！。"
        }
    }
}

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

over_value = 80
over_value_memory = 20
port = ''
result = 'pass'
stepResult = 'pass'
log_cpu = cli('show cpu')
log_cpuList = log_cpu.split('\r\n')
reg = /(Slot\s+[^ ]+):\s+[^ ]+/
foreach data in log_cpuList:
	r = data.match(reg)
	if r != null:
		slot = r[1]
		log_unit = get_log_unit(slot)
		if isFail(log_unit):
			port = port + slot + '、'
			stepResult = 'fail'
		end
	end
end

if stepResult == 'fail':
	message = '设备板卡出现CPU高于'+over_value+'%,建议现场观察一段时间！'
    change_title(message)
    add_problem('patch_cpu',[port,over_value])
	result = 'fail'
end

log_memory = cli('show memory')
reg_memory = /System Memory:\s+(\d+)KB total,\s+(\d+)KB used,\s+(\d+)KB free/
r = log_memory.match(reg_memory)
if r != null:
	free_memory = r[3]
	total_memory = r[1]
	left_value = free_memory/total_memory
	over_sta = over_value_memory/100
	if left_value < over_sta :
		message_memory = '设备系统内存可用不足'+over_value_memory+'%,建议现场观察一段时间!'
		change_title(message_memory)
		add_problem('patch_memory',[over_value_memory])
		result = 'fail'
	end
end

if result == 'fail':
	return 'warn'
end

return 'success'

def isFail(str):
	result = 0
	strList = str.split('\r\n')
	reg = /CPU utilization in five seconds:([^ ]+)%/
	foreach data in strList:
		r = data.match(reg)
		if r != null:
			value = r[1]
			if value > over_value:
				result = 1
			end
			break
		end
	end
	return result
end

def get_log_unit(slot):
	result = ''
	mySlot = ''
	isBegin = 0
	reg = /(Slot\s+[^ ]+):\s+[^ ]+/
	foreach data in log_cpuList:
		r = data.match(reg)
		if r != null:
			mySlot = r[1]
			if mySlot == slot:
				isBegin = 1
			end
		end
		if data.indexOf('five minutes') != -1 && isBegin == 1 :
			isBegin = 0
			break
		end
		if isBegin == 1:
			result = result + data + '\r\n'
		end
	end
	return result
end
