Walk() public method

SNMP WALK operation
When using SNMP version 1, walk is performed using GET-NEXT calls. When using SNMP version 2, walk is performed using GET-BULK calls.
public Walk ( SnmpVersion version, string rootOid ) : AsnType>.Dictionary
version SnmpVersion SNMP protocol version. Acceptable values are SnmpVersion.Ver1 and /// SnmpVersion.Ver2
rootOid string OID to start WALK operation from. Only child OIDs of the rootOid will be /// retrieved and returned
return AsnType>.Dictionary
Exemplo n.º 1
0
        /// <summary>
        /// 读取根节点
        /// </summary>
        /// <param name="Ip">ip地址</param>
        /// <param name="outinfo">输出到那个字典</param>
        public void SnmpAgent(Dictionary<string, List<string>> Ip_OID, Dictionary<string, Dictionary<string, string>> outinfo)
        {
            #region 新方法

            foreach (KeyValuePair<string, List<string>> MyKVP in Ip_OID)
            {
                try
                {
                    SimpleSnmp snmp = new SimpleSnmp(MyKVP.Key, "public");
                    if (!snmp.Valid)
                    {
                        continue;
                    }
                    Dictionary<string, string> Info = new Dictionary<string, string>();
                    VbCollection col = new VbCollection();
                    TrapAgent agent = new TrapAgent();
                    foreach (string bc in MyKVP.Value)
                    {
                        Dictionary<Oid, AsnType> result = snmp.Walk(SnmpVersion.Ver2, bc);
                        if (result == null)
                        {
                            continue;
                        }
                        foreach (KeyValuePair<Oid, AsnType> kvp in result)
                        {
                            try
                            {
                                if (!Info.ContainsKey(kvp.Key.ToString()))
                                {
                                    Info.Add(kvp.Key.ToString(), kvp.Value.ToString());
                                }
                                else
                                {
                                    Info.Remove(kvp.Key.ToString());
                                    Info.Add(kvp.Key.ToString(), kvp.Value.ToString());
                                }

                            }
                            catch (Exception ex)
                            {
                                msg.SaveTempMessage(ex.Source.ToString(), ex.Message.ToString(), DateTime.Now.ToString());
                            }
                        }
                    }
                    if (!outinfo.ContainsKey(MyKVP.Key))
                    {
                        outinfo.Add(MyKVP.Key, Info);
                    }
                    else
                    {
                        outinfo.Remove(MyKVP.Key);
                        outinfo[MyKVP.Key] = Info;
                    }
                }
                catch (Exception ex)
                {
                    msg.SaveTempMessage(ex.Source.ToString(), ex.Message.ToString(), DateTime.Now.ToString());
                }
            }
            #endregion
        }
        private void WaitForCurrentConnectionsToDrain(string farm, string server, string snmpEndpoint, int snmpPort, string snmpCommunity, DateTime timeout)
        {
            if (DateTime.Now > timeout)
                throw new ConDepLoadBalancerException(string.Format("Timed out while taking {0} offline in load balancer.", server));

            Logger.Verbose("Connecting to snmp using " + snmpEndpoint + " on port " + snmpPort + " with community " + snmpCommunity);
            var snmp = new SimpleSnmp(snmpEndpoint, snmpPort, snmpCommunity);

            Logger.Verbose("Getting snmp info about farm " + farm);
            var farmResult = snmp.Walk(SnmpVersion.Ver2, FARM_NAMES_OID);
            var farmOid = farmResult.Single(x => x.Value.Clone().ToString() == farm);

            var id = farmOid.Key.ToString();
            var start = farmOid.Key.ToString().LastIndexOf(".");
            var farmSubId = id.Substring(start + 1, id.Length - start - 1);

            Logger.Verbose("Getting snmp info about server " + server);
            var serverResult = snmp.Walk(SnmpVersion.Ver2, SERVER_NAMES_OID + farmSubId);
            var serverOid = serverResult.Single(x => x.Value.Clone().ToString() == server);

            var serverId = serverOid.Key.ToString();
            start = serverOid.Key.ToString().LastIndexOf(".");
            var serverSubId = serverId.Substring(start + 1, serverId.Length - start - 1);

            Logger.Verbose("Getting current server sessions for server " + server);
            var pdu = Pdu.GetPdu();
            pdu.VbList.Add(SERVER_CUR_SESSIONS_OID + farmSubId + "." + serverSubId);
            var currentSessionsVal = snmp.Get(SnmpVersion.Ver2, pdu);
            var val = currentSessionsVal.Single().Value.Clone() as Counter64;

            if (val > 0)
            {
                Logger.Verbose("Server " + server + " has " + val + " active sessions.");
                var waitInterval = 3;
                Logger.Warn(string.Format("There are still {0} active connections on server {1}. Waiting {2} second before retry.", val, server, waitInterval));
                Thread.Sleep(1000 * waitInterval);
                WaitForCurrentConnectionsToDrain(farm, server, snmpEndpoint, snmpPort, snmpCommunity, timeout);
            }

            Logger.Verbose("Server " + server + " has 0 active session and is now offline.");
        }
