Search in sources :

Example 16 with Argument

use of org.opennms.netmgt.model.notifd.Argument in project opennms by OpenNMS.

the class MicroblogDMNotificationStrategyTest method configureArgs.

@Override
public List<Argument> configureArgs() {
    List<Argument> arguments = super.configureArgs();
    Argument arg = new Argument("-ublog", null, "jeffg", false);
    arguments.add(arg);
    return arguments;
}
Also used : Argument(org.opennms.netmgt.model.notifd.Argument)

Example 17 with Argument

use of org.opennms.netmgt.model.notifd.Argument in project opennms by OpenNMS.

the class MicroblogNotificationStrategyIT method postNotice.

@Ignore
@Test
public void postNotice() {
    NotificationStrategy ns = new MicroblogNotificationStrategy(m_daoConfigResource);
    List<Argument> arguments = configureArgs();
    Assert.assertEquals("NotificationStrategy should return 0 on success", 0, ns.send(arguments));
}
Also used : Argument(org.opennms.netmgt.model.notifd.Argument) NotificationStrategy(org.opennms.netmgt.model.notifd.NotificationStrategy) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 18 with Argument

use of org.opennms.netmgt.model.notifd.Argument in project opennms by OpenNMS.

the class TicketNotificationStrategy method send.

/**
 * {@inheritDoc}
 */
@Override
public int send(List<Argument> arguments) {
    String eventID = null;
    String eventUEI = null;
    String noticeID = null;
    // Pull the arguments we're interested in from the list.
    for (Argument arg : arguments) {
        LOG.debug("arguments: {} = {}", arg.getSwitch(), arg.getValue());
        if ("eventID".equalsIgnoreCase(arg.getSwitch())) {
            eventID = arg.getValue();
        } else if ("eventUEI".equalsIgnoreCase(arg.getSwitch())) {
            eventUEI = arg.getValue();
        } else if ("noticeid".equalsIgnoreCase(arg.getSwitch())) {
            noticeID = arg.getValue();
        }
    }
    // Make sure we have the arguments we need.
    if (StringUtils.isBlank(eventID)) {
        LOG.error("There is no event-id associated with the notice-id='{}'. Cannot create ticket.", noticeID);
        return 1;
    } else if (StringUtils.isBlank(eventUEI)) {
        LOG.error("There is no event-uei associated with the notice-id='{}'. Cannot create ticket.", noticeID);
        return 1;
    }
    // Determine the type of alarm based on the UEI.
    AlarmType alarmType = getAlarmTypeFromUEI(eventUEI);
    if (alarmType == AlarmType.NOT_AN_ALARM) {
        LOG.warn("The event type associated with the notice-id='{}' is not an alarm. Will not create ticket.", noticeID);
        return 0;
    }
    // We know the event is an alarm, pull the alarm and current ticket details from the database
    AlarmState alarmState = getAlarmStateFromEvent(Integer.parseInt(eventID));
    if (alarmState.getAlarmID() == 0) {
        LOG.error("There is no alarm-id associated with the event-id='{}'. Will not create ticket.", eventID);
        return 1;
    }
    /* Log everything we know so far.
         * The tticketid and tticketstate are only informational.
         */
    LOG.info("Got event-uei='{}' with event-id='{}', notice-id='{}', alarm-type='{}', alarm-id='{}', tticket-id='{}'and tticket-state='{}'", eventUEI, eventID, noticeID, alarmType, alarmState.getAlarmID(), alarmState.getTticketID(), alarmState.getTticketState());
    sendCreateTicketEvent(alarmState.getAlarmID(), eventUEI);
    return 0;
}
Also used : Argument(org.opennms.netmgt.model.notifd.Argument)

Example 19 with Argument

use of org.opennms.netmgt.model.notifd.Argument in project opennms by OpenNMS.

the class TicketNotificationStrategyTest method buildArguments.

protected List<Argument> buildArguments(String eventID, String eventUEI) {
    List<Argument> arguments = new ArrayList<>();
    arguments.add(new Argument("eventID", null, eventID, false));
    arguments.add(new Argument("eventUEI", null, eventUEI, false));
    return arguments;
}
Also used : Argument(org.opennms.netmgt.model.notifd.Argument) ArrayList(java.util.ArrayList)

Example 20 with Argument

