snmp4j trap
snmp4j 中怎样调用trap,有这样一个例子import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.DefaultUdpTransportMapping;
public class TrapTest{
public static void main(String[] args){
try {
TransportMapping transport = new DefaultUdpTransportMapping(new UdpAddress("192.168.32.219/162"));
//创建一个处理消息的snmp实例
Snmp snmp = new Snmp(transport);
//CommandResponder是一个listener,用以处理获取的trap消息
CommandResponder trapPrinter = new CommandResponder(){
public synchronized void processPdu(CommandResponderEvent e){
PDU command = e.getPDU();
if(command!=null){
//这里示例输出trap的内容.具体的trap解析等工作在这里进行.
System.out.println(command.toString());
}
}
};
//在snmp实例中添加CommandResponder listener
snmp.addCommandResponder(trapPrinter);
System.out.println("start listening!");
//开始启动trap监听.listen()方法内部启动了一个线程,这个线程监听发送到transport中定义的端口
//的消息.
transport.listen();
System.out.println(transport.isListening());//测试监听是否正常
//等待一段测试时间,在这段时间可以发送trap信息测试.
} catch (Exception e){
e.printStackTrace();
}
}
}
我运行不能接受到trap,请问还需要别的设置吗?我启动的windows xp上的snmp服务,怎样让xp发送trap?
--------------------编程问答-------------------- 帮你顶顶
--------------------编程问答-------------------- 看看,没有弄过:) --------------------编程问答-------------------- 我也正纠结这个问题呢,帮你顶 --------------------编程问答-------------------- 你这个只是接收的代码,trap是设备主动发送的,你不能控制。可以自己代码实现发送
package com.snmp.test;
import java.io.IOException;
import java.util.Vector;
import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
public class SendTrap {
private Snmp snmp;
private Address addr;
private TransportMapping transport = null;
/**
* @param args
*/
public static void main(String[] args) {
SendTrap st = new SendTrap();
st.initComm();
st.setTrap();
}
public void initComm(){
addr = GenericAddress.parse("udp:192.168.0.115/162"); //代理程序的地址和端口(发送方)
try {
transport = new DefaultUdpTransportMapping(new UdpAddress("192.168.0.100/161")); //接收trap的地址和端口(本机IP)
snmp = new Snmp(transport);
CommandResponder trapRec = new CommandResponder(){
public synchronized void processPdu(CommandResponderEvent e){
PDU command = e.getPDU(); //接收PDU
if(null != command){
System.out.println(command.toString());
}
}
};
snmp.addCommandResponder(trapRec);
transport.listen();
} catch (IOException e) {
System.out.println(e);
}
}
public synchronized void listen(){
System.out.println("Waitting for traps ......");
try {
this.wait();
} catch (InterruptedException e) {
System.out.println("Waitting for traps: " + e);
System.exit(-1);
}
}
public ResponseEvent sendPDU(PDU pdu) throws IOException{
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public")); //设置团体字符串
target.setAddress(addr);
target.setRetries(5); //通信失败重新尝试的次数
target.setTimeout(5000); //超时
target.setVersion(SnmpConstants.version2c); //版本
return snmp.send(pdu, target);
}
/**
* 解析resp
* @param resp
*/
public void readResponse(ResponseEvent resp){
if(null != resp && null != resp.getResponse()){ //解析
Vector<? extends VariableBinding> recBs = resp.getResponse().getVariableBindings();
for(int i = 0 ;i < recBs.size() ; i++){
VariableBinding recB = recBs.elementAt(i);
System.out.println(recB.getOid() + " : " + recB.getVariable());
}
}
}
/**
* getPDU
* @throws IOException
*/
public void getPDU() throws IOException{
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 })));
pdu.setType(PDU.GET);
readResponse(sendPDU(pdu));
}
/**
* setPDU
* @throws IOException
*/
public void setPDU() throws IOException{
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 })
,new OctetString("OCT"))); //set
pdu.setType(PDU.GET);
readResponse(sendPDU(pdu));
}
public void setTrap() {
// 构造Trap PDU
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.0"),
new OctetString("SnmpTrap")));
pdu.setType(PDU.TRAP);
try {
sendPDU(pdu);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e);
}
System.out.println("Trap sent successfully.");
}
}
另外你的接收端不知道可以不,我给你段代码再:
package com.snmp.test;
import java.io.IOException;
import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.DefaultUdpTransportMapping;
public class TestTrap {
// private MultiThreadedMessageDispatcher dispatcher;
// private ThreadPool threadPool;
private Snmp snmp;
private Address addr;
private TransportMapping transport = null;
/**
* @param args
*/
public static void main(String[] args) {
TestTrap tt = new TestTrap();
tt.initComm();
tt.listen();
}
/**
* 初始化方法,
*/
public void initComm(){
addr = GenericAddress.parse("udp:192.168.0.104/161"); //代理程序的地址和端口(发送方)
try {
transport = new DefaultUdpTransportMapping(new UdpAddress("192.168.0.100/162")); //接收trap的地址和端口
snmp = new Snmp(transport); //实例化Snmp
CommandResponder trapRec = new CommandResponder(){
public synchronized void processPdu(CommandResponderEvent e){
PDU command = e.getPDU(); //接收PDU
if(null != command){
System.out.println(command.toString());
}
}
};
snmp.addCommandResponder(trapRec);
transport.listen();
} catch (IOException e) {
System.out.println(e);
}
}
public synchronized void listen(){
System.out.println("Waitting for traps ......");
try {
this.wait();
} catch (InterruptedException e) {
System.out.println("Waitting for traps: " + e);
System.exit(-1);
}
}
}
2段代码都是上边的ip是发送方下边为接收放。而且你运行这些代码的话控制台回显会被发送的测试类覆盖不能看到,建议找其他机器安装snmp运行发送代码。
另外可以使用SolarWinds这个工具模拟发送trap,记住ip里不要出现127.0.0.1。
补充:企业软件 , WebSphere