SNMP Manager Application
This chapter describes how to develop JEUS SNMP Manager applications and how to configure them.
1. Overview
SNMP Managers that conforms to the SNMP (SNMPv1, SNMPv2, and SNMPv3) standard can be integrated with JEUS SNMP.
For more information about SNMP, refer to the SNMP Management Framework related specification (Jakarta management 1.1 specification). |
2. SNMP Client Program Sample
Application types vary depending on the occasion. This section shows a client program that uses the SNMP4J API.
SNMP4J is an open source API and provides SNMP implementation classes for J2SE 1.4 or higher. The SNMP client application, that uses SNMP4J API, must perform the following steps.
-
Set the SNMP object OID and the PDU type.
-
Set the address and port number of the target, where SNMP information will be retrieved from, and set the SNMP version. Since JEUS SNMP uses UDP, use the UDPAddress.
-
Create an SNMP message object and then a message to send.
-
Send a message and receive a response.
Example
The following is a SNMP client application program example that uses the SNMP4J API.
import java.net.*;
import java.util.*;
import org.snmp4j.*;
import org.snmp4j.PDU;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.transport.*;
import org.snmp4j.smi.*;
public class SnmpclientSample{
static int defaultPort = 9999;
static String defaultIP = "127.0.0.1";
static String defaultOID = "1.3.6.1.4.1.14586.100.2.1";
static void testGetNext(String oid) throws java.io.IOException {
//1. Make Protocol Data Unit
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(defaultOID)));
pdu.setType(PDU.GETNEXT);
//2. Make target
CommunityTarget target = new CommunityTarget();
UdpAddress targetAddress = new UdpAddress();
targetAddress.setInetAddress(InetAddress.getByName(defaultIP));
targetAddress.setPort(defaultPort);
target.setAddress(targetAddress);
target.setCommunity(new OctetString("public"));
target.setVersion(SnmpConstants.version1);
//3. Make SNMP Message. Simple!
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
//4. Send Message and Recieve Response
snmp.listen();
ResponseEvent response = snmp.send(pdu, target);
if (response.getResponse() == null) {
System.out.println("Error: There is some problems.");
} else {
Vector variableBindings = response.getResponse().getVariableBindings();
for( int i = 0; i < variableBindings.size(); i++){
System.out.println(variableBindings.get(i));
}
}
snmp.close();
}
public static void main(String[] args) throws java.io.IOException {
// get the SNMP port number
if (args.length > 0) {
defaultPort = Integer.parseInt (args[0]);
}
System.out.println ("PORT : " + defaultPort);
// get the ip address of the machine that the SNMP agent runs on
if (args.length > 1) {
defaultIP = args[1];
}
System.out.println ("IP : " + defaultIP);
// get the OID number that you want to get the value of
if (args.length > 2) {
defaultOID = args[2];
}
System.out.println ("OID : " + defaultOID);
try {
testGetNext(defaultOID);
} catch (Exception ex) {
System.out.println ("ex *** : " + ex);
ex.printStackTrace ();
}
}
}
Refer to SNMP4J Official Documentation for information about SNMP4J API. |