lextudio / sharpsnmplib

C# SNMP Library (#SNMP) - Open source SNMP implementation for C# and .NET
https://sharpsnmp.com
MIT License
357 stars 152 forks source link

OperationException - wrong response sequence: expected 5250, received 96063 #353

Closed lextudio-support closed 6 days ago

lextudio-support commented 6 days ago

Hi Guys,

I have a little strange problem. Sometime I get an "OperationException - wrong response sequence: expected 5250, received 96063" error in my log. I can imagine that it is be course that sharpsnmp get the wrong ID back or something, so can you confirm my suspicion?

What I think happens:

1. I request a snmp value from a device
2. The requests times out
3. I request a new snmp value from the same device
4. I get a response back from the first pull and sharpsnmp throws an operationexception

Does that sound like a resonable explination on what could be happening?

Some time I see that the data returned is "leaking" from one snmpget to another, I was wondering if this could be the case. Please note that I have many threads running with this line of code (one thread pr. dst IP).

Hope you guys have an idea.

Some additional details:

SharpSnmpLib.dll 6.0.11107.0
Mono 2.6.3
Debian Linux 5.0.7
StackTrace: at Lextm.SharpSnmpLib.Messaging.MessageFactory.GetResponse (System.Net.IPEndPoint receiver, System.Byte[] bytes, Int32 number, Int32 timeout, Lextm.SharpSnmpLib.Security.UserRegistry registry, System.Net.Sockets.Socket socket) [0x00000] in :0

Here is a snip of my code:

        public string Get(string ip, string community, string oid, int timeout)
        {
            System.Reflection.MethodBase baseMethod = System.Reflection.MethodBase.GetCurrentMethod();
            string curMethod = baseMethod.DeclaringType.Namespace + "." + baseMethod.DeclaringType.Name + "." + baseMethod.Name;

            ISnmpMessage response;
            string s = "";
            try
            {
                List<Variable> vList = new List<Variable>();
                Variable item = new Variable(new ObjectIdentifier(oid));
                vList.Add(item);

                Random random = new Random();
                GetRequestMessage message = new GetRequestMessage(random.Next(1, 100000), VersionCode.V2, new OctetString(community), vList);

                IPAddress ipadr;
                if (IPAddress.TryParse(ip, out ipadr))
                {
                    IPEndPoint endpoint = new IPEndPoint(ipadr, 161);
                    response = message.GetResponse(timeout, endpoint, _udp);
                }
                else
                {
                    Logging.AddLog("Could not parse IP address (" + ip + "), returning \"\"", Logging.LogLevel.Warning, curMethod, "N/A");
                    return "";
                }

                s = response.Pdu.Variables[0].Data.ToString();
                Logging.AddLog("Returning: " + s, Logging.LogLevel.Debug, curMethod, ip);

            }
            catch (SocketException ex)
            {
                Logging.AddLog("Err connecting to host, got message: " + ex.Message, Logging.LogLevel.Warning, curMethod, ip);
                Stats.Stats.Current.FiveMinuteAggStats.AddStat(1, "Snmp_SocketExceptions");
                return "";
            }
            catch (Lextm.SharpSnmpLib.Messaging.TimeoutException)
            {
                Logging.AddLog("Timeout to host, returning nothing.", Logging.LogLevel.Notice, curMethod, ip);
                Stats.Stats.Current.FiveMinuteAggStats.AddStat(1, "Snmp_TimeoutException");
                return "";

            }
            catch (Exception ex)
            {
                Logging.AddLog(ex, curMethod, ip);
                return "";

            }

            return s;

        }

Cheers

Esben

Original Reported Date: 2011-03-22T08:52:19.13-07:00 Original CodePlex Discussion Thread ID: 250704

lextudio-support commented 6 days ago

Copied from CodePlex without authors:

Hi Esben,

A workaround is to use a larger timeout value for such devices. That should avoid such issues. I am still investigating what can be changed in #SNMP to prevent such problems completely, but that may not be included in our upcoming 7.0 release.

Regards,

Lex

Original Posted Date: 2011-03-24T06:29:56.823-07:00

lextudio-support commented 6 days ago

Copied from CodePlex without authors:

Hi Lex,

Thanks for your reply. I have increesed the timeout now.

I have one other issue with this. Sometimes, but not always sharpsnmp does not get it right and does not trow an exception but forwards it to my code even though that it should. I am not quite sure why that is and it only happens in ~1% of the cases I catch it with code like this:

                if (!response.Pdu.Variables[0].Id.ToString().Contains(oid))
                {
                    Logging.AddLog("SNMP Request OID was: '" + oid + "', but we got request OID back: '" +response.Pdu.Variables[0].Id + "'", Logging.LogLevel.Warning, curMethod, ip);
                    Stats.Stats.Current.FiveMinuteAggStats.AddStat(1, "Snmp_ModuleLeak");
                    return "";
                }

just after having received the response back.

Do you know why that could happen? as you have already done the check in your lib.

I am running mono 2.6.3 on a Debian 5.0.7

Cheers

Esben

Original Posted Date: 2011-03-24T08:53:45.123-07:00

lextudio-support commented 6 days ago

Copied from CodePlex without authors:

For this new issue, I think you need to capture network packets now to better understand the underlying data. Wireshark may be an option.

Then you can see what OID is there inside the request, and what is there inside the response.

If the response shows a difference OID from the request, then I believe it is an agent issue, not a #SNMP one.

Regards,

Lex

Original Posted Date: 2011-03-25T03:21:07.933-07:00

lextudio-support commented 6 days ago

Marked as Answer Date: 2013-10-06T20:44:03.43-07:00