Search in sources :

Example 11 with Value

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

the class OnmsRestEventsClient method toEvent.

public Event toEvent(OnmsEvent onmsEvent) {
    Event event = new Event();
    if (onmsEvent.getId() != null)
        event.setDbid(onmsEvent.getId());
    if (onmsEvent.getEventUei() != null)
        event.setUei(onmsEvent.getEventUei());
    if (onmsEvent.getEventCreateTime() != null)
        event.setCreationTime(onmsEvent.getEventCreateTime());
    // event.setService()
    if (onmsEvent.getSeverityLabel() != null)
        event.setSeverity(onmsEvent.getSeverityLabel());
    if (onmsEvent.getEventDescr() != null)
        event.setDescr(onmsEvent.getEventDescr());
    if (onmsEvent.getEventHost() != null)
        event.setHost(onmsEvent.getEventHost());
    List<Parm> parmColl = new ArrayList<>();
    if (onmsEvent.getEventParameters() != null) {
        List<OnmsEventParameter> params = onmsEvent.getEventParameters();
        for (OnmsEventParameter onmsEventParameter : params) {
            String parmName = onmsEventParameter.getName();
            String type = onmsEventParameter.getType();
            String value = onmsEventParameter.getValue();
            Parm parm = new Parm();
            parm.setParmName(parmName);
            Value parmvalue = new Value();
            parmvalue.setType(type);
            parmvalue.setContent(value);
            parm.setValue(parmvalue);
            parmColl.add(parm);
        }
    }
    // add node label as param
    if (onmsEvent.getNodeLabel() != null) {
        Parm parm = new Parm();
        parm.setParmName(NODE_LABEL);
        Value parmValue = new Value();
        parm.setValue(parmValue);
        parmValue.setType("string");
        parmValue.setEncoding("text");
        parmValue.setContent(onmsEvent.getNodeLabel());
        parmColl.add(parm);
    }
    event.setParmCollection(parmColl);
    if (onmsEvent.getEventLogMsg() != null) {
        Logmsg logmsg = new Logmsg();
        logmsg.setContent(onmsEvent.getEventLogMsg());
        event.setLogmsg(logmsg);
    }
    if (onmsEvent.getNodeId() != null) {
        Integer i = onmsEvent.getNodeId();
        Long l = Long.valueOf(i.longValue());
        event.setNodeid(l);
    }
    return event;
}
Also used : Logmsg(org.opennms.netmgt.xml.event.Logmsg) ArrayList(java.util.ArrayList) Value(org.opennms.netmgt.xml.event.Value) OnmsEvent(org.opennms.netmgt.model.OnmsEvent) Event(org.opennms.netmgt.xml.event.Event) Parm(org.opennms.netmgt.xml.event.Parm) OnmsEventParameter(org.opennms.netmgt.model.OnmsEventParameter)

Example 12 with Value

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

the class SyslogEventForwarderTest method testForwardEvent.

/**
 * Test forward event.
 *
 * @throws Exception the exception
 */
@Test
public void testForwardEvent() throws Exception {
    SyslogEventForwarder forwarder = new SyslogEventForwarder();
    forwarder.initialize("localTest1");
    OnmsNode node = new OnmsNode();
    node.setForeignSource("TestGroup");
    node.setForeignId("1");
    node.setId(1);
    node.setLabel("p-brane");
    Event event = new Event();
    event.setUei("uei.opennms.org/junit/testEvent");
    event.setNodeid(1l);
    event.setDbid(100);
    event.setLogmsg(new Logmsg());
    event.getLogmsg().setContent("something is wrong");
    Parm param = new Parm();
    param.setParmName("forwardEventToActivitySyslog");
    Value value = new Value();
    value.setContent("true");
    param.setValue(value);
    event.setParmCollection(new ArrayList<Parm>());
    event.getParmCollection().add(param);
    event.setSeverity("Major");
    forwarder.forward(event, node);
    Thread.sleep(100);
    BufferedReader r = new BufferedReader(new StringReader(m_logStream.readStream()));
    List<String> messages = new LinkedList<>();
    String line = null;
    while ((line = r.readLine()) != null) {
        messages.add(line);
        Thread.sleep(10);
    }
    Assert.assertTrue("Log messages sent: 1, Log messages received: " + messages.size(), 1 == messages.size());
    messages.forEach(System.out::println);
    Assert.assertTrue(messages.get(0).contains("EVENT 100 FOR NODE p-brane IN TestGroup: something is wrong"));
    forwarder.reload();
    forwarder.shutdown();
}
Also used : Logmsg(org.opennms.netmgt.xml.event.Logmsg) OnmsNode(org.opennms.netmgt.model.OnmsNode) Parm(org.opennms.netmgt.xml.event.Parm) LinkedList(java.util.LinkedList) Value(org.opennms.netmgt.xml.event.Value) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) Event(org.opennms.netmgt.xml.event.Event) Test(org.junit.Test)

Example 13 with Value

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

the class PollerEventProcessor method interfaceReparentedHandler.

/**
 * This method is responsible for processing 'interfacReparented' events. An
 * 'interfaceReparented' event will have old and new nodeId parms associated
 * with it. Node outage processing hierarchy will be updated to reflect the
 * new associations.
 *
 * @param event
 *            The event to process.
 */
private void interfaceReparentedHandler(Event event) {
    LOG.debug("interfaceReparentedHandler: processing interfaceReparented event for {}", event.getInterface());
    // Verify that the event has an interface associated with it
    if (event.getInterfaceAddress() == null)
        return;
    InetAddress ipAddr = event.getInterfaceAddress();
    // Extract the old and new nodeId's from the event parms
    String oldNodeIdStr = null;
    String newNodeIdStr = 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 nodeid
        if (parmName.equals(EventConstants.PARM_OLD_NODEID)) {
            oldNodeIdStr = parmContent;
        } else // new nodeid
        if (parmName.equals(EventConstants.PARM_NEW_NODEID)) {
            newNodeIdStr = parmContent;
        }
    }
    // 
    if (oldNodeIdStr == null || newNodeIdStr == null) {
        LOG.error("interfaceReparentedHandler: old and new nodeId parms are required, unable to process.");
        return;
    }
    PollableNode oldNode;
    PollableNode newNode;
    try {
        oldNode = getNetwork().getNode(Integer.parseInt(oldNodeIdStr));
        if (oldNode == null) {
            LOG.error("interfaceReparentedHandler: Cannot locate old node {} belonging to interface {}", oldNodeIdStr, ipAddr);
            return;
        }
        newNode = getNetwork().getNode(Integer.parseInt(newNodeIdStr));
        if (newNode == null) {
            LOG.error("interfaceReparentedHandler: Cannot locate new node {} to move interface to.  Also, grammar error: ended a sentence with a preposition.", newNodeIdStr);
            return;
        }
        PollableInterface iface = oldNode.getInterface(ipAddr);
        if (iface == null) {
            LOG.error("interfaceReparentedHandler: Cannot locate interface with ipAddr {} to reparent.", ipAddr);
            return;
        }
        iface.reparentTo(newNode);
    } catch (final NumberFormatException nfe) {
        LOG.error("interfaceReparentedHandler: failed converting old/new nodeid parm to integer, unable to process.");
        return;
    }
}
Also used : PollableNode(org.opennms.netmgt.poller.pollables.PollableNode) Value(org.opennms.netmgt.xml.event.Value) Parm(org.opennms.netmgt.xml.event.Parm) PollableInterface(org.opennms.netmgt.poller.pollables.PollableInterface) InetAddress(java.net.InetAddress)

Example 14 with Value

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

the class DataSender method handleRtcSubscribe.

/**
 * Inform the data sender of the new listener
 */
@EventHandler(uei = EventConstants.RTC_SUBSCRIBE_EVENT_UEI)
public void handleRtcSubscribe(Event event) {
    List<Parm> list = event.getParmCollection();
    if (list == null) {
        LOG.warn("{} ignored - info incomplete (null event parms)", event.getUei());
        return;
    }
    String url = null;
    String clabel = null;
    String user = null;
    String passwd = null;
    String parmName = null;
    Value parmValue = null;
    String parmContent = null;
    for (Parm parm : list) {
        parmName = parm.getParmName();
        parmValue = parm.getValue();
        if (parmValue == null)
            continue;
        else
            parmContent = parmValue.getContent();
        if (parmName.equals(EventConstants.PARM_URL)) {
            url = parmContent;
        } else if (parmName.equals(EventConstants.PARM_CAT_LABEL)) {
            clabel = parmContent;
        } else if (parmName.equals(EventConstants.PARM_USER)) {
            user = parmContent;
        } else if (parmName.equals(EventConstants.PARM_PASSWD)) {
            passwd = parmContent;
        }
    }
    // check that we got all required parms
    if (url == null || clabel == null || user == null || passwd == null) {
        LOG.warn("{} did not have all required information. Values contained url: {} catlabel: {} user: {} passwd: {}", event.getUei(), url, clabel, user, passwd);
    } else {
        subscribe(url, clabel, user, passwd);
        LOG.debug("{} subscribed {}: {}: {}", event.getUei(), url, clabel, user);
    }
}
Also used : Value(org.opennms.netmgt.xml.event.Value) Parm(org.opennms.netmgt.xml.event.Parm) EventHandler(org.opennms.netmgt.events.api.annotations.EventHandler)

Example 15 with Value

use of org.opennms.netmgt.xml.event.Value 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)

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