There are several methods in the SNMP protocol adaptor for sending traps to remote managers. They differ in their method signatures, depending upon whether or not you need to specify the destination host. When no host is specified, the SNMP protocol adaptor relies on the trap group definition in access control lists (ACL), as described below.
In all cases, traps are sent to the port specified by the current value of the TrapPort attribute on the SnmpAdaptorServer MBean. In our simple agent, we set the trap port to 8086, but this can be changed at any time by a custom MIB implementation or a management application.
This is the method that was used in Example 12-2 to send traps, along with its v2 equivalent (see the Javadoc API for a description of the parameters):
sendV1Trap( int generic, int specific, java.util.Vector varBindList )
sendV2Trap( SnmpOid trapOid, java.util.Vector varBindList )
Using these methods, you must first define the trap group in an access control list. See "Access Control Lists (ACL)" for a formal definition of the trap group and instructions for defining the ACL file when starting the agent. By default, these lists are file-based, but you may implement other mechanisms, as described in "Custom Access Control".
In this example we provide the following template file:
acl = { ... } trap = { { trap-community = public hosts = yourmanager } } |
The trap group lists all of the hosts to which the SNMP protocol adaptor will send every trap. A community definition associates a community name with a list of hosts specified either by their hostname or by their IP address. All hosts in a community definition will receive the trap in a PDU identified by the community name.
Since access control and trap recipients share the same file, you must fully define the access control when you want to send traps using the ACL mechanism.
Given this definition, traps will be sent to a host called yourmanager, and the community string of the trap PDU would contain the value public. By adding community definitions to this file, you can specify all hosts which will receive traps along with the community string for each host or group of hosts.
If the ACL file is not defined, or if the trap group is empty, the default behavior of these methods is to send a trap only to the localhost.
The other two methods of the SNMP protocol adaptor, one for each trap version, let you send a trap to a specified recipient:
sendV1Trap( java.net.InetAddress address, java.lang.String cs, ... )
sendV2Trap( java.net.InetAddress address, java.lang.String cs, ... )
In both cases, these methods take an address and a community string, in addition to the version-specific trap information. The address is an InetAddress object which is usually instantiated by its static methods getLocalHost or getByName. The latter method returns a valid InetAddress object when given a string representing a hostname or IP address.
The cs parameter is the community string, a name that the agent and manager exchange to help identify one another. The string given will be used as the community when sending the trap PDU.
Either one of these methods sends a trap to a single manager using a single community string. The ACL trap group mechanism is better suited to sending traps to multiple managers, though it requires the setup of a trap group. Note that even if a trap group is in use, the two methods above only send one trap to the specified host address.