![]() |
|||
![]() |
![]() ![]() |
![]() |
![]() ![]() |
![]() |
![]() ![]() |
![]() |
| |||
16.3.2 Traps in the Agent and AgentV3 ExamplesBefore starting the SNMP agent again, edit the jdmk.acl file to replace the occurrences of yourmanager with the name of a host running an SNMP manager. You can start the example SNMPv3 agent by replacing Agent with AgentV3. Start the agent, specifying the InetAddressAcl file as a property:
In these commands, nbTraps is the number of traps that the agent sends. Set it to a small integer to avoid too much output. If you omit this parameter, traps are sent continuously. If you do not have an SNMP manager or a second host, do not copy the InetAddressAcl file or specify it as a property. In the absence of the trap-community definitions, the traps are addressed to the trap port on the local host. And even if no manager is running, we can still see the agent sending the traps. See 17.1.4 SNMP Trap Handler for details about receiving traps.
|
; import com.sun.management.comm.SnmpAdaptorServer; public class StandAloneSnmpAgent { static SnmpAdaptorServer snmpAdaptor = null; private static int nbTraps = -1; public static void main(String args[]) { // Parse command line and enable tracing [...] try { // The agent is started on a non standard SNMP port: 8085 int port = 161; port = 8085; snmpAdaptor = new SnmpAdaptorServer(port); // Start the adaptor snmpAdaptor.start(); // Send a coldStart SNMP Trap snmpAdaptor.setTrapPort(new Integer(port+1)); snmpAdaptor.snmpV1Trap(0, 0, null); // Create the MIB you want in the agent (ours is MIB-II subset) RFC1213_MIB mib2 = new RFC1213_MIB_IMPL()(); // Initialize the MIB so it creates the associated MBeans mib2.init(); // Bind the MIB to the SNMP adaptor snmpAdaptor.addMib(mib2); // Optional: create a LinkTrapGenerator int ifIndex = 1; LinkTrapGenerator trapGenerator = new LinkTrapGenerator(ifIndex, nbTraps); trapGenerator.start(); } catch (Exception e) { e.printStackTrace(); } } // Needed to get a reference on the SNMP adaptor object static public SnmpAdaptorServer getSnmpAdaptor() { return snmpAdaptor; } } |
As this example demonstrates, the standalone agent uses exactly the same MIB MBeans, with the same customization, as our other agents. However, instead of registering them in the MBean server, they are only instantiated. And whereas the registration process creates all subordinate MBeans of the MIB, now we must call its init method explicitly.
The init method performs the same function as the preRegister method, only it does not register the MBean with the MBean server. Each of the group MBeans then has two constructors, one with and one without a reference to the MBean server. When table entries are added dynamically, the corresponding object only registers the new entry's MBean if the MBean server reference is non-null; that is, only if the MBean is not instantiated in a standalone agent.
The mibgen tool automatically generates both the pre-registration methods and the init methods in the MIB MBeans. Therefore, no special action is necessary to use them in either a regular agent or a standalone agent. If you use a standalone agent for memory considerations, you can remove the registration process from the generated MBean and only customize the init process.
Example 16-9 Customizations in the Generated RFC1213_MIB_Impl.java File
class RFC1213_MIB_IMPL extends RFC1213_MIB { public RFC1213_MIB_IMPL() { super(); } /** * Passing it a name in order to register the same mib in 2 MBeanServer. */ public RFC1213_MIB_IMPL(String name) { super(); mibName = name; } protected Object createSnmpMBean(String groupName, String groupOid, ObjectName groupObjname, MBeanServer server) { // Note that when using standard metadata, // the returned object must implement the "InterfacesMBean" // interface. // if (server != null) return new SnmpImpl(this,server); else return new SnmpImpl(this); } protected Object createSystemMBean(String groupName, String groupOid, ObjectName groupObjname, MBeanServer server) { // Note that when using standard metadata, // the returned object must implement the "InterfacesMBean" // interface. // if (server != null) return new SystemImpl(this,server); else return new SystemImpl(this); } protected Object createInterfacesMBean(String groupName, String groupOid, ObjectName groupObjname, MBeanServer server) { // Note that when using standard metadata, // the returned object must implement the "InterfacesMBean" // interface. // if (server != null) return new InterfacesImpl(this,server); else return new InterfacesImpl(this); } } |
![]() ![]() |