Skip to content

Commit

Permalink
Added support for sending some environment variables to the shell scr…
Browse files Browse the repository at this point in the history
…ipts. For now, I support: "${message}", "${alertDescription}" and "${alertCondition}.

Added a log file to debug.
  • Loading branch information
MMartinezV committed Jul 28, 2017
1 parent 9d72c0b commit dbb7e35
Showing 1 changed file with 81 additions and 11 deletions.
92 changes: 81 additions & 11 deletions src/main/java/ir/elenoon/ExeCommandAlarmCallBack.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,105 @@
import org.graylog2.plugin.configuration.fields.ConfigurationField;
import org.graylog2.plugin.configuration.fields.TextField;
import org.graylog2.plugin.streams.Stream;
import org.graylog2.plugin.Message;
import org.graylog2.plugin.MessageSummary;
import java.io.FileWriter;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

/**
* This is the plugin. Your class should implement one of the existing plugin
* interfaces. (i.e. AlarmCallback, MessageInput, MessageOutput)
*/
public class ExeCommandAlarmCallBack implements AlarmCallback{
private Configuration configs;
private FileWriter log = null;


@Override
public void initialize(Configuration stream)
throws AlarmCallbackConfigurationException {
configs = new Configuration(stream.getSource());

// copied from telegram plugin)
String logFilepath = configs.getString("filelog");
if (logFilepath != null && logFilepath.length() > 0) {
try {
log = new FileWriter(logFilepath, true);
log.write("Logging started\n");
log.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}

@Override
public void call(Stream arg0, CheckResult arg1)
public void call(Stream stream, CheckResult result)
throws AlarmCallbackException {
try {
Runtime.getRuntime().exec(new String[]{"bash","-c",configs.getString("bashCommand")});
//
// copied from graylog-plugin-sensu
//

// alert & messages attribs
String output = new String();
String title = stream.getTitle();
String alertDescription = result.getResultDescription();
String time = result.getTriggeredAt().toString();
String alertCondition = result.getTriggeredCondition().toString();
String messageBacklog = new String();
if (result.getMatchingMessages().size() == 0) {
messageBacklog += "No message backlog available.";
} else {
for (MessageSummary message : result.getMatchingMessages()) {
messageBacklog += message.getMessage() + "\n\n\n";
}
}

// output message for debuging
output += "Stream \"" + title + "\" raised alert. \n";
output += "Alert description: " + alertDescription + "\n";
output += "Triggered at: " + time + "\n";
output += "Alert condition: " + alertCondition;
output += "Last messages accounting for this alert: \n";
output += messageBacklog + "\n";

// expand the command
String bashCommand = configs.getString("bashCommand");
output += "Bash command: " + bashCommand + "\n";
bashCommand = findAndReplaceAll("\\$\\{message\\}", messageBacklog, bashCommand);
bashCommand = findAndReplaceAll("\\$\\{alertDescription\\}", alertDescription, bashCommand);
bashCommand = findAndReplaceAll("\\$\\{alertCondition\\}", alertCondition, bashCommand);
output += "Bash command expanded: " + bashCommand + "\n";

// write the log
if (log != null) {
log.write(output);
log.write('\n');
log.flush();
}

// exec the command
Runtime.getRuntime().exec(new String[]{"bash","-c",bashCommand});

} catch (IOException e) {
e.printStackTrace();
}
}


private static String findAndReplaceAll( String pattern, String replaceWith, String inputString) {
Pattern p = Pattern.compile(pattern);
Matcher matcher = p.matcher(inputString);
return matcher.replaceAll(replaceWith);
}

@Override
public void checkConfiguration() throws ConfigurationException {
String command = configs.getString("bashCommand");
if (command.isEmpty())
throw new ConfigurationException("Fill the bash command field.");

}

@Override
Expand All @@ -52,14 +127,9 @@ public String getName() {
@Override
public ConfigurationRequest getRequestedConfiguration() {
final ConfigurationRequest configurationRequest = new ConfigurationRequest();
configurationRequest.addField(new TextField("bashCommand", "Bash Command", "", "", ConfigurationField.Optional.NOT_OPTIONAL));
configurationRequest.addField(new TextField("bashCommand", "Bash Command", "", "use \"${message}\", \"${alertDescription}\", \"${alertCondition}\" to forward graylog info", ConfigurationField.Optional.NOT_OPTIONAL));
// Manu (20170726). Add filelog to debug
configurationRequest.addField(new TextField("filelog", "File log", "/tmp/execCommandCallBack.log", "File path for debug logging"));
return configurationRequest;
}

@Override
public void initialize(Configuration arg0)
throws AlarmCallbackConfigurationException {
configs = new Configuration(arg0.getSource());
}

}

0 comments on commit dbb7e35

Please sign in to comment.