
{
    "name"  : "端口是否正确插入光模块与光纤",
    "problems" : {
        "absent" : {
            "desc"      : "【问题】端口${0}未插入光模块",
            "advice"    : "【建议】插入光模块"
        },
        "giwith10g" : {
            "desc"      : "【问题】端口${0}为千兆光口，但是实际插入的是万兆光模块",
            "advice"    : "【建议】建议更换为千兆光模块"
        },
        "tengiwith100" : {
            "desc"      : "【问题】端口${0}为万兆光口，但是实际插入的是百兆光模块",
            "advice"    : "【建议】建议更换为万兆光模块"
        },
		"is_ruijie"		:{
			"desc"		: "【问题】使用非锐捷光模块，可能导致端口状态异常",
			"advice"	: "【建议】建议更换锐捷光模块"
		},
		"is_RXorTX"		:{
			"desc"		: "【问题】检测到端口上光强异常",
			"advice"	: 	["【建议】"
							,"\t1.建议确认端口上是否有插入光纤或者光纤是否插紧。"
							,"\t2.建议确认端口上的光纤与光模块是否损坏，确认方法是：分别更换光纤与光模块进行测试。"
							,"\t3.建议确认端口上光纤的距离是否超过最大部署距离，如果超过，建议在2台设备中间使用小交换机作为中转设备,保证2个端口间的光纤距离小于最大部署距离。或者调整设备位置或网线部署，使网线长度小于最大部署距离。"
							,"\t4.确认端口与对端设备的波长是否一致，如果不一致，建议更换光纤或者光模块，使得2个端口波长一致。确认方法为：在设备上使用CLI命令： show interface端口号transceiver diagnosis。"
							,"\t5.确认使用的光纤类型是否正确：小于100米的建议使用多模光纤，大于100米的建议使用单模光纤。其中光纤上有标明为G 651的是多模光纤，标明G 652的是常规单模光纤。"
							,"\t6.确认光纤收发端是否插反，确认方法：拔出光纤，对调光纤的收发端，再插入。"
									]
		}	
				}
}
---------------------------code---------------------------
#端口是否正确插入光模块与光纤
RXword = undefined
TXword = undefined
_fiber_model = undefined
_fiber_model1 = undefined
matchStr1 = undefined
matchStr2 = undefined 
_MFS_fiber_max_dis = undefined
_OM1_fiber_max_dis = undefined
_OM2_fiber_max_dis = undefined
_OM3_fiber_max_dis = undefined
_fiber_len = undefined
_fiber_type = undefined 
_RXlight_intensity = undefined
_TXlight_intensity = undefined
_DDM_support = "true"
_Records = {}
_Records["DDM_support"]= _DDM_support
param = {}
param["status"] = precondition('status')
param["type"] = precondition('type')
param["name"] = precondition('name')

def jumpOut(proType):
    name = param["name"]
    if proType == 'absent':
        add_problem('absent',[name])
        return 'warn'
    end
    if proType == 'giwith10g':
        add_problem('giwith10g',[name])
        return 'warn'
    end
    if proType == 'tengiwith100':
        add_problem('tengiwith100',[name])
        return 'warn'
    end
	if proType == 'is_ruijie':
		add_problem('is_ruijie',[name])
		return 'warn'
	end
	if proType == 'is_RXorTX':
		add_problem('is_RXorTX',[name])
		return 'warn'
	end
    return 'success'
end

