I'm trying to create a standalone java application to send SNMP v3 Traps along with engineId.
I'm using snmp4j 2.5.3 and Java 1.8
Below is the code snippet which I am using.
When I provide engineId while addUser, it throws "Message processing model 3 returned error: Unknown security name".
When I remove, it works fine. But, my SNMP manager not recognizing the Trap.
// Sample code snippet
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(new OctetString(engineId))), 0);
SecurityProtocols.getInstance().addPrivacyProtocol(new PrivAES192());
SecurityProtocols.getInstance().addPrivacyProtocol(new PrivAES256());
SecurityProtocols.getInstance().addPrivacyProtocol(new Priv3DES());
SecurityModels.getInstance().addSecurityModel(usm);
switch (securityLevel) {
case SecurityLevel.NOAUTH_NOPRIV:
snmp.getUSM().addUser(new OctetString(username), new OctetString(engineId), new UsmUser(new OctetString(username), null,
null, null, null));
break;
case SecurityLevel.AUTH_NOPRIV:
snmp.getUSM().addUser(new OctetString(username), new OctetString(engineId), new UsmUser(new OctetString(username), auth,
new OctetString(authpassPhrase), null, null));
break;
case SecurityLevel.AUTH_PRIV:
snmp.getUSM().addUser(new OctetString(username), new OctetString(engineId), new UsmUser(new OctetString(username), auth,
new OctetString(authpassPhrase), privacy, new OctetString(privacypassPhrase)));
break;
default:
System.err.println("Undefined Security level for SNMP v3");
System.exit(1);
}
UserTarget target = new UserTarget();
target.setAddress(targetAddress);
target.setRetries(1);
target.setTimeout(5000);
target.setVersion(SnmpConstants.version3);
target.setSecurityLevel(securityLevel);
target.setSecurityName(new OctetString(username));
ScopedPDU pdu = new ScopedPDU();
pdu.setType(ScopedPDU.NOTIFICATION);
pdu.setRequestID(new Integer32(1234));
pdu.add(new VariableBinding(SnmpConstants.sysUpTime));
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, SnmpConstants.linkDown));
pdu.add(new VariableBinding(new OID(Oid), new Integer32(123)));
snmp.send(pdu, target);
System.out.println("Sending Trap to (IP:Port)=> " + ipAddress + ":" + port);
snmp.close();
I expect the SNMP Manager should recognize the Trap and it should be received at SNMP Manager.
What am I missing here?
Thanks in advance.