import string
from pysnmp import session
class snmptrap (session.session):
"""Send SNMP trap to remote SNMP process
"""
def __init__ (self, agent, community):
"""Explicitly call superclass's constructor as it gets
overloaded by this class constructor and pass a few
arguments alone.
"""
session.session.__init__ (self, agent, community)
def run (self, enterprise, addr, generic, specific, timeticks, \
objids=[], values=[]):
"""Send SNMP trap message to remote SNMP agent.
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']).
See RFC-1157 for details.
"""
# Convert string type enterprise Object ID into numeric representation
enterprise = self.str2nums (enterprise)
# Convert string type Object IDs into numeric representation
# and BER encode them
encoded_objids = map (self.encode_oid, map (self.str2nums, objids))
# BER encode the values of MIB variables (assuming they are strings!)
encoded_values = map (self.encode_string, values)
# Build a complete SNMP trap message
question = self.encode_trap (enterprise, addr, generic, specific,
timeticks, encoded_objids, encoded_values)
# Try to send SNMP message to SNMP agent (expect no response)
self.send (question)
if __name__ == '__main__':
"""Run the module if it's invoked for execution
"""
import sys
# Set up defaults
port = 162
# Make sure we have got enough args
if len (sys.argv) < 8:
print 'Usage: %s [object-id:value [[object-id:value] ... ]]' % sys.argv[0]
sys.exit (1)
# Process command line arguments
try:
generic = int(sys.argv[5])
except ValueError:
print 'Bad generic trap value (should be integer)'
sys.exit (1)
else:
if generic > 6:
print 'Bad generic trap value (should be integer in range 0..6)'
sys.exit (1)
try:
specific = int(sys.argv[6])
except ValueError:
print 'Bad specific trap value (should be integer)'
sys.exit (1)
try:
uptime = long(sys.argv[7])
except ValueError:
print 'Bad uptime value (should be in secs since epoch)'
sys.exit (1)
# Initialize lists of passed Object-ID's and values
objids = []
values = []
# Parse Object-IDs and values from command line arguments
for arg in sys.argv[8:]:
# Split token on Object-ID and value
token = string.split(arg, ':')
# Add them to the dictionary
if len(token) == 2:
# Add Object-ID and value into two lists
objids.append (token[0])
values.append (token[1])
else:
# Report a falure
print 'Bad argument: ', arg
# Bad arguments, exiting
sys.exit (1)
# Split token on agent name and port
token = string.split(sys.argv[1], ':')
# Add them to the dictionary
if len(token) == 2:
agent = token[0]
try:
port = int(token[1])
except ValueError:
print 'Bad port value (should be integer)'
sys.exit(1)
elif len(token) == 1:
agent = token[0]
else:
print 'Bad agent name: ' + sys.argv[1]
sys.exit (1)
# Create an instance of snmptrap class
instance = snmptrap (agent, sys.argv[2])
# Set up port at SNMP session object
instance.port = port
# Send a trap and expect no response from remote SNMP agent
instance.run (sys.argv[3], sys.argv[4], generic, specific,\
uptime, objids, values)
print 'SNMP trap sent to ' + str((agent, port))
Need help? Try PySNMP mailing lists.
|