Graphs with LineGraphBean

 

note.jpg

Note: Go through the Tutorial example on GET, GET NEXT (UI) that explain the development of a simple UI application.

 

WebNMS API provides two Graph beans namely LineGraphBean and BarGraphBean for plotting the values polled from one or more variables from the remote agent. The SnmpPoller component is used for polling the agent.

 

The SnmpPoller component polls the agent and generates ResultEvent events. The Graph bean components implement the ResultListener interface, which listens for these ResultEvent events. These result events contain the result data.

 

The following bean components can be used to develop this application.

In this example, we use the LineGraphBean component. We need to import the com.adventnet.snmp.beans and com.adventnet.snmp.ui packages along with JDK packages.

 

import javax.swing.*;

import java.awt.event.*;

import com.adventnet.snmp.beans.*;

import com.adventnet.snmp.ui.*;

 

We will define the class and the static main function required for the Java applications. We implement the ResultListener interface which listens for the SNMP responses. We also instantiate all the Java beans components we use. The PropertySettings bean is used to set properties of other beans. Refer Tutorial Example on GET, GET NEXT (UI).

 

public class SnmpGraph implements ResultListener

{

SnmpPoller poller = new SnmpPoller();

PropertySettings pptysettings = new PropertySettings();

LineGraphBean graph = new LineGraphBean();

public static void main(String args[])

{

 

Now, we set some parameters for the SnmpPoller bean.

 

poller.setTargetHost("localhost");

// can be changed using property settings bean

poller.setObjectID(".1.3.6.1.2.1.4.3.0");

// plots iplnReceives from RFC-1213-MIB

 

We need to add VetoableChangeListener to the PropertySettings bean and register a listener to receive the events from this bean. We register the SnmpPoller bean so that it is notified of any changes in the Property Settings bean and updated accordingly.

 

pptysettings.addVetoableChangeListener(poller);

 

Now, we add ResultListener to SnmpPoller for ResultEvents.

 

poller.addResultListener(graph>

 

Now, we implement setResult() of the Result Listener interface so that we get the following response.

 

public void setNumericResult(long l)

{

}

public void setResult(ResultEvent result)

{

//plot the results in the graph try

try {

graph.setResult(result.getNumericValue());

}

catch (DataException de) {

System.out.println("Error in getting agent data: "+ de + result.getErrorString());

}

}

public void setStringResult(String s){

}

 

Now, we can compile the source and view the results. The releaseResources() method of SnmpTarget class can be called to close the sessions. You can build similar applications effortlessly. For example, you can use BarGraphBean instead of LineGraphBean.

 

The screenshot of this tutorial example program is as follows.

 

snmpgraph.jpg

 

Refer the source code of the following examples for more information.

 

Tutorial Available In

Tutorial for using LineGraphBean

<tutorials/beans_ui_api/SnmpGraph.java>

Tutorial for using BarGraphBean

<tutorials/beans_ui_api/SnmpBarGraph.java>

Comprehensive example on using LineGraphBean

<examples/uiapplications/lineGraphDemo.java>



Copyright © 2012, ZOHO Corp. All Rights Reserved.