![]() |
|||
![]() |
![]() ![]() |
![]() |
![]() ![]() |
![]() |
![]() ![]() |
![]() |
| |||
After the MIB is initialized, it only needs to be bound to the SNMP adaptor, as in the other agents; except that in the standalone case, we use the setSnmpAdaptor method which takes a direct reference to the SNMP adaptor instead of an object name. That is all you need to do when programing a standalone SNMP agent. You can subclass the MIB and override the group MBean factory method to instantiate your own customized MBean class, replacing for instance new Interfaces() with new InterfacesImpl(), as shown in the code example. 16.4.1 Running the Standalone Agent Example
|
$ java -classpath classpath StandAloneSnmpAgent nbTraps |
If you have not copied the jdmk.acl file to the configuration directory, add the following property to your command line:
-Djdmk.acl.file=examplesDir/current/Snmp/Agent/jdmk.acl |
You should see the same initialization messages as with the simple agent. Then, you should see the agent sending out a trap every two seconds. If you have an SNMP manager application, you can send requests to the agent and receive the traps. See "Developing an SNMP Manager" for example applications you can use.
The only limitation of a standalone agent is that you cannot access or manage the SNMP adaptor and MIB MBeans in the dynamic management sense. However, the SNMP adaptor still relies on the InetAddressAcl file for access control and traps, unless you have customized the InetAddressAcl mechanism, and you can implement other security schemes as described in 19.2.1 Enabling User-Based Access Control.
Press Control-C when you have finished running the standalone agent.
It is possible to create multiple SNMP agents in the Java virtual machine (JVM). A multiple agent is an agent that instantiates more than one SNMP adaptor server. This enables you to create multiple SNMP MIBs using that agent, with each MIB having its own individual security configuration. The main advantage of this is to reduce the burden on the JVM.
You can create multiple agents using all three versions of SNMP. However, the multiple agent demonstrated in the following example is an SNMPv3 agent that creates and registers two SNMP adaptor servers in the MBean server. Registration in the MBean server enables the MIBs generated to be managed through the HTML adaptor.
Example 16-10 MultipleAgentV3 Example
public class MultipleAgentV3 { static SnmpV3AdaptorServer snmpAdaptor1 = null; static SnmpV3AdaptorServer snmpAdaptor2 = null; //Set the number of traps // private static int nbTraps = -1; public static void main(String args[]) { MBeanServer server; ObjectName htmlObjName; ObjectName snmpObjName1; ObjectName snmpObjName2; ObjectName mibObjName1; ObjectName mibObjName2; ObjectName trapGeneratorObjName; int htmlPort = 8082; // Initialize trace property. // [...] // Create and start the HTML adaptor. // [...] // Create and start the first SNMP adaptor. int snmpPort = 8085; snmpObjName1 = new ObjectName(domain + ":class=SnmpAdaptorServer,protocol=snmp,port=" + snmpPort); snmpAdaptor1 = new SnmpV3AdaptorServer(snmpPort); server.registerMBean(snmpAdaptor1, snmpObjName1); snmpAdaptor1.registerUsmMib(server, null); snmpAdaptor1.start(); // Send a coldStart SNMP Trap. // Use port = snmpPort+1. // print("NOTE: Sending a coldStart SNMP trap"+ " to each destination defined in the ACL file..."); snmpAdaptor1.setTrapPort(new Integer(snmpPort+1)); snmpAdaptor1.snmpV1Trap(0, 0, null); snmpAdaptor1.enableSnmpV1V2SetRequest(); println("Done."); // Create and start the second SNMP adaptor. // snmpPort = 8087; snmpObjName2 = new ObjectName(domain + ":class=SnmpAdaptorServer,protocol=snmp,port=" + snmpPort); Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MISC, "Agent", "main", "Adding SNMP adaptor to MBean server with name \n\t"+ snmpObjName2); println("NOTE: SNMP Adaptor is bound on UDP port " + snmpPort); // Instantiate second adaptor instantiation. // SnmpEngineParameters params = new SnmpEngineParameters(); params.setSecurityFile("jdmk2.security"); //Create the adaptor passing it the parameters. snmpAdaptor2 = new SnmpV3AdaptorServer(params, null, null, snmpPort, null); server.registerMBean(snmpAdaptor2, snmpObjName2); snmpAdaptor2.registerUsmMib(server, null); snmpAdaptor2.start(); // Send a coldStart SNMP Trap. // Use port = snmpPort+1. // print("NOTE: Sending a coldStart SNMP trap"+ " to each destination defined in the ACL file..."); snmpAdaptor2.setTrapPort(new Integer(snmpPort+1)); snmpAdaptor2.snmpV1Trap(0, 0, null); println("Done."); // Create the first and second instances of MIB II // (RFC 1213) and add them to the MBean server. // mibObjName1 = new ObjectName("snmp:class=RFC1213_MIB"); mibObjName2 = new ObjectName("snmp:class=RFC1213_MIB_2"); // Create 2 instances of the customized MIB // RFC1213_MIB mib2 = new RFC1213_MIB_IMPL(); RFC1213_MIB mib2_2 = new RFC1213_MIB_IMPL("RFC1213_MIB_2"); server.registerMBean(mib2, mibObjName1); server.registerMBean(mib2_2, mibObjName2); // Bind the SNMP adaptor to the MIB in order to make the MIB // mib2.setSnmpAdaptor(snmpAdaptor, "TEST-CONTEXT"); mib2_2.setSnmpAdaptor(snmpAdaptor2, "TEST-CONTEXT"); // //Multiple agent is ready to answer SNMP requests. // } catch (Exception e) { e.printStackTrace(); } } } |
![]() ![]() |