Search in sources :

Example 1 with CommandResponder

use of org.snmp4j.CommandResponder 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

CommandResponder (org.snmp4j.CommandResponder)1 CommandResponderEvent (org.snmp4j.CommandResponderEvent)1 ScopedPDU (org.snmp4j.ScopedPDU)1 Snmp (org.snmp4j.Snmp)1 UserTarget (org.snmp4j.UserTarget)1 PrivAES192 (org.snmp4j.security.PrivAES192)1 USM (org.snmp4j.security.USM)1 UsmUser (org.snmp4j.security.UsmUser)1 Address (org.snmp4j.smi.Address)1 GenericAddress (org.snmp4j.smi.GenericAddress)1 IpAddress (org.snmp4j.smi.IpAddress)1 OID (org.snmp4j.smi.OID)1 OctetString (org.snmp4j.smi.OctetString)1 UdpAddress (org.snmp4j.smi.UdpAddress)1 VariableBinding (org.snmp4j.smi.VariableBinding)1 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)1