Search in sources :

Example 16 with Value

use of org.opennms.netmgt.xml.event.Value in project opennms by OpenNMS.

the class SnmpTrapHelper method forwardV2Trap.

/**
 * Create an SNMP V2 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 forwardV2Trap(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();
    SnmpTrapBuilder packet = SnmpUtils.getV2TrapBuilder();
    if ("v1".equals(version)) {
        // converting V1 trap to V2 (see RFC2576)
        addVarBinding(packet, SNMP_SYSUPTIME_OID, EventConstants.TYPE_SNMP_TIMETICKS, Long.toString(snmpInfo.getTimeStamp()));
        String oid;
        if (snmpInfo.getGeneric() == ENTERPRISE_SPECIFIC && snmpInfo.hasSpecific()) {
            oid = snmpInfo.getId() + ".0." + snmpInfo.getSpecific();
        } else {
            oid = SNMP_TRAPS + '.' + (snmpInfo.getGeneric() + 1);
        }
        addVarBinding(packet, SNMP_TRAP_OID, EventConstants.TYPE_SNMP_OBJECT_IDENTIFIER, oid);
        // add the V1 var bindings
        boolean addrPresent = false;
        boolean communityPresent = false;
        boolean enterprisePresent = false;
        int i = 0;
        for (Parm parm : event.getParmCollection()) {
            Value value = parm.getValue();
            try {
                addVarBinding(packet, parm.getParmName(), value.getType(), value.getEncoding(), value.getContent());
            } catch (SnmpTrapHelperException e) {
                throw new SnmpTrapHelperException(e.getMessage() + " in event parm[" + i + "]");
            }
            if (SNMP_TRAP_ADDRESS_OID.equals(parm.getParmName())) {
                addrPresent = true;
            } else if (SNMP_TRAP_COMMUNITY_OID.equals(parm.getParmName())) {
                communityPresent = true;
            } else if (SNMP_TRAP_ENTERPRISE_OID.equals(parm.getParmName())) {
                enterprisePresent = true;
            }
            i++;
        }
        if (!addrPresent) {
            addVarBinding(packet, SNMP_TRAP_ADDRESS_OID, EventConstants.TYPE_SNMP_IPADDRESS, event.getSnmphost());
        }
        if (!communityPresent) {
            addVarBinding(packet, SNMP_TRAP_COMMUNITY_OID, EventConstants.TYPE_SNMP_OCTET_STRING, snmpInfo.getCommunity());
        }
        if (!enterprisePresent) {
            addVarBinding(packet, SNMP_TRAP_ENTERPRISE_OID, EventConstants.TYPE_SNMP_OBJECT_IDENTIFIER, snmpInfo.getId());
        }
    } else if ("v2".equals(version)) {
        addVarBinding(packet, SNMP_SYSUPTIME_OID, EventConstants.TYPE_SNMP_TIMETICKS, Long.toString(snmpInfo.getTimeStamp()));
        String oid;
        if (snmpInfo.getGeneric() == ENTERPRISE_SPECIFIC) {
            oid = snmpInfo.getId() + "." + snmpInfo.getSpecific();
        } else {
            oid = SNMP_TRAPS + '.' + (snmpInfo.getGeneric() + 1);
        }
        addVarBinding(packet, SNMP_TRAP_OID, EventConstants.TYPE_SNMP_OBJECT_IDENTIFIER, oid);
        int i = 0;
        for (Parm parm : event.getParmCollection()) {
            Value value = parm.getValue();
            try {
                addVarBinding(packet, 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(), packet);
}
Also used : Value(org.opennms.netmgt.xml.event.Value) Snmp(org.opennms.netmgt.xml.event.Snmp) Parm(org.opennms.netmgt.xml.event.Parm) SnmpTrapBuilder(org.opennms.netmgt.snmp.SnmpTrapBuilder)

Example 17 with Value

use of org.opennms.netmgt.xml.event.Value in project opennms by OpenNMS.

the class JtiTelemetryIT method sendnewSuspectEvent.

public static OnmsNode sendnewSuspectEvent(Executor executor, InetSocketAddress opennmsHttp, TestEnvironment m_testEnvironment, boolean isMinion, Date startOfTest) throws ClientProtocolException, IOException {
    Event minionEvent = new Event();
    minionEvent.setUei("uei.opennms.org/internal/discovery/newSuspect");
    minionEvent.setHost(SENDER_IP);
    minionEvent.setInterface(SENDER_IP);
    minionEvent.setInterfaceAddress(Inet4Address.getByName(SENDER_IP));
    minionEvent.setSource("system-test");
    minionEvent.setSeverity("4");
    if (isMinion) {
        Parm parm = new Parm();
        parm.setParmName("location");
        Value minion = new Value("MINION");
        parm.setValue(minion);
        List<Parm> parms = new ArrayList<>();
        parms.add(parm);
        minionEvent.setParmCollection(parms);
    }
    String xmlString = JaxbUtils.marshal(minionEvent);
    executor.execute(Request.Post(String.format("http://%s:%d/opennms/rest/events", opennmsHttp.getAddress().getHostAddress(), opennmsHttp.getPort())).bodyString(xmlString, ContentType.APPLICATION_XML)).returnContent();
    InetSocketAddress pgsql = m_testEnvironment.getServiceAddress(ContainerAlias.POSTGRES, 5432);
    HibernateDaoFactory daoFactory = new HibernateDaoFactory(pgsql);
    EventDao eventDao = daoFactory.getDao(EventDaoHibernate.class);
    NodeDao nodeDao = daoFactory.getDao(NodeDaoHibernate.class);
    Criteria criteria = new CriteriaBuilder(OnmsEvent.class).eq("eventUei", EventConstants.NEW_SUSPECT_INTERFACE_EVENT_UEI).ge("eventTime", startOfTest).eq("ipAddr", Inet4Address.getByName(SENDER_IP)).toCriteria();
    await().atMost(1, MINUTES).pollInterval(10, SECONDS).until(DaoUtils.countMatchingCallable(eventDao, criteria), greaterThan(0));
    final OnmsNode onmsNode = await().atMost(1, MINUTES).pollInterval(5, SECONDS).until(DaoUtils.findMatchingCallable(nodeDao, new CriteriaBuilder(OnmsNode.class).eq("label", SENDER_IP).ge("createTime", startOfTest).toCriteria()), notNullValue());
    assertNotNull(onmsNode);
    if (isMinion) {
        assertThat(onmsNode.getLocation().getLocationName(), is("MINION"));
    }
    LOG.info(" New suspect event has been sent and node has been created for IP : {}", SENDER_IP);
    return onmsNode;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) HibernateDaoFactory(org.opennms.smoketest.utils.HibernateDaoFactory) OnmsNode(org.opennms.netmgt.model.OnmsNode) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) Parm(org.opennms.netmgt.xml.event.Parm) Criteria(org.opennms.core.criteria.Criteria) NodeDao(org.opennms.netmgt.dao.api.NodeDao) EventDao(org.opennms.netmgt.dao.api.EventDao) Value(org.opennms.netmgt.xml.event.Value) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Event(org.opennms.netmgt.xml.event.Event) OnmsEvent(org.opennms.netmgt.model.OnmsEvent)

Example 18 with Value

use of org.opennms.netmgt.xml.event.Value in project opennms by OpenNMS.

the class Collectd method handlePrimarySnmpInterfaceChanged.

/**
 * Process the 'primarySnmpInterfaceChanged' event. Extract the old and
 * new primary SNMP interface addresses from the event parms. Any
 * CollectableService objects located in the collectable services list
 * which match the IP address of the old primary interface and have a
 * service name of "SNMP" are flagged for deletion. This will ensure that
 * the old primary interface is no longer collected against. Finally the
 * new primary SNMP interface is scheduled. The packages are examined and
 * new CollectableService objects are created, initialized and scheduled
 * for collection.
 *
 * @param event
 *            The event to process.
 * @throws InsufficientInformationException
 */
