/// <summary> /// 删除对应管理站的对应索引的命令 /// </summary> /// <param name="peerAddress">对端IP</param> /// <param name="nIndex">对应索引</param> /// <returns></returns> public bool DelTraceTask(string peerAddress, int nIndex) { SnmpAgentInfo snmpAgent = GetSnmpAgent(peerAddress, 2, 2000); string index = string.Format(".{0}", nIndex); int count = ListDelTrace.Count; Vb[] vbs = new Vb[count]; int curNum = 0; foreach (TraceItem item in ListDelTrace) { vbs[curNum] = new Vb(new Oid(CommonOid + item.Oid + index), new Integer32(6)); curNum++; } IEnumerator <Vb> getRes = null; try { getRes = SnmpSessionHelper.Instance.Set(snmpAgent, vbs); return(true); } catch (SnmpErrorStatusException snmpex) { log.Error(string.Format("启动网元{0}的删除命令失败!, 原因为:{1}", snmpAgent.PeerAddress, snmpex.Message)); } catch (System.Exception ex) { log.Error(string.Format("启动网元{0}的删除命令失败!, 原因为:{1}", snmpAgent.PeerAddress, ex.Message)); } return(false); }
/// <summary> /// 下发Get命令 /// </summary> /// <param name="peerAddress">对端IP</param> /// <param name="nIndex">对应索引</param> /// <returns></returns> public IEnumerator <Vb> GetTraceTask(string peerAddress, int nIndex) { SnmpAgentInfo snmpAgent = GetSnmpAgent(peerAddress, 2, 2000); string index = string.Format(".{0}", nIndex); int count = ListGetTrace.Count; Vb[] vbs = new Vb[count]; int curNum = 0; foreach (TraceItem item in ListGetTrace) { vbs[curNum] = new Vb(CommonOid + item.Oid + index); curNum++; } IEnumerator <Vb> getRes = null; try { getRes = SnmpSessionHelper.Instance.Get(snmpAgent, vbs); return(getRes); } catch (SnmpErrorStatusException snmpex) { log.Error(string.Format("查询网元{0}MIB版本号失败!, 原因为:{1}", snmpAgent.PeerAddress, snmpex.Message)); } catch (System.Exception ex) { log.Error(string.Format("查询网元{0}MIB版本号失败!, 原因为:{1}", snmpAgent.PeerAddress, ex.Message)); } return(null); }
/// <summary> /// 循环查询没有使用的index下发 /// </summary> /// <param name="peerAddress"></param> /// <returns></returns> public int AddTaskWithOutIndex(string peerAddress) { Random random = new Random(); IEnumerator <Vb> getRes = null; SnmpAgentInfo snmpAgent = GetSnmpAgent(peerAddress, 2, 2000); //Vb[] vbs = new Vb[count]; int nIndex = random.Next(64); getRes = GetTraceTask(peerAddress, nIndex); while (null != getRes) { nIndex = random.Next(64); getRes = GetTraceTask(peerAddress, nIndex); } if (AddTraceTask(peerAddress, nIndex)) { return(nIndex); } return(-1); }
public SnmpAgentInfo GetSnmpAgent(string peerAddress, int retryNum, int iterval) { SnmpAgentInfo snmpAgent = new SnmpAgentInfo(); snmpAgent.PeerAddress = IPAddress.Parse(peerAddress); snmpAgent.Community = "private"; snmpAgent.RetryTime = retryNum; /*失败重复次数*/ snmpAgent.TimeOut = iterval; /*重发间隔时间*/ return(snmpAgent); }
/// <summary> /// 验证远端的信息是否正确 /// </summary> /// <param name="peerIP"></param> /// <param name="strCommunity"></param> /// <returns></returns> protected bool ValidPeer(SnmpAgentInfo peer) { if (peer.PeerAddress == IPAddress.None || peer.PeerAddress == IPAddress.Any) { throw new SnmpException("IPAddress is not valid."); } if (peer.Community.Length < 1 || peer.Community.Length > 50) { throw new SnmpException("Community is not valid."); } if (peer.Version != SnmpVersion.Ver1 && peer.Version != SnmpVersion.Ver2) { throw new SnmpInvalidVersionException("Support SNMP version 1 and 2 only."); } return(true); }
/// <summary> /// SNMP Operation Execute /// </summary> /// <example>Set operation in SNMP version 1: /// <code> /// </code> /// /// /// </example> /// <param name="version">SNMPAgentInfo. 对端SNMP Agent相关配置信息</param> /// <param name="pdu">PduType. 要执行的操作类型:Get/Set/GetNext/GetBulk</param> /// <param name="pdu">Vb[]. 操作参数</param> /// <returns>Result of the SNMP request in a IEnumerator<vb> </returns> public IEnumerable <Vb> Excute(SnmpAgentInfo peer, PduType pduType, IEnumerable <Vb> vbs) { //验证对端地址信息是否正确 if (!this.ValidPeer(peer)) { Log.Error("SnmpAgentInfo校验失败!"); return(null); } //组装PDU Pdu pdu = this.PacketPdu(pduType, vbs); //GetBulk包需要设置NonRepeaters和MaxRepetitions两个字段 if (PduType.GetBulk == pdu.Type) { pdu.NonRepeaters = peer.NonRepeaters; pdu.MaxRepetitions = peer.MaxRepetitions; } using (var _target = new UdpTarget(peer.PeerAddress, peer.Port, peer.TimeOut, peer.RetryTime)) { SnmpPacket result = null; try { var param = new AgentParameters(peer.Version, new OctetString(peer.Community)); result = _target.Request(pdu, param); } catch (Exception ex) { Log.Error(ex.Message); throw; } if (result != null) { if (result.Pdu.ErrorStatus == 0) { return(result.Pdu); } throw new SnmpErrorStatusException("Agent responded with an error", result.Pdu.ErrorStatus, result.Pdu.ErrorIndex); } } return(null); }
/// <summary> /// 下发添加命令 /// </summary> /// <param name="peerAddresss"></param> /// <param name="nIndex"></param> /// <returns></returns> public bool AddTraceTask(string peerAddresss, int nIndex) { SnmpAgentInfo snmpAgent = GetSnmpAgent(peerAddresss, 2, 2000); string index = string.Format(".{0}", nIndex); int count = ListAddTrace.Count; Vb[] vbs = new Vb[count]; int curNum = 0; TraceItem inter = new TraceItem(); foreach (TraceItem item in ListAddTrace) { if ("跟踪任务类型" == item.DisplayName) { inter = item; } if ("跟踪管理对象OID" == item.DisplayName) { if (null != inter) { if ("" == item.Value) { item.Value = "0"; } string curValue = CommonOid + SpcialDeal(inter.Value) + item.Value; vbs[curNum] = new Vb(new Oid(CommonOid + item.Oid + index), new Oid(curValue)); } else { } } else { if ("行状态" == item.DisplayName) { vbs[curNum] = new Vb(new Oid(CommonOid + item.Oid + index), new Integer32(4)); } else { vbs[curNum] = new Vb(new Oid(CommonOid + item.Oid + index), item.GetValue()); } } curNum++; } IEnumerator <Vb> getRes = null; try { getRes = SnmpSessionHelper.Instance.Set(snmpAgent, vbs); return(true); } catch (SnmpErrorStatusException snmpex) { log.Error(string.Format("启动网元{0}的删除命令失败!, 原因为:{1}", snmpAgent.PeerAddress, snmpex.Message)); } catch (System.Exception ex) { log.Error(string.Format("启动网元{0}的删除命令失败!, 原因为:{1}", snmpAgent.PeerAddress, ex.Message)); } return(false); }
/// <summary> /// The get next. /// SNMP GET-NEXT request /// </summary> /// <param name="peer"> /// The peer. /// </param> /// <param name="vbs"> /// The vbs. /// </param> /// <returns> /// Result of the SNMP request in a dictionary format with Oid => AsnType values/>. /// </returns> public IEnumerable <Vb> GetNext(SnmpAgentInfo peer, Vb[] vbs) { return(this.Excute(peer, PduType.GetNext, vbs)); }
/// <summary> /// SNMP SET request /// </summary> /// <example> /// Set operation in SNMP version 1: /// <code> /// String snmpAgent = "10.10.10.1"; /// String snmpCommunity = "private"; /// SimpleSnmp snmp = new SimpleSnmp(snmpAgent, snmpCommunity); /// // Create a request Pdu /// List<Vb> vbList = new List<Vb>(); /// Oid setOid = new Oid("1.3.6.1.2.1.1.1.0"); // sysDescr.0 /// OctetString setValue = new OctetString("My personal toy"); /// vbList.Add(new Vb(setOid, setValue)); /// Dictionary<Oid, AsnType> result = snmp.Set(SnmpVersion.Ver1, list.ToArray()); /// if( result == null ) { /// Console.WriteLine("Request failed."); /// } else { /// Console.WriteLine("Success!"); /// } /// </code> /// To use SNMP version 2, change snmp.Set() method call first parameter to SnmpVersion.Ver2. /// </example> /// <param name="peer"> /// The peer. /// </param> /// <param name="vbs"> /// Vb array containing Oid/AsnValue pairs for the SET operation /// </param> /// <returns> /// Result of the SNMP request in a dictionary format with Oid => AsnType values /// </returns> public IEnumerable <Vb> Set(SnmpAgentInfo peer, IEnumerable <Vb> vbs) { return(this.Excute(peer, PduType.Set, vbs)); }