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;
}
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));
}
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;
}
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;
}
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;
}
Aggregations