{
    "name"  : "检查光强是否异常",
    "problems" : {
        "manual" : {
            "desc"      : ["【问题】检测到端口${0}上光强异常："
                           ,"\t最大发送光强：${1}\t最小发送光强：${2}\t实际发送光强：${3}\t最大接收光强：${4}\t最小接收光强：${5}\t实际接收光强：${7}"],
            "advice"    : ["【建议】"
                           ,"\t1.建议确认端口上是否有插入光纤或者光纤是否插紧。"
                           ,"\t2.建议确认端口上的光纤与光模块是否损坏，确认方法是：分别更换光纤与光模块进行测试。"
                           ,"\t3.建议确认端口上光纤的距离是否超过最大部署距离，如果超过，建议在2台设备中间使用小交换机作为中转设备，保证2个端口间的光纤距离小于最大部署距离。或者调整设备位置或网线部署，使网线长度小于最大部署距离。"
                           ,"\t4.确认端口与对端设备的波长是否一致，如果不一致，建议更换光纤或者光模块，使得2个端口波长一致。确认方法为：在设备上使用CLI命令：show interface ${7} transceiver diagnosis"
                           ,"\t5.确认使用的光纤类型是否正确：小于100米的建议使用多模光纤，大于100米的建议使用单模光纤。其中光纤上有标明为G 651的是多模光纤，标明G 652的是常规单模光纤。"
                           ,"\t6.确认光纤收发端是否插反，确认方法：拔出光纤，对调光纤的收发端，再插入。"]
        }
    },
    "fiber_model_info" : {
        "XG-SFP-SR-MM850":[850,-1,-5,0.5,-7.5,300],
        "XG-SFP-LR-SM1310":[1310,0.5,-8.2,0.5,-10.3,10000],
        "XG-SFP-ER-SM1550":[1550,4,-4.7,-1,-11.3,40000],
        "XG-SFP-ZR-SM1550":[1550,4,0,-7,-24,80000],
        "10GBASE-SR-XFP":[850,-1,-5,0.5,-7.5,300],
        "10GBASE-LR-XFP":[1310,0.5,-4.8,0.5,-10.3,10000],
        "10GBASE-ER-XFP":[1550,2,-1,-1,-11.3,40000],
        "XG-XFP-ZR80-SM1550":[1550,4,0,-7,-23,80000],
        "FE-SFP-LX-MM1310":[1310,-14,-22,-14,-30,2000],
        "FE-SFP-LH15-SM1310":[1310,-8,-15,-8,-28,15000],
        "Mini-GBIC-SX":[850,-3,-9.5,0,-17,550],
        "Mini-GBIC-LX":[1310,-3,-9.5,-3,-20,10000],
        "GE-eSFP-SX-MM850":[850,-3,-9.5,-3,-17,550],
        "GE-eSFP-LX-SM1310":[1310,-3,-9.5,-3,-20,10000],
        "Mini-GBIC-LH40":[1310,3,-2,-3,-22,40000],
        "Mini-GBIC-ZX50":[1550,0,-5,-3,-22,50000],
        "Mini-GBIC-ZX80":[1550,4.7,0,-3,-22,80000],
        "Mini-GBIC-ZX100":[1550,5,0,-9,-30,100000],
        "SDH155-SFP-SX-MM850":[850,-4,-10,0,-25,500],
        "SDH155-SFP-SX-MM1310":[1310,-14,-20,-14,-30,2000],
        "SDH155-SFP-LH15-SM1310":[1310,-8,-15,-8,-28,15000],
        "SDH155-SFP-LH40-SM1310":[1310,0,-5,-8,-34,40000],
        "SDH155-SFP-LH80-SM1310":[1310,0,-5,-8,-34,80000],
        "40G-QSFP-SR-MM850":[">840<860",2.4,-7.6,2.4,-9.5,150],
        "40G-QSFP-LR4-SM1310":[">1264.5<1277.5>1284.5<1297.5>1304.5<1317.5>1324.5<1337.5",2.3,-7,2.3,-13.7,10000]
    }
}
---------------------------code-------------------------------
#检查光强是否异常
param = {}
param["status"] = precondition('status')
param["type"] = precondition('type')
param["name"] = precondition('name')
fiber_model_info = get_configure_value('fiber_model_info')
_fiber_rx = undefined
_fiber_tx = undefined
_fiber_model_spec = undefined
attributes = {}

def jumpOut(proType): 
    name = param["name"]
    fiber1 = _fiber_model_spec[1]
    fiber2 = _fiber_model_spec[2]
    fiber3 = _fiber_model_spec[3]
    fiber4 = _fiber_model_spec[4]
    if proType == "manual":
        addProblem("manual",[name, fiber1, fiber2, _fiber_tx, fiber3, fiber4, _fiber_rx, name])
        return "warn"
    end
    return 'success'
end

def processFiber_model_info(attributes, fiber_model_info):
    if attributes['fiber_model'] != undefined :
        if fiber_model_info[attributes['fiber_model']] != undefined:
            index = attributes['fiber_model']
            attributes['fiber_model_spec'] = fiber_model_info[index]
        end
    end
end

def start():
    name = param["name"]
    cmd = format('show interface %s transceiver diagnosis', name)
    response = cli(cmd)
    if isInvalidCli(response):
        add_reason('无效指令：show interface %s transceiver diagnosis')
        return 'skip' 
    end
    if response.match(/TX power.*[\r\n]+(?:\S+[ ]+){3}(\S*\(ok\)\S*\s+){2}/i) != null: 
        return "success"
    end
    _fiber_rx = response.match(/TX power.*[\r\n]+(?:\S+[ ]+){3}([0-9\-.]+)/i) 
    _fiber_tx = response.match(/TX power.*[\r\n]+(?:\S+[ ]+){4}([0-9\-.]+)/i)
    if _fiber_rx.length > 1 && _fiber_tx.length > 1:
        _fiber_rx = _fiber_rx[1]
        _fiber_tx - _fiber_tx[1]
        processFiber_model_info(attributes, fiber_model_info)
        if attributes['fiber_model_spec'] != undefined:
            _fiber_model_spec = attributes['fiber_model_spec']
        else:
            _fiber_model_spec = ['-','-','-','-','-','-']
        end
        res = jumpOut("manual")
        return res
    else:
        return "skip"    
    end
end

key = "checkFiberInsert_power"
if get_share_value(key) != true:
    return "skip"
end
start()


