Search in sources :

Example 6 with CommandResponderEvent

use of org.snmp4j.CommandResponderEvent in project LogHub by fbacchella.

the class SNMPTrapGeneratorClient2 method sendSnmpV3Trap.

/**
 * Sends the v3 trap
 */
private static void sendSnmpV3Trap() {
    try {
        Address targetAddress = GenericAddress.parse("udp:" + ipAddress + "/" + port);
        TransportMapping<?> transport = new DefaultUdpTransportMapping();
        Snmp snmp = new Snmp(transport);
        USM usm = new USM(SecurityProtocols.getInstance().addDefaultProtocols(), new OctetString(MPv3.createLocalEngineID()), 0);
        SecurityProtocols.getInstance().addPrivacyProtocol(new PrivAES192());
        SecurityModels.getInstance().addSecurityModel(usm);
        transport.listen();
        snmp.getUSM().addUser(new OctetString("MD5DES"), new UsmUser(new OctetString("MD5DES"), AuthMD5.ID, new OctetString("UserName"), PrivAES128.ID, new OctetString("UserName")));
        // Create Target
        UserTarget target = new UserTarget();
        target.setAddress(targetAddress);
        target.setRetries(1);
        target.setTimeout(11500);
        target.setVersion(SnmpConstants.version3);
        target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
        target.setSecurityName(new OctetString("MD5DES"));
        // Create PDU for V3
        ScopedPDU pdu = new ScopedPDU();
        pdu.setType(ScopedPDU.NOTIFICATION);
        pdu.add(new VariableBinding(SnmpConstants.sysUpTime));
        pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, SnmpConstants.linkDown));
        pdu.add(new VariableBinding(new OID(trapOid), new OctetString("Major")));
        // Send the PDU
        snmp.send(pdu, target);
        System.out.println("Sending Trap to (IP:Port)=> " + ipAddress + ":" + port);
        snmp.addCommandResponder(new CommandResponder() {

            public void processPdu(CommandResponderEvent arg0) {
                System.out.println(arg0);
            }
        });
        snmp.close();
    } catch (Exception e) {
        System.err.println("Error in Sending Trap to (IP:Port)=> " + ipAddress + ":" + port);
        System.err.println("Exception Message = " + e.getMessage());
    }
}
Also used : OctetString(org.snmp4j.smi.OctetString) Address(org.snmp4j.smi.Address) IpAddress(org.snmp4j.smi.IpAddress) UdpAddress(org.snmp4j.smi.UdpAddress) GenericAddress(org.snmp4j.smi.GenericAddress) ScopedPDU(org.snmp4j.ScopedPDU) CommandResponderEvent(org.snmp4j.CommandResponderEvent) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) OID(org.snmp4j.smi.OID) UsmUser(org.snmp4j.security.UsmUser) USM(org.snmp4j.security.USM) PrivAES192(org.snmp4j.security.PrivAES192) Snmp(org.snmp4j.Snmp) UserTarget(org.snmp4j.UserTarget) VariableBinding(org.snmp4j.smi.VariableBinding) CommandResponder(org.snmp4j.CommandResponder)

Aggregations

CommandResponderEvent (org.snmp4j.CommandResponderEvent)6 IpAddress (org.snmp4j.smi.IpAddress)5 OID (org.snmp4j.smi.OID)5 OctetString (org.snmp4j.smi.OctetString)5 Event (loghub.Event)4 PDUv1 (org.snmp4j.PDUv1)4 VariableBinding (org.snmp4j.smi.VariableBinding)4 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)4 HashMap (java.util.HashMap)3 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)3 Pipeline (loghub.Pipeline)3 Properties (loghub.configuration.Properties)3 Ignore (org.junit.Ignore)3 Test (org.junit.Test)3 MessageDispatcherImpl (org.snmp4j.MessageDispatcherImpl)3 TransportIpAddress (org.snmp4j.smi.TransportIpAddress)3 UdpAddress (org.snmp4j.smi.UdpAddress)3 PDU (org.snmp4j.PDU)2 Snmp (org.snmp4j.Snmp)2 Address (org.snmp4j.smi.Address)2