KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SNMPSample


1
2 import snmp.*;
3 import java.math.*;
4 import java.net.*;
5
6
7
8 public class SNMPSample
9 {
10
11     public static void main(String JavaDoc args[])
12     {
13
14         try
15         {
16
17             // create a communications interface to a remote SNMP-capable device;
18
// need to provide the remote host's InetAddress and the community
19
// name for the device; in addition, need to supply the version number
20
// for the SNMP messages to be sent (the value 0 corresponding to SNMP
21
// version 1)
22
InetAddress hostAddress = InetAddress.getByName("10.0.1.1");
23             String JavaDoc community = "public";
24             int version = 0; // SNMPv1
25

26             SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community);
27             
28             
29             
30             // now send an SNMP GET request to retrieve the value of the SNMP variable
31
// corresponding to OID 1.3.6.1.2.1.2.1.0; this is the OID corresponding to
32
// the device identifying string, and the type is thus SNMPOctetString
33
String JavaDoc itemID = "1.3.6.1.2.1.1.1.0";
34             
35             System.out.println("Retrieving value corresponding to OID " + itemID);
36             
37             // the getMIBEntry method of the communications interface returns an SNMPVarBindList
38
// object; this is essentially a Vector of SNMP (OID,value) pairs. In this case, the
39
// returned Vector has just one pair inside it.
40
SNMPVarBindList newVars = comInterface.getMIBEntry(itemID);
41             
42             // extract the (OID,value) pair from the SNMPVarBindList; the pair is just a two-element
43
// SNMPSequence
44
SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));
45             
46             // extract the object identifier from the pair; it's the first element in the sequence
47
SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
48             
49             // extract the corresponding value from the pair; it's the second element in the sequence
50
SNMPObject snmpValue = pair.getSNMPObjectAt(1);
51             
52             // print out the String representation of the retrieved value
53
System.out.println("Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
54             
55             // the retrieved value can be obtained from the SNMPObject using the getValue method;
56
// the return type of the method is the generic base class Object, and must be cast to
57
// the appropriate actual Java type; in this case, for an SNMPOctetString, the underlying
58
// Java type is a byte array[]
59
byte[] javaByteArrayValue = (byte[])snmpValue.getValue();
60             
61             
62             
63             // now send an SNMP GET request to retrieve the value of the SNMP variable
64
// corresponding to OID 1.3.6.1.2.1.1.3.0; this is the OID corresponding to
65
// the uptime of the device, and the return type is thus SNMPTimeTicks
66
itemID = "1.3.6.1.2.1.1.3.0";
67             
68             System.out.println("Retrieving value corresponding to OID " + itemID);
69             
70             // the getMIBEntry method of the communications interface returns an SNMPVarBindList
71
// object; this is essentially a Vector of SNMP (OID,value) pairs. In this case, the
72
// returned Vector has just one pair inside it.
73
newVars = comInterface.getMIBEntry(itemID);
74             
75             // extract the (OID,value) pair from the SNMPVarBindList; the pair is just a two-element
76
// SNMPSequence
77
pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));
78             
79             // extract the object identifier from the pair; it's the first element in the sequence
80
snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
81             
82             // extract the corresponding value from the pair; it's the second element in the sequence
83
snmpValue = pair.getSNMPObjectAt(1);
84             
85             // print out the String representation of the retrieved value
86
System.out.println("Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
87             
88             // the retrieved value can be obtained from the SNMPObject using the getValue method;
89
// the return type of the method is the generic base class Object, and must be cast to
90
// the appropriate actual Java type; in this case, for SNMPTimeTicks, which is a subclass
91
// of SNMPInteger, the actual type is BigInteger (which permits arbitrarily large values to
92
// be represented).
93
BigInteger javaIntegerValue = (BigInteger)snmpValue.getValue();
94             
95             
96             
97             // now send an SNMP GET request to simultaneously retrieve the value of the SNMP variables
98
// corresponding to OIDs 1.3.6.1.2.1.1.1.0 to 1.3.6.1.2.1.1.5.0
99
String JavaDoc[] itemIDs = {"1.3.6.1.2.1.1.1.0", "1.3.6.1.2.1.1.2.0", "1.3.6.1.2.1.1.3.0", "1.3.6.1.2.1.1.4.0", "1.3.6.1.2.1.1.5.0"};
100             
101             System.out.println("Retrieving value corresponding to OIDs: ");
102             for (int i = 0; i < itemIDs.length; i++)
103             {
104                 System.out.println(" " + itemIDs[i]);
105             }
106             
107             // the getMIBEntry method of the communications interface returns an SNMPVarBindList
108
// object; this is essentially a Vector of SNMP (OID,value) pairs. In this case, the
109
// returned Vector has several pairs inside it.
110
newVars = comInterface.getMIBEntry(itemIDs);
111             
112             // extract the (OID,value) pairs from the SNMPVarBindList; each pair is just a two-element
113
// SNMPSequence
114
for (int i = 0; i < newVars.size(); i++)
115             {
116                 pair = (SNMPSequence)(newVars.getSNMPObjectAt(i));
117                 
118                 // extract the object identifier from the pair; it's the first element in the sequence
119
snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
120                 
121                 // extract the corresponding value from the pair; it's the second element in the sequence
122
snmpValue = pair.getSNMPObjectAt(1);
123                 
124                 // print out the String representation of the retrieved value
125
System.out.println("Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
126             }
127             
128             // now do get-next for the OIDS above; this will return the values for the OIDs following
129
// each of the supplied OIDs
130
System.out.println("Retrieving values _following_ OIDs: ");
131             for (int i = 0; i < itemIDs.length; i++)
132             {
133                 System.out.println(" " + itemIDs[i]);
134             }
135             
136             newVars = comInterface.getNextMIBEntry(itemIDs);
137             
138             // extract the (OID,value) pairs from the SNMPVarBindList; each pair is just a two-element
139
// SNMPSequence
140
for (int i = 0; i < newVars.size(); i++)
141             {
142                 pair = (SNMPSequence)(newVars.getSNMPObjectAt(i));
143                 
144                 // extract the object identifier from the pair; it's the first element in the sequence
145
snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
146                 
147                 // extract the corresponding value from the pair; it's the second element in the sequence
148
snmpValue = pair.getSNMPObjectAt(1);
149                 
150                 // print out the String representation of the retrieved value
151
System.out.println("Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
152             }
153             
154             
155             
156             // next, retrieve an entire table, and print out the results
157
// This uses the simple getMIBTable method, in which a single base OID is supplied,
158
// and all of the values of OIDs which start with that base are retrieved, one element
159
// at a time - this in effect retrieves the elements column-wise: all the elements in
160
// the first column are retrieved first, then all the elements in the second column, etc.
161
// This is an artifact of the way the OIDs of the elements in a table are formed in SNMP.
162
String JavaDoc baseID = "1.3.6.1.2.1.2.2.1";
163             
164             System.out.println("Retrieving table corresponding to base OID " + baseID);
165             
166             SNMPVarBindList tableVars = comInterface.retrieveMIBTable(baseID);
167             
168             System.out.println("Number of table entries: " + tableVars.size());
169             
170             // extract the (OID,value) pairs from the SNMPVarBindList; each pair is just a two-element
171
// SNMPSequence
172
for (int i = 0; i < tableVars.size(); i++)
173             {
174                 pair = (SNMPSequence)(tableVars.getSNMPObjectAt(i));
175                 
176                 // extract the object identifier from the pair; it's the first element in the sequence
177
snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
178                 
179                 // extract the corresponding value from the pair; it's the second element in the sequence
180
snmpValue = pair.getSNMPObjectAt(1);
181                 
182                 // print out the String representation of the retrieved value
183
System.out.println("Retrieved OID: " + snmpOID + ", type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
184             }
185             
186             
187             
188             // Next, retrieve an entire table, but row-by-row rather than entry-by-entry
189
// This uses the multiple getMIBTable method, in which an array of base OIDs is supplied,
190
// and sequences of the values of OIDs which start with that base are retrieved. This returns
191
// the elements row-wise: all the elements corresponding to the first row, followed by the
192
// elements in the second row, etc. Note also that only elements from the columns supplied
193
// are retrieved; this differs from the previous retrieval, in which all of the table's
194
// elements, from all columns, are retrieved. This method is thus more selective in the
195
// information retrieved. It is also more efficient, in that all of the elements in a single
196
// row are retrieved with a single get-request - multiple OIDs are requested in each message.
197
String JavaDoc[] baseIDs = {"1.3.6.1.2.1.2.2.1.1", "1.3.6.1.2.1.2.2.1.2", "1.3.6.1.2.1.2.2.1.3", "1.3.6.1.2.1.2.2.1.4"};
198             
199             System.out.println("Retrieving table columns corresponding to base OIDs ");
200             for (int i = 0; i < baseIDs.length; i++)
201             {
202                 System.out.println(" " + baseIDs[i]);
203             }
204             
205             tableVars = comInterface.retrieveMIBTable(baseIDs);
206             
207             System.out.println("Number of table entries: " + tableVars.size());
208             
209             // extract the (OID,value) pairs from the SNMPVarBindList; each pair is just a two-element
210
// SNMPSequence
211
for (int i = 0; i < tableVars.size(); i++)
212             {
213                 pair = (SNMPSequence)(tableVars.getSNMPObjectAt(i));
214                 
215                 // extract the object identifier from the pair; it's the first element in the sequence
216
snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
217                 
218                 // extract the corresponding value from the pair; it's the second element in the sequence
219
snmpValue = pair.getSNMPObjectAt(1);
220                 
221                 // print out the String representation of the retrieved value
222
System.out.println("Retrieved OID: " + snmpOID + ", type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
223             }
224             
225             
226             
227             // now send an SNMP SET request to set the value of the SNMP variable
228
// corresponding to OID 1.3.6.1.2.1.1.4.0; this is the OID corresponding to
229
// the device contact person, and the type is thus SNMPOctetString;
230
// to set a new value, a string is supplied
231
itemID = "1.3.6.1.2.1.1.4.0";
232             
233             SNMPOctetString newValue = new SNMPOctetString("Jon S");
234             
235             System.out.println("Setting value corresponding to OID " + itemID);
236             System.out.println("New value: " + newValue.toString());
237             
238             // the setMIBEntry method of the communications interface returns the SNMPVarBindList
239
// corresponding to the supplied OID and value
240
// This call will probably cause an SNMPSetException to be thrown, since the
241
// community name "public" is probably not the read/write password of the device
242
newVars = comInterface.setMIBEntry(itemID, newValue);
243             
244             
245             
246             // now send an SNMP SET request to set the values of the SNMP variables
247
// corresponding to OID 1.3.6.1.2.1.1.4.0 and 1.3.6.1.2.1.1.5.0; the latter
248
// is the OID corresponding to the device name, and the type is thus SNMPOctetString;
249
// to set a new value, a string is supplied
250
String JavaDoc[] setItemIDs = {"1.3.6.1.2.1.1.4.0", "1.3.6.1.2.1.1.5.0"};
251             
252             SNMPOctetString[] newValues = {new SNMPOctetString("Jon"), new SNMPOctetString("Jon's device")};
253             
254             System.out.println("Setting value corresponding to OIDs " + itemID);
255             for (int i = 0; i < setItemIDs.length; i++)
256             {
257                 System.out.println(" " + setItemIDs[i] + ", new values " + newValues[i]);
258             }
259             
260             // the setMIBEntry method of the communications interface returns the SNMPVarBindList
261
// corresponding to the supplied OID and value
262
// This call will probably cause an SNMPSetException to be thrown, since the
263
// community name "public" is probably not the read/write password of the device
264
newVars = comInterface.setMIBEntry(setItemIDs, newValues);
265             
266             
267                 
268         }
269         catch(Exception JavaDoc e)
270         {
271             System.out.println("Exception during SNMP operation: " + e + "\n");
272         }
273         
274     }
275
276 }
Popular Tags