8.0 Instrumenting the Generated Code

 


8.1 Overview
8.2 Generated Code Structure

8.3 Instrumenting the Generated Code


 

8.1 Overview

 

To get the device or application specific data, a few files in the generated source files need to be instrumented to include the desired function. This will depend on how the MIB is defined, how the data associated with the MIB can be accessed, how it needs to be delivered to the Manager etc. This section clearly explains the files generated for a MIB and the steps involved in instrumenting the same for retrieving application specific details.

 

8.2 Generated Code Structure

 

On generating code for a MIB, the following files get generated by default in <Agent Toolkit Home>/snmpprojects/projectname/agent/src/com/myCompany/myPackage directory.

Let us see in detail how these files are organized.

 

8.2.1 Main File

 

The Agent Main file is normally generated as WebNMSSnmpAgent.java. This Main File name can be modified as per your requirement by specifying it in the text field provided for Agent Name in Project -> Settings menu bar -> Source Generation Panel -> General of MIB Compiler UI.

 

The Main methods are generated in the Main file and it is very important to know the code organization of this file as this would help you implement methods at a later stage for attaining specific functionality.

 

Composition of Main File

 

Any main file will be organized as given below.

8.2.2 Request Handler File for a Scalar/Tabular Group

 

Request Handler Files get generated for every group of the MIB and these handler files takes care of forwarding the requests from the Manager to the respective OID. Thus, the basic functionality of this file is to register each module with the Agent and handle all requests from the Managers such as
GET, GET-NEXT, and SET. The Request Handler files also handles row creation in case of a table. The methods get generated by default and you need not edit this file.

 

XXXRequestHandler.java file for a Scalar Group

XXXRequestHandler.java file for a Tabular Group

8.2.3 Instrument File for a Scalar

 

These are the files that get created for every scalar group and implements InstrumentHandlerInterface. The file can be edited to retrieve specific or expected values, i.e. you can modify this file depending upon your requirements. Each instrument file contains a getter and setter for every scalar variable, and also a getAttributes and setAttributes method.

 

The getAttributes and setAttributes method will be called from their respective handlers when there is a GET, GET-NEXT, or SET requests to the scalar variables. The advantage of using these methods are

8.2.4 Entry File for a Table

 

These files get generated for tables and are similar to Instrument files generated for scalar variables, but extends BaseTableEntry class. They contain getters and setters for each variable in the table and also getAttributes and setAttributes method. These entry files are stub files to perform user implementation and pertains to the same advantages mentioned in the Instrument file.

 

8.2.5 Persistence Files

 

xxxFileToVector.java file is generated when the storage option chosen during generation is Text file. This file implements com.adventnet.snmp.snmp2.agent.UpdateListener and has methods such as readFromfile() and writeIntoFile() for reading and writing into the text file. It is generated to make it simple for the users and to make the data available to the Agent in a flat file. You can also disable the option for generating "xxxFileToVector" by choosing RAM storage option in the MIB Compiler UI. This does not store the values anywhere except in runtime memory.

Alternatively, the xxxXMLToVector.java file is generated when the XML storage option is chosen before generation.

 

8.2.6 Notification File

 

<MainFileName>Trap.java: This file has required methods for sending traps/notifications for the objects, which are grouped under Notification-Type/Trap-Type macros in the MIB module.

 

8.3 Instrumenting the Generated Code

 

Editing a file for a desired function (Instrumentation) needs to be done based on the application and the requirement of the user. This section has been explained assuming that the code generated for RFC1213 MIB is instrumented for retrieving specific values from the "sysDescr" scalar node ofsystemGroup and "ifDescr" column of ifTablegroup. Assume that the Agent is a v2c Agent.

 

Editing Files Generated for Scalars

 

The getter method of the scalar variable sysDescr in the System group of RFC 1213 MIB would be generated as follows in the SystemInstrument.java

 

/**

* Handles the SNMP Get Request for sysDescr

public String getSysDescr()
throws AgentException {

// Fill up the stub with required processing

return sysDescr;

}

On sending a GET request, the value of the sysDescr scalar variable is retrieved.

 

You can fill in your own stubs here. The instrumentation code has to be filled between these tags in order to support code merging when regenerated.

 

/* User code starts here */
/* User code ends here */

 

These are the custom tags which can be used anywhere in the generated code. You just have to include your code in the file within these tags. When code is regenerated, the MIB Compiler looks for these tags and preserves the changes in the newly generated code. If  either of the tags is not given, code merging will not be proper. Please refer to the topic "Code Merging" for more information.

 

Similarly, the setter method of the scalar variable sysDescr would be generated as follows.

/**
* Handles the SNMP Set Request for sysDescr
* @param value - The String value to be set
*/

public synchronized void setSysDescr(String value){

// Fill up the stub with required processing

sysDescr = value;
}

 

On sending a SET request to this scalar object with a new value, the new value is set to the OID. You can also edit these files to fill up your required processing.

 

Editing Files Generated for Tables

 

The getter methods of the table variable "ifDescr" in the ifTable group would be generated as follows in the ifEntry.java

/**
* Handles the SNMP Get Request for ifDescr
*/
public String getIfDescr()
throws AgentException {

// fill up with necessary processing

return ifDescr;
}

 

On doing a GET request for this column with the instance value would retrieve the value of that columnar variable.

Similarly, the setter methods of the table variable "ifDescr" would be generated as follows. On sending a SET request to this variable the value will be SET.

/**
* Handles the SNMP Set Request for ifDescr
* @param value - The String value to be set
*/

public synchronized void setIfDescr(String value){
// Fill up with necessary processing

ifDescr = value;
}

 

Edit these files to include the required values.

 

Code Merging

 

Code merging, is an option provided for users who prefer to shift between releases. It is mainly used for migration purposes. Say, if a developer using 5.0 version of Agent Toolkit decides to migrate to the 5.1 version, then he can make use of this Merging option wherein the manually added code (using the tags) is merged with the code, present in the file generated by the new version of the toolkit, to attain the functions available in the old version.If this option is not enabled before regenerating a file, all the code added by the user would be lost.

The user code is normally  added to the generated files using the following tags.

 

// User code starts here
//Add your code here....
// User code ends here

OR

/* User code starts here */

Add your code here
/* User code ends here */


OR

// WebNMS code ends here

// Your code can be added here
// WebNMS code starts here

 

These are the custom tags which can be used anywhere in the generated code. The user has to just include his code in the file with these tags. When regenerated, the Mib Compiler looks for these tags and preserves the changes in the newly generated code. If either of the tags are not given merging will not be proper. Please note that the last tags are also supported and when these get generated in certain files, the user has to just include the codes between these tags which is generated, by default. Thus code merging is supported on all generated java files.

 

Enabling Code Merging Option in the Mib Compiler UI

 

This feature is enabled by default in the tool. It can be enabled/disabled in the Mib Compiler UI as follows:
  1. Click Project->Settings->Source Generation->General
  2. Enable/Disable Merge with previous stub files in the General panel of Source Code Generation
 

Copyright © 2013, ZOHO Corp. All Rights Reserved.