[SNMP4J] trap notification

Alessandro.DOstilio at alcatel.it Alessandro.DOstilio at alcatel.it
Tue Sep 19 11:51:42 CEST 2006


Hello, I'm trying to receive traps notification from a network element. My 
code is taken from the few on line examples. When I create the USM, if I 
use MPV3 i can call the mpv3.createLocalEngineID()method, but for a MPv2? 
I'd like to know if this code is right and if should work fine, otherwise 
where are the errors?
Thank you,
Alessandro

Address targetAddress = GenericAddress.parse("udp:127.0.0.1/161");
        TransportMapping transport = null;
        Snmp snmp = null;
        try {
            transport = new DefaultUdpTransportMapping();
            snmp = new Snmp(transport);

        } catch (IOException ex) {
            ex.printStackTrace();
        }
        MPv2c mpv2c =
 (MPv2c)snmp.getMessageProcessingModel(MessageProcessingModel.MPv2c);
        USM usm = new USM(SecurityProtocols.getInstance(),
                          new OctetString( ??? ), 0);
        SecurityModels.getInstance().addSecurityModel(usm);
 
        snmp.getUSM().addUser(new OctetString("MD5DES"),
                              new UsmUser(new OctetString("admin"),
                AuthMD5.ID, "MD5DESUserAuthPassword",
                PrivDES.ID, "MD5DESUserPrivPassword"));

 
        CommandResponder trapPrinter = new CommandResponder() {
            public synchronized void processPdu(CommandResponderEvent e) {
                PDU command = e.getPDU();
                if (command != null) {
                    System.out.println("da ale_listen: " + 
command.toString());
                }
            }
        };
        snmp.addNotificationListener(targetAddress, trapPrinter);


        // setting up target
        CommunityTarget c_target = new CommunityTarget();
        c_target.setCommunity(new OctetString("public"));
        c_target.setAddress(targetAddress);
        c_target.setRetries(2);
        c_target.setTimeout(1500);
        c_target.setVersion(SnmpConstants.version2c);
 
        PDU pdu = new PDU();
        pdu.setType(PDU.INFORM);
        pdu.add(new VariableBinding(SnmpConstants.snmpTrapEnterprise, new 
OID
                                    ("1.3.6.1.4.1.37")));
        pdu.add(new VariableBinding(SnmpConstants.snmpTrapAddress));

 
        try {
            snmp.listen();
            snmp.send(pdu, c_target, transport);
        } catch (IOException ex2) {
            ex2.printStackTrace();
        }


More information about the SNMP4J mailing list