如何在Camel SNMP中接收Snmp V3陷阱?

时间:2018-11-20 04:12:08

标签: apache-camel snmp snmp4j

我试图使用Apache Camel SNMP组件获取Snmp v3陷阱。我同时收到了v1和v2陷阱,但无法获得v3。

import java.util.Date;
import java.util.Objects;

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;
import org.apache.camel.main.MainSupport;
import org.apache.camel.main.MainListenerSupport;
import org.apache.camel.Processor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.camel.component.snmp.*;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.CamelContext;
import org.apache.camel.StartupListener;
import org.snmp4j.security.*;
import org.snmp4j.PDU;
import java.util.*;
import org.snmp4j.smi.*;
import org.snmp4j.mp.*;
import org.apache.camel.model.dataformat.*; 

public final class Snmp2 {

    public static void main(String[] args) throws Exception {

        // CamelContext contains methods to add routes, components etc
        CamelContext context = new DefaultCamelContext();

        // work with Snmp component
        context.addComponent("snmp", new SnmpComponent());

        // Add the route
        context.addRoutes(new RouteBuilder() {
                public void configure() {

                // When a SNMP TRAP is received on port 162 from any source..
                System.out.println(new OctetString("MD5DES"));
                System.out.println(SnmpConstants.version3);
                System.out.println(AuthMD5.ID);
//              from("snmp:0.0.0.0:162?protocol=udp&type=TRAP&securityName="+new OctetString("MD5DES")+"&snmpVersion="+SnmpConstants.version3+"&securityLevel="+SecurityLevel.NOAUTH_NOPRIV).process(myProcessor);
                from("snmp:0.0.0.0:162?protocol=udp&type=TRAP&securityName="+new OctetString("MD5DES")+"&snmpVersion="+SnmpConstants.version3+"&securityLevel="+SecurityLevel.AUTH_PRIV+"&authenticationPassphrase=UserName&privacyPassphrase=PasswordUser&authenticationProtocol=MD5&privacyProtocol=DES").process(myProcessor).to("file://D:/traps.log");
                }
            });

        // Get notification when the context is started..
        context.addStartupListener(new StartupListener(){
            public void onCamelContextStarted(CamelContext context, boolean alreadyStarted)
            {
                System.out.println("Camel context is started");
            }
        });

            // Start the context
            context.start();

            // Sleep for sometime, so that the context can keep running
            // otherwise the program exits
            Thread.sleep(1000000);

            context.stop();
    }

    public static Processor myProcessor = new Processor() {
        public void process(Exchange trap) throws Exception {

            System.out.println(trap.getIn().getHeaders());

            // getIn() means get the inbound message
            // Since we are using Snmp, we get SnmpMessage object, we need to typecast from Message
            SnmpMessage msg=(SnmpMessage)trap.getIn();

            // PDU refers to SNMP4J PDU class, the camel SNMP component internally uses it
            PDU pdu=msg.getSnmpMessage();



            // Get the VariableBindings (i.e.) the list of OID and value pairs
            // print them each
            pdu.getVariableBindings().forEach(System.out::println);
            /*
            Vector<? extends VariableBinding> v=pdu.getVariableBindings();

                for(VariableBinding b:v)
                {
                    System.out.println(b.getSyntax());
                }
            */
        }
    };

    public static class PrintBean {


        // This also does the same thing
        public void print(String msg)
        {
                System.out.println(msg);
        }
    }

}

我正在发送陷阱,但陷阱发送成功。但是我无法进入myProcessor

0 个答案:

没有答案