Class SnmpTrapHelper


  • public class SnmpTrapHelper
    extends java.lang.Object
    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
    • Constructor Summary

      Constructors 
      Constructor Description
      SnmpTrapHelper()
      Constructs a new SNMPTrapHelper.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addVarBinding​(SnmpTrapBuilder trap, java.lang.String name, java.lang.String type, java.lang.String value)
      Create a new variable binding and add it to the specified SNMP V1 trap.
      void addVarBinding​(SnmpTrapBuilder trap, java.lang.String name, java.lang.String type, java.lang.String encoding, java.lang.String value)
      Create a new variable binding and add it to the specified SNMP V1 trap.
      SnmpV1TrapBuilder 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.
      SnmpV2TrapBuilder createV2Inform​(java.lang.String trapOid, java.lang.String sysUpTime)
      Create an SNMP V2 inform with the specified trap object ID, and sysUpTime value.
      SnmpTrapBuilder createV2Trap​(java.lang.String trapOid, java.lang.String sysUpTime)
      Create an SNMP V2 trap with the specified trap object ID, and sysUpTime value.
      SnmpV3TrapBuilder createV3Inform​(java.lang.String trapOid, java.lang.String sysUpTime)
      Create an SNMP V3 trap with the specified trap object ID, and sysUpTime value.
      SnmpV3TrapBuilder createV3Trap​(java.lang.String trapOid, java.lang.String sysUpTime)
      Create an SNMP V3 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.
      java.lang.String getLocalEngineID()
      This helper method helps SNMP trap daemon administrator to set up authentication An snmpv3 trap is sent using the sender EngineID that needs to be known over remote trap receivers
      void sendTL1AutonomousMsgTrap​(Event event, java.lang.String destAddr, int destPort, java.lang.String trapVersion, java.lang.String community)
      Create an SNMP trap, based on the content of an event derived from a TL1 autonomous message received by Tl1d, and forward the trap to the specified address and port.
      void stop()
      Stops the SnmpTrapHelper.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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.
      • createV1Trap

        public SnmpV1TrapBuilder createV1Trap​(java.lang.String entId,
                                              java.lang.String agentAddr,
                                              int generic,
                                              int specific,
                                              long timeStamp)
                                       throws java.net.UnknownHostException
        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.
        Throws:
        java.net.UnknownHostException - if any.
      • createV2Trap

        public SnmpTrapBuilder 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 - if any.
      • createV2Inform

        public SnmpV2TrapBuilder createV2Inform​(java.lang.String trapOid,
                                                java.lang.String sysUpTime)
                                         throws SnmpTrapHelperException
        Create an SNMP V2 inform 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 - if any.
      • createV3Trap

        public SnmpV3TrapBuilder createV3Trap​(java.lang.String trapOid,
                                              java.lang.String sysUpTime)
                                       throws SnmpTrapHelperException
        Create an SNMP V3 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 - if any.
      • createV3Inform

        public SnmpV3TrapBuilder createV3Inform​(java.lang.String trapOid,
                                                java.lang.String sysUpTime)
                                         throws SnmpTrapHelperException
        Create an SNMP V3 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 - if any.
      • getLocalEngineID

        public java.lang.String getLocalEngineID()
        This helper method helps SNMP trap daemon administrator to set up authentication An snmpv3 trap is sent using the sender EngineID that needs to be known over remote trap receivers
        Returns:
        The local engine ID
      • addVarBinding

        public void addVarBinding​(SnmpTrapBuilder trap,
                                  java.lang.String name,
                                  java.lang.String type,
                                  java.lang.String value)
                           throws SnmpTrapHelperException
        Create 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 - if any.
      • addVarBinding

        public void addVarBinding​(SnmpTrapBuilder trap,
                                  java.lang.String name,
                                  java.lang.String type,
                                  java.lang.String encoding,
                                  java.lang.String value)
                           throws SnmpTrapHelperException
        Create 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 - if any.
      • 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 - if any.
      • 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 - if any.
      • 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 - if any.
      • sendTL1AutonomousMsgTrap

        public void sendTL1AutonomousMsgTrap​(Event event,
                                             java.lang.String destAddr,
                                             int destPort,
                                             java.lang.String trapVersion,
                                             java.lang.String community)
                                      throws SnmpTrapHelperException,
                                             java.net.UnknownHostException
        Create an SNMP trap, based on the content of an event derived from a TL1 autonomous message received by Tl1d, and forward the trap to the specified address and port. The type of trap created depends on the value of the "trapVersion" parameter. The "community" parameter determines the SNMP community string of the resulting trap, defaulting to "public".
        Parameters:
        event - The event upon which the trap content should be based
        destAddr - The address to which the trap should be sent
        destPort - The port to which the trap should be sent
        trapVersion - The SNMP version ("v1" or "v2c") of the trap
        community - The SNMP community string for the trap (defaults to "public")
        Throws:
        SnmpTrapHelperException - if any.
        java.net.UnknownHostException - if any.
        Throws - SnmpTrapHelperException if the event is not of the appropriate type.
        Throws - UnknownHostException if agent-addr resolution fails for the case of an SNMPv1 trap
        Throws - SnmpTrapHelperException if the event is not of the appropriate type.
        Throws - UnknownHostException if agent-addr resolution fails for the case of an SNMPv1 trap