{
    "name"  : "是否存在环路",
    "problems" : {
        "loop" : {
            "desc"      : "${0}",
            "advice"    : ["【建议】建议开启RLDP防环功能，或者开启风暴控制功能。"
                          ,"\t开启RLDP防环功能示例："
                          ,"\t\tRuijie#config"
                          ,"\t\tRuijie(config)#rldp enable "
                          ,"\t\tRuijie(config)#interface GigabitEthernet 0/1 "
                          ,"\t\tRuijie(config-if-range)#rldp port loop-detect block "
                          ,"\t\tRuijie(config-if-range)#spanning-tree bpdufilter enable "
                          ,"\t\tRuijie(config-if-range)#en "
                          ,"\t\tRuijie#write "
                          ,"\t开启广播风暴控制示例： "
                          ,"\t\tRuijie#config "
                          ,"\t\tRuijie(config)#interface GigabitEthernet 0/1 "
                          ,"\t\tRuijie(config-if-range)#spanning-tree portfast "
                          ,"\t\tRuijie(config-if-range)#spanning-tree bpduguard enable "
                          ,"\t\tRuijie(config-if-range)#storm-control broadcast "
                          ,"\t\tRuijie(config-if-range)#end"
                          ,"\t\tRuijie#write"] 
        }
    }
}
---------------------------code-------------------------------
#是否存在环路
resultStr = ''
showIntfCmdCount = 0

def showIntfFinish(showIntfCmdCount,resultStr) :
    showIntfCmdCount = showIntfCmdCount - 1
    if showIntfCmdCount == 0 :
        response = cli('show logging')
        if response.search('Invalid input | Unknown command.') != -1 :
            add_reason('无效指令：show logging')
            return 'skip'    
        end
        print response
        reg = /MAC-4-FLAPPING:.*is flapping between .*/g
        elems = response.match(reg)
        if elems == null :
            if resultStr == '' :
                return 'success'
            else :
                add_problem('loop',[resultStr])
                return 'warn'
            end
        end
        i = 0
        while i < elems.length :
            elem = elems[i]
            elemArr = elem.match(/between .* and .*/g)
            elem = elemArr[0]
            intfs = elem.split(' and ')
            intfsTmep0 = intfs[0]
            intfsTmep1 = intfs[1]
            intfs[0] = intfsTmep0.replace('between ', '')
            intfs[1] = intfsTmep1.replace('.', '')
            resultStr = resultStr + intfs[0] + '和' + intfs[0] + '存在地址漂移，可能由环路导致'
            i = i + 1
        end
        add_problem("loop",[resultStr])
        return 'warn'
    end
end

response = cli('show interface status | include up')
if response.search('Invalid input | Unknown command.') != -1 :
    add_reason('无效指令：show interface status | include up')
    return 'skip'    
end
lines = response.split(/\r?\n/)
if response.indexOf('---------------------------------') != -1 :
    showIntfCmdCount = showIntfCmdCount + 1
    result = showIntfFinish(showIntfCmdCount,resultStr)
    return result
else :
    i = 0
    while i < lines.length :
        line = lines[i]
        if line.search('Ethernet') != -1 :
            elems = line.split(/\s+/)
            intfName = elems[0] + ' ' + elems[1]
            speed = elems[5]
            if speed.indexOf('0') == -1 || speed.indexOf('K') == -1 || speed.indexOf('M') == -1 || speed.indexOf('G') == -1 :
               speed = elems[4]
            end
            lengthTemp = speed.length - 1
            bandWidth = speed.substring(0,lengthTemp)
            bandWidthUnit = speed[lengthTemp]
            if bandWidthUnit == '0' :
                bandWidth = bandWidth * 10
            else if bandWidthUnit == 'K' :
                bandWidth = bandWidth * 1000
            else if bandWidthUnit == 'M' :
                bandWidth = bandWidth * 1000000
            else if bandWidthUnit == 'G' :
                bandWidth = bandWidth * 1000000000
            end
            showIntfCmdCount = showIntfCmdCount + 1
            cmd = 'show interfaces ' + intfName + ' counters'
            response = cli(cmd)
            if response.search('Invalid input | Unknown command.') != -1 :
                result = showIntfFinish(showIntfCmdCount,resultStr)
                return result
            else :
                intputVal = ''
                ucastVal = ''
                mcastVal = ''
                bcastRate = ''
                inputReg = /input rate\s+:\s*(\d+)\s+bits\/sec/
                responseTemp = response.match(inputReg)
                if responseTemp.length > 1 :
                    responseTemp = responseTemp[1]
                    intputVal = parse_int_catch(responseTemp)
                end

                rUcastRateReg = /[\r\n]+\s*InUcastPkts\s+:\s+(\d+)/
                responseTemp = response.match(rUcastRateReg)
                if responseTemp.length > 1 :
                    responseTemp = responseTemp[1]
                    ucastVal = parse_int_catch(responseTemp)
                end
             
                rMcastRateReg = /[\r\n]+\s*InMulticastPkts\s+:\s+(\d+)/
                responseTemp = response.match(rMcastRateReg)
                if responseTemp.length > 1 :
                    responseTemp = responseTemp[1]
                    mcastVal = parse_int_catch(responseTemp)
                end

                rBcastRateReg = /[\r\n]+\s*InBroadcastPkts\s+:\s+(\d+)/
                responseTemp = response.match(rBcastRateReg)
                if responseTemp.length > 1 :
                    responseTemp = responseTemp[1]
                    bcastRate = parse_int_catch(responseTemp)
                end

                if intputVal == "解析出错" || ucastVal == "解析出错" || mcastVal == "解析出错" || bcastRate == "解析出错" :
                    result = showIntfFinish(showIntfCmdCount,resultStr)
                    return result
                else :
                    if (intputVal * 2) < bandWidth :
                        result = showIntfFinish(showIntfCmdCount,resultStr)
                        return result
                    end 
                    if mcastVal * 10 > ucastVal :
                        resultStr = resultStr + intfName + '可能存在环路'
                        result = showIntfFinish(showIntfCmdCount,resultStr)
                        return result
                    end        
                    if bcastRate * 10 > ucastVal :
                        resultStr = resultStr + intfName + '可能存在环路'
                        result = showIntfFinish(showIntfCmdCount,resultStr)
                        return result
                    end
                    result = showIntfFinish(showIntfCmdCount,resultStr)
                    return result                 
                end
               
            end
        end
        i = i + 1
    end
end