import types
from pysnmp import msession
class snmpbulk (msession.multisession):
"""Fetch a bulk of variables from remote SNMP process
"""
def __init__ (self):
"""Explicitly call superclass's constructor as it gets
overloaded by this class constructor and pass a few
arguments alone.
"""
msession.multisession.__init__ (self)
def run (self, agent, community, objids):
"""Issue a bulk of SNMP GET requests and wait for replies to
come. The objids argument should be a list of strings where
each string represents a Object ID in dotted numbers notation
(e.g. ['.1.3.6.1.4.1.307.3.2.1.1.1.4.1']).
"""
# Convert string type Object ID's into numeric representation
numeric_objids = map (self.str2nums, objids)
# BER encode SNMP Object ID's to query
encoded_objids = map (self.encode_oid, numeric_objids)
# Since we are going to _query_ SNMP agent for Object ID's
# associated value, there will be no variable values passed to
# SNMP agent.
encoded_values = []
# Walk over supplied Object ID's
for encoded_objid in encoded_objids:
# Build and submit complete SNMP message of type 'GETREQUEST',
# pass it a list of BER encoded Object ID's (only one as we are going to
# query a number of Object ID's in bulk) to query and an empty list
# of values associated with these Object ID's (empty list as there is
# no point to pass any variables values along the SNMP GET request).
self.submit_request (agent, community, 'GETREQUEST', [ encoded_objid ], encoded_values)
# At this point we get a number of submitted SNMP GET requests. Now,
# send them out all at once.
self.dispatch ()
# At this point we get all the replies from SNMP agents we expected (some
# may time out). Retrieve a list of tuples (encoded_objid, encoded_value)
encoded_pairs = self.retrieve ()
# Parse out encoded_objid's
encoded_objids = reduce (lambda x, y: x + y, map (lambda x: x[0], encoded_pairs))
# Parse out encoded_vals
encoded_values = reduce (lambda x, y: x + y, map (lambda x: x[1], encoded_pairs))
# Decode BER encoded Object ID.
objids = map (self.decode_value, encoded_objids)
# Decode BER encoded values associated with Object ID's.
values = map (self.decode_value, encoded_values)
# Return a tuble of two lists holding Object ID's and associated
# values extracted from SNMP agent replies.
return (objids, values)
# Run the module if it's invoked for execution
if __name__ == '__main__':
# The sys module is required for exit() function
import sys
# Make sure we have got enough args
if len (sys.argv) < 4:
# Report a usage error
print 'Usage: %s ' % sys.argv[0]
# Incomplete command line arguments, exiting
sys.exit (1)
# Create an instance of snmpget class
instance = snmpbulk ()
# Run snmpbulk against passed Object ID's and expect it to return a list
# of Object ID's and corresponding values as reported by SNMP agent
(objids, values) = instance.run (sys.argv[1], sys.argv[2], sys.argv[3:])
# Convert two lists into a list of tuples for easier printing
results = map (None, objids, values)
# Convert two lists into a list of tuples for easier printing
results = map (None, objids, values)
# Just print them out
for (objid, value) in results:
response = response + objid + ' ---> ' + repr(value)
Need help? Try PySNMP mailing lists.
|