private void handlePrimarySnmpInterfaceChanged(Event event) throws InsufficientInformationException {
    EventUtils.checkNodeId(event);
    EventUtils.checkInterface(event);
    LOG.debug("primarySnmpInterfaceChangedHandler:  processing primary SNMP interface changed event...");
    // 
    if (!event.getService().equals("SNMP"))
        return;
    // Extract the old and new primary SNMP interface addresses from the
    // event parms.
    // 
    String oldPrimaryIfAddr = null;
    String parmName = null;
    Value parmValue = null;
    String parmContent = null;
    for (Parm parm : event.getParmCollection()) {
        parmName = parm.getParmName();
        parmValue = parm.getValue();
        if (parmValue == null)
            continue;
        else
            parmContent = parmValue.getContent();
        // old primary SNMP interface (optional parameter)
        if (parmName.equals(EventConstants.PARM_OLD_PRIMARY_SNMP_ADDRESS)) {
            oldPrimaryIfAddr = parmContent;
        }
    }
    if (oldPrimaryIfAddr != null) {
        // 
        synchronized (getCollectableServices()) {
            CollectableService cSvc = null;
            ListIterator<CollectableService> liter = getCollectableServices().listIterator();
            while (liter.hasNext()) {
                cSvc = liter.next();
                final InetAddress addr = (InetAddress) cSvc.getAddress();
                final String addrString = str(addr);
                if (addrString != null && addrString.equals(oldPrimaryIfAddr)) {
                    synchronized (cSvc) {
                        // Got a match! Retrieve the CollectorUpdates
                        // object
                        // associated
                        // with this CollectableService.
                        CollectorUpdates updates = cSvc.getCollectorUpdates();
                        // Now set the deleted flag
                        updates.markForDeletion();
                        LOG.debug("primarySnmpInterfaceChangedHandler: marking {} as deleted for service SNMP.", oldPrimaryIfAddr);
                    }
                    // Now safe to remove the collectable service from
                    // the collectable services list
                    liter.remove();
                }
            }
        }
    }
    // Now we can schedule the new service...
    // 
    scheduleForCollection(event);
    LOG.debug("primarySnmpInterfaceChangedHandler: processing of primarySnmpInterfaceChanged event for nodeid {} completed.", event.getNodeid());
}
Also used : Value(org.opennms.netmgt.xml.event.Value) Parm(org.opennms.netmgt.xml.event.Parm) InetAddress(java.net.InetAddress)

