File: SNMPSample.java

package info (click to toggle)
java-snmp 1.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 556 kB
  • ctags: 526
  • sloc: java: 4,712; xml: 43; makefile: 39
file content (276 lines) | stat: -rw-r--r-- 15,019 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276

import snmp.*;
import java.math.*;
import java.net.*;



public class SNMPSample
{

    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.2.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 GET request to simultaneously retrieve the value of the SNMP variables
            // corresponding to OIDs 1.3.6.1.2.1.1.1.0 to 1.3.6.1.2.1.1.5.0
            String[] 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"};
            
            System.out.println("Retrieving value corresponding to OIDs: ");
            for (int i = 0; i < itemIDs.length; i++)
            {
                System.out.println("  " + itemIDs[i]);
            }
            
            // 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 several pairs inside it.
            newVars = comInterface.getMIBEntry(itemIDs);
            
            // extract the (OID,value) pairs from the SNMPVarBindList; each pair is just a two-element
            // SNMPSequence
            for (int i = 0; i < newVars.size(); i++)
            {
                pair = (SNMPSequence)(newVars.getSNMPObjectAt(i));
                
                // 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());
            }
            
            // now do get-next for the OIDS above; this will return the values for the OIDs following
            // each of the supplied OIDs
            System.out.println("Retrieving values _following_ OIDs: ");
            for (int i = 0; i < itemIDs.length; i++)
            {
                System.out.println("  " + itemIDs[i]);
            }
            
            newVars = comInterface.getNextMIBEntry(itemIDs);
            
            // extract the (OID,value) pairs from the SNMPVarBindList; each pair is just a two-element
            // SNMPSequence
            for (int i = 0; i < newVars.size(); i++)
            {
                pair = (SNMPSequence)(newVars.getSNMPObjectAt(i));
                
                // 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());
            }
            
            
            
            // next, retrieve an entire table, and print out the results
            // This uses the simple getMIBTable method, in which a single base OID is supplied,
            // and all of the values of OIDs which start with that base are retrieved, one element
            // at a time - this in effect retrieves the elements column-wise: all the elements in
            // the first column are retrieved first, then all the elements in the second column, etc.
            // This is an artifact of the way the OIDs of the elements in a table are formed in SNMP.
            String baseID = "1.3.6.1.2.1.2.2.1";
            
            System.out.println("Retrieving table corresponding to base OID " + baseID);
            
            SNMPVarBindList tableVars = comInterface.retrieveMIBTable(baseID);
            
            System.out.println("Number of table entries: " + tableVars.size());
            
            // extract the (OID,value) pairs from the SNMPVarBindList; each pair is just a two-element
            // SNMPSequence
            for (int i = 0; i < tableVars.size(); i++)
            {
                pair = (SNMPSequence)(tableVars.getSNMPObjectAt(i));
                
                // 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 OID: " + snmpOID + ", type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
            }
            
            
            
            // Next, retrieve an entire table, but row-by-row rather than entry-by-entry
            // This uses the multiple getMIBTable method, in which an array of base OIDs is supplied,
            // and sequences of the values of OIDs which start with that base are retrieved. This returns
            // the elements row-wise: all the elements corresponding to the first row, followed by the 
            // elements in the second row, etc. Note also that only elements from the columns supplied 
            // are retrieved; this differs from the previous retrieval, in which all of the table's
            // elements, from all columns, are retrieved. This method is thus more selective in the
            // information retrieved. It is also more efficient, in that all of the elements in a single 
            // row are retrieved with a single get-request - multiple OIDs are requested in each message.
            String[] 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"};
            
            System.out.println("Retrieving table columns corresponding to base OIDs ");
            for (int i = 0; i < baseIDs.length; i++)
            {
                System.out.println("  " + baseIDs[i]);
            }
            
            tableVars = comInterface.retrieveMIBTable(baseIDs);
            
            System.out.println("Number of table entries: " + tableVars.size());
            
            // extract the (OID,value) pairs from the SNMPVarBindList; each pair is just a two-element
            // SNMPSequence
            for (int i = 0; i < tableVars.size(); i++)
            {
                pair = (SNMPSequence)(tableVars.getSNMPObjectAt(i));
                
                // 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 OID: " + snmpOID + ", type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
            }
            
            
            
            // now send an SNMP SET request to set the value of the SNMP variable
            // corresponding to OID 1.3.6.1.2.1.1.4.0; this is the OID corresponding to
            // the device contact person, and the type is thus SNMPOctetString;
            // to set a new value, a string is supplied
            itemID = "1.3.6.1.2.1.1.4.0";    
            
            SNMPOctetString newValue = new SNMPOctetString("Jon S");
            
            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);
            
            
            
            // now send an SNMP SET request to set the values of the SNMP variables
            // corresponding to OID 1.3.6.1.2.1.1.4.0 and 1.3.6.1.2.1.1.5.0; the latter
            // is the OID corresponding to the device name, and the type is thus SNMPOctetString;
            // to set a new value, a string is supplied
            String[] setItemIDs = {"1.3.6.1.2.1.1.4.0", "1.3.6.1.2.1.1.5.0"};    
            
            SNMPOctetString[] newValues = {new SNMPOctetString("Jon"), new SNMPOctetString("Jon's device")};
            
            System.out.println("Setting value corresponding to OIDs " + itemID);
            for (int i = 0; i < setItemIDs.length; i++)
            {
                System.out.println("  " + setItemIDs[i] + ", new values " + newValues[i]);
            }
            
            // 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(setItemIDs, newValues);
            
            
                
        }
        catch(Exception e)
        {
            System.out.println("Exception during SNMP operation:  " + e + "\n");
        }
        
    }

}