I have to send an SNMP v3 Trap to a server using SNMP4J but I can't seem to make it work. With a TCP dump, the remote server guys confirm they're receiving packets but they seem to be malformed in some way.
Code is below:
TransportMapping transportMapping = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transportMapping);
OctetString localEngineId = new OctetString(MPv3.createLocalEngineID());
USM usm = new USM(SecurityProtocols.getInstance()
.addDefaultProtocols(), new OctetString(
MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
OctetString securityName = new OctetString(username);
OctetString authPassphrase = new OctetString(authPassword);
OctetString privPassphrase = new OctetString(privPassword);
OID authProtocol = AuthHMAC192SHA256.ID;
OID privProtocol = PrivAES128.ID;
snmp.getUSM().addUser(securityName, new UsmUser(securityName, authProtocol, authPassphrase, privProtocol, privPassphrase));
UserTarget target = new UserTarget();
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target.setSecurityName(securityName);
target.setAddress(new UdpAddress(agentIp + "/" + agentPort));
target.setVersion(SnmpConstants.version3);
ScopedPDU pdu = new ScopedPDU();
pdu.setType(PDU.SET);
pdu.setContextEngineID(localEngineId);
pdu.add(new VariableBinding(new OID(trapOID), new OctetString(
"beginraisetime:2023-09-10 16:00:00;" +
"endraisetime:" +
"2023-09-13 15:00:00" + ";")));
pdu.setContextName(new OctetString("context name"));
System.out.println("Sending V3 trap");
System.out.println("PDU: " + pdu);
snmp.listen();
snmp.send(pdu, target);
snmp.close();
I tried to find answers online, there is not a lot of help but I keep reading issues about the Engine ID.. maybe there's something wrong in the way it's generated.