Re: [pysnmp-users] varbind persistence in my trap object?
Brought to you by:
elie
From: Ilya E. <il...@gl...> - 2003-08-04 08:29:12
|
A list for users of pure-Python SNMP framework <pysnmp-users.lists.sourceforge.net> List-Unsubscribe: <https://lists.sourceforge.net/lists/listinfo/pysnmp-users>, <mailto:pys...@li...?subject=unsubscribe> List-Archive: <http://sourceforge.net/mailarchive/forum.php?forum=pysnmp-users> Pete, In fact both approaches should work exactly the same. The only difference is the point where you pass encoded oids&vals to trap object. > trap = v2c.TRAP() > encoded_oids = [] > encoded_vals = [] > [add the encoded oids and vals] > host.send(trap.encode(encoded_oids=encoded_oids, encoded_vals=encoded_vals)) So, what is the problem with the above? > This is what I first tried, but was never able to get working because > each time a trap was fired, the trap contained all of the varbinds > from the previous trap (i.e. they kept appending): > > def add_varbind(trap, oid, val): > trap['encoded_oids'].append(oid) > trap['encoded_vals'].append(val) > > trap = v2c.TRAP() > [add the encoded oids and vals using 'add_varbind(trap, oid, val)' function] > encoded_trap = trap.encode() > host.send(encoded_trap) > > This never worked; yet I am not sure why because I create myself a new > trap object each time a trap is fired and I stuff it with the > appropriate values. Clearly, I am using the API wrong. I searched > for a method to clear the object state (trap.clear() didn't work). That's probably because you're doing append() to var-bind list. Try assignment (=) instead. > My questions are: > > 1) Should I just abandon the second approach? Or am I just using the > API incorrectly in this case? I do not see much difference in these approaches. The bottom line is to have a single persistent trap object stuffed with new oids&vals on each trap. > 2) In both examples above, the 'host' object (created from > role.manager()) is created once in the lifetime of the program. Is > it safe to use this object multiple times for each trap that I > send? Yes, to the extent of slight performance penalty (objects and socket creation etc.). BTW, I suggest you trying pysnmp-3.x (its native API) instead of pysnmp-2.x as it's hopefully has a more clear design and implementation. Hope this helps, ilya |