Skip to content

Instantly share code, notes, and snippets.

@VirtueMe
Forked from glennblock/modInput_1.js
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save VirtueMe/9145773 to your computer and use it in GitHub Desktop.

Select an option

Save VirtueMe/9145773 to your computer and use it in GitHub Desktop.
//modinput.js
var splunk = require('splunk-sdk');
var DataType = splunk.modularInput.DataType;
exports.getScheme = function() {
var scheme = {
name: 'Random Numbers',
description: 'Streams events containing a random number'
arguments: [
{
name: 'min',
desc: 'Minimum random number to be produced by this input.',
type: DataType.number,
requiredOnCreate: true
},
{
name: 'max',
desc: 'Minimum random number to be produced by this input.',
type: DataType.number,
requiredOnCreate: true
}
]
};
return scheme;
}
exports.validateInput(definition) {
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);
}
//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(inputName, inputItem, ew) {
var min = parseFloat(inputItem.min);
var max = parseFloat(inputItem.max);
event = {
stanza: inputName,
data: sprintf('number=%s', Math.floor((Math.random()*max)+min));
}
ew.write(event);
}
//modinput.js
//this variant introduces an Argument class to make the syntax terser.
var splunk = require('splunk-sdk');
var DataType = splunk.modularInput.DataType;
var RequiredOnCreate = splunk.modularInput.Argument.RequiredOnCreate;
exports.getScheme = function() {
var scheme = {
name: 'Random Numbers',
desc: 'Streams events containing a random number'
args: [
// this part seems to screem ** let us make a tool that autogenerates the initialization part of it **
new RequiredOnCreate('min', 'Minimum random number to be produced by this input.', DataType.number),
new RequiredOnCreate('max', 'Maximum random number to be produced by this input', DataType.number)
]
};
return scheme;
}
exports.validateInput(definition) {
var min = parseFloat(definition.min);
var max = parseFloat(definition.max);
if (min < max)
throw sprintf('min must be less than max; found min=%f, max=%f', min, max);
}
//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(inputName, inputItem, ew) {
var min = parseFloat(inputItem.min);
var max = parseFloat(inputItem.max);
event = {
stanza: inputName,
data: sprintf('number=%s', Math.floor((Math.random()*max)+min));
}
ew.write(event);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment