/** * Create an SNMP V2 trap with the specified trap object ID, and sysUpTime * value. * * @param trapOid * The trap object id. * @param sysUpTime * The system up time. * @return The newly-created trap. * @exception Throws * SnmpTrapHelperException if the trap cannot be created for * any reason. * @throws org.opennms.netmgt.scriptd.helper.SnmpTrapHelperException if any. */ public SnmpTrapBuilder createV2Trap(String trapOid, String sysUpTime) throws SnmpTrapHelperException { SnmpTrapBuilder packet = SnmpUtils.getV2TrapBuilder(); addVarBinding(packet, SNMP_SYSUPTIME_OID, EventConstants.TYPE_SNMP_TIMETICKS, sysUpTime); addVarBinding(packet, SNMP_TRAP_OID, EventConstants.TYPE_SNMP_OBJECT_IDENTIFIER, trapOid); return packet; }
/** * Create an SNMP V2 trap based on the content of the specified trap configuration, and send it to the appropriate destination. * * @param trapConfig The trap configuration mapping object * @throws SnmpTrapException if any. */ private void forwardV2Trap(SnmpTrapConfig trapConfig) throws SnmpTrapException { SnmpTrapBuilder trap = SnmpUtils.getV2TrapBuilder(); populateTrapBuilder(trap, trapConfig); try { SnmpAgentConfig config = getAgentConfig(trapConfig); trap.send(config.getAddress().getHostAddress(), config.getPort(), config.getReadCommunity()); } catch (Throwable e) { throw new SnmpTrapException("Failed to send trap "+e.getMessage(), e); } }
SnmpTrapBuilder packet = SnmpUtils.getV2TrapBuilder();
/** * <p>sendV2Trap</p> * * @throws java.lang.Exception if any. */ public void sendV2Trap() throws Exception { SnmpObjId enterpriseId = SnmpObjId.get(getEnterpriseId()); boolean isGeneric = false; SnmpObjId trapOID; if (SnmpObjId.get(".1.3.6.1.6.3.1.1.5").isPrefixOf(enterpriseId)) { isGeneric = true; trapOID = enterpriseId; } else { trapOID = SnmpObjId.get(enterpriseId, new SnmpInstId(getSpecificId())); // XXX or should it be this // trap OID = enterprise + ".0." + specific; } SnmpTrapBuilder pdu = SnmpUtils.getV2TrapBuilder(); pdu.addVarBind(SnmpObjId.get(".1.3.6.1.2.1.1.3.0"), SnmpUtils.getValueFactory().getTimeTicks(0)); pdu.addVarBind(SnmpObjId.get(".1.3.6.1.6.3.1.1.4.1.0"), SnmpUtils.getValueFactory().getObjectId(trapOID)); if (isGeneric) { pdu.addVarBind(SnmpObjId.get(".1.3.6.1.6.3.1.1.4.3.0"), SnmpUtils.getValueFactory().getObjectId(enterpriseId)); } pdu.addVarBind(SnmpObjId.get(".1.3.6.1.4.1.5813.20.1"), SnmpUtils.getValueFactory().getOctetString(getVarbind().getBytes())); pdu.send(InetAddressUtils.str(getHostInetAddress()), getPort(), getCommunity()); }