OpenNMS API 1.2.3

org.opennms.netmgt.scriptd.helper
Class SnmpTrapHelper

java.lang.Object
  extended byorg.opennms.netmgt.scriptd.helper.SnmpTrapHelper
All Implemented Interfaces:
SnmpTrapHandler

public class SnmpTrapHelper
extends java.lang.Object
implements SnmpTrapHandler

This "helper" class provides a convenience interface for generating and forwarding SNMP traps. This class was created in order to make it easier to write simple scripts to generate traps based on events or to forward traps, using scripting languages that are able to access Java classes (such as BeanShell).

Author:
Jim Doble , OpenNMS.org

Nested Class Summary
private  class SnmpTrapHelper.SnmpCounter32Factory
          Variable binding factory for SnmpCounter32
private  class SnmpTrapHelper.SnmpCounter64Factory
          Variable binding factory for SnmpCounter64
private  class SnmpTrapHelper.SnmpGauge32Factory
          Variable binding factory for SnmpGuage32
private  class SnmpTrapHelper.SnmpInt32Factory
          Variable binding factory for SnmpInt32
private  class SnmpTrapHelper.SnmpIPAddressFactory
          Variable binding factory for SnmpIPAddress
private  class SnmpTrapHelper.SnmpNullFactory
          Variable binding factory for SnmpNull
private  class SnmpTrapHelper.SnmpObjectIdFactory
          Variable binding factory for SnmpObjectId
private  class SnmpTrapHelper.SnmpOctetStringFactory
          Variable binding factory for SnmpOctetString
private  class SnmpTrapHelper.SnmpOpaqueFactory
          Variable binding factory for SnmpOpaque
private  class SnmpTrapHelper.SnmpTimeTicksFactory
          Variable binding factory for SnmpTimeTicks
private static interface SnmpTrapHelper.VarBindFactory
          Common interface for all variabe binding factories
 
Field Summary
private static int ENTERPRISE_SPECIFIC
          The SNMP generic value for an enterprise-specific trap
private  java.util.HashMap m_factoryMap
          Map of factories for generating different types of SNMP variable binding content
private  SnmpTrapSession m_trapSession
          Trap session for sending traps
private static java.lang.String SNMP_SYSUPTIME_OID
          The sysUpTimeOID, which should be the first varbind in a V2 trap
private static java.lang.String SNMP_TRAP_ADDRESS_OID
          The snmpTrapAddress, which may occur in a V2 trap
private static java.lang.String SNMP_TRAP_COMMUNITY_OID
          The snmpTrapCommunity, which may occur in a V2 trap
private static java.lang.String SNMP_TRAP_ENTERPRISE_OID
          The snmp trap enterprise OID, which if present in a V2 trap is the last varbind
private static java.lang.String SNMP_TRAP_OID
          The snmpTrapOID, which should be the second varbind in a V2 trap
private static java.lang.String SNMP_TRAPS
          OID prefix for generic SNMP traps
 
Constructor Summary
SnmpTrapHelper()
          Constructs a new SNMPTrapHelper.
 
