SnmpTrap 线程 内附代码,eclipse可正常运行测试!

SnmpUtilSendTrap:发送snmp trap信息到trap接收端。


import java.io.IOException;
import java.util.Vector;

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.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

/**
 * 本类用于向管理进程发送Trap信息
 *
 * @author
 *
 */
public class SnmpUtilSendTrap {

    private Snmp snmp = null;

    private Address targetAddress = null;

    @SuppressWarnings("rawtypes")
    public void initComm() throws IOException {

        // 设置管理进程的IP和端口
        targetAddress = GenericAddress.parse("udp:目标IP/162");//such as:192.168.0.1
        TransportMapping transport = new DefaultUdpTransportMapping();
        snmp = new Snmp(transport);
        transport.listen();

    }

    /**
     * 向管理进程发送Trap报文
     *
     * @throws IOException
     */
    public void sendPDU() throws IOException {
        
        // 设置 target
        CommunityTarget target = new CommunityTarget();
        target.setAddress(targetAddress);

        // 通信不成功时的重试次数
        target.setRetries(2);
        // 超时时间
        target.setTimeout(1500);
        target.setCommunity(new OctetString("Public"));
        // snmp版本
        target.setVersion(SnmpConstants.version2c);

        // 创建 PDU
        PDU pdu = new PDU();
        pdu.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.1.1.1.1"), new OctetString("SnmpTrap-2 Good Morning")));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.1.1.1.3.1"), new OctetString("Good Afternoon Hello Word!!!")));
        pdu.setType(PDU.TRAP);

        // 向Agent发送PDU,并接收Response
        ResponseEvent respEvnt = snmp.send(pdu, target);

        // 解析Response
        if (respEvnt != null && respEvnt.getResponse() != null) {
            Vector<? extends VariableBinding> recVBs = respEvnt.getResponse().getVariableBindings();
            for (int i = 0; i < recVBs.size(); i++) {
                VariableBinding recVB = recVBs.elementAt(i);
                System.out.println(recVB.getOid() + " : " + recVB.getVariable());
            }
        }
    }

    public static void main(String[] args) {
        try {
            SnmpUtilSendTrap util = new SnmpUtilSendTrap();
            util.initComm();
            util.sendPDU();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}



SnmpTrapResponse:接收snmp trap的信息,来自send端。

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Vector;

import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
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.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TcpAddress;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultTcpTransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.MultiThreadedMessageDispatcher;
import org.snmp4j.util.ThreadPool;

/**
 * 本类用于监听代理进程的Trap信息
 *
 * @author
 */
public class SnmpTrapResponse implements CommandResponder {

    private MultiThreadedMessageDispatcher dispatcher;
    private Snmp snmp = null;
    private Address listenAddress;
    private ThreadPool threadPool;

    /*public SnmpTrapResponse() {
        BasicConfigurator.configure();
    }*/

    @SuppressWarnings("rawtypes")
    private void init() throws UnknownHostException, IOException {
        InetAddress inet = InetAddress.getLocalHost();
        String IP = inet.getHostAddress();
        threadPool = ThreadPool.create("Trap", 2);
        dispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());
        listenAddress = GenericAddress.parse(System.getProperty("snmp4j.listenAddress", IP+"/162"));
        System.out.println(IP);                                                                                                                // 监听IP与监听端口
        TransportMapping transport;
        // 对TCP与UDP协议进行处理
        if (listenAddress instanceof UdpAddress) {
            transport = new DefaultUdpTransportMapping((UdpAddress) listenAddress);
        } else {
            transport = new DefaultTcpTransportMapping((TcpAddress) listenAddress);
        }
        snmp = new Snmp(dispatcher, transport);
        snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1());
        snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c());
        snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3());
        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);
        snmp.listen();
    }

    public void run() {
        try {
            init();
            snmp.addCommandResponder(this);
            System.out.println("开始监听Trap信息!");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 实现CommandResponder的processPdu方法, 用于处理传入的请求、PDU等信息 当接收到trap时,会自动进入这个方法
     *
     * @param respEvnt
     */
    public void processPdu(CommandResponderEvent respEvnt) {
        // 取设备的IP地址
        String IP = respEvnt.getPeerAddress().toString();
        System.out.println("***************************");

        String ip[] = IP.split("/");
        System.out.println("设备的IP地址:" + ip[0]);
        PDU command = respEvnt.getPDU();
        System.out.println("PDU信息:" + command.toString());
        GregorianCalendar gcal = new GregorianCalendar(); // 获得当前时间
        // 设定格式yyyy-mm-dd hh:mm:ss
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date2 = gcal.getTime(); // 将当前时间转成日期对象
        String datetime = df.format(date2); // 获得符合格式的字符串,当前日期时间
        System.out.println("当前日期:" + datetime);
        // 解析Response
        if (respEvnt != null && respEvnt.getPDU() != null) {
            Vector<? extends VariableBinding> recVBs = respEvnt.getPDU().getVariableBindings();
            for (int i = 0; i < recVBs.size(); i++) {
                VariableBinding recVB = recVBs.elementAt(i);
                System.out.println(recVB.getOid() + " : " + recVB.getVariable());
            }
        }
        
    }

    public static void main(String[] args) {
        SnmpTrapResponse multithreadedtrapreceiver = new SnmpTrapResponse();
        multithreadedtrapreceiver.run();
    }

}



我在项目中遇到代码中红色显示的部分:

InetAddress inet = InetAddress.getLocalHost();
        String IP = inet.getHostAddress();


在Linux中,启动线程的时候,监听端口不是设备的真实ip,而是 127.0.0.1 / 162,这时,无论send端如何发送trap信息,监听端,都是无法接收到的。。。

后来经过查询源码、度娘、以及同事大牛的帮助,才知道可以使用:

//InetAddress inet = InetAddress.getLocalHost();
        String IP = "0.0.0.0";  //inet.getHostAddress();


z这时候,监听的端口虽然不是你设备的真实ip,但是是它的代言人 0.0.0.0 / 162,就可以正常的接收和解析了。


如果,有朋友希望得到的trap信息(oid),解析出来,可以试试我之前的文章snmpget请求的文章,谢谢!


snmpget请求: SNMPGET
posted @ 2022-01-27 18:36  zhangdaopin  阅读(232)  评论(0编辑  收藏  举报