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
         }
    )});
}
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

The Data Connection - November 2026

Machine data keeps growing, and every new source brings practical questions: where should it live, how ...

Splunk Auto Ingestion Parallel Pipeline Scaling

Why this feature matters Many Splunk environments experience ingestion pressure long before the host is fully ...

Splunk SecOps on AWS: A New Agentic SecOps Experience

Key takeaways At AWS:reinvent, Splunk, in partnership with Amazon Web Services (AWS), announced Splunk SecOps ...