Method Summary
 void addVarBinding(SnmpPduPacket packet, java.lang.String name, java.lang.String type, java.lang.String value)
          Crate a new variable binding and add it to the specified SNMP V2 trap.
 void addVarBinding(SnmpPduPacket packet, java.lang.String name, java.lang.String type, java.lang.String encoding, java.lang.String value)
          Crate a new variable binding and add it to the specified SNMP V2 trap.
 void addVarBinding(SnmpPduTrap trap, java.lang.String name, java.lang.String type, java.lang.String value)
          Crate a new variable binding and add it to the specified SNMP V1 trap.
 void addVarBinding(SnmpPduTrap trap, java.lang.String name, java.lang.String type, java.lang.String encoding, java.lang.String value)
          Crate a new variable binding and add it to the specified SNMP V1 trap.
 SnmpPduTrap createV1Trap(java.lang.String entId, java.lang.String agentAddr, int generic, int specific, long timeStamp)
          Create an SNMP V1 trap with the specified enterprise IS, agent address, generic ID, specific ID, and time stamp.
 SnmpPduPacket createV2Trap(java.lang.String trapOid, java.lang.String sysUpTime)
          Create an SNMP V2 trap with the specified trap object ID, and sysUpTime value.
 void forwardTrap(Event event, java.lang.String destAddr, int destPort)
          Create an SNMP trap, based on the content of the specified event, and forward the trap to the specified address and port.
 void forwardV1Trap(Event event, java.lang.String destAddr, int destPort)
          Create an SNMP V1 trap, based on the content of the specified event, and forward the trap to the specified address and port.
 void forwardV2Trap(Event event, java.lang.String destAddr, int destPort)
          Create an SNMP V2 trap, based on the content of the specified event, and forward the trap to the specified address and port.
 void sendTrap(java.lang.String community, SnmpPduPacket packet, java.lang.String destAddr, int destPort)
          Send the specified SNMP V2 trap to the specified address and port, with the specified community string.
 void sendTrap(java.lang.String community, SnmpPduTrap trap, java.lang.String destAddr, int destPort)
          Send the specified SNMP V1 trap to the specified address and port, with the specified community string.
 void snmpReceivedTrap(SnmpTrapSession session, java.net.InetAddress agent, int port, SnmpOctetString community, SnmpPduPacket pdu)
           This method is defined to handle SNMPv2 traps that are received by the session.
 void snmpReceivedTrap(SnmpTrapSession session, java.net.InetAddress agent, int port, SnmpOctetString community, SnmpPduTrap pdu)
           This method is define to handle SNMPv1 traps that are received by the session.
 void snmpTrapSessionError(SnmpTrapSession session, int error, java.lang.Object ref)
           This method is invoked if an error occurs in the trap session.
 void stop()
          Stops the SnmpTrapHelper.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SNMP_SYSUPTIME_OID

private static final java.lang.String SNMP_SYSUPTIME_OID
The sysUpTimeOID, which should be the first varbind in a V2 trap

See Also:
Constant Field Values

SNMP_TRAP_OID

private static final java.lang.String SNMP_TRAP_OID
The snmpTrapOID, which should be the second varbind in a V2 trap

See Also:
Constant Field Values

SNMP_TRAP_ADDRESS_OID

private static final java.lang.String SNMP_TRAP_ADDRESS_OID
The snmpTrapAddress, which may occur in a V2 trap

See Also:
Constant Field Values

SNMP_TRAP_COMMUNITY_OID

private static final java.lang.String SNMP_TRAP_COMMUNITY_OID
The snmpTrapCommunity, which may occur in a V2 trap

See Also:
Constant Field Values

SNMP_TRAP_ENTERPRISE_OID

private static final java.lang.String SNMP_TRAP_ENTERPRISE_OID
The snmp trap enterprise OID, which if present in a V2 trap is the last varbind

See Also:
Constant Field Values

SNMP_TRAPS

private static final java.lang.String SNMP_TRAPS
OID prefix for generic SNMP traps

See Also:
Constant Field Values

ENTERPRISE_SPECIFIC

private static final int ENTERPRISE_SPECIFIC
The SNMP generic value for an enterprise-specific trap

See Also:
Constant Field Values

m_factoryMap

private java.util.HashMap m_factoryMap
Map of factories for generating different types of SNMP variable binding content


m_trapSession

private SnmpTrapSession m_trapSession
Trap session for sending traps

Constructor Detail

SnmpTrapHelper

public SnmpTrapHelper()
Constructs a new SNMPTrapHelper.

Method Detail

stop

public void stop()
Stops the SnmpTrapHelper. If there is a valid SnmpTrapSession, that trap session is stopped.


snmpReceivedTrap

public void snmpReceivedTrap(SnmpTrapSession session,
                             java.net.InetAddress agent,
                             int port,
                             SnmpOctetString community,
                             SnmpPduPacket pdu)
Description copied from interface: SnmpTrapHandler

This method is defined to handle SNMPv2 traps that are received by the session. The parameters allow teh handler to determine the host, port, and community string of the received PDU

Specified by:
snmpReceivedTrap in interface SnmpTrapHandler
Parameters:
session - The SNMP session
agent - The remote sender
port - The remote senders port
community - The community string
pdu - The SNMP pdu

snmpReceivedTrap

