示例#1
0
        /// <summary>
        /// 异步Set操作
        /// </summary>
        /// <param name="lmtPdu"></param>
        /// <param name="requestId"></param>
        /// <param name="strIpAddr"></param>
        /// <returns></returns>
        public int SnmpSetAsync(CDTLmtbPdu lmtPdu, out long requestId, string strIpAddr)
        {
            requestId = 0;

            Log.Debug("========== SnmpGetSync() Start ==========");
            string msg = string.Format("pars: lmtPdu={0}, requestId={1}, strIpAddr={2}"
                                       , lmtPdu, requestId, strIpAddr);

            Log.Debug(msg);

            if (string.IsNullOrEmpty(strIpAddr))
            {
                Log.Error("参数strIpAddr为空");
                return(-1);
            }
            if (lmtPdu == null)
            {
                Log.Error("参数lmtPdu为空");
                return(-1);
            }

            // TODO: 根据基站ip获取Lmtor信息
            //LMTORINFO* pLmtorInfo = CDTAppStatusInfo::GetInstance()->GetLmtorInfo(remoteIpAddr);

            SnmpHelper snmp = m_SnmpAsync;

            if (null == snmp)
            {
                msg = string.Format("基站[{0}]的snmp连接不存在,无法下发snmp命令");
                Log.Error(msg);
                return(-1);
            }

            // TODO
            lmtPdu.setReqMsgType((int)PduType.Set);


            Pdu pdu = new Pdu();

            requestId = pdu.RequestId;
            // TODO:
            bool rs = LmtPdu2SnmpPdu(out pdu, lmtPdu, strIpAddr);

            if (!rs)
            {
                Log.Error("LmtPdu2SnmpPdu()转换错误");
            }

            SnmpAsyncResponse snmpCallback = new SnmpAsyncResponse(this.SnmpCallbackFun);

            rs = snmp.SetRequestAsync(pdu, snmpCallback);

            if (!rs)
            {
                Log.Error("SNMP 异步请求错误");
                return(-1);
            }

            return(0);
        }
示例#2
0
        /// <summary>
        /// SNMP请求回调方法
        /// </summary>
        /// <param name="result"></param>
        /// <param name="packet"></param>
        private void SnmpCallbackFun(AsyncRequestResult result, SnmpPacket packet)
        {
            string logMsg = string.Format("SNMP异步请求结果: AsyncRequestResult = {0}", result);

            Log.Info(logMsg);

            if (result == AsyncRequestResult.NoError)
            {
                SnmpV2Packet packetv2 = (SnmpV2Packet)packet;

                if (packetv2 == null)
                {
                    Log.Error("SNMP request error, response is null.");
                    return;
                }

                CDTLmtbPdu lmtPdu = new CDTLmtbPdu();
                bool       rs     = SnmpPdu2LmtPdu(packetv2, m_SnmpAsync.m_target, lmtPdu, 0, false);

                // TODO
                // 发消息
            }

            return;
        }
示例#3
0
 public void CopyPduInfo(ref CDTLmtbPdu pPdu)
 {
     if (null != m_lmtbPduInfo)
     {
         pPdu.m_lmtbPduInfo = m_lmtbPduInfo;
     }
 }
示例#4
0
        /// <summary>
        /// 同步Set操作
        /// </summary>
        /// <param name="lmtPdu"></param>
        /// <param name="requestId"></param>
        /// <param name="strIpAddr"></param>
        /// <param name="timeOut"></param>
        /// <returns></returns>
        public int SnmpSetSync(CDTLmtbPdu lmtPdu, out long requestId, string strIpAddr, long timeOut)
        {
            requestId = 0;

            Log.Debug("========== SnmpGetSync() Start ==========");
            string msg = string.Format("pars: lmtPdu={0}, requestId={1}, strIpAddr={2}, timeOut={3}"
                                       , lmtPdu, requestId, strIpAddr, timeOut);

            Log.Debug(msg);

            if (string.IsNullOrEmpty(strIpAddr))
            {
                Log.Error("参数strIpAddr为空");
                return(-1);
            }
            if (lmtPdu == null)
            {
                Log.Error("参数lmtPdu为空");
                return(-1);
            }

            // TODO: 根据基站ip获取Lmtor信息
            //LMTORINFO* pLmtorInfo = CDTAppStatusInfo::GetInstance()->GetLmtorInfo(remoteIpAddr);

            SnmpHelper snmp = m_SnmpSync;

            if (null == snmp)
            {
                msg = string.Format("基站[{0}]的snmp连接不存在,无法下发snmp命令");
                Log.Error(msg);
                return(-1);
            }

            // TODO
            lmtPdu.setReqMsgType((int)PduType.Set);


            Pdu pdu = new Pdu();
            // TODO:
            string community = "private";
            bool   rs        = LmtPdu2SnmpPdu(out pdu, lmtPdu, strIpAddr);

            SnmpV2Packet result = snmp.SetRequest(pdu);

            if (result == null)
            {
                Log.Error("SNMP request error, response is null.");
                return(-1);
            }

            rs = SnmpPdu2LmtPdu(result, snmp.m_target, lmtPdu, 0, false);

            return(0);
        }
