/** * 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 event, and forward the trap to * the specified address and port. It is assumed that the specified event represents an SNMP V1 or * V2 trap that was received by OpenNMS (TrapD). * * @param event The event upon which the trap content should be based * @param destAddr The address to which the trap should be forwarded * @param destPort The port to which the trap should be forwarded * @exception Throws SnmpTrapHelperException if the variable binding cannot be added to the trap * for any reason. * @throws org.opennms.netmgt.scriptd.helper.SnmpTrapHelperException if any. */ public void forwardV1Trap(Event event, String destAddr, int destPort) throws SnmpTrapHelperException { // the event must correspond to an SNMP trap Snmp snmpInfo = event.getSnmp(); if (snmpInfo == null) { throw new SnmpTrapHelperException( "Cannot forward an event with no SNMP info: " + event.getUei()); } // check the version of the original trap String version = snmpInfo.getVersion(); SnmpV1TrapBuilder trap = SnmpUtils.getV1TrapBuilder(); if ("v1".equals(version)) { trap.setEnterprise(SnmpObjId.get(snmpInfo.getId())); InetAddress agentAddress; agentAddress = InetAddressUtils.addr(event.getSnmphost()); if (agentAddress == null) { throw new SnmpTrapHelperException("Invalid ip address."); } trap.setAgentAddress(agentAddress); if (snmpInfo.hasGeneric()) { trap.setGeneric(snmpInfo.getGeneric()); } if (snmpInfo.hasSpecific()) { trap.setSpecific(snmpInfo.getSpecific()); } trap.setTimeStamp(snmpInfo.getTimeStamp()); // varbinds int i = 0; for (Parm parm : event.getParmCollection()) { try { Value value = parm.getValue(); addVarBinding( trap, parm.getParmName(), value.getType(), value.getEncoding(), value.getContent()); } catch (SnmpTrapHelperException e) { throw new SnmpTrapHelperException(e.getMessage() + " in event parm[" + i + "]"); } finally { i++; } } } else if ("v2".equals(version)) { // converting V2 trap to V1 (see RFC2576) trap.setEnterprise(SnmpObjId.get(snmpInfo.getId())); String addr = null; for (Parm parm : event.getParmCollection()) { if (SNMP_TRAP_ADDRESS_OID.equals(parm.getParmName())) { addr = parm.getValue().getContent(); break; } } if (addr == null) { addr = "0.0.0.0"; } InetAddress agentAddress; agentAddress = InetAddressUtils.addr(addr); if (agentAddress == null) { throw new SnmpTrapHelperException("Invalid ip address."); } trap.setAgentAddress(agentAddress); trap.setGeneric(snmpInfo.getGeneric()); trap.setSpecific(snmpInfo.getSpecific()); trap.setTimeStamp(snmpInfo.getTimeStamp()); // varbinds int i = 0; for (Parm parm : event.getParmCollection()) { Value value = parm.getValue(); // omit any parms with type=Counter64 if (!(EventConstants.TYPE_SNMP_COUNTER64.equals(value.getType()))) { try { addVarBinding( trap, parm.getParmName(), value.getType(), value.getEncoding(), value.getContent()); } catch (SnmpTrapHelperException e) { throw new SnmpTrapHelperException(e.getMessage() + " in event parm[" + i + "]"); } } i++; } } else { throw new SnmpTrapHelperException("Invalid SNMP version: " + version); } // send the trap sendTrap(destAddr, destPort, snmpInfo.getCommunity(), trap); }