Example 19 with Value

use of org.opennms.netmgt.xml.event.Value in project opennms by OpenNMS.

the class EventBuilder method addParam.

/**
 * <p>addParam</p>
 *
 * @param parmName a {@link java.lang.String} object.
 * @param val a {@link java.lang.String} object.
 * @return a {@link org.opennms.netmgt.model.events.EventBuilder} object.
 */
public EventBuilder addParam(final String parmName, final boolean val) {
    if (parmName != null) {
        final Value value = new Value();
        value.setContent(val ? "true" : "false");
        final Parm parm = new Parm();
        parm.setParmName(parmName);
        parm.setValue(value);
        this.addParam(parm);
    }
    return this;
}
Also used : Value(org.opennms.netmgt.xml.event.Value) Parm(org.opennms.netmgt.xml.event.Parm)

Example 20 with Value

use of org.opennms.netmgt.xml.event.Value in project opennms by OpenNMS.

the class SyntaxToEvent method processSyntax.

/**
 * <p>processSyntax</p>
 *
 * @param name a {@link java.lang.String} object.
 * @param value a {@link org.opennms.netmgt.snmp.SnmpValue} object.
 * @return a {@link org.opennms.netmgt.xml.event.Parm} object.
 */
public static Parm processSyntax(final String name, final SnmpValue value) {
    final Value val = new Value();
    boolean found = false;
    for (int i = 0; i < m_syntaxToEvents.length; i++) {
        if (m_syntaxToEvents[i].getTypeId() == -1 || m_syntaxToEvents[i].getTypeId() == value.getType()) {
            val.setType(m_syntaxToEvents[i].getType());
            String encoding = null;
            boolean displayable = false;
            try {
                displayable = value.isDisplayable();
            } catch (ArrayIndexOutOfBoundsException aioobe) {
            // Eat it
            // This should not be necessary when NMS-7547 is fixed
            }
            if (displayable) {
                if (name.matches(".*[Mm][Aa][Cc].*")) {
                    encoding = EventConstants.XML_ENCODING_MAC_ADDRESS;
                } else {
                    encoding = EventConstants.XML_ENCODING_TEXT;
                }
            } else {
                if (value.getBytes().length == 6) {
                    encoding = EventConstants.XML_ENCODING_MAC_ADDRESS;
                } else {
                    encoding = EventConstants.XML_ENCODING_BASE64;
                }
            }
            val.setEncoding(encoding);
            val.setContent(EventConstants.toString(encoding, value));
            found = true;
            break;
        }
    }
    if (!found) {
        throw new IllegalStateException("Internal error: fell through the " + "bottom of the loop.  The syntax-to-events array might not have a " + "catch-all for Object");
    }
    final Parm parm = new Parm();
    parm.setParmName(name);
    parm.setValue(val);
    return parm;
}
Also used : Value(org.opennms.netmgt.xml.event.Value) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) Parm(org.opennms.netmgt.xml.event.Parm)

Aggregations

Value (org.opennms.netmgt.xml.event.Value)33 Parm (org.opennms.netmgt.xml.event.Parm)31 InetAddress (java.net.InetAddress)5 Event (org.opennms.netmgt.xml.event.Event)5 ArrayList (java.util.ArrayList)4 Date (java.util.Date)3 Test (org.junit.Test)3 OnmsNode (org.opennms.netmgt.model.OnmsNode)3 PollableNode (org.opennms.netmgt.poller.pollables.PollableNode)3 EventHandler (org.opennms.netmgt.events.api.annotations.EventHandler)2 OnmsEvent (org.opennms.netmgt.model.OnmsEvent)2 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)2 PollableInterface (org.opennms.netmgt.poller.pollables.PollableInterface)2 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)2 Logmsg (org.opennms.netmgt.xml.event.Logmsg)2 Snmp (org.opennms.netmgt.xml.event.Snmp)2 BufferedReader (java.io.BufferedReader)1 StringReader (java.io.StringReader)1 InetSocketAddress (java.net.InetSocketAddress)1 HashMap (java.util.HashMap)1