SnmpUtil.java
3.85 KB
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
package com.sitech.ismp.coll.i2000.alarm;
import java.io.IOException;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.MessageDispatcher;
import org.snmp4j.MessageDispatcherImpl;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.mp.MPv1;
import org.snmp4j.mp.MPv2c;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.MessageProcessingModel;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.MultiThreadedMessageDispatcher;
import org.snmp4j.util.ThreadPool;
public class SnmpUtil extends Thread implements CommandResponder {
static Logger logger = Logger.getLogger("I2000");
public static final int DEFAULT = 0;
public static final int WALK = 1;
private static Snmp _snmp = null;
private int _numThreads = 1;
private ThreadPool _threadPool = null;
private TransportMapping _transport = null;
protected int _operation = DEFAULT;
static {
if (System.getProperty("log4j.configuration") == null) {
BasicConfigurator.configure();
}
}
public SnmpUtil(String host, int port) {
// 初始化接受
initReceiver(host, port);
}
public void initReceiver(String host, int port) {
Address address = new UdpAddress(host + "/" + port);
try {
_transport = new DefaultUdpTransportMapping((UdpAddress) address);
} catch (IOException ioex) {
logger.error("Unable to bind to IP and port: " + ioex);
// System.exit(-1);
}
_threadPool = ThreadPool.create(this.getClass().getName(), _numThreads);
MessageDispatcher mtDispatcher = new MultiThreadedMessageDispatcher(
_threadPool, new MessageDispatcherImpl());
// add message processing models
mtDispatcher.addMessageProcessingModel(new MPv1());
mtDispatcher.addMessageProcessingModel(new MPv2c());
mtDispatcher.addMessageProcessingModel(new MPv3());
logger.info("new MPv3()");
// add all security protocols
SecurityProtocols.getInstance().addDefaultProtocols();
_snmp = new Snmp(mtDispatcher, _transport);
/*byte[] localEngineID =
((MPv3)_snmp.getMessageProcessingModel(MessageProcessingModel.MPv3)).createLocalEngineID();
USM usm = new USM(SecurityProtocols.getInstance(),
new OctetString(localEngineID), 0);
SecurityModels.getInstance().addSecurityModel(usm);
_snmp.setLocalEngine(localEngineID, 0, 0);
*/ if (_snmp != null) {
_snmp.addCommandResponder(this);
} else {
logger.error("Unable to create Target object");
// System.exit(-1);
}
}
public synchronized void listen() {
try {
_transport.listen();
} catch (IOException ioex) {
logger.error("Unable to listen: " + ioex);
// System.exit(-1);
}
logger.info("BOMC v3 Waiting for traps..");
try {
this.wait();// Wait for traps to come in
} catch (InterruptedException ex) {
logger.error("Interrupted while waiting for traps: " + ex);
// System.exit(-1);
}
}
/**
* Process an incoming request, report or notification PDU synchronized
*/
public synchronized void processPdu(CommandResponderEvent e) {
logger.info("processPdu.....");
PDU cmd = e.getPDU();
if (cmd != null) {
NeatenPDU neaten = new NeatenPDU();
neaten.neatenPdu(cmd);
}
}
}