def start():
	name = param["name"]
	cmd = format('show interface %s transceiver', name)
	response = cli(cmd)
	matchStr = response.match(/[ ]*Transceiver Type[ ]*:[ ]*(\S+)/i)
	if matchStr != null:
		_fiber_model = matchStr[0]
	end

	response1 = cli('show version')
	matchStr1 = response1.split(/\r?\n/)
	pri=0
	while pri<matchStr1.length:
		Dait=matchStr1[pri]
		if Dait.match(/[ ]*System description[ ]*:[ ]*/):
			matchStr1=Dait
			break
		end
		pri =pri+1
	end
	if matchStr1 != undefined:
		_fiber_model1 = matchStr1
	end

	cmd = format('show interface %s transceiver diagnosis', name)
	response2 = cli(cmd)
	if response2 != null:
		matchStr2 = response2.split(/\r?\n/)
	end
	t=1	
	while t < matchStr2.length:
		cs = matchStr2[t-1]
		css = matchStr2[t]
		if cs.indexOf("RX power") != -1  &&  cs.indexOf("TX power") != -1:
			Rrep=cs.indexOf("RX power")
			Trep=cs.indexOf("TX power")
			RXstring=css.substring(Rrep)
			TXstring=css.substring(Trep)
			rex=/-?\w\S+/
			RXword=RXstring.match(rex)
			TXword=TXstring.match(rex)
			break
		end
		t=t+1
	end

	if response.match("the transceiver is absent") == null:
		matchaaStr_ = response.split(/\r?\n/)
		j=1
		while j<matchaaStr_.length:
			rel=matchaaStr_[j-1]
			pes = matchaaStr_[j]
			j=j+1
			if rel.match(/[ ]*Transceiver[ ]*Type[ ]*:[ ]*/i) != null:
				patt =rel.match(/[ ]*Transceiver[ ]*Type[ ]*:[ ]*/i)
				index = patt.length
				_fiber_type = rel.substring(index)
				_Records["fiber_type"] = _fiber_type
			end
			if rel.match(/[ ]*wavelength\(nm\)[ ]*:[ ]*/i) != null:
				_fiber_len = rel.match(/\d+/g)
				_Records["fiber_len"]= _fiber_len
			end
			if rel.match(/[ ]*SMF[ ]*fiber[ ]*/i) !=  null:
				_MFS_fiber_max_dis = pes.match(/\d+\.?\d+k?m/i)
				_Records["MFS_fiber_max_dis"] = _MFS_fiber_max_dis
			end
			if rel.match(/um[ ]*OM1[ ]*fiber[ ]*/i) !=  null:
				_OM1_fiber_max_dis = pes.match(/\d+\.?\d+k?m/i)
				_Records["OM1_fiber_max_dis"] = _OM1_fiber_max_dis
			end
			if rel.match(/um[ ]*OM2[ ]*fiber[ ]*/i) !=  null:
				_OM2_fiber_max_dis = pes.match(/\d+\.?\d+k?m/i)
				_Records["OM2_fiber_max_dis"] = _OM2_fiber_max_dis
			end
			if rel.match(/um[ ]*OM3[ ]*fiber[ ]*/i) !=  null:
				_OM3_fiber_max_dis = pes.match(/\d+\.?\d+k?m/i)
				_Records["OM3_fiber_max_dis"] = _OM3_fiber_max_dis
			end
		end
	else:
		res=jumpOut('absent')
		return res
	end
	
	if name.search(/gigabitethernet/i) == -1:
		if name.search(/tengigabitethernet/i) == -1 &&  _fiber_model1.search(/Switch[ ]*\(s2910\)/i) != -1:      
			if _fiber_model.search(/unknow/i) != -1:
				res=jumpOut('is_ruijie')
				return res
				if response2.match(/This module doesn't support DDM/i) != null:
					_DDM_support = "false"
					_Records["DDM_support"]= _DDM_support
				end
				if RXword.search(/ok/i)!=-1 && TXword.search(/ok/i)!=-1:
					_RXlight_intensity = RXword.match(/-?\d+\.?\d+/)
					_TXlight_intensity = TXword.match(/-?\d+\.?\d+/)
					_Records["RXlight_intensity"] = _RXlight_intensity
					_Records["TXlight_intensity"] = _TXlight_intensity
				else:                                                                     
					_RXlight_intensity = RXword.match(/-?\d+\.?\d+/)
					_TXlight_intensity = TXword.match(/-?\d+\.?\d+/)
					_Records["RXlight_intensity"] = _RXlight_intensity
					_Records["TXlight_intensity"] = _TXlight_intensity
					res=jumpOut('is_RXorTX')
					return res
				end
			end
		end
	end

	if _fiber_model.search(/10GBASE/i) != -1:
		res=jumpOut('giwith10g')
		return res
		if _fiber_model.search(/unknow/i) != -1:
			res=jumpOut('is_ruijie')
			return res
			if response2.match(/This module doesn't support DDM/i) != null:
				_DDM_support = "false"
				_Records["DDM_support"]= _DDM_support
			end
			if RXword.search(/ok/i)!=-1 && TXword.search(/ok/i)!=-1:
				_RXlight_intensity = RXword.match(/-?\d+\.?\d+/)
				_TXlight_intensity = TXword.match(/-?\d+\.?\d+/)
				_Records["RXlight_intensity"] = _RXlight_intensity
				_Records["TXlight_intensity"] = _TXlight_intensity
			else:                                                                     
				_RXlight_intensity = RXword.match(/-?\d+\.?\d+/)
				_TXlight_intensity = TXword.match(/-?\d+\.?\d+/)
				_Records["RXlight_intensity"] = _RXlight_intensity
				_Records["TXlight_intensity"] = _TXlight_intensity
				res=jumpOut('is_RXorTX')
				return res
			end
		end
	end

	if _fiber_model.search(/100BASE/i) != -1: 
		res=jumpOut('tengiwith100')	
		return res
		if _fiber_model.search(/unknow/i) != -1:
			res=jumpOut('is_ruijie')
			return res
			if response2.match(/This module doesn't support DDM/i) != null:
				_DDM_support = "false"
				_Records["DDM_support"]= _DDM_support
			end
			if RXword.search(/ok/i)!=-1 && TXword.search(/ok/i)!=-1:
				_RXlight_intensity = RXword.match(/-?\d+\.?\d+/)
				_TXlight_intensity = TXword.match(/-?\d+\.?\d+/)
				_Records["RXlight_intensity"] = _RXlight_intensity
				_Records["TXlight_intensity"] = _TXlight_intensity
			else:                                                                     
				_RXlight_intensity = RXword.match(/-?\d+\.?\d+/)
				_TXlight_intensity = TXword.match(/-?\d+\.?\d+/)
				_Records["RXlight_intensity"] = _RXlight_intensity
				_Records["TXlight_intensity"] = _TXlight_intensity
				res=jumpOut('is_RXorTX')
				return res
			end
		end
	end	
end

start()








