| 
|||||||||||
| PREV NEXT | FRAMES NO FRAMES | ||||||||||
See:
          Description
| Packages | |
| snmp | The snmp package defines classes used to communicate with SNMP devices. | 
The complete API documentation for the package is provided through JavaDoc 
 documentation. This introduction is intended to provide an overview of the
 package, with particular focus on the classes an end user will need to directly
 interface with to incorporate the package into a project. 
    
  Much of the top-level user functionality is provided by the class SNMPv1CommunicationInterface.
 This constructor for this class opens a datagram socket to a specified host
 for SNMP communication on the standard port (192). Several methods of the
 class then provide a means to retrieve and set the value of SNMP variables
 on the remote device by supplying a string specifying an object identifier
 and an appropriate SNMP object for set operations. The example program listed
below (Example 1
) illustrates the use of the communication interface class to open a connection 
to a remote device, get the value of two variables corresponding to OIDs 1.3.6.1.2.1.1.1.0 
and 1.3.6.1.2.1.1.3.0, and set the value of a third variable. The source
code for the SNMP Inquisitor shows the use of the package in a general-purpose
utility which permits SNMP querying and modification of a remote device.
The constructor for the SNMPv1CommunicationInterface takes three parameters: 
an integer giving the SNMP version in use (generally always 0), the remote 
device address supplied as an InetAddress object, and a string giving the 
SNMP community to be used for the operations.
Once the communication interface has been created to communicate with a
device, the values of SNMP variables can be retrieved with calls to the getMIBEntry()
method. This takes as argument a string with the variable's OID; the return
value is somewhat complex, due to SNMP's ability to query multiple variables
in a single message (though this particular method retrieves only one value
at a time). The return value is of type SNMPVarBindList, a subclass of SNMPSequence
which holds a sequence of (OID, value) pairs. Thus the retrieved SNMP value
is accessed as follows:
 
 import snmp.*;
 import java.util.*;
 import java.math.*;
 import java.net.*;
 
 
 
 public class SampleSNMP
 {
 
     public static void main(String args[]) 
     {
 
         try
         {
 
             // create a communications 
interface to a remote SNMP-capable device;
             // need to provide 
the remote host's InetAddress and the community
             // name for the 
device; in addition, need to  supply the version number
             // for the SNMP 
messages to be sent (the value 0 corresponding to SNMP
             // version 1)
             InetAddress hostAddress 
= InetAddress.getByName("10.0.1.1");
             String community 
= "public";
             int version = 0;    
// SNMPv1
             
             SNMPv1CommunicationInterface 
comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community);
             
             
             
             // now send an
SNMP GET request to retrieve the value of the SNMP variable
             // corresponding 
to OID 1.3.6.1.2.1.1.1.0; this is the OID corresponding to
             // the device identifying 
string, and the type is thus SNMPOctetString
             String itemID = 
"1.3.6.1.2.1.1.1.0";
             
             System.out.println("Retrieving 
value corresponding to OID " + itemID);
             
             // the getMIBEntry 
method of the communications interface returns an SNMPVarBindList
             // object; this 
is essentially a Vector of SNMP (OID,value) pairs. In this case, the
             // returned Vector 
has just one pair inside it.
             SNMPVarBindList 
newVars = comInterface.getMIBEntry(itemID);
             
             // extract the
(OID,value) pair from the SNMPVarBindList; the pair is just a two-element
             // SNMPSequence
             SNMPSequence pair 
= (SNMPSequence)(newVars.getSNMPObjectAt(0));
             
             // extract the
object identifier from the pair; it's the first element in the sequence
             SNMPObjectIdentifier 
snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
             
             // extract the
corresponding value from the pair; it's the second element in the sequence
             SNMPObject snmpValue 
= pair.getSNMPObjectAt(1);
             
             // print out the 
String representation of the retrieved value
             System.out.println("Retrieved 
value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
             
             // the retrieved 
value can be obtained from the SNMPObject using the getValue method;
             // the return type 
of the method is the generic base class Object, and must be cast to 
             // the appropriate 
actual Java type; in this case, for an SNMPOctetString, the underlying
             // Java type is 
a byte array[]
             byte[] javaByteArrayValue 
= (byte[])snmpValue.getValue();
             
             
             
             // now send an
SNMP GET request to retrieve the value of the SNMP variable
             // corresponding 
to OID 1.3.6.1.2.1.1.3.0; this is the OID corresponding to
             // the uptime of 
the device, and the return type is thus SNMPTimeTicks
             itemID = "1.3.6.1.2.1.1.3.0";
             
             System.out.println("Retrieving 
value corresponding to OID " + itemID);
             
             // the getMIBEntry 
method of the communications interface returns an SNMPVarBindList
             // object; this 
is essentially a Vector of SNMP (OID,value) pairs. In this case, the
             // returned Vector 
has just one pair inside it.
             newVars = comInterface.getMIBEntry(itemID);
             
             // extract the
(OID,value) pair from the SNMPVarBindList; the pair is just a two-element
             // SNMPSequence
             pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));
             
             // extract the
object identifier from the pair; it's the first element in the sequence
             snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
             
             // extract the
corresponding value from the pair; it's the second element in the sequence
             snmpValue = pair.getSNMPObjectAt(1);
             
             // print out the 
String representation of the retrieved value
             System.out.println("Retrieved 
value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
             
             // the retrieved 
value can be obtained from the SNMPObject using the getValue method;
             // the return type 
of the method is the generic base class Object, and must be cast to 
             // the appropriate 
actual Java type; in this case, for SNMPTimeTicks, which is a subclass
             // of SNMPInteger, 
the actual type is BigInteger (which permits arbitrarily large values to 
             // be represented).
             BigInteger javaIntegerValue 
= (BigInteger)snmpValue.getValue();
             
             
             
             // now send an
SNMP SET request to set the value of the SNMP variable
             // corresponding 
to OID 1.3.6.1.2.1.1.1.0; this is the OID corresponding to
             // the device identifying 
string, and the type is thus SNMPOctetString;
             // to set a new 
value, a string is supplied
             itemID = "1.3.6.1.2.1.1.1.0";    
             
             SNMPOctetString 
newValue = new SNMPOctetString("New device name");
             
             System.out.println("Setting 
value corresponding to OID " + itemID);
             System.out.println("New 
value: " + newValue.toString());
             
             // the setMIBEntry 
method of the communications interface returns the SNMPVarBindList
             // corresponding 
to the supplied OID and value
             // This call will 
probably cause an SNMPSetException to be thrown, since the
             // community name 
"public" is probably not the read/write password of the device 
             newVars = comInterface.setMIBEntry(itemID, 
newValue);
                 
             
                 
         }
         catch(Exception e)
         {
             System.out.println("Exception 
during SNMP operation:  " + e + "\n");
         }
         
     }
 
 }  
   
The following table
 lists the Java class corresponding to each standard  SNMP datatype, and
the class used to represent the value internally. Note  that most of the
Java classes just use the name of the SNMP type with "SNMP"  prefixed. 
  
| SNMP Datatype | Java SNMP Class | Java SNMP Base Class | Internal value representation | 
| Integer | SNMPInteger | SNMPObject | BigInteger | 
| Uinteger32 | SNMPUInteger32 | SNMPInteger | BigInteger (but value "wraps" at 2^32) | 
| Gauge | SNMPGauge32 | SNMPInteger | BigInteger (but value "pegs" at 2^32 - 1) | 
| Counter32 | SNMPCounter32 | SNMPInteger | BigInteger (but value "wraps" at 2^32) | 
| Counter64 | SNMPCounter64 | SNMPInteger | BigInteger (but value "wraps" at 2^64) | 
| TimeTicks | 
    SNMPTimeTicks | 
    SNMPInteger | 
    BigInteger | 
   
| Octet string | SNMPOctetString | SNMPObject | byte[] | 
| IP address | 
    SNMPIPAddress | 
    SNMPOctetString | 
    byte[4]  | 
   
| NSAP address (MAC address) | 
    SNMPNSAPAddress | 
    SNMPOctetString | 
    byte[6] | 
   
| Object identifier | 
        SNMPObjectIdentifier | 
        SNMPObject | 
        int[] (contains the components of the dotted-integer
 representation of the OID) | 
      
| Sequence | 
        SNMPSequence | 
        SNMPObject | 
        Vector | 
      
| VarBind | SNMPVariablePair | SNMPSequence | Vector | 
| VarBindList | SNMPVarBindList | SNMPSequence | Vector | 
| Null | 
        SNMPNull | 
        SNMPObject | 
        
  | 
|||||||||||
| PREV NEXT | FRAMES NO FRAMES | ||||||||||