Re: [pysnmp-users] SNMP MIB resolution issue
Brought to you by:
elie
From: Ilya E. <il...@gl...> - 2017-02-27 21:11:03
|
The best thing to do here is to figure out which MIBs you are walking (e.g. your SNMP Agent implements), then pass them to the `loadMibs()` function to make pysnmp loading just these MIBs (plus their dependencies). You can of course use `loadMibs()` without parameters, however then pysnmp will load up all MIBs it could reach. That may contribute to excessive memory consumption. > On 27 Feb 2017, at 15:45, Brett Kugler <bre...@gd...> wrote: > > After some more study, I found that if I use the loadMibs() function when I declare the ObjectType(), before I send them through nextCmd(), it works. I was attempting to resolve them after the fact. This doesn’t really explain why it would work at times and not others, but does give me a resolution to my real problem, not getting OIDs resolved properly. > > Thank you, > Brett > > From: Brett Kugler [mailto:bre...@gd... <mailto:bre...@gd...>] > Sent: Thursday, February 23, 2017 10:32 AM > To: Ilya Etingof <il...@gl... <mailto:il...@gl...>> > Cc: pys...@li... <mailto:pys...@li...> > Subject: Re: [pysnmp-users] SNMP MIB resolution issue > > Thank you for the prompt response. I may have provided too much information and confused the issue. I did attempt to use the loadMibs() methods, but I still received the same results. > > Since there are many layers, it is hard for me to produce a minimal reproduction, but I will see if I can spare some time this weekend to find one. Here is a general flow: > > I specify a varbind ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets')) > Without any loading of MIBs, PySNMP converts this to the proper OID (this must mean it’s resolving the MIBs on my system, right?) > I then pull those values from my devices using nextCmd() to walk the table (this is done in a multi-threaded blast using a Threadpool via python’s pool.starmap()) > Once I have all the results collated, I write them out to files based on the IP addresses they were retrieved from, this is where I see issues. > Now is where things get hard to explain, but I will do my best. During different executions of my code (multiple runs), the behavior I see when using pretty print to display the same OIDs from the same device changes. Sometimes, I get the expected output ('IF-MIB', 'ifInOctets', (InterfaceIndex(16875520),)) and other times I get the ‘incorrect’ output ('SNMPv2-SMI', 'mib-2', (ObjectName('2.2.1.10.16875520'),)) – this occurs on a device by device basis. All OIDs for a given device either print correctly, or they don’t. And this occurs even if I do not attempt to load any MIBs from any external sources (and when I do). > There is a wrinkle to all this – as I mentioned, I’m using threading to read multiple devices at once. When I read fewer devices, this behavior does not occur. I have not yet narrowed it down to determine how many devices need to be run concurrently for this to happen. Also – I was having a lot of issues with the asyncio module for PySNMP, so I am using the synchronous PySNMP functions wrapped in my threads. Since each thread gets its own SNMPEngine instance, my understanding was that makes it thread safe – and again, everything appears to work fine, with the exception of this OID to MIB translation issue. > > This is a pretty complex sequence, so I understand that it can be hard to envision – I was hoping this might have been a known issue. I will try to provide a full sample that recreates the issue to help with troubleshooting. > > Brett > > From: Ilya Etingof [mailto:il...@gl... <mailto:il...@gl...>] > Sent: Tuesday, February 21, 2017 6:03 PM > To: Brett Kugler <bre...@gd... <mailto:bre...@gd...>> > Cc: pys...@li... <mailto:pys...@li...> > Subject: Re: [pysnmp-users] SNMP MIB resolution issue > > > Hi Brett, > > I believe there is no randomness here. I’d guess it really depends on whether the MIB (that maps an OID to a human-friendly name) is loaded into pysnmp or not. > > If you know which MIBs your SNMP agent implements, you could load them in advance before you run your SNMP manager app. To load particular MIBs you could use .loadMibs(‘FIRST-MIB’, ’SECOND-MIB’) method: > > http://pysnmp.sourceforge.net/docs/api-reference.html#pysnmp.smi.rfc1902.ObjectIdentity.loadMibs <http://pysnmp.sourceforge.net/docs/api-reference.html#pysnmp.smi.rfc1902.ObjectIdentity.loadMibs> > > There is currently no automatic way in pysnmp to figure out which MIB maps given OID and load it up on demand. However this feature is being developed and should appear in pysnmp sooner rather than later. ;-) > > Let me know if you need further help. > > On 20 Feb 2017, at 23:04, Brett Kugler <bre...@gd... <mailto:bre...@gd...>> wrote: > > I am running the latest version of PySNMP (4.3.3) and am having issues with my OIDs not consistently converting to friendly format. Here is an example of pulling values from the following oids: > oids = [ > ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets')), > ObjectType(ObjectIdentity('IF-MIB', 'ifOutOctets')) > ] > > There are no issues retrieving the values from all my devices using nextCmd. > > When I go to convert the OIDs received back to friendly text, I am getting intermittent results. For simplicity, here is an example of what I’ve tried: > > # results is a list of tuples, [(ip, varbinds),…] > for device in results: > outfile = open(device[0], 'w+') > for varbinds in device[1]: > outfile.write(' = '.join([x.prettyPrint() for x in varbinds])) > outfile.write('\n') > outfile.close() > > This produces a dump of all the bytes in/out for all the interfaces on a device. > > Here’s where things go wrong – when I execute my code, sometimes devices will display the friendly text properly, and sometimes they will not. If the translation fails, it fails for all the varbinds on the device. If it succeeds, they are all correct. Over multiple executions, devices will either be correct or incorrect, seemingly randomly. > > Here is a sample of correct vs. incorrect output. > right - ('IF-MIB', 'ifInOctets', (InterfaceIndex(16875520),)) - wrong - ('SNMPv2-SMI', 'mib-2', (ObjectName('2.2.1.10.16875520'),)) > > Here is a sampling of runs against devices, and their failues: > Nope 10.128.109.11 10.128.109.11 10.128.109.11 Nope 10.128.109.11 > 10.128.109.12 Nope 10.128.109.12 Nope 10.128.109.12 10.128.109.12 > 10.128.11.168 10.128.11.168 10.128.11.168 Nope 10.128.11.168 > Nope 10.128.11.2 Nope 10.128.11.2 10.128.11.2 Nope 10.128.11.2 > 10.128.11.3 10.128.11.3 10.128.11.3 10.128.11.3 > > In all cases the underlying OID is correct – it’s simply the translation that is failing. > > I have tried to assign MIB sources either with addAsn1MibSource() or just addMibSource() as well as playing around with the getMibSymbol() function. I would accept that I am simply doing something wrong, but the intermittent nature of this working/not working has me thinking there’s something else going on. > > Any advice? Thanks in advance! > > Brett > |