Search in sources :

Example 1 with SnmpV1TrapBuilder

use of org.opennms.netmgt.snmp.SnmpV1TrapBuilder in project opennms by OpenNMS.

the class TrapHandlerITCase method sendV1Trap.

public void sendV1Trap(String enterprise, int generic, int specific, LinkedHashMap<String, SnmpValue> varbinds) throws Exception {
    SnmpV1TrapBuilder pdu = SnmpUtils.getV1TrapBuilder();
    pdu.setEnterprise(SnmpObjId.get(enterprise));
    pdu.setGeneric(generic);
    pdu.setSpecific(specific);
    pdu.setTimeStamp(0);
    pdu.setAgentAddress(m_ip);
    Iterator<Map.Entry<String, SnmpValue>> it = varbinds.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, SnmpValue> pairs = it.next();
        pdu.addVarBind(SnmpObjId.get(pairs.getKey()), pairs.getValue());
    }
    pdu.send(getHostAddress(), m_trapdConfig.getSnmpTrapPort(), "public");
}
Also used : SnmpV1TrapBuilder(org.opennms.netmgt.snmp.SnmpV1TrapBuilder) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with SnmpV1TrapBuilder

use of org.opennms.netmgt.snmp.SnmpV1TrapBuilder in project opennms by OpenNMS.

the class TrapHandlerITCase method sendV1Trap.

public void sendV1Trap(String enterprise, int generic, int specific) throws Exception {
    SnmpV1TrapBuilder pdu = SnmpUtils.getV1TrapBuilder();
    pdu.setEnterprise(SnmpObjId.get(enterprise));
    pdu.setGeneric(generic);
    pdu.setSpecific(specific);
    pdu.setTimeStamp(0);
    pdu.setAgentAddress(m_ip);
    pdu.send(getHostAddress(), m_trapdConfig.getSnmpTrapPort(), "public");
}
Also used : SnmpV1TrapBuilder(org.opennms.netmgt.snmp.SnmpV1TrapBuilder)

Example 3 with SnmpV1TrapBuilder

use of org.opennms.netmgt.snmp.SnmpV1TrapBuilder in project opennms by OpenNMS.

the class SnmpTrapHelper method forwardV1Trap.

/**
     * 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();
            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);
}
Also used : SnmpV1TrapBuilder(org.opennms.netmgt.snmp.SnmpV1TrapBuilder) Value(org.opennms.netmgt.xml.event.Value) Snmp(org.opennms.netmgt.xml.event.Snmp) Parm(org.opennms.netmgt.xml.event.Parm) InetAddress(java.net.InetAddress)

Example 4 with SnmpV1TrapBuilder

use of org.opennms.netmgt.snmp.SnmpV1TrapBuilder in project opennms by OpenNMS.

the class SnmpTrapHelper method createV1Trap.

/**
     * 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;
}
Also used : SnmpV1TrapBuilder(org.opennms.netmgt.snmp.SnmpV1TrapBuilder)

Example 5 with SnmpV1TrapBuilder

use of org.opennms.netmgt.snmp.SnmpV1TrapBuilder in project opennms by OpenNMS.

the class TrapdIT method testSnmpV1TrapSend.

@Test
public void testSnmpV1TrapSend() throws Exception {
    String localhost = "127.0.0.1";
    InetAddress localAddr = InetAddressUtils.addr(localhost);
    SnmpV1TrapBuilder pdu = SnmpUtils.getV1TrapBuilder();
    pdu.setEnterprise(SnmpObjId.get(".1.3.6.1.4.1.5813"));
    pdu.setGeneric(1);
    pdu.setSpecific(0);
    pdu.setTimeStamp(666L);
    pdu.setAgentAddress(localAddr);
    EventBuilder defaultTrapBuilder = new EventBuilder("uei.opennms.org/default/trap", "trapd");
    defaultTrapBuilder.setInterface(localAddr);
    defaultTrapBuilder.setSnmpVersion("v1");
    m_mockEventIpcManager.getEventAnticipator().anticipateEvent(defaultTrapBuilder.getEvent());
    EventBuilder newSuspectBuilder = new EventBuilder(EventConstants.NEW_SUSPECT_INTERFACE_EVENT_UEI, "trapd");
    newSuspectBuilder.setInterface(localAddr);
    m_mockEventIpcManager.getEventAnticipator().anticipateEvent(newSuspectBuilder.getEvent());
    pdu.send(localhost, m_trapdConfig.getSnmpTrapPort(), "public");
    // Allow time for Trapd and Eventd to do their magic
    Thread.sleep(5000);
}
Also used : SnmpV1TrapBuilder(org.opennms.netmgt.snmp.SnmpV1TrapBuilder) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

SnmpV1TrapBuilder (org.opennms.netmgt.snmp.SnmpV1TrapBuilder)8 InetAddress (java.net.InetAddress)3 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)2 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)1 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)1 SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)1 Parm (org.opennms.netmgt.xml.event.Parm)1 Snmp (org.opennms.netmgt.xml.event.Snmp)1 Value (org.opennms.netmgt.xml.event.Value)1