{
    "name"  : "诊断IPSec协商状态",
    "problems" : {
        "IPSec_status" : {
            "desc"      : "【问题】会话目的IP:${0};会话状态：${1} ",
            "advice"    : "【建议】${2}"
        }
    },
    
    "status" : {
        "MM_SI1_WR1,MM_SA_SETUP"  : 1,
        "MM_SR1_WI2,MM_KEY_EXCH"  : 2,
        "MM_SI2_WR2,MM_KEY_EXCH"  : 3,
        "MM_SR2_WI3,MM_VERIFY"    : 4,
        "MM_SI3_WR3,MM_VERIFY"    : 5,
        "AG_SI1_WR1,AG_VERIFY"    : 6,
        "AG_SR1_WI2,AG_VERIFY"    : 7,
        "IKE_IDLE"                : 8,
        "QM_SI1_WR1"              : 9,
        "QM_SR1_WI2"              : 10,
        "QM_IDLE"                 : 11  
    },
    
    "suggest" : {
        "MM_SI1_WR1,MM_SA_SETUP"  : "主模式第一个报文没响应，首先可通过PING包检测链路是否畅通，然后检查一阶段配置是否有误",
        "MM_SR1_WI2,MM_KEY_EXCH"  : "检查一阶段策略配置",
        "MM_SI2_WR2,MM_KEY_EXCH"  : "检查一阶段策略配置",
        "MM_SR2_WI3,MM_VERIFY"    : "检查一阶段预共享密钥或证书配置",
        "MM_SI3_WR3,MM_VERIFY"    : "检查一阶段预共享密钥或证书配置",
        "AG_SI1_WR1,AG_VERIFY"    : "积极模式第一个报文没响应，首先可通过PING包检测链路是否畅通，然后检查一阶段配置是否有误",
        "AG_SR1_WI2,AG_VERIFY"    : "检查一阶段预共享密钥或证书配置",
        "IKE_IDLE"                : "一阶段完成",
        "QM_SI1_WR1"              : "检查二阶段策略配置",
        "QM_SR1_WI2"              : "检查二阶段策略配置",
        "QM_IDLE"                 : "二阶段完成" 
    }
}
---------------------------code-------------------------------
#source_ip =  precondition('src_ip')
dest_ip = precondition('dst_ip')

res = cli('show crypto state')
sleep(3000)
res = cli('show crypto state')
sleep(3000)
res = cli('show crypto state')
#res = "Ruijie#show cry st \r\n destination       dport source            sport state                    st-id pa-st mapid mode  ir    xauth_user          lifetime  \r\n  123.7.178.177     54943 222.143.23.254    4500   MM_SR1_WI2, MM_KEY_EXCH                0     65535 65535 9     2     hnsxxzx             84706    \r\n   123.7.178.177    56991 222.143.23.254    4500   AG_SR1_WI2,AG_VERIFY                1     65535 65535 9     2     hnsxxzx             84405   \r\n  120.194.116.68    9216  222.143.23.254    4500   IKE_IDLE                2     65535 65535 9     2     hnsxxzx             84437   \r\n  120.194.116.68    9216  222.143.23.254    4500   AG_SR1_WI2,AG_VERIFY                2     65535 65535 9     2     hnsxxzx             84437   \r\n"

print res
resArr = res.split(/\r?\n/ig)
print resArr

i = 2
IPSecFailedJson = {}
status = get_configure_value('status')
suggest = get_configure_value('suggest')
is_problem_found = false
while i <= (resArr.length - 1):

    singleRes = resArr[i]
    #含有异常隧道
    if ((singleRes.indexOf('IKE_IDLE') == -1) && (singleRes.indexOf('QM_IDLE') == -1)) :
        singles = singleRes.trim()
        singles = singles.replace(/, /,',')
        singleArr = singles.split(/\s+/)
        singleKey = singleArr[0]
        singleStatus = singleArr[4]
		print singleStatus
		if singleStatus != null :
			#已经保存过该异常隧道
			if IPSecFailedJson.hasOwnProperty(singleKey) :
				oldStatus = IPSecFailedJson[singleKey]
				oldStatusLevel = status[oldStatus]
				currentStatusLevel = status[singleStatus]
				if oldStatusLevel < currentStatusLevel :
					IPSecFailedJson[singleKey] =  singleStatus
				end
			else :
				IPSecFailedJson[singleKey] =  singleStatus
				
			end
			if (dest_ip == '' || dest_ip == null ):
				is_problem_found = true
				currentSuggest = suggest[singleStatus]
				if currentSuggest != null:
					add_problem('IPSec_status', [singleKey,singleStatus,currentSuggest])
			    end
			end
		else :
		    if resArr.length == 3 :
				is_problem_found = true
				add_problem('IPSec_status', [0,0,"当前没有建立任何IPSec会话，首先请使用PING包检测链路是否连通，其次检查对应接口是否使能了IPSec，如果使能了IPSec，请再检查LAC端ACL的配置能否匹配需要加密的报文"])
			end
		
		end
    end 
    
    i = i + 1
end

print IPSecFailedJson
hasProp = false
if dest_ip != '' && dest_ip != null :
	foreach dest_ip in IPSecFailedJson :
		hasProp = true;  
		break;  
	end

	print dest_ip
	statusString = ''
	suggestString = ''
		
	if !hasProp :
		return 'success'
	else :
		foreach dest_ip in IPSecFailedJson :
		
			currentStatus = IPSecFailedJson[dest_ip]
			currentSuggest = suggest[currentStatus]
			
			if currentStatus != null && currentSuggest != null :
				statusString = currentStatus
				suggestString = currentSuggest
			end 
		end
		add_problem('IPSec_status', [dest_ip,statusString, suggestString])
		
		return 'warn'
	end
else :	
	if is_problem_found :
	    return 'warn'
    else :
	    return 'success'
	end
end



    