示例#5
0
        public int Clone(CDTLmtbPdu pLmtbPdu)
        {
            if (null == pLmtbPdu)
            {
                return(-1);
            }

            if (this == pLmtbPdu)
            {
                return(0);
            }

            //TODO 赋值

            return(0);
        }
示例#6
0
        /// <summary>
        /// 同步Get操作
        /// </summary>
        /// <param name="lmtPdu"></param>
        /// <param name="requestId"></param>
        /// <param name="strIpAddr"></param>
        /// <param name="timeOut"></param>
        /// <returns></returns>
        public int SnmpGetSync(CDTLmtbPdu lmtPdu, out long requestId, string strIpAddr, long timeOut)
        {
            requestId = 0;

            Log.Debug("========== SnmpGetSync() Start ==========");
            string msg = string.Format("pars: lmtPdu={0}, requestId={1}, strIpAddr={2}, timeOut={3}"
                                       , lmtPdu, requestId, strIpAddr, timeOut);

            Log.Debug(msg);

            if (string.IsNullOrEmpty(strIpAddr))
            {
                Log.Error("strIpAddr is null");
                return(-1);
            }
            if (lmtPdu == null)
            {
                Log.Error("参数lmtPdu为空");
                return(-1);
            }

            // TODO: 根据基站ip获取Lmtor信息
            //LMTORINFO* pLmtorInfo = CDTAppStatusInfo::GetInstance()->GetLmtorInfo(remoteIpAddr);

            SnmpHelper snmp = m_SnmpSync;

            if (null == snmp)
            {
                msg = string.Format("基站[{0}]的snmp连接不存在,无法下发snmp命令");
                Log.Error(msg);
                return(-1);
            }

            /*
             * List<string> vbList = new List<string>();
             *
             * int vbCount = lmtPdu.VbCount();
             *
             * for (int i = 0; i < vbCount; i++)
             * {
             *  CDTLmtbVb lmtVb =  lmtPdu.GetVbByIndexEx(i);
             *  string oid = lmtVb.get_Oid();
             *  vbList.Add(oid);
             * }
             */

            // SnmpV2Packet result = snmp.GetRequest(vbList);

            Pdu  pdu;
            bool rs = LmtPdu2SnmpPdu(out pdu, lmtPdu, strIpAddr);

            if (!rs)
            {
                Log.Error("LmtPdu2SnmpPdu()转换错误");
                return(-1);
            }

            SnmpV2Packet result = snmp.GetRequest(pdu);

            if (result == null)
            {
                Log.Error("SNMP request error, response is null.");
                return(-1);
            }
            requestId = result.Pdu.RequestId;

            rs = SnmpPdu2LmtPdu(result, snmp.m_target, lmtPdu, 0, false);


            return(0);
        }
示例#7
0
        /// <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);
        }
示例#8
0
        /// <summary>
        /// 将LmtPdu转换为snmpPdu
        /// </summary>
        /// <param name="pdu"></param>
        /// <param name="lmtPdu"></param>
        /// <param name="strRemoteIp"></param>
        /// <returns></returns>
        private bool LmtPdu2SnmpPdu(out Pdu pdu, CDTLmtbPdu lmtPdu, string strRemoteIp)
        {
            pdu = new Pdu();

            string           strMibPreFix = "";
            string           strOid;
            string           strTmpOid;
            string           strValue;
            SNMP_SYNTAX_TYPE strSyntaxType;

            int lmtVbCount = lmtPdu.VbCount();

            for (int i = 0; i < lmtVbCount; i++)
            {
                CDTLmtbVb cDTLmtbVb = lmtPdu.GetVbByIndexEx(i);

                strTmpOid = cDTLmtbVb.get_Oid();

                strSyntaxType = cDTLmtbVb.get_Syntax();

                strValue = cDTLmtbVb.get_Value();


                if (SNMP_SYNTAX_TYPE.SNMP_SYNTAX_OCTETS == strSyntaxType)
                {
                    /*对于像inetipAddress和DateandTime需要做一下特殊处理,把内存值转换为显示文本*/
                    // CString strNodeType = GetNodeTypeByOIDInCache(csIpAddr, strOID, strMIBPrefix);
                    string strNodeType = "";

                    if ("DateandTime".Equals(strNodeType))
                    {
                        // TODO
                        // strValue = strValue;
                    }
                    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))
                    {
                    }
                }

                // TODO

                Vb vb = new Vb(new Oid(strTmpOid));

                SetVbValue(ref vb, strSyntaxType, strValue);

                pdu.VbList.Add(vb);
            } // end for


            return(true);
        }
