![]() ![]() ![]() |
12.1 Overview
12.2 Generating Traps
12.3 Manager Details for Sending Traps
12.5 Testing the Agent with Traps
12.6 Knowing the Details of the Trap Generated
12.7 Validating and Filtering Outgoing Traps
12.8 Implementing Your Own Table for Storing Manager Information
12.9 Implementing Informs
12.10 Sending Traps for Log Messages, implemented using Log4j
12.11 Running the Example
In addition to retrieving data from the Managed Resource and sending response to them, Agents have the ability to send unsolicited messages to Managers when they detect some significant event. An unsolicited message of this sort is called Trap (in SNMPv1) or Notification (in SNMPv2 and SNMPv3). The WebNMS Java Agent supports both Traps and Notifications in all the three versions of SNMP. Informs, on the other hand, are Notifications sent from the Agent, with a request to the Manager to acknowledge the receipt of such a Notification.
For the SNMP Agent to generate a Trap/Notification, the respective MIB should have definition for that Trap/Notification. To define a Trap Type Construct (in SNMPv1 MIB Module) or Notification Type Construct (in SNMPv2c Module) using MIB Editor, please refer to the 4.6 Adding a Trap/Notification section.
12.2.1 Method Generated for Traps
For each Trap/ Notification type construct in the MIB modules of the agent, the following method will be generated in the file <MainFileName>Trap.java:
public boolean send<Notification Type/Trap Type name>(Hashtable notifHash, Hashtable instanceHash,Vector extraVarbinds){ return sendNotification(".1.3.6.1.4.1.2162.4.7",1,notifHash,instanceHash, extraVarbinds); |
Here the user is required to provide the inputs to the above methods. The required inputs are as follows:
notifHash: Here a Hashtable having Table/Scalar GroupName is provided as key and a Vector having list of objects under this group as element.
instanceHash: A Hashtable having TableName is provided as key and instance (as int array) as element. For sending traps/notifications for Table objects, instance (as int array) is required. If instance part is not provided the objects are considered as scalar.
extraVarbinds: List of SnmpVarBind objects. In case, the user need to send some extra variable bindings with this trap/notification, this option can be used.
With the above inputs, the method sendNotificfation() is called internally to send the trap/notification to the manager.
|
Note: The names provided for the managed objects in notifHash and instanceHash should be exactly the same as that of the MIB module used for agent development. |
12.2.2 Generating Trap on SET
It is possible to generate traps while setting a value to a variable for which trap is defined. The trap generation method is invoked on SNMP SET and is called from the respective xxxRequestHandler.java file. By default, this option is enabled . To view this,
Choose Project -> Settings menu from MIB Compiler UI.
Select General from the Source Generation panel.
You can find the Generate Trap on Set option checked.
12.3 Manager Details for Sending Traps
To notify the manager about the significant state of change in the agent, it is required to store the manager information in the agent side. To maintain the manager's information, we make use of the Trap Forwarding Tables of AGENT-SNMP-CONFIG-MIB.
Before moving into the Trap Forwarding Table, let us see a brief on IPv6 implementation in SNMP agent.
IPv4 is the generally used addressing model. Agent Toolkit introduces IPv6 (IP Version 6 protocol) model, which is a completely new and redesigned network-layer protocol defining an advanced addressing format of IPv4. IPv6 is a 128-bit identifier, addressing interfaces and set of interfaces. Agent Toolkit SNMP agent also provides the IPv6 addressing support apart from IPv4 for any network communication between the agent and the manager. Accordingly, the changes have been implemented in the trap forwarding table.
|
Note: When the agent needs to support IPv6 addressing model, it must run on JDK1.4.1 and later. |
12.3.1 v1v2TrapForwardingTable
The v1v2TrapForwarding Table with the OID .1.3.6.1.4.1.2162.10.3.1.2.2.1 has the following columns defined in it for storing v1/v2c Manager's information:
v1v2ManagerHostType (Index Column): The type of addressing model to represent the network address (IPv4/IPv6).
v1v2ManagerHost (Index Column): The network address of the Manager to which the Trap should be sent.
v1v2ManagerPort (Index Column): The Port Number of the Manager in which Traps will be listened.
v1v2ManagerVersion: SNMP Version of the Trap sent.
v1v2ManagerCommunity: Community in the Trap.
v1v2ManagerTimeOut: Time Out Value.
v1v2ManagerRetries: Retries Count.
v1v2ManagerStatus: Status of the Manager Entry.
12.3.2 v3TrapForwardingTable
The v3TrapForwardingTable with the OID .1.3.6.1.4.1.2162.10.3.1.2.2.2 has the following columns defined in it for storing v3 Manager's information:
v3ManagerHostType: The type of addressing model to represent the IP Address (IPv4/IPv6).
v3ManagerHost (Index Column): The IP Address of the Manager to which the Trap should be sent.
v3ManagerPort (Index Column): The Port Number of the Manager in which Traps will be listened.
v3ManagerVersion: SNMP Version of the Trap.
v3ManagerCommunity: Community in the Trap.
v3ManagerUserName: The name of the user.
v3ManagerUserSecModel: The Security Model used.
v3UserSecLevel: The Security Level of the user.
v3ManagerUserContextName: The Context Name of the user.
v3ManagerTimeOut: Timeout value.
v3ManagerRetries: Number of retries that can be made.
v3ManagerStatus: Row Status of the entry.
This table is multilingual by nature, i.e., it can forward any version of traps to the managers registered in the table. For a v3 trap, the agent calls the IsAccessAllowed() method according to VACM MIB for each variable binding OID and trap OID. If there is no access for the user in the VACM tables, the notification will not be sent to that management target.
There are two ways to add Manager entries in the Trap Forwarding Table. It can be done either : (1) Before Agent start-up or (2) During runtime.
To specify the manager entries at startup, you can use MIB Compiler UI, Text File / XML File, Runtime Memory or the API.
Using MIB Compiler UI
Choose the Project ->Settings menu of MIB Compiler UI. Please note that the version of the Agent has to be V1/V2c for working with v1v2TrapForwarding Table and V3 for working with V3TrapForwardingTable. This can be specified in the Project -> Settings menu -> General panel.
Select v1v2TrapForwardingTable or v3TrapForwardingTable in the Trap Panel.
Click Add. By default, you will have an entry with values : IPv4/IPv6, 127.0.0.1/0:0:0:0:0:0:0:1, 8003, Version 2, public, 5000, 0 and Active 1 in V1V2TrapForwardingTable and IPv4/IPv6, 0.0.0.0.0.0.0.1/127.0.0.1, 8003, Version 3, public, noAuthUser, 3, noAuthNoPriv, noAuth, 5000, 0, Active 1 for V3 Trap Forwarding Table. A wizard pops up wherein you can specify the manager entries (v1/v2 or v3).
Click OK. The manager entry is added.
Using Text File / XML File
The entries configured in MIB Compiler UI are stored in the configuration files, V1V2TrapForwardingTable.xml or V1V2TrapForwardingTable.txt after generation. These files can be found under the <Agent Toolkit Home>/snmpprojects/projectname/agent/bin/conf directory.
To specify the type of storage option,
Choose the Project -> Settings menu of MIB Compiler UI.
Select the v1v2 or v3TrapForwardingTable from the Trap panel.
Choose XML File or Text File from the Storage Type option. By default, XML File is chosen.
These files can also be edited to add manager entries. The V1V2TrapForwardingTable.xml given below has been edited for adding a new manager with host "128.0.0.1" and manager port "8004".
<?xml version="1.0" encoding="UTF-8"?> <Table> <row> <column name="managerHostType" value="1"></column> <column name="managerHost" value="127.0.0.1"></column> <column name="managerPort" value="8003" ></column> <column name="version" value="2" ></column> <column name="community" value="public" ></column> <column name="timeOut" value="1000" ></column> <column name="retries" value="0" ></column> <column name="rowStatus" value="1" ></column> </row>
//Adding new row <row> <column name="managerHostType" value="1"></column> <column name="managerHost" value="128.0.0.1" ></column> <column name="managerPort" value="8004" ></column> <column name="version" value="2" ></column> <column name="community" value="public" ></column> <column name="timeOut" value="1000" ></column> <column name="retries" value="0" ></column> <column name="rowStatus" value="1" ></column> </row>
</Table> |
Similarly, IPv6 entries can be added in the V1V2cTrapForwardingTable.xml or .txt file and v3 entries in the V3TrapForwardingTable.xml or .txt file. Please note that the agent has to be restarted for the changes to take effect. You can also access the new entries without restarting the agent. Make use of the following method in the main file for the agent to read the entries directly from the text file.
trapListener.setRefreshTrapTable(int refreshTime) |
This method has to be added after the code for trapForwardingTable Request Handler instantiation. Please note that this method is in the SnmpTrapService class common to both V1V2 and V3 Trap Forwarding Tables.
Using Runtime Memory
Runtime memory can be used to store the Manager information in the Agent Memory . Using this option will not store the entries in text files or xml files.
To use the run time memory for adding Manager entries dynamically,
Choose the Project -> Settings menu of MIB Compiler UI.
Select v1v2TrapForwardingTable or v3TrapForwardingTable from the Trap panel.
Select Runtime Memory from Storage Type.
Add entries using the Add option.
After this, follow the steps given in adding entries "From the Manager" section of During Runtime (12.4.2) explained below. Please note that once the agent is killed, the added entries are removed from the memory.
Using API calls
For adding an entry through API calls, an entry similar to the one given below has to be included next to the trapListener.setTrapForwardingTable (forwardingTable) method in the generated Main file.
For v1v2TrapForwarding Table
forwardingTable.addForwardingEntry(com.adventnet.snmp.snmp2.agent.SimpleTrapForwardingTable.createForwardingEntry(new Integer(1), "127.0.0.1", new Integer(8004), new Integer(2), "public", new Long(5000), new Long (0)); |
For v3TrapForwarding Table
forwardingTable.addV3ForwardingEntry(com.adventnet.snmp.snmp2.agent.V3SimpleTrapForwardingTable.createV3ForwardingEntry(new Integer(1), "127.0.0.1", new Integer(8004), new Integer(3), "public", "noAuthUser", new Long(3), new Integer(1), "noAuth", new Long(5000), new Long (0)); |
After adding these entries save the Main file and compile it. As per the entries added in V1 V2 Trap Forwarding Table / v3 Trap Forwarding Table, Traps will be forwarded to the Manager running at port 8004. You should be able to receive Traps at port 8004.
12.4.2 During Run Time (Dynamic Registration)
Manager entries can be added dynamically during runtime.
From the Manager
To add an Entry from remote,
Load the AGENT-SNMP-CONFIG-MIB in MIB Browser application.
Select v1v2TrapForwardingTable or v3TrapForwardingTable from the traptables module of agentConfiguration group.
By selecting the respective table and clicking SNMP Table icon in MIB Browser will open up a wizard wherein entries can be added to the Trap Forwarding Table on clicking Add.
The entries added from remote get updated in the text/xml file.
Please note that it is possible to access the table from remote only if "Remote Configuration" is enabled in the Trap Panel (V1V2TrapForwardingTable/V3TrapForwardingTable) of Project -> Settings menu in MIB Compiler UI.
You can also enable "Remote Configuration" option using the following API call if it is not chosen before generation.
forTable = new com.adventnet.snmp.snmp2.agent.SimpleTrapForwardingTable(forwardingTable); forTable.addRegistrationListener(hdlr); |
This method has to be included in the initAdventNetSnmpAgent() method.
By default, the generated agent sends coldstart trap at the agent startup to the managers being configured. The user can disable the trap at agent startup by enabling the option provided in the MIB Compiler UI by choosing Project->Settings->Source Generation ->Disable Startup Trap.
The Startup Trap can also be disabled by adding the following code in the main file before restartSnmpAgent( ) method
super.disableStartUpTrap(true); |
12.6 Testing the Agent for Traps
To test the Trap feature,
Start the MIB Browser application.
Open the Trap Viewer in MIB Browser application and start listening for Traps in the Manager's port specified in the Trap forwarding Table.
Load the respective MIB in the MIB Browser application and start the Agent at the port (say default port) 8001 using run.bat or run.sh from <Agent Toolkit Home>/snmpprojects/projectname/agent/bin directory .
At first, you receive a cold start trap. The details of the Trap can be viewed by selecting the Trap and right clicking on it.
Now, SET a value to a variable for which Trap is defined.
Value would be set for the variable and a Trap will be received in the Manager's Port (mentioned in the Trap Forwarding Table).
12.6 Knowing the Details of the Trap Generated (Reliable Traps)
There are times where the generated Traps could have been missed or lost during communication. In a situation like this, the Manager might not be aware as to how many traps were generated by the Agent and the details of those Traps. To overcome the uncertainty involved in receiving traps, the option of sending Reliable Traps is provided.
12.6.1 Implementing Reliable Traps
This feature can be implemented either from MIB Compiler UI or by using API calls.
Using MIB Compiler UI
Select Project -> Settings menu from the menu bar of MIB Compiler UI.
Select General from the Trap Panel.
Check Reliable Traps option.
By default, the Maximum number of Traps that can be stored by the Agent is 10 and so the last 10 traps are stored here. This value is configurable.
Using API Calls
Add the following piece of code in the Main file generated before the code for restartSnmpAgent.
trapListener.addRegistrationListener(hdlr); trapListener.setReliableTraps(true, 10); |
12.6.2 Accessing the Trap Details
To know the information of Traps generated from the Agent's two tables, namely the notificationLogTable and notificationVarbindTable have been included under the notificationLogs group of AGENT-SNMP-CONFIG-MIB file. These tables help the Manager in finding out the details of the Traps by just sending a query to the Agent. Try acquiring the Trap details by loading the MIB in MIB Browser and querying.
The notificationLogTable stores information of the Traps sent from the Agent (Request ID, Version of the Trap, Time etc.,). The Varbind Table stores information of the varbind values present in the Trap PDU (such as OID, Value, Type, etc.,).
12.7 Validating and Filtering Outgoing Traps
There are times where the Traps generated by an Agent might be of no use to a Manager. In this case, the Agent drops those Traps without sending them to that particular Manager. This restriction is specified in a condition whereby every generated trap checks this condition and it is validated. If necessary, it is sent to the Manager, else dropped.
To implement this feature, add the following piece of code in the Agent Main File for implementing the feature. Instead of instantiating SnmpTrapService, you have to instantiate the TrapValidator class (replace SnmpTrapService with TrapValidator) wherever it is present in the Main file that is generated, along with the correct constructor that you are using in this class.
class TrapValidator extends SnmpTrapService{ /** * The Constructor with TrapSessionPort from where the * Traps will be sent from the Agent. */ public TrapValidator(int port){ super(port); } /** * The method to Validate the Traps. This will get called before the sendTrap method. * Sends the Traps to the Managers in the TFTable. You have to Instrument the * validating conditions in this method fulfilling your requirement. * @param pdu The Trap SnmpPDU. * @param managerNode The ManagerHost for which the Trap is targeted. * @param managerPort The ManagerPort for which the Trap is targeted. * @returns true if the Trap is Valid and false if the Trap is Invalid. */ // Sample Condition // This will restrict any Trap from the Agent, if the TargetPort is 8004 // and Host is localhost. public boolean validateTrap(com.adventnet.snmp.snmp2.SnmpPDU pdu, String managerHost, int managerPort){ if (managerPort == 8004 && managerHost.equals("127.0.0.1")){ return false; } else { return true; } } } |
Load Agent-Sample-MIB in MIB Compiler. Let the Agent be a v2c Agent.
The v1v2 Trap Forwarding Table in Project -> Settings menu should have an entry with the Manager port as 8004 and Host as 127.0.0.1.
Generate code for the same.
Instrument the Main file by adding the piece of code given above. SnmpTrapService should be replaced with TrapValidator.
Compile the code. The Agent gets created at the output directory.
Test the Agent, by sending a SET request to a Trap defined variable.
Open the Trap Browser and listen for Traps. If Traps are listened at port 8004, this particular Trap should not be received.
12.8 Implementing Your Own Table for Storing the Manager Information
You can also define your own Trap Forwarding Table instead of using the default Trap Forwarding TABLE in AGENT-SNMP-CONFIG-MIB. To achieve this purpose: -
Define a similar Trap Forwarding Table in your MIB using the OBJECT TYPE TABLE construct in MIB Editor. Please refer to "Adding a Table" in Creating a MIB section for using the construct.
The OID structure of the newly defined table should be same as the TrapForwardingTable in AGENT-SNMP-CONFIG-MIB after the enterprise level. (.1.3.6.1.4.1. your enterpriseOID) .
Add the following piece of code above the code for restartSnmpAgent in the Main file, if it is a v2c Agent. The entry is similar for a V3 Agent also. trapListener.setForwardingEntryOID(".1.3.6.1.4.1.2345.10.3.1.2.2.1.1");
trapListener.setForwardingEntryOID(".1.3.6.1.4.1.2345.10.3.1.2.2.1.1"); trapListener.addRegistrationListener(hdlr); |
.1.3.6.1.4.1.2345.10.3.1.2.2.1.1 is a sample entry oid.
Now, compile the code and test the Agent for Traps.
Traps will be generated to the interested Managers as specified in your Trap Forwarding Table.
SNMP Notifications can be sent as Traps or Inform requests. Traps are unreliable because the Manager does not send any acknowledgment on receiving the Trap. So, the Agent cannot determine if the Trap was received by the Manager. However, an SNMP Manager that receives an Inform request acknowledges the message with an SNMP response PDU. If the Manager does not receive an inform request, it does not send a response. If the Agent does not receive a response at all, the inform request can be sent again.
12.9.1 Implementing Informs
Informs can be implemented by adding the following piece of code in the generated Main file before the code for restartSnmpAgent
trapListener.setInformFlag(true); |
Save the file and compile the code. This would send all the Trap messages to the Manager in the form of Informs and wait for an acknowledgment message. To know more on testing, please refer to Testing Informs topic.
12.9.2 Testing Informs
To test the same,
Start the MIB Browser application.
Load the MIB (using which the Agent is created) and start the Agent.
Now, SET a value to a variable for which Trap is defined. (Assuming that "Generate Trap on Set" option is enabled in the MIB Compiler UI Project -> Settings menu before generation).
Value would be set for the variable and an Inform Request will be sent to the Manager.
This Inform Request sent to the Manager can be viewed using Trap Viewer icon. Hence, open the Trap Viewer wizard before sending a request
This will listen for Inform requests. Please note that it is not possible to send Informs from an SNMPv1 Agent.
12.10 Sending Traps for Log4j Log Messages
Agent Toolkit has a facility for sending Traps for the Log4j Log Messages using SNMPTrapAppender utility. To enable SNMPTrapAppender utility, a new class extending SNMPTrapAppender class of SNMPTrapAppender utility is bundled with the product. Using this class, there are two ways by which Traps can be sent for the Log4j log messages. They are :
Using the Agent's Own SnmpTrapService Method and
Using the XML File Configuration
12.10.1 Using the Agent's own SnmpTrapService.
In this approach, the existing SnmpTrapService (i.e. in WebNMS's Agent) is used to send the Log messages as Traps. Here the Traps are forwarded to all the Managers present in the Trap Forwarding Table which is associated with the Agent's SnmpTrapService. The Trap PDU is filled with the details of the Entries in the Forwarding Table.
The following are the steps that illustrates the method:
Add the following code in the generated
main file.
// The logger to log the messages private org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(getClass().getName()); // boolean to specify whether the log4j has been initilized private boolean log4jInitialized = false; /** * This method initilaizes the SNMPTrapAppender with the Agent's own TrapService. * This must be used if the Traps are to be forwarded based on the Forwarding Table * associated with this Agent. */ private void initAppenderWithTrapService(){ // Instantiating Trap Appender with our Agent's SnmpTrapService com.adventnet.utils.appender.SnmpTrapAppenderExtension appender = new com.adventnet.utils.appender.SnmpTrapAppenderExtension(); appender.setName("WARN_LOG"); appender.setImplementationClassName("com.adventnet.utils.appender.SnmpTrapSenderImpl"); appender.setApplicationTrapOID(".1.3.6.1.4.1.2162.4.3.2"); appender.setEnterpriseOID(".1.3.6.1.4.1.2162"); appender.setTrapIndex(4); appender.setThreshold(org.apache.log4j.Level.WARN); appender.setForwardStackTraceWithTrap("true"); appender.setLayout(new org.apache.log4j.PatternLayout()); appender.setTrapSource(1); appender.setTrapListener(trapListener); log4jInitialized = true; logger.addAppender(appender); logger.debug("This is a Debug Message"); logger.error("An Error occurred here"); logger.info("Here's some info about that"); logger.fatal("Help! I'm dying out here!"); logger.fatal("Here's an Exception!", new Exception("Exception message")); } |
Call the method initAppenderWithTrapService( ) in the generated main file after restartSnmpAgent() method.
The log messages that are at the higher level of the threshold being set will be converted to trap. Here the default threshold level is WARN and all the log messages higher to this level will be sent as trap by using this utility.
12.10.2 Using the XML file Configuration
In this approach, the SNMPTrapAppender is configured with the details read from a XML file. Here the Log Message Traps are forwarded to a Single Manager specified in the XML file. A sample appender configuration file, SnmpTrapAppenderTestConfig.xml is bundled with the product under <Agent Toolkit Home>/conf/appenderConfig/ directory.
The following are the steps that illustrates the method:
Copy the configuration file to <Agent Toolkit Home>/snmpprojects/projectname/agent/bin/conf directory.
Add the following code in the generated main file.
// The logger to log the messages private org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(getClass().getName()); // boolean to specify whether the log4j has been initilized private boolean log4jInitialized = false; /** * Method to initialize the SnmpTrapAppender based on a XML config file. */ public void initAppenderWithConfigFile(){ String confFile = "conf/SnmpTrapAppenderTestConfig.xml"; if (!log4jInitialized) { try { org.apache.log4j.xml.DOMConfigurator.configure(confFile); log4jInitialized = true; } catch (Exception ex) { org.apache.log4j.helpers.LogLog.error("Error initializing logging!", ex); } } if (!log4jInitialized) org.apache.log4j.helpers.LogLog.error("Logging initialization error."); else { logger.debug("This is a Debug Message"); logger.error("An Error occured here"); logger.info("Here's some info about that"); logger.fatal("Help! I'm dying out here!"); logger.fatal("Here's an Exception!", new Exception("Exception message")); } |
Call the method initAppenderWithConfigFile( ) in the generated main file after restartSnmpAgent( ) method.
The log messages that are above the threshold level will be converted to trap. Here by default, the threshold level is DEBUG and all the log messages above this level will be sent as traps.
Thus the utility can be used according to the environment chosen.
For simple testing with WebNMS Agent Toolkit,
Create a project.
Generate the code for the agent.
Add "log4j-1.2.8.jar" and "snmpTrapAppender_1_2_91.jar" in the classpath settings of run.bat\sh file. These jars are available in <Agent Toolkit Home>/jars directory.
Implement SNMPTrapAppender functionality by using anyone of the above methods.
Save the file & Compile the code.
Start the TrapBrowser at localhost : 8003 port from the MIB Browser application.
Also, start the Agent.
You will get the following traps namely :
The Standard coldStart trap,
Traps, if the log messages are above the threshold level.
Thus Traps can be sent for any kind of log messages.
To know more about Traps, please go through the example available in <Agent Toolkit Home>/examples/snmp/traps directory. The "simpletrap" example sends a Trap to the Agent on every SET request. The main file TrapExample.java forwards the trap to the Manager specified in it. The readme.html will help you understand how to run this example.
![]() ![]() ![]() |