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());
}
}
Aggregations