Exemplo n.º 3
0
       public bool SnmpWalkTest(string host, string community, string oid)
       {

           try
           {
               SimpleSnmp snmp = new SimpleSnmp(host, community);
               if (!snmp.Valid)
               {
                   return false;
               }
               Dictionary<Oid, AsnType> result = snmp.Walk(SnmpVersion.Ver2,
                                                         oid);
               if (result == null)
               {
                   return false;
               }
               LogResult(result);
               return true;
           }
           catch (Exception ex)
           {
               return false;
           }
       }
Exemplo n.º 4
0
        private void SNMPGetInfo()
        {
            if (this.krbSNMP.Checked)
            {
                try
                {
                    SimpleSnmp snmp = new SimpleSnmp(Properties.Settings.Default.ip, this.kcbCommunity.SelectedItem.ToString());
                    snmp.Retry = 5;
                    snmp.Timeout = 60000;
                    snmp.SuppressExceptions = false;
                    string oid = Properties.Settings.Default.oid;
                    Dictionary<Oid, AsnType> result = null;
                    if (this.kcbMethod.SelectedItem.ToString() == "Get NEXT")
                    {
                        Pdu pdu = new Pdu();
                        pdu.Type = PduType.GetNext;
                        pdu.VbList.Add(oid);
                        result = snmp.GetNext(SnmpVersion.Ver1, pdu);
                    }
                    else if (this.kcbMethod.SelectedItem.ToString() == "GET")
                    {
                        // Create a request Pdu
                        Pdu pdu = new Pdu();
                        pdu.Type = PduType.Get;
                        pdu.VbList.Add(oid);
                        result = snmp.GetNext(SnmpVersion.Ver1, pdu);
                    }
                    else if (this.kcbMethod.SelectedItem.ToString() == "Walk")
                    {
                        result = snmp.Walk(SnmpVersion.Ver2, oid);
                    }
                    else if (this.kcbMethod.SelectedItem.ToString() == "Get BULK")
                    {
                        //Using GetBulk
                        Pdu pdu = new Pdu();
                        pdu.Type = PduType.GetBulk;
                        pdu.VbList.Add(oid);
                        pdu.NonRepeaters = 0;
                        pdu.MaxRepetitions = Convert.ToInt32(this.krypTxtBxRepeaters.Text);
                        result = snmp.GetBulk(pdu);
                    }

                    ktbResult.SuspendLayout();
                    ktbResult.Clear();
                    if (result == null)
                    {
                        ktbResult.AppendText("Request failed.\r\n");
                    }
                    else
                    {
                        ktbResult.SuspendLayout();
                        foreach (KeyValuePair<Oid, AsnType> entry in result)
                        {
                            ktbResult.AppendText(String.Format("{0} = {1}: {2}\r\n", entry.Key.ToString(), SnmpConstants.GetTypeName(entry.Value.Type),
                              entry.Value.ToString()));
                        }
                        ktbResult.ResumeLayout();
                    }
                }
                catch (Exception snmpEx)
                {
                    ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show(snmpEx.Message,
                        "SNMP Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                ktbResult.Focus();
                ktbResult.SelectionStart = 0;
                ktbResult.SelectionLength = 0;
                ktbResult.ScrollToCaret();
                ktbResult.ResumeLayout();
            }
        }
Exemplo n.º 5
0
 public static Dictionary<Oid, AsnType> Walk(string ip, string oid, int port = 161, int timeout = 2000, int retries = 1)
 {
     var snmp = new SimpleSnmp(ip, port, "s3cur3", timeout, retries);
     return snmp.Walk(SnmpVersion.Ver2, oid);
 }