示例#9
0
        /// <summary>
        /// 执行一条类型为Get的同步操作命令
        /// </summary>
        /// <param name="cmdName"></param>
        /// <param name="requestId"></param>
        /// <param name="strIndex"></param>
        /// <param name="strIpAddr"></param>
        /// <param name="lmtPdu"></param>
        /// <returns></returns>
        public int CmdGetSync(string cmdName, out long requestId, string strIndex
                              , string strIpAddr, ref CDTLmtbPdu lmtPdu, bool isPrint = false
                              , bool needCheck = false, long timeOut = 0)
        {
            requestId = 0;

            Log.Info("CmdGetSync() start");
            string msg = string.Format("cmdName={0},requestId={1},strIndex={2}, strIpAddr={3}"
                                       , cmdName, requestId, strIndex, strIpAddr);

            Log.Info(msg);

            if (string.IsNullOrEmpty(cmdName) || string.IsNullOrEmpty(strIpAddr))
            {
                return(-1);
            }

            // TODO: 数据库先打桩
            string strMibList = "";

            // test
            if (cmdName.Equals("aaa"))
            {
                strMibList = "100.1.9.1.1|100.1.9.1.2";
            }
            else
            {
                strMibList = "100.1.2.1.1.1.5.3";
            }

            if (string.IsNullOrEmpty(strMibList))
            {
                return(-1);
            }

            // 转换为oid数组
            string[] mibList = StringToArray(strMibList);
            if (mibList == null)
            {
                return(-1);
            }


            // TODO : bNeedCheck
            if (needCheck)
            {
            }

            // TODO:
            // 获取oid的前缀
            string        strPreFixOid = "1.3.6.1.4.1.5105";
            StringBuilder sbOid        = new StringBuilder();

            foreach (string v in mibList)
            {
                sbOid.Clear();
                sbOid.AppendFormat("{0}.{1}{2}", strPreFixOid, v, strIndex);
                CDTLmtbVb vb = new CDTLmtbVb();
                vb.set_Oid(sbOid.ToString());
                lmtPdu.AddVb(vb);
            }

            lmtPdu.setCmdName(cmdName);
            lmtPdu.setPrintId(isPrint);
            lmtPdu.setSyncId(true);

            // 根据ip获取当前基站的snmp实例
            LmtbSnmpEx lmtbSnmpEx = DTLinkPathMgr.GetInstance().getSnmpByIp(strIpAddr);
            int        rs         = lmtbSnmpEx.SnmpGetSync(lmtPdu, out requestId, strIpAddr, timeOut);

            if (rs != 0)
            {
                Log.Error("执行lmtbSnmpEx.SnmpGetSync()方法错误");
            }

            return(0);
        }
示例#10
0
        /// <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 CmdSetAsync(String cmdName, out long requestId, Dictionary <string, string> name2Value
                               , string strIndex, string strIpAddr, bool isPrint = false, bool needCheck = false)
        {
            requestId = 0;
            int rs = 0;

            CDTLmtbPdu lmtPdu = new CDTLmtbPdu();

            // 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);
            } // end foreach

            lmtPdu.setCmdName(cmdName);
            lmtPdu.setPrintId(isPrint);
            lmtPdu.setSyncId(true);

            LmtbSnmpEx lmtbSnmpEx = DTLinkPathMgr.GetInstance().getSnmpByIp(strIpAddr);

            rs = lmtbSnmpEx.SnmpSetAsync(lmtPdu, out requestId, strIpAddr);
            if (rs != 0)
            {
                Log.Error("执行lmtbSnmpEx.SnmpSetAsync()方法错误");
            }

            return(rs);
        }