/// <summary> /// 将snmp类型的pdu转换为LmtSnmp的pdu /// </summary> /// <param name="snmpPackage"></param> /// <param name="target"></param> /// <param name="lmtPdu"></param> /// <param name="reason"></param> /// <param name="isAsync"></param> private bool SnmpPdu2LmtPdu(SnmpV2Packet snmpPackage, UdpTarget target , CDTLmtbPdu lmtPdu, int reason, bool isAsync) { string logMsg; if (lmtPdu == null) { Log.Error("参数[lmtPdu]为空"); return(false); } // TODO // stru_LmtbPduAppendInfo appendInfo; logMsg = string.Format("snmpPackage.Pdu.Type = {0}", snmpPackage.Pdu.Type); Log.Debug(logMsg); logMsg = string.Format("PduType.V2Trap={0}", PduType.V2Trap); // TODO if (snmpPackage.Pdu.Type != PduType.V2Trap) // Trap { } else { } lmtPdu.Clear(); lmtPdu.set_LastErrorIndex(snmpPackage.Pdu.ErrorIndex); lmtPdu.set_LastErrorStatus(snmpPackage.Pdu.ErrorStatus); lmtPdu.SetRequestId(snmpPackage.Pdu.RequestId); // ip and port IPAddress srcIpAddr = target.Address; int port = target.Port; lmtPdu.set_SourceIp(srcIpAddr.ToString()); lmtPdu.set_SourcePort(port); lmtPdu.set_Reason(reason); //lmtPdu.SetPduType(snmpPackage.Pdu.Type); // TODO /* * LMTORINFO* pLmtorInfo = CDTAppStatusInfo::GetInstance()->GetLmtorInfo(csIpAddr); * if (pLmtorInfo != NULL && pLmtorInfo->m_isSimpleConnect && pdu.get_type() == sNMP_PDU_TRAP) * { * Oid id; * pdu.get_notify_id(id); * CString strTrapOid = id.get_printable(); * if (strTrapOid != "1.3.6.1.4.1.5105.100.1.2.2.3.1.1") * { * //如果是简单连接网元的非文件传输结果事件,就不要往上层抛送了 * return FALSE; * } * } */ //如果是错误的响应,则直接返回 if (lmtPdu.get_LastErrorStatus() != 0 || reason == -5) { return(true); } // 转换vb // 对于Trap消息,我们自己额外构造两个Vb,用来装载时间戳和trap Id if (snmpPackage.Pdu.Type == PduType.V2Trap) // Trap { // TODO: } foreach (Vb vb in snmpPackage.Pdu.VbList) { logMsg = string.Format("ObjectName={0}, Type={1}, Value={2}" , vb.Oid.ToString(), SnmpConstants.GetTypeName(vb.Value.Type), vb.Value.ToString()); Log.Debug(logMsg); CDTLmtbVb lmtVb = new CDTLmtbVb(); lmtVb.set_Oid(vb.Oid.ToString()); // SnmpConstants.GetSyntaxObject(AsnType.OCTETSTRING); // SnmpConstants.GetTypeName(vb.Value.Type); // TODO // lmtVb.set_Syntax(vb.Value.GetType()); // TODO:不确定对不对??????? if (AsnType.OCTETSTRING == vb.Value.Type) { /*对于像inetipAddress和DateandTime需要做一下特殊处理,把内存值转换为显示文本*/ // CString strNodeType = GetNodeTypeByOIDInCache(csIpAddr, strOID, strMIBPrefix); string strNodeType = ""; if ("DateandTime".Equals(strNodeType)) { } else if ("inetaddress".Equals(strNodeType)) { } else if ("MacAddress".Equals(strNodeType)) { } else if ("Unsigned32Array".Equals(strNodeType)) { } else if ("Integer32Array".Equals(strNodeType) || "".Equals(strNodeType)) { } else if ("MncMccType".Equals(strNodeType)) { } } string value = vb.Value.ToString(); lmtVb.set_Value(value); lmtPdu.AddVb(lmtVb); } // end foreach //如果得到的LmtbPdu对象里的vb个数为0,说明是是getbulk响应,并且没有任何实例 //为方便后面统一处理,将错误码设为资源不可得 if (lmtPdu.VbCount() == 0) { // TODO: SNMP_ERROR_RESOURCE_UNAVAIL lmtPdu.set_LastErrorStatus(13); lmtPdu.set_LastErrorIndex(1); } return(true); }
/// <summary> /// 执行一条类型为Set的同步操作命令 /// </summary> /// <param name="cmdName"></param> /// <param name="requestId"></param> /// <param name="name2Value"></param> /// <param name="strIndex"></param> /// <param name="strIpAddr"></param> /// <param name="lmtPdu"></param> /// <param name="isPrint"></param> /// <param name="needCheck"></param> /// <param name="timeOut"></param> /// <returns></returns> public int CmdSetSync(String cmdName, out long requestId, Dictionary <string, string> name2Value , string strIndex, string strIpAddr, ref CDTLmtbPdu lmtPdu, bool isPrint = false , bool needCheck = false, long timeOut = 0) { requestId = 0; int rs = 0; if (lmtPdu == null) { return(-1); } lmtPdu.Clear(); // TODO: 从数据库获取命令对应的oid string strMibList = "100.1.2.2.2.1.2|100.1.2.2.2.1.3|100.1.2.2.2.1.4|100.1.2.2.2.1.5|100.1.2.2.2.1.6|100.1.2.2.2.1.7"; if (string.IsNullOrEmpty(strMibList)) { return(-1); } // 将字符串转换为oid数组 string[] mibList = StringToArray(strMibList); if (mibList == null) { return(-1); } string strIndexFmt = string.Format(".{0}", strIndex.Trim('.')); // TODO if (needCheck) { } // 获取oid的前缀 string strPreFixOid = "1.3.6.1.4.1.5105"; StringBuilder sbOid = new StringBuilder(); string strMibValue; string strTyep; string strMibName; foreach (string v in mibList) { sbOid.Clear(); sbOid.AppendFormat("{0}.{1}{2}", strPreFixOid, v, strIndexFmt); CDTLmtbVb vb = new CDTLmtbVb(); vb.set_Oid(sbOid.ToString()); strTyep = ""; strMibName = ""; strMibValue = ""; // TODO: 从数据库中获取oid的名称和数据类型 MibNodeInfoTest mibNodeInfoTest = GetMibNodeInfoByOID(strIpAddr, v); strTyep = mibNodeInfoTest.strType; strMibName = mibNodeInfoTest.strMibName; if (name2Value.ContainsKey(strMibName)) { strMibValue = name2Value[strMibName]; } else { continue; } vb.set_Value(strMibValue); // TODO: vb.set_Syntax(GetAsnTypeByMibType(strTyep)); lmtPdu.AddVb(vb); } lmtPdu.setCmdName(cmdName); lmtPdu.setPrintId(isPrint); lmtPdu.setSyncId(true); LmtbSnmpEx lmtbSnmpEx = DTLinkPathMgr.GetInstance().getSnmpByIp(strIpAddr); rs = lmtbSnmpEx.SnmpSetSync(lmtPdu, out requestId, strIpAddr, timeOut); if (rs != 0) { Log.Error("执行lmtbSnmpEx.SnmpGetSync()方法错误"); } return(rs); }