Splunk Dev

How can I create a transforms.conf stanza from SplunkJS?

David
Splunk Employee
Splunk Employee

I want to create a new lookup file. In transforms.conf, that's pretty simple, it should look something like this:

[myLookup6]
filename = myLookup.csv

But how do I do that from SplunkJS?

I've found the appropriate docs, but haven't been able to divine the answer:
http://docs.splunk.com/DocumentationStatic/JavaScriptSDK/1.1/splunkjs.Service.Configurations.html
http://docs.splunk.com/DocumentationStatic/JavaScriptSDK/1.8.0/splunkjs.Service.ConfigurationFile.ht...
http://docs.splunk.com/DocumentationStatic/JavaScriptSDK/1.1/splunkjs.Service.ConfigurationStanza.ht...

1 Solution

David
Splunk Employee
Splunk Employee

After a lot of trial and error, and then the help of a partner who had done even more trial and error (and is a better programmer than me), I got the answer.

The overall process is:

  1. Create a service object
  2. Instantiate the configurations object with your scope (app, user, etc.)
  3. Fetch the existing configurations for your file
  4. Create a ConfigurationFile object with a blank config, or pull the existing one
  5. With your file, create a new stanza

Here is the code that worked for me, on Splunk 6.5:

function dvtest(){
    var appscope={}
    appscope['owner'] = Splunk.util.getConfigValue("USERNAME");
    appscope['app'] = utils.getCurrentApp()
    appscope['sharing'] = "app"
    var mystanza = []
    mystanza['filename'] = "myLookup.csv"
    var svc = mvc.createService();
    var files = svc.configurations(appscope);
    var fileDeferred = $.Deferred();
    files.fetch({ 'search': 'name=transforms"' }, function(err, files) {
       var transformsFile = files.item("transforms");
       if (!transformsFile) {
           //  Create the file here
           transformsFile = files.create('transforms', function(err, transformsFile) {
              if (err) {
                  // Error case, throw an exception dialog to user here.
                  return;
              }
              fileDeferred.resolve(transformsFile);
           });
       }
       else {
          fileDeferred.resolve(transformsFile)
       }
    });
    fileDeferred.done(function(transformsFile) {
         transformsFile.create("myLookup6", mystanza, function(err, stanza) {
             // Stanza now created -- go check transforms.conf if you don't believe me
         }
    )});
}

View solution in original post

lknecht_splunk
Splunk Employee
Splunk Employee

Hi there David,

Please take a look at this example application that has been built.

Developer Guidance - Setup View Example For Splunk:
https://splunkbase.splunk.com/app/3728/

Source code can be found here:
https://github.com/splunk/Developer_Guidance_Setup_View_Example

This app uses a setup view to configure Splunk configuration files. While the goal is different, the solution is the same, and much of the code can be reused in the goal you're seeking to accomplish.

It is meant to serve as a point of reference.

Please let me know if that's not sufficient.

0 Karma

David
Splunk Employee
Splunk Employee

After a lot of trial and error, and then the help of a partner who had done even more trial and error (and is a better programmer than me), I got the answer.

The overall process is:

  1. Create a service object
  2. Instantiate the configurations object with your scope (app, user, etc.)
  3. Fetch the existing configurations for your file
  4. Create a ConfigurationFile object with a blank config, or pull the existing one
  5. With your file, create a new stanza

Here is the code that worked for me, on Splunk 6.5:

function dvtest(){
    var appscope={}
    appscope['owner'] = Splunk.util.getConfigValue("USERNAME");
    appscope['app'] = utils.getCurrentApp()
    appscope['sharing'] = "app"
    var mystanza = []
    mystanza['filename'] = "myLookup.csv"
    var svc = mvc.createService();
    var files = svc.configurations(appscope);
    var fileDeferred = $.Deferred();
    files.fetch({ 'search': 'name=transforms"' }, function(err, files) {
       var transformsFile = files.item("transforms");
       if (!transformsFile) {
           //  Create the file here
           transformsFile = files.create('transforms', function(err, transformsFile) {
              if (err) {
                  // Error case, throw an exception dialog to user here.
                  return;
              }
              fileDeferred.resolve(transformsFile);
           });
       }
       else {
          fileDeferred.resolve(transformsFile)
       }
    });
    fileDeferred.done(function(transformsFile) {
         transformsFile.create("myLookup6", mystanza, function(err, stanza) {
             // Stanza now created -- go check transforms.conf if you don't believe me
         }
    )});
}
Get Updates on the Splunk Community!

Introducing the Splunk Community Dashboard Challenge!

Welcome to Splunk Community Dashboard Challenge! This is your chance to showcase your skills in creating ...

Get the T-shirt to Prove You Survived Splunk University Bootcamp

As if Splunk University, in Las Vegas, in-person, with three days of bootcamps and labs weren’t enough, now ...

Wondering How to Build Resiliency in the Cloud?

IT leaders are choosing Splunk Cloud as an ideal cloud transformation platform to drive business resilience,  ...