public void snmpReceivedTrap(SnmpTrapSession session,
                             java.net.InetAddress agent,
                             int port,
                             SnmpOctetString community,
                             SnmpPduTrap pdu)
Description copied from interface: SnmpTrapHandler

This method is define to handle SNMPv1 traps that are received by the session. The parameters allow the handler to determine the host, port, and community string of the received PDU.

Specified by:
snmpReceivedTrap in interface SnmpTrapHandler
Parameters:
session - The SNMP session
agent - The Trap sender
port - The port of the sender
community - The community string
pdu - The SNMP trap pdu

snmpTrapSessionError

public void snmpTrapSessionError(SnmpTrapSession session,
                                 int error,
                                 java.lang.Object ref)
Description copied from interface: SnmpTrapHandler

This method is invoked if an error occurs in the trap session. The error code that represents the failure will be passed in the second parameter, 'error'. The error codes can be found in the class SnmpTrapSession class.

If a particular PDU is part of the error condition it will be passed in the third parameter, 'pdu'. The pdu will be of the type SnmpPduRequest or SnmpPduTrap object. The handler should use the "instanceof" operator to determine which type the object is. Also, the object may be null if the error condition is not associated with a particular PDU.

Specified by:
snmpTrapSessionError in interface SnmpTrapHandler
Parameters:
session - The SNMP Trap Session
error - The error condition value.
ref - The PDU reference, or potentially null. It may also be an exception.

sendTrap

public void sendTrap(java.lang.String community,
                     SnmpPduTrap trap,
                     java.lang.String destAddr,
                     int destPort)
              throws SnmpTrapHelperException
Send the specified SNMP V1 trap to the specified address and port, with the specified community string.

Parameters:
community - The community string to be used.
trap - The trap to be sent.
destAddr - The IP address to which the trap should be sent.
destPort - The port to which the trap should be sent.
Throws:
Throws - SnmpTrapHelperException if the trap cannot be sent for any reason.
SnmpTrapHelperException

sendTrap

public void sendTrap(java.lang.String community,
                     SnmpPduPacket packet,
                     java.lang.String destAddr,
                     int destPort)
              throws SnmpTrapHelperException
Send the specified SNMP V2 trap to the specified address and port, with the specified community string.

Parameters:
community - The community string to be used.
packet - The trap to be sent.
destAddr - The IP address to which the trap should be sent.
destPort - The port to which the trap should be sent.
Throws:
Throws - SnmpTrapHelperException if the trap cannot be sent for any reason.
SnmpTrapHelperException

createV1Trap

public SnmpPduTrap createV1Trap(java.lang.String entId,
                                java.lang.String agentAddr,
                                int generic,
                                int specific,
                                long timeStamp)
Create an SNMP V1 trap with the specified enterprise IS, agent address, generic ID, specific ID, and time stamp.

Parameters:
entId - The enterprise ID for the trap.
agentAddr - The agent address for the trap.
generic - The generic ID for the trap.
specific - The specific ID for the trap.
timeStamp - The time stamp for the trap.
Returns:
The newly-created trap.

createV2Trap

public SnmpPduPacket createV2Trap(java.lang.String trapOid,
                                  java.lang.String sysUpTime)
                           throws SnmpTrapHelperException
Create an SNMP V2 trap with the specified trap object ID, and sysUpTime value.

Parameters:
trapOid - The trap object id.
sysUpTime - The system up time.
Returns:
The newly-created trap.
Throws:
Throws - SnmpTrapHelperException if the trap cannot be created for any reason.
SnmpTrapHelperException

addVarBinding

public void addVarBinding(SnmpPduTrap trap,
                          java.lang.String name,
                          java.lang.String type,
                          java.lang.String value)
                   throws SnmpTrapHelperException
Crate a new variable binding and add it to the specified SNMP V1 trap. The value encoding is assumed to be XML_ENCODING_TEXT.

Parameters:
trap - The trap to which the variable binding should be added.
name - The name (a.k.a. "id") of the variable binding to be created
type - The type of variable binding to be created
value - The variable binding value
Throws:
Throws - SnmpTrapHelperException if the variable binding cannot be added to the trap for any reason.
SnmpTrapHelperException

addVarBinding

public void addVarBinding(SnmpPduTrap trap,
                          java.lang.String name,
                          java.lang.String type,
                          java.lang.String encoding,
                          java.lang.String value)
                   throws SnmpTrapHelperException
Crate a new variable binding and add it to the specified SNMP V1 trap.

Parameters:
trap - The trap to which the variable binding should be added.
name - The name (a.k.a. "id") of the variable binding to be created
type - The type of variable binding to be created
encoding - Describes the way in which the value content has been encoded (i.e. XML_ENCODING_TEXT, or XML_ENCODING_BASE64)
value - The variable binding value
Throws:
Throws - SnmpTrapHelperException if the variable binding cannot be added to the trap for any reason.
SnmpTrapHelperException

addVarBinding

public void addVarBinding(SnmpPduPacket packet,
                          java.lang.String name,
                          java.lang.String type,
                          java.lang.String value)
                   throws SnmpTrapHelperException
Crate a new variable binding and add it to the specified SNMP V2 trap. The value encoding is assumed to be XML_ENCODING_TEXT.

Parameters:
packet - The trap to which the variable binding should be added.
name - The name (a.k.a. "id") of the variable binding to be created
type - The type of variable binding to be created
value - The variable binding value
Throws:
Throws - SnmpTrapHelperException if the variable binding cannot be added to the trap for any reason.
SnmpTrapHelperException

addVarBinding

public void addVarBinding(SnmpPduPacket packet,
                          java.lang.String name,
                          java.lang.String type,
                          java.lang.String encoding,
                          java.lang.String value)
                   throws SnmpTrapHelperException
Crate a new variable binding and add it to the specified SNMP V2 trap.

Parameters:
packet - The trap to which the variable binding should be added.
name - The name (a.k.a. "id") of the variable binding to be created
type - The type of variable binding to be created
encoding - Describes the way in which the value content has been encoded (i.e. XML_ENCODING_TEXT, or XML_ENCODING_BASE64)
value - The variable binding value
Throws:
Throws - SnmpTrapHelperException if the variable binding cannot be added to the trap for any reason.
SnmpTrapHelperException

forwardV1Trap

public void forwardV1Trap(Event event,
                          java.lang.String destAddr,
                          int destPort)
                   throws SnmpTrapHelperException
Create an SNMP V1 trap, based on the content of the specified event, and forward the trap to the specified address and port. It is assumed that the specified event represents an SNMP V1 or V2 trap that was received by OpenNMS (TrapD).

Parameters:
event - The event upon which the trap content should be based
destAddr - The address to which the trap should be forwarded
destPort - The port to which the trap should be forwarded
Throws:
Throws - SnmpTrapHelperException if the variable binding cannot be added to the trap for any reason.
SnmpTrapHelperException

forwardV2Trap

public void forwardV2Trap(Event event,
                          java.lang.String destAddr,
                          int destPort)
                   throws SnmpTrapHelperException
Create an SNMP V2 trap, based on the content of the specified event, and forward the trap to the specified address and port. It is assumed that the specified event represents an SNMP V1 or V2 trap that was received by OpenNMS (TrapD).

Parameters:
event - The event upon which the trap content should be based
destAddr - The address to which the trap should be forwarded
destPort - The port to which the trap should be forwarded
Throws:
Throws - SnmpTrapHelperException if the variable binding cannot be added to the trap for any reason.
SnmpTrapHelperException

forwardTrap

public void forwardTrap(Event event,
                        java.lang.String destAddr,
                        int destPort)
                 throws SnmpTrapHelperException
Create an SNMP trap, based on the content of the specified event, and forward the trap to the specified address and port. It is assumed that the specified event represents an SNMP V1 or V2 trap that was received by OpenNMS (TrapD). The type of trap to be created depends on the type of the original trap (i.e. if the original trap was an SNMP V1 trap, an SNMP V1 trap will be created; if the original trap was an SNMP V2 trap, an SNMP V2 trap will be created).

Parameters:
event - The event upon which the trap content should be based
destAddr - The address to which the trap should be forwarded
destPort - The port to which the trap should be forwarded
Throws:
Throws - SnmpTrapHelperException if the variable binding cannot be added to the trap for any reason.
SnmpTrapHelperException

OpenNMS API 1.2.3

Generated by eevans on May 29 2005 2015.