{
    "name"  : "Ipsec转发不通综合诊断",
    "problems" : {
        "CryptoIpsecOptional" : {
            "desc"      : "【问题】没有关闭安全检测",
            "advice"    : "【建议】关闭安全规则"
        },
		"CryptoState" : {
            "desc"      : "【问题】ipsec协商不成功",
            "advice"    : "【建议】如果状态不正确，参照VPDN-IPSEC协商不通规则设计排查"
        },
		"CryptoIpsecSa" : {
            "desc"      : "【问题】隧道加密和解密统计数不正常",
            "advice"    : "【建议】请联系400处理"
        },
		"INTERESTFLOWOVERLAP" : {
            "desc"      : "【问题】有感兴流重叠，呈现该选项",
            "advice"    : "【建议】重新配置感兴流"
        },
		"NoSetKey" : {
            "desc"      : "【问题】IPSEC流私有空间标志位有异常",
            "advice"    : "【建议】清除隧道，流表老化后重新协商"
        },   
		"SERVDROPCNTS" : {
            "desc"      : "【问题】设备出现丢包",
            "advice"    : "【建议】呈现丢包的模块"
        }
    }
}
---------------------------code-------------------------------
InterestFlowIP = precondition('InterestFlowIP')
TunnelInternetIp = precondition('TunnelInternetIp')

def six() :
results=[]

	cmd="show efmp count"
	i=0
	j=0

	flagArr=[]
	while i<5:
	    temp=cli(cmd)
		results.push(temp)
		sleep(2000)
		i=i+1
	end

    while j<results.length-1 :
	    k=0
	    temp1=results[j]
	    resultArr=temp1.split(/\r?\n/)
		print 'resultArr='+resultArr
		  while k<resultArr.length :
		    keyValue=resultArr[k]
			print 'keyValue='+keyValue
		       if ((keyValue.indexOf("SERV DROP CNTs") != -1) || keyValue.indexOf("SERV-DROP-CNTS") != -1):
			      temp2=resultArr[k+1]
		          flag = temp2.match(/[0-9]+/)
				     if(flag>=0 || flag!=null || flag!=undefined) :
					 	 flagArr.push(flag)
					 end
				  print 'flag ='+flag 
		       end
			k=k+1
		  end
		j=j+1
	end
    
	z=0
	lostPre=0
	print 'flagArr='+flagArr
	while z<flagArr.length-1 :
	      lostPre=flagArr[z]
		  lostNext=flagArr[z+1]
		  print 'lostPre='+lostPre
		  print 'lostNext='+lostNext
		  if(lostPre!=null):
		     lostPre = parse_int(lostPre)
		  end
		  if(lostNext!=null):
		     lostNext = parse_int(lostNext)
		  end	  
	      if(lostPre!=null && lostNext!=null&&lostPre<lostNext):
		      add_problem("SERVDROPCNTS")
              return "warn"
		   end
		z=z+1
	end
 return 'success'

end

def five() :
    cmd1="show ip fpm private 4 | inc "+InterestFlowIP
	cmd2="show ip fpm private 4 | inc "+TunnelInternetIp
	cmd3="show ip fpm private 5 | inc "+InterestFlowIP
	cmd4="show ip fpm private 5 | inc "+TunnelInternetIp
	
	res1=cli(cmd1) 
	resArr1=res1.split(/\r?\n/)
	i=0
	while i<resArr1.length-1 :
	    temp=resArr1[i]
		if(temp!=null&&temp.match(InterestFlowIP)!=null&&temp.match(TunnelInternetIp)!=null&&temp.indexOf("1(0)")==-1):
		  add_problem('FlowIncorrect')
	      return 'warn'
		end
		i=i+1
	end
	
	res2=cli(cmd2) 
	resArr2=res2.split(/\r?\n/)
	j=0
	while j<resArr2.length-1 :
	    temp2=resArr2[j]
		if(temp2!=null&&temp2.match(InterestFlowIP)!=null&&temp2.match(TunnelInternetIp)!=null&&temp2.indexOf("0(1)")==-1):
		  add_problem('FlowIncorrect')
	      return 'warn'
		end
		j=j+1
	end
	
	res3=cli(cmd3) 
	resArr3=res3.split(/\r?\n/)
	k=0
	while k<resArr3.length-1 :
	    temp3=resArr3[k]
		if(temp3!=null&&temp3.match(InterestFlowIP)!=null&&temp3.match(TunnelInternetIp)!=null&&temp3.indexOf("1(0)")==-1):
		  add_problem('FlowIncorrect')
	      return 'warn'
		end
		k=k+1
	end
	
	res4=cli(cmd4) 
	resArr4=res4.split(/\r?\n/)
	z=0
	while z<resArr4.length-1 :
	    temp4=resArr4[j]
		if(temp4!=null&&temp4.match(InterestFlowIP)!=null&&temp4.match(TunnelInternetIp)!=null&&temp4.indexOf("0(1)")==-1):
		  add_problem('FlowIncorrect')
	      return 'warn'
		end
		z=z+1
	end
	   
	res = six()
    return res
end

def fourth() :
    response = cli('show log')

    print 'response =' +response
    if response.indexOf("INTEREST_FLOW_OVERLAP")!= -1:
	    add_problem("INTERESTFLOWOVERLAP")
	    return "warn"
    end
	
	res = five()
    return res
end

def third() :
    results=[]
	cmd="show crypto ipsec sa"
	i=0
	j=0

	while i<5:
	    temp=cli(cmd)
		results.push(temp)
		sleep(2000)
		i=i+1
	end

    while j<results.length-1 :
		k=0
	    temp1=results[j]
	    resultArr=temp1.split(/\r?\n/)
		print 'resultArr='+resultArr
		  while k<resultArr.length :
		    keyValue=resultArr[k]
			print 'keyValue='+keyValue
		       if keyValue.indexOf("pkts encaps:") != -1 :
		          flag = keyValue.match(/[0-9]+/)
				     if(flag!=null && flag!=undefined) :
					     flag = parse_int(flag)
						    if(flag<=0):
							   add_problem("CryptoIpsecSa")
                               return "warn"
							end 
					 end
				  print 'flag ='+flag 
		       end
			k=k+1
		  end
		j=j+1
		
	end
	
	res = fourth()
    return res
end


def second() :
    i=0
    j=0
    cmd = 'show crypto state'
	res = cli(cmd)
	resArr = res.split(/\r?\n/) 
    if resArr.length > 0 :
      while i < resArr.length:
	    resArrTmp = resArr[i]
	       if (resArrTmp.indexOf("IKE_IDLE")!= -1 || resArrTmp.indexOf("QM_IDLE")!= -1 ):
	          j=j+1
           end
	       i=i+1
	    end
    end
	
	if j<1 :
	  add_problem("CryptoState")
	  return "warn"
	end
	
	res = third()
    return res
end

result=cli("sh running-config | inc optional")

if result.indexOf("crypto ipsec optional")!=-1:
    add_problem('CryptoIpsecOptional')
    return 'warn'
end

resultF = second()
return resultF