File: SNMPMultithreadTest.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 (111 lines) | stat: -rw-r--r-- 3,645 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
import snmp.*;
import java.net.*;



public class SNMPMultithreadTest
					implements Runnable
{

    SNMPv1CommunicationInterface comInterface;
    
    public class RetrieveThread extends Thread
    {
        public int i;
        
        public RetrieveThread(Runnable r, int i)
        {
            super(r);
            this.i = i;
        }
        
    }
    
    
    public static void main(String args[]) 
    {
        SNMPMultithreadTest app = new SNMPMultithreadTest();
    }
    
    
    public SNMPMultithreadTest() 
    {

        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
            
            comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community);
            
            comInterface.setSocketTimeout(500);
            
            for (int i = 0; i < 1000; i++)
            {
                RetrieveThread retrievalThread = new RetrieveThread(this, i);
                retrievalThread.start();
            }
            
            
        }
        catch(Exception e)
        {
            System.out.println("Exception during SNMP operation:  " + e + "\n");
        }
        
    }
    
    
    public void run()
    {
        int threadIndex = ((RetrieveThread)Thread.currentThread()).i;
        
        while (true)
        {
            try
	        {
	
	            // 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("Thread " + threadIndex + ": 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("Thread " + threadIndex + ": Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
	            
	                
	        }
	        catch(Exception e)
	        {
	            System.out.println("Exception during SNMP operation:  " + e + "\n");
	        }
	        
        }
        
    }

}