Build your own SNMP Monitoring Tool

SNMP is a protocol used widely for monitoring and extracting status information of a working device. Using SNMP protocol, you can extract the present information about a certain device. It is widely used for monitoring network devices such as Routers. But, practically any device which supports SNMP protocol can be used. As an example consider a SNMP enabled network printer inside an organization. The Network admin can issue necessary commands to the SNMP enabled device to get the status of the printer to check the status of the printer to see whether it is working as it should. Furthermore, the SNMP enabled device can be configured to issue an alert once a condition is met. As an example, there may be a Network printer which can be configured to make an alert once the ink level drops below a certain level. The breadth of SNMP is determined by the capabilities embedded into the device itself.
Another very useful scenario is where a JVM(Java Virtual Machine) can be SNMP enabled to be monitored by the admin. Suppose, there is a server with an instance of JVM running. Then the JVM can be configures to issue a notification when the JVM crashes. Thus, the admin can take necessary action to restore the Server soon.

This post is intended for an audience with fundamental knowledge in Java and SNMP to build a tool for SNMP Monitoring.

I used the snmp.jar available for download with NetBeans IDE for the development purposes.
(You can also use snmp4j.jar which is very much similar to this)
There is not much of initialization stuff to do. You just need to add the Jar file to your project. It is very easy!!!

Once added, you can begin using the Classes provided by the API to get the job done.

Before we move on, lets get an idea of OID(Object Identifiers) related to SNMP. OID s are very much used in SNMP to give a specific ID for an attribute available by the device. It is arranged in a tree structure and is subjected to a specific layout. There are lots of documents available to get an in depth view of OIDs. As far as we are concerned, we would be using a set of OIDs which all SNMP enabled devices will support which happen to be the basics. Some of these OIDs include the Name of the device, Uploaded Bytes, Downloaded Bytes, Total Up time etc. We would be using these OIDs for the demo.

snmp.jar provides a way of querying an SNMP enabled device for such OIDs. We can do it in the following manner.

/*****************************************************************/
int version=0;
String communityString="public";
InetAddress host=InetAddress.getByName("192.168.1.1');
SNMPv1CommunicationInterface comInterface=new SNMPv1CommunicationInterface(version,host,communityString);

//This correspods to the OID =1.3.6.1.2.1.5.0 which gives the name of the device
String identifier="1.3.6.1.2.1.1.5.0";


SNMPVarBindList list=comInterface.getMIBEntry(identifier);
SNMPSequence pair=(SNMPSequence)list.getSNMPObjectAt(0);

SNMPObject obj=pair.getSNMPObjectAt(1);
SNMPOctetString octet=(SNMPOctetString)obj;

octet.toString();

/***************************************************************/


First consider the LOC

int version=0;

This is used to indicate that we intend to use SNMP version 1 (There are version 2 supporting devices. In such case, it is said to use 1 instead of 0.)




Next consider the following LOC

String communityString="public";

This indicates to use the values which are accesed by public. If you do not intend to change them, then this is fine.




The following LOC is used to reffer to the device IP.
InetAddress host=InetAddress.getByName("192.168.1.1');

InetAddress Class is really useful for representing an IP address. It provides a static method 'getByName' which accepts a String of the IP. Thus, we use it for handling IP addresses.




What we actually do is to first establish a Communication channel between us and the device and we pass the necessary OIDs through it. This is clearly exhibited through the following LOC.

SNMPv1CommunicationInterface comInterface=new SNMPv1CommunicationInterface(version,host,communityString);

The SNMPv1CommunicationInterface class's constructor accepts three parameters.
i) Version
ii) Host
ii) Communication String

We simply pass the relevant values which we produced earlier.

NOTE: Actually I cannot understand why the class's constructor SNMPv1CommunicationInterface(version,host,communityString) has the parameter version. Since this class's name states SNMP Version 1, then it should NOT be used for making connections with SNMP version 2 devices. There should be a seperate class for that. If the class can be used to make connections with several versions of SNMP, then the class name should be more abstract, say SNMPCommunicationInterface, where the version is not included in the Class's name. Anyway, I feel there is a slight mismatch in the naming convention used. Regardless of that, this is a very handy library to use and if anyone can clarify the issue, I'd be highly grateful.




This is the OID representing the name of the device.
String identifier="1.3.6.1.2.1.1.5.0";




Communication interface object has a method 'getMIBEntry' which we use to send the necessary OID. We use this to send the necessary OID to the device.

SNMPVarBindList list=comInterface.getMIBEntry(identifier);

This method returns an Object of type SNMPVarBindList. This is very much like an array of objects.





SNMPSequence pair=(SNMPSequence)list.getSNMPObjectAt(0);
According to our request, we would get an array with only one object located at 0th position. Thus, we get the SNMP object at location zero by the above LOC. This object happens to be an Object of type SNMPSequence. Thus, we can safely cast it to that.




SNMPObject obj=pair.getSNMPObjectAt(1);

The SNMPSequence Object we receive has basically two values(I'm not certain whether this object can have values similar to an array). The OID, and the respective value. The OID is positioned at oth position and the respective value is at 1st index. Since we are only interested in the value, we use the object at location 1 only.





SNMPOctetString octet=(SNMPOctetString)obj;
octet.toString();

The object located at index 1 is of type SNMPOctedString. We can safely use the toString() method to get the value of the object.




By using such OID values, we can get the necessary values of the Device.




Calculating the Upload/Download speed

Calculating the up/down speed can be done as follows.

  • Get the downloaded bytes.
  • After an interval again get the downloaded bytes.
  • Divide the difference by time.

This is the same for upload speed.




So, by using some imagination, you can come up with a really good software which you can use to monitor the state of a SNMP enabled device.

Here are some of the screenshots of mine.










Comments

esikcini said…
hi

i am interested with snmp on java with netbeans platform but i couldnt understand your sample program is that possible to get its codes? it will be very helpful for me
thanks...
tuvaoh said…
where is the code source of this project can t find it !!
Anonymous said…
This comment has been removed by the author.

Popular posts from this blog

Encrypt and Decrypt Images Using Java

Build your own Network sniffer

ASP Response.Write newline