use of org.opennms.netmgt.model.notifd.Argument in project opennms by OpenNMS.

the class CommandExecutor method execute.

/**
 * {@inheritDoc}
 *
 * This method executes the command using a Process. The method will decide
 * if an input stream needs to be used.
 */
@Override
public int execute(String commandLine, List<Argument> arguments) {
    int returnCode = 0;
    List<String> commandList = new ArrayList<>();
    commandList.add(commandLine);
    final StringBuilder streamBuffer = new StringBuilder();
    boolean streamed = false;
    // put the non streamed arguments into the argument array
    for (Argument curArg : arguments) {
        // only non streamed arguments go into this list
        if (!curArg.isStreamed()) {
            if (curArg.getSubstitution() != null && !curArg.getSubstitution().trim().equals("")) {
                commandList.add(curArg.getSubstitution());
            }
            if (curArg.getValue() != null && !curArg.getValue().trim().equals("")) {
                commandList.add(curArg.getValue());
            }
        } else {
            streamed = true;
            LOG.debug("streamed argument found");
            if (curArg.getSubstitution() != null && !curArg.getSubstitution().trim().equals("")) {
                streamBuffer.append(curArg.getSubstitution());
            }
            if (!curArg.getValue().trim().equals("")) {
                streamBuffer.append(curArg.getValue());
                LOG.debug("Streamed argument value: {}", curArg.getValue());
            }
        }
    }
    try {
        // set up the process
        String[] commandArray = new String[commandList.size()];
        commandArray = commandList.toArray(commandArray);
        if (LOG.isDebugEnabled()) {
            StringBuffer list = new StringBuffer();
            list.append("{ ");
            for (int i = 0; i < commandArray.length; i++) {
                if (i != 0) {
                    list.append(", ");
                }
                list.append(commandArray[i]);
            }
            list.append(" }");
            LOG.debug(list.toString());
        }
        Process command = Runtime.getRuntime().exec(commandArray);
        // see if we have streamed arguments
        if (streamed) {
            // make sure the output we are writing is buffered
            BufferedWriter processInput = new BufferedWriter(new OutputStreamWriter(command.getOutputStream(), StandardCharsets.UTF_8));
            // put the streamed arguments into the stream
            LOG.debug("Streamed arguments: {}", streamBuffer);
            processInput.write(streamBuffer.toString());
            processInput.flush();
            processInput.close();
        }
        // now wait for 30 seconds for the command to complete, if it times
        // out log a message
        // wait for 60 seconds
        long timeout = 30000;
        long start = System.currentTimeMillis();
        String commandResult = "Command timed out (30 seconds)";
        while ((System.currentTimeMillis() - start) < timeout) {
            try {
                returnCode = command.exitValue();
                commandResult = "Command-line binary completed with return code " + returnCode;
                break;
            } catch (IllegalThreadStateException e) {
            }
            synchronized (this) {
                wait(1000);
            }
        }
        LOG.debug(commandResult);
    } catch (IOException e) {
        LOG.error("Error executing command-line binary: {}", commandLine, e);
    } catch (InterruptedException e) {
        LOG.error("Error executing command-line binary: {}", commandLine, e);
    }
    return returnCode;
}
Also used : Argument(org.opennms.netmgt.model.notifd.Argument) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

Argument (org.opennms.netmgt.model.notifd.Argument)29 ArrayList (java.util.ArrayList)12 NotificationStrategy (org.opennms.netmgt.model.notifd.NotificationStrategy)8 Test (org.junit.Test)7 Ignore (org.junit.Ignore)3 JUnitHttpServer (org.opennms.core.test.http.annotations.JUnitHttpServer)3 JSONObject (org.json.simple.JSONObject)2 NodeDao (org.opennms.netmgt.dao.api.NodeDao)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Date (java.util.Date)1 NameValuePair (org.apache.http.NameValuePair)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1 JavaMailer (org.opennms.javamail.JavaMailer)1 AsteriskOriginator (org.opennms.netmgt.asterisk.utils.AsteriskOriginator)1 MockNotification (org.opennms.netmgt.mock.MockNotification)1 NotificationAnticipator (org.opennms.netmgt.mock.NotificationAnticipator)1 OnmsAssetRecord (org.opennms.netmgt.model.OnmsAssetRecord)1