Skip to content

Instantly share code, notes, and snippets.

@glennblock
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save glennblock/9144543 to your computer and use it in GitHub Desktop.
Save glennblock/9144543 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(..);
// lifecycle events
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
});
exports.getScheme = function() {
var scheme = new {
name: 'Random Numbers',
desc: 'Streams events containing a random number'
args: [
{name:'min', desc:'Minimum random number to be produced by this input.', type:DataType.number, requiredOnCreate:true},
{name:'max', desc:'Maximum random number to be produced by this input.', type:DataType.number, requiredOnCreate:true}
]
//can also be done with an object
//
// var args = [];
// var arg = new Arg();
// arg.name = 'min';
// arg.desc = 'Minimum random number to be produced by this input.';
// arg.type = DataType.number;
// arg.requiredOnCreate = true;
// args.push(arg);
};
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();
}
//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 {name:name,data:sprintf('number=%s', Math.floor((Math.random()*max)+min)), sourecetype:'random_numbers');
//the writer is a node Writeable stream
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();
}
splunk.modularInput.execute(exports, module); //passing the module allows checking if this is the entry process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment