{
    "name"  : "检查指定接口是否存在异常状态",
    "problems" : {
        "root_gurad" : {
            "desc"      : "【问题】接口配置的ROOT GUARD生效，导致端口被阻断。",
            "advice"    : "【建议】请确认接口上是否必要配置ROOT GUARD功能。如果需要配置，则需要人工排查该接口对端设备是否出现识别生成树根桥错误。可通过在接口上no spanning-tree guard root关闭接口上的ROOT GURAD功能。"
        },
        "loop_gurad" : {
            "desc"      : "【问题】接口配置的LOOP GUARD生效，导致端口被阻断。",
            "advice"    : "【建议】请确认接口上是否必要配置LOOP GUARD功能。如果需要配置，则需要人工排查该接口是否处于环路中，且环路是否有阻断,可通过在接口上no spanning-tree guard loop关闭接口上的LOOP GURAD功能。"
        },
		"no_port" : {
            "desc"      : "【问题】当前接口为路由口，不参与计算。",
            "advice"    : "【建议】请人工确认是否该接口需要参与生成树；"
        },
        "port_down" : {
            "desc"      : "【问题】当前查询接口存在物理链路Down掉。",
            "advice"    : "【建议】请确认链路是否正常。"
        },
		"shut_down" : {
            "desc"      : "【问题】当前查询接口被手动shutdown掉",
            "advice"    : "【建议】请确认该接口是否需要shutdown。"
        },
		"no_bpud" : {
            "desc"      : "【问题】当前接口配置了BPDU过滤，不参与计算",
            "advice"    : "【建议】请人工确认是否实际需要在该接口下配置BPDU FILTER,可通过spanning-tree bpdufilter disable关闭"
        },
        "BPUD" : {
            "desc"      : "【问题】当前接口配置了BPDU防护，功能生效后不参与计算；",
            "advice"    : "【建议】请人工确认是否实际需要在该接口下配置BPDU GUARD，可通过spanning-tree bpduguard disable关闭"
        },
		"LAST" : {
            "desc"      : "【问题】当前接口与对端设备可能出现BPDU报文单通情况，导致生成树协商异常。",
            "advice"    : "【建议】建议在当前接口、以及其对端互联设备的接口上分别抓包确认是本机未发出BPDU，或是对端未收到BPDU。"
        }
    }
}

---------------------------code-------------------------------
def fourth() :
	sleep(35000)
	cmd = "show spanning-tree int" + intf_name
	response = cli(cmd)
	print response
	if response.indexOf("PortState : discarding:") != -1 && response.indexOf("PortRole : designatedPort") != -1 && response.indexOf("Inconsistent states : normal") != -1 :
		add_problem('LAST')
		return 'warn'
	end
end

intf_name = precondition('name')
cmd = "show spanning-tree int " + intf_name
response = cli(cmd)
print response
if response.indexOf("PortState : discarding") != -1 && response.indexOf("PortRole : designatedPort") != -1 && response.indexOf("Inconsistent states : Root Inconsistent") != -1 :
	add_problem('root_gurad')
    return 'warn'
else if response.indexOf("PortState : discarding") != -1 && response.indexOf("PortRole : designatedPort") != -1 && response.indexOf("Inconsistent states : Loop Inconsistent") != -1 :
	add_problem('loop_gurad')
    return 'warn'
else if response.indexOf("PortState : discarding") != -1 && response.indexOf("PortRole : designatedPort") != -1 && response.indexOf("Inconsistent states : normal") != -1 :
	fourth()
else if response.indexOf("no spanning tree info available") != -1:
	cmd = "show int " + intf_name
	result = cli(cmd)
	print result
	cmd1 = intf_name + " is DOWN  , line protocol is DOWN "
	cmd2 = intf_name + " is administratively down  , line protocol is DOWN"
	if result.indexOf("Port-type") == -1 :
		add_problem('no_port')
		return 'warn'
	else if result.indexOf(cmd1) != -1 :
		add_problem('port_down')
		return 'warn'
	else if result.indexOf(cmd2) != -1 :
		add_problem('shut_down')
		return 'warn'
	end
	cmd = "show run int " + intf_name
	result = cli(cmd)
	print result
	results = response.split(/\r?\n/ig)
	if results.length < 3 :
		fourth()
	else if result.indexOf("no switchport") != -1 :
		add_problem('no_port')
		return 'warn'
	else if result.indexOf("spanning-tree bpdufilter enable") != -1 :
		add_problem('no_bpud')
		return 'warn'	
	else if result.indexOf("panning-tree bpduguard enable") != -1 :
		add_problem('BPUD')
		return 'warn'
	else :
		fourth()
	end	
end

return 'success'
