{
    "name"  : "DHCP地址池是否正常",
    "problems" : {
        "dhcp12" : {
            "desc"      : "【问题】未配置与网关一致的DHCP地址池",
            "advice"    : ["【建议】配置DHCP地址池 "
                          ,"配置示例："
                          ,"\tRuijie#config"
                          ,"\tRuijie(config)# ip dhcp pool pool_name"
                          ,"\tRuijie(dhcp-config)#network addr_range addr_mask"
                          ,"\tRuijie(dhcp-config)#default-router 网关地址"
                          ,"\tRuijie(dhcp-config)#end"
                          ,"\tRuijie#write"]
        },
        "dhcp34" : {
            "desc"      : "【问题】未配置DHCP地址池",
            "advice"    : ["【建议】配置DHCP地址池 "
                          ,"配置示例："
                          ,"\tRuijie#config"
                          ,"\tRuijie(config)# ip dhcp pool pool_name"
                          ,"\tRuijie(dhcp-config)#network addr_range addr_mask"
                          ,"\tRuijie(dhcp-config)#default-router 网关地址"
                          ,"\tRuijie(dhcp-config)#end"
                          ,"\tRuijie#write"]
        },
        "dhcp56" : {
            "desc"      : "【问题】DHCP地址池中的IP地址被耗尽",
            "advice"    : ["【建议】"
                          ,"1. 检查地址租期是否合理。当前地址租期为 ${0}，建议缩短地址租期。"
                          ,"2. 检查下联设备数是否超过地址池中的IP地址数。如果超过，建议增加地址池中的地址。"]
        },    
        "dhcp78" : {
            "desc"      : "【问题】DHCP地址池中的IP地址被耗尽",
            "advice"    : ["【建议】将地址租期配置为2小时 "
                          ,"配置示例："
                          ,"\tRuijie#config"
                          ,"\tRuijie(config)#ip dhcp pool %s"
                          ,"\tRuijie(dhcp-config)#lease 0 2"
                          ,"\tRuijie(dhcp-config)#end"
                          ,"\tRuijie#write"]
        },        
        "dhcp910" : {
            "desc"      : "【问题】地址池${0}未配置默认网关",
            "advice"    : ["【建议】为地址池${1}配置默认网关 "
                          ,"配置示例："
                          ,"\tRuijie#config"
                          ,"\tRuijie(config)#ip dhcp pool ${2}"
                          ,"\tRuijie(dhcp-config)#default-router 网关地址"
                          ,"\tRuijie(dhcp-config)#end"
                          ,"\tRuijie#write"]
        },   
        "dhcp1112" : {
            "desc"      : "【问题】地址池 ${0} 未将默认网关设置为除外地址",
            "advice"    : ["【建议】将默认网关设置为除外地址 "
                          ,"配置示例："
                          ,"\tRuijie#config"
                          ,"\tRuijie(config)#ip dhcp excluded-address 网关地址"
                          ,"\tRuijie(dhcp-config)#end"
                          ,"\tRuijie#write"]
        }
    }
}
---------------------------code-------------------------------
#DHCP地址池是否正常
param = {}
param["role"] = precondition('role')
param["gateway"] = precondition('gateway')
no_support_dev = false
deviceMode = get_device_model()
if deviceMode == 'RG-S2900G-E/P' || deviceMode == 'RG-S2600G-I' :
    no_support_dev = true
end
dhcp_pool_names = []
dhcp_pool_info = {}
dhcp_excluded = []
dhcp_pool_remaind = []
dhcp_pool_name = null
times = 0

if no_support_dev:
    return "success"
end

def getVersion(softStr) :
    if softStr.search("10.") != -1 :
        return 10
    end
    if softStr.search("11.") != -1 :
        return 11
    end
end
#8如果网关地址与地址池的地址网段是相同网段，且未被设置为排除地址
#若不是相同网段前面已经排除了 直接return了  步骤4中
def check_default_router_exclude():
    exist = false
    foreach key in dhcp_pool_info :
        if key == dhcp_pool_name:
            networkTmp = dhcp_pool_info[key]
            networkTmp = networkTmp["network"]
            netmaskTmp = dhcp_pool_info[key]
            netmaskTmp = netmaskTmp["netmask"]
            ip_or_mask = mask(networkTmp, netmaskTmp)
            k = 0 
            dhcp_pool_infoKey = dhcp_pool_info[key]
            gatewayTmp = dhcp_pool_infoKey["gateway"]
            while k < gatewayTmp.length:
                ip_or_mask_defalut_gateway = gatewayTmp[k]
                ip_or_mask_defalut_gateway = mask(ip_or_mask_defalut_gateway)
                
                if ip_or_mask == ip_or_mask_defalut_gateway:
                    is_exclude = false
                    startaddr = dhcp_pool_infoKey["startaddr"]
                    startaddrLong = ipToLong(startaddr)
                    endaddr = dhcp_pool_infoKey["endaddr"]
                    endaddrLong = ipToLong(endaddr)                    
                    gatewayIPArr = dhcp_pool_infoKey["gateway"]
                    gatewayIP = gatewayIPArr[0]
                    gatewayIPLong = ipToLong(gatewayIP)    
                    
                    if startaddr :
                        if endaddr:
                            if startaddrLong > gatewayIPLong || endaddrLong < gatewayIPLong :
                                is_exclude = true  
                            end   
                        else :
                            if startaddrLong != gatewayIPLong:
                                is_exclude = true
                            end
                        end
                    end
                    if is_exclude:
                        break
                    end
                    i = 0 
                    while i < dhcp_excluded.length:
                        startip = dhcp_excluded[i]
                        startip = startip["startip"]
                        if startip :
                            endip = dhcp_excluded[i]
                            endip = endip["endip"]   
                            if endip != "":
                                startipLong = ipToLong(startip)
                                endipLong = ipToLong(endip)
                                if startipLong <= gatewayIPLong && endipLong >= gatewayIPLong:
                                    is_exclude = true
                                end
                            else :
                                if startipLong == gatewayIPLong:
                                    is_exclude = true
                                end
                            end
                        end
                        i = i + 1 
                    end
                    if is_exclude:
                        break
                    end
                    if exist == false:
                        add_problem("dhcp1112",[dhcp_pool_name])    # @fanbh 文档中要求自动修复，这里未实现
                        return "warn"
                    end
                else :
                    
                end
                k = k + 1 
            end
        end
    end
end
#7检测默认网关有无内容
def check_default_router():
    foreach key in dhcp_pool_info :
        dhcp_pool_infoKey = dhcp_pool_info[key]
        dhcp_pool_infoKeygateway = dhcp_pool_infoKey["gateway"]
        gatewayCount = dhcp_pool_infoKeygateway.length
        if gatewayCount == 0 :
            add_problem("dhcp910",[dhcp_pool_name,dhcp_pool_name, dhcp_pool_name])
            return "warn"
        else :
            res = check_default_router_exclude()
            return res
        end
    end
end
#6检测租期信息
def check_lease_legal():
    foreach key in dhcp_pool_info :
        dhcp_pool_infoKey = dhcp_pool_info[key]
        if key == dhcp_pool_name :
            dhcp_pool_infoTmp = dhcp_pool_infoKey["lease_infinite"]
            if dhcp_pool_infoTmp :
                add_problem("dhcp78")
                return "warn"
            else :
                dhcp_pool_infolease_days = dhcp_pool_infoKey["lease_days"]
                dhcp_pool_infolease_hours = dhcp_pool_infoKey["lease_hours"]
                dhcp_pool_infolease_minutes = dhcp_pool_infoKey["lease_minutes"]
                lease_str = dhcp_pool_infolease_days +'天'+dhcp_pool_infolease_hours+'时'+dhcp_pool_infolease_minutes+'分'               
                add_problem("dhcp56",[lease_str])
                return "warn"
            end
        end
    end
    res = check_default_router()
    return res
end
#5查看地址池剩余ip
def check_pool_remained():
    response = cli('show ip dhcp pool')
    exist = false
    results = response.split(/\r?\n/ig)
    j = 3
    while j < results.length:
        resultTmp = results[j]
        arr = resultTmp.split(/\s\s+/ig)
        if arr.length != 5:
            continue
        end
        remained = arr[3]
        pool_name = arr[0]   
        dhcp_pool_remaind["remained"] = remained
        dhcp_pool_remaind["pool_name"] = pool_name                 
        j = j + 1 
    end
    remained_num = 0
    k = 0
    while k < dhcp_pool_remaind.length:
        dhcp_pool_remaind_tmp = dhcp_pool_remaind[k]
        dhcp_pool_remaind_tmp = dhcp_pool_remaind_tmp["pool_name"]
        if dhcp_pool_remaind_tmp == dhcp_pool_name:
            remainedTmp = dhcp_pool_remaind[k]
            remainedTmp = remainedTmp["remained"]
            remained_num = parse_int(remainedTmp)
            break
        end
        k = k + 1
    end
    if remained_num:
        res = check_default_router()
        return res
    else :
        res = check_lease_legal()
        return res
    end
end

def check_pool_remained_10x():
    response = cli("show dhcp-server pool")
    exist = false
    results = response.split(/\r?\n/ig)
    j = 5
    while j < results.length:
        resultTmp = results[j]
        arr = resultTmp.split(/\s\s+/ig)
        if arr.length == 5:
            remained = ar[2]
            pool_name = arr[0]
            dhcp_pool_remaind["remained"] = remained
            dhcp_pool_remaind["pool_name"] = pool_name                        
        else if arr.length == 6:
            remained = arr[3]
            pool_name = arr[0]
            dhcp_pool_remaind["remained"] = remained
            dhcp_pool_remaind["pool_name"] = pool_name            
        end
        j = j + 1 
    end
    remained_num = 0
    k = 0
    while k < dhcp_pool_remaind.length:
        dhcp_pool_remaind_tmp = dhcp_pool_remaind[k]
        dhcp_pool_remaind_tmp = dhcp_pool_remaind_tmp["pool_name"]
        if dhcp_pool_remaind_tmp == dhcp_pool_name:
            remainedTmp = dhcp_pool_remaind[k]
            remainedTmp = remainedTmp["remained"]
            remained_num = parse_int(remainedTmp)
            break
        end
        k = k + 1
    end
    if remained_num:
        res = check_default_router()
        return res
    else :
        res = check_lease_legal()
        return res
    end
end
#4查看已有的地址池的分配网段中是否与前置条件中的网关地址在同一网段，若在并获取相应的地址池名
def check_pool_legal():
    exist = false
    foreach key in dhcp_pool_info :
        add1 = dhcp_pool_info[key]
        add1 = add1["network"]
        mask1 = dhcp_pool_info[key]
        mask1 = mask1["netmask"]
        gateway = param["gateway"]       
        ip_or_mask = mask(add1, mask1)
        ip_or_mask_pri = mask(gateway, mask1)
        if ip_or_mask == ip_or_mask_pri :
            dhcp_pool_name = key
            exist = true
        end            
    end
    if dhcp_pool_name == null:
        add_problem("dhcp34")
        return "warn"
    else :
        softVerion = get_device_softvesion()
        if getVersion(softVerion) == 10:
             res = check_pool_remained_10x()
             return res
        else if getVersion(softVerion) == 11:
            res = check_pool_remained()
            return res
        else :
            return "success"
        end         
    end
end

def get_pool_name_10x_info():
    length = dhcp_pool_names.length
    if times < length:
        cmd = "show run | begin ip dhcp pool " + dhcp_pool_names[times]
        strt = "ip dhcp pool " + dhcp_pool_names[times]     
        response = cli(cmd)
        exist = false
        _exist = false
        _exist1 = false
        _exist_infinite = false
        _exist_time = false
        results = response.split(/\r?\n/ig)  
        dhcp_pool_namesTmp = dhcp_pool_names[times]
        str = "/^ip dhcp pool " + dhcp_pool_namesTmp + "$/gi"
        re = eval_script(str)   
        j = 1
        while j < results.length:
            arr = results[j]
            arr = arr.trim()
            arr = arr.split(" ")
            resultTmp = results[j]
            arrTmp = arr[j]
            if resultTmp.search(re) != -1 :
                _exist = true
                continue
            end
            arr0 = arr[0]
            if arr0 == '!' :
                if _exist == true:
                    break
                end
            end
            if resultTmp.search("network") != -1 :
                exist =true
                network = arr[1]
                netmask = arr[2]
            end
            if resultTmp.search("default-router") != -1 :
                _exist1 = true
                arr_gateway = resultTmp.replace("default-router", '')
                arr_gateway = arr_gateway.trim()
                arr_gateway = arr_gateway.split(' ')
                gateway = arr_gateway
            end
            if resultTmp.search("lease") != -1 :
                if  resultTmp.search("lease infinite") != -1 :
                    _exist_infinite = true
                else :
                    resultTmp = results[j]
                    resultTmp = resultTmp.trim()
                    lease_arr = resultTmp.split(' ')
                    lease_days = lease_arr[1]
                    lease_hours = lease_arr[2]
                    lease_minutes = lease_arr[3]
                    _exist_time = true;                              
                end
            end
            j = j + 1
        end
        if exist :
            if isIPv4(network) && isIPv4(netmask) :
                dhcp_pool_info = forDhcp_PoolInfo(dhcp_pool_info,dhcp_pool_names,times,_exist1,_exist_infinite,_exist_time,network,netmask,gateway,lease_days,lease_hours,lease_minutes)
            end
        end
        times = times + 1
        res = get_pool_name_10x_info()
        return res
    else :
        response = cli('show run | in ip dhcp excluded-address')
        exist = false
        results = response.split(/\r?\n/ig)
        j = 1
        while j < results.length:
            resultsTmp = results[j]
            if resultTmp.search("ip dhcp excluded-address") != -1 :
                excluded_address = resultsTmp.split(' ')
                startip = excluded_address[3]
                endip = excluded_address[4]
                dhcp_excluded["startip"] = startip
                dhcp_excluded["endip"] = endip        
            end
            j = j + 1
        end
        res = check_pool_legal()
        return res
    end
end

#2,3
def  get_pool_name_info() :
    length = dhcp_pool_names.length
    if times < length :
        cmdTag = dhcp_pool_names[times]
        cmd = 'show dhcp pool ' + cmdTag
        response = cli(cmd)
        if response.search('Invalid input') != -1 :
            return "skip"
        end
        exist = false
        results = response.split(/\r?\n/ig)
        j = 1
        while j < results.length:
            resultsTmp = results[j]
            arr = resultTmp.split(/\s?:\s?/)    # @fanbh 这里的正则不能将多余的空格去掉吧？  /\s+:\s+/
            if resultTmp.search("network") != -1 :
                exist = true
                network = arr[1]
            end
            if resultTmp.search("netmask") != -1 :
                netmask = arr[1]
            end
            if resultTmp.search("lease-infinite") != -1 || resultTmp.search("lease-infinite") != -1 :   # @fanbh 这里||前后一样，应该是复制后忘记更改了吧
                lease_infinite = arr[1]
            end
            if resultTmp.search("lease-days") != -1 :
                lease_days = arr[1]
            end
            if resultTmp.search("lease-hours") != -1 :
                lease_hours = arr[1]
            end
            if resultTmp.search("lease-minutes") != -1 :
                lease_minutes = arr[1]
            end
            if resultTmp.search("gateway") != -1 :
                gateway = arr[1]
                gateway = gateway.trim()
                gateway = gateway.split(" ")
            end  
            j = j + 1            
        end
        if exist == true :
            gateway0 = gateway[0]
            if gateway0 == "":
                gateway = []
            end
            if isIPv4(network) && isIPv4(netmask) :
                dhcp_pool_info = forDhcp_PoolInfo2(dhcp_pool_info,dhcp_pool_names,times,network,netmask,gateway,lease_days,lease_hours,lease_minutes)
            end
        end
        times = times + 1
        res = get_pool_name_info()
    else :
        response = cli('show run | in ip dhcp excluded-address')
        exist = false
        results = response.split(/\r?\n/ig)
        j = 1
        while j < results.length:
            resultsTmp = results[j]
            if resultTmp.search("ip dhcp excluded-address") != -1 :
                excluded_address = resultsTmp.split(' ')
                startip = excluded_address[3]
                endip = excluded_address[4]
                dhcp_excluded["startip"] = startip
                dhcp_excluded["endip"] = endip                    
            end
            j = j + 1
        end
        res = check_pool_legal()
        return res        
    
    end
end

role = param["role"]
gateway = param["gateway"]
if role == "server":
    if gateway == '' || gateway == ' ' :
        return "success"
    end
    response = cli("show run | include ip dhcp pool")
    results = response.split(/\r?\n/ig)
    exist = false
    i = 1
    while i < results.length :
        resultTmp = results[i]
        if resultTmp.search("ip dhcp pool") != -1 :
            pool_name = results[i]
            pool_name = pool_name.split(" ")
            pool_name = pool_name[3]
            if pool_name != undefined:
                exist = true
                dhcp_pool_names.push(pool_name)
            end
        end
        i = i + 1
    end
    if  exist == false:
        add_problem("dhcp12")
        return "warn"
    end
    softVerion = get_device_softvesion()
    if getVersion(softVerion) == 10:
         res = get_pool_name_10x_info()
         return res
    else if getVersion(softVerion) == 11:
        res = get_pool_name_info()
        return res
    else :
        return "success"
    end
end
return "success"






