package com.perforce.p4splunk; import java.io.IOException; import java.util.Random; import javax.xml.stream.XMLStreamException; import com.splunk.modularinput.Argument; import com.splunk.modularinput.Event; import com.splunk.modularinput.EventWriter; import com.splunk.modularinput.InputDefinition; import com.splunk.modularinput.MalformedDataException; import com.splunk.modularinput.Scheme; import com.splunk.modularinput.Script; import com.splunk.modularinput.SingleValueParameter; import com.splunk.modularinput.ValidationDefinition; public class Main extends Script { public static void main(String[] args) { new Main().run(args); } @Override public Scheme getScheme() { Scheme scheme = new Scheme("perforce_jnl"); scheme.setDescription("Generates events containing a random number."); scheme.setUseExternalValidation(true); scheme.setUseSingleInstance(true); // Update README/inputs.conf.spec. Argument p4port = new Argument("p4port"); p4port.setDataType(Argument.DataType.STRING); p4port.setDescription("Perforce server port e.g. 'localhost:1666'"); p4port.setRequiredOnCreate(true); scheme.addArgument(p4port); Argument p4user = new Argument("p4user"); p4user.setDataType(Argument.DataType.STRING); p4user.setDescription("Username to authenticate against the Perforce server"); p4user.setRequiredOnCreate(true); scheme.addArgument(p4user); Argument p4pass = new Argument("p4pass"); p4pass.setDataType(Argument.DataType.STRING); p4pass.setDescription("Password or Ticket to authenticate against the Perforce server"); p4pass.setRequiredOnCreate(false); scheme.addArgument(p4pass); return scheme; } @Override public void validateInput(ValidationDefinition definition) throws Exception { String p4port = ((SingleValueParameter) definition.getParameters().get( "p4port")).getValue(); if (p4port == null || p4port.isEmpty()) { throw new Exception("p4port not defined!"); } String p4user = ((SingleValueParameter) definition.getParameters().get( "p4user")).getValue(); if (p4user == null || p4user.isEmpty()) { throw new Exception("p4user not defined!"); } // ConnectionConfig config = new ConnectionConfig(p4port, false, null); // String error = ConnectionFactory.testConnection(config); // if (error != null) { // throw new Exception(error); // } } // Finally, the real action: splunk calls the modular input with no // arguments, streams a bunch of XML describing // the inputs to stdin, and waits for XML on stdout describing events. // // If you set setUseSingleInstance(true) on the scheme in getScheme, it will // pass all the instances of this input // to a single instance of this script and it's your job to handle them all. // Otherwise, it starts a JVM for each // instance of the input. // // We are using a single instance, and starting a thread for each instance // of the modular input. For scripts that // are not single instance, it is simpler to do the work directly in the // streamEvents method. @Override public void streamEvents(InputDefinition inputs, EventWriter ew) throws MalformedDataException, XMLStreamException, IOException { for (String inputName : inputs.getInputs().keySet()) { Thread t = new Thread(new Generator(ew, inputName, 0, 10)); t.run(); } } // A Runnable that generates events with a random number in the proper range // every half second. All the important // stuff to look at is in the run method. class Generator implements Runnable { private double min, max; EventWriter ew; String inputName; public Generator(EventWriter ew, String inputName, double min, double max) { super(); this.min = min; this.max = max; this.ew = ew; this.inputName = inputName; } public void run() { // First we log an INFO message that this thread has started. This // will show up in splunkd.log and in // Splunk's _internal index. // EventWriter provides both log and synchronizedLog (one a // synchronized version of the other). In // this case, synchronizing at the level of each log message and // event is exactly what we want. In // more complicated cases, you may want to use the unsynchronized // version and do your own // synchronization. ew.synchronizedLog(EventWriter.INFO, "Random number generator " + inputName + " started, generating numbers between " + Double.toString(min) + " and " + Double.toString(max)); final Random randomGenerator = new Random(); while (true) { // Write a new event. The minimum that you must set on an event // is the stanza it is supposed to // go to (which you can skip if your modular input is not single // instance, and the data of the // event. Event event = new Event(); event.setStanza(inputName); event.setData("number=" + (randomGenerator.nextDouble() * (max - min) + min)); try { ew.writeEvent(event); } catch (MalformedDataException e) { ew.synchronizedLog(EventWriter.ERROR, "MalformedDataException in writing event to input" + inputName + ": " + e.toString()); } try { Thread.sleep(500); } catch (InterruptedException e) { return; } } } } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#3 | 10090 | Paul Allen |
Submit journal @pv@ entry as "key=value key=value etc..." + Basic password support + Journal limit and start index + Basic Journal db table filter + Open/Close the Perforce connection each iteration |
||
#2 | 10077 | Paul Allen | Add export command -- not yet working. | ||
#1 | 10058 | Paul Allen | Java version of Perforce Modular Input |