{
    "name"  : "诊断IPSec协商状态",
    "problems" : {
        "IPSec_status" : {
            "desc"      : "[问题] ${0}",
            "advice"    : "[建议] ${1}"
        }
    },
    
    "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"  : "主模式第一个报文没响应，首先检查底层链路是否畅通，然后检查一阶段配置是否有误",
        "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"    : "野蛮模式第一个报文没响应，首先检查底层链路是否畅通，然后检查一阶段配置是否有误",
        "AG_SR1_WI2,AG_VERIFY"    : "检查一阶段预共享密钥或证书配置",
        "IKE_IDLE"                : "一阶段完成",
        "QM_SI1_WR1"              : "检查二阶段策略配置",
        "QM_SR1_WI2"              : "检查二阶段策略配置",
        "QM_IDLE"                 : "二阶段完成" 
    }
}
---------------------------code-------------------------------
res = precondition('srcConf')
resArr = res.split(/\r?\n/ig)

print '-------------'
res = cli("show cry st")
print res
res = cli("show cry st")
print res
res = cli("show cry st")
print res
print '==============='

i = 2
IPSecFailedJson = {}
status = get_configure_value('status')

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]
        #已经保存过该异常隧道
        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
    end 
    
    i = i + 1
end

suggest = get_configure_value('suggest')

hasProp = false
foreach IP in IPSecFailedJson :
    hasProp = true;  
    break;  
end
    
statusString = ''
suggestString = ''
    
if !hasProp :
    return 'success'
else :
    foreach IP in IPSecFailedJson :
    
        currentStatus = IPSecFailedJson[IP]
        currentSuggest = suggest[currentStatus]
        
        if statusString == '' :
            statusString = currentStatus
        else :
            statusString = statusString + ';' + currentStatus
        end
        
        if suggestString == '' :
            suggestString = currentSuggest
        else :
            suggestString = suggestString + ';' + currentSuggest
        end
    end
    
    add_problem('IPSec_status', [statusString, suggestString])
    return 'warn'
    
end

    