Skip to content

Instantly share code, notes, and snippets.

@itay
Forked from glennblock/modInput_1.js
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itay/9277148 to your computer and use it in GitHub Desktop.
Save itay/9277148 to your computer and use it in GitHub Desktop.
//modinput.js
//A few prototypes namely Arg and Event are introduced to make the code more readable, toolable (syntax completion in editors/ides)
//and less error prone.
var splunk = require('splunk-sdk');
var mod = splunk.modularInput;
var logger = mod.logger;
// logger.info(..);
// logger.error(..);
exports.getScheme = function() {
var scheme = new {
name: 'Random Numbers',
desc: 'Streams events containing a random number'
args: [
new mod.Arg('min', 'Minimum random number to be produced by this input.', DataType.number, true),
new mod.Arg('max', 'Maximum random number to be produced by this input', DataType.number, true)
]
};
return scheme;
}
exports.validateInput(definition, done) {
var min = parseFloat(definition.min);
var max = parseFloat(defintiion.max);
if (min < max) {
throw sprintf('min must be less than max; found min=%f, max=%f', min, max);
}
done();
}
mod.on("init", function(done) {
// Before any stream events have been called
});
mod.on("complete", function(done) {
// After all stream events have been called
});
mod.on("teardown", function(done) {
// After all done() calls are done
});
//We will invoke this for each input so a developer does not have to write boilerplate looping code.
//they can check the inputName if they are supporting multiple instances.
exports.streamEvents = function(name, definition, writer, done) {
var min = parseFloat(definition.min);
var max = parseFloat(definition.max);
//Event contains a convenience constructor with the most common properties, addl props can be set on the object.
var event = new mod.Event (name,sprintf('number=%s', Math.floor((Math.random()*max)+min)), "random_numbers" );
//the writer wraps a node stream and writes the xml element as bytes to the wire
writer.write(event);
// IDEA
// logger.getLogger(name).error("foo")
//Once data is no longer received the user calls the done callback.
//If this input is continually streaming in results, then this will not get called.
done();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment