/** * Create an SNMP V1 trap with the specified enterprise IS, agent address, * generic ID, specific ID, and time stamp. * * @param entId * The enterprise ID for the trap. * @param agentAddr * The agent address for the trap. * @param generic * The generic ID for the trap. * @param specific * The specific ID for the trap. * @param timeStamp * The time stamp for the trap. * @return The newly-created trap. * @throws java.net.UnknownHostException if any. */ public SnmpV1TrapBuilder createV1Trap(String entId, String agentAddr, int generic, int specific, long timeStamp) throws UnknownHostException { SnmpV1TrapBuilder trap = SnmpUtils.getV1TrapBuilder(); trap.setEnterprise(SnmpObjId.get(entId)); trap.setAgentAddress(InetAddressUtils.addr(agentAddr)); trap.setGeneric(generic); trap.setSpecific(specific); trap.setTimeStamp(timeStamp); return trap; }
/** * Create an SNMP V1 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 forwardV1Trap(SnmpTrapConfig trapConfig) throws SnmpTrapException { SnmpV1TrapBuilder trap = SnmpUtils.getV1TrapBuilder(); trap.setEnterprise(SnmpObjId.get(trapConfig.getEnterpriseId())); trap.setAgentAddress(trapConfig.getHostAddress()); if (trapConfig.hasGeneric()) { trap.setGeneric(trapConfig.getGeneric()); } if (trapConfig.hasSpecific()) { trap.setSpecific(trapConfig.getSpecific()); } trap.setTimeStamp(System.currentTimeMillis() / 1000); addParameters(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); } }
/** * <p>sendV1Trap</p> * * @throws java.lang.Exception if any. */ public void sendV1Trap() throws Exception { SnmpV1TrapBuilder pdu = SnmpUtils.getV1TrapBuilder(); pdu.setEnterprise(SnmpObjId.get(getEnterpriseId())); pdu.setGeneric(getGenericId()); pdu.setSpecific(getSpecificId()); pdu.setTimeStamp(0); InetAddress agentAddress = getHostInetAddress(); pdu.setAgentAddress(agentAddress); pdu.addVarBind(SnmpObjId.get(".1.3.6.1.4.1.5813.20.1"), SnmpUtils.getValueFactory().getOctetString(getVarbind().getBytes())); pdu.send(InetAddressUtils.str(agentAddress), getPort(), getCommunity()); }
SnmpV1TrapBuilder trap = SnmpUtils.getV1TrapBuilder();