Dashboards & Visualizations
Highlighted

How does one access Splunk's global environment tokens from JavaScript extensions (via SplunkJS)?

Champion

Splunk 6.5 added global environment tokens that are accessible in SimpleXML (http://docs.splunk.com/Documentation/Splunk/6.5.0/Viz/tokens#Use_global_tokens_to_access_environment...).

My question is how does one obtain these token values from SplunkJS such as in a JavaScript extension to a SimpleXML dashboard?

Trying to obtain the values from defaultTokenModel or submittedTokenModel returns no result, and exploring those objects in the Chrome debugger does not indicate they would be defined in those objects.

I understand some of these values are available via rest API calls. I am specifically interested in getting them via SplunkJS.

Highlighted

Re: How does one access Splunk's global environment tokens from JavaScript extensions (via SplunkJS)?

Influencer

Here's an example:

require(['splunkjs/mvc', 'splunkjs/mvc/simplexml/ready!'], function(mvc) {

    var envTokenModel = mvc.Components.get('env');

    // Grab a specific env token
    console.log(envTokenModel.get('app'));

    // Log all env tokens
    console.log(envTokenModel.toJSON());

    // React to env token changes:
    envTokenModel.on('change', function() {
        console.log(arguments);
    });

});

View solution in original post

Highlighted

Re: How does one access Splunk's global environment tokens from JavaScript extensions (via SplunkJS)?

Champion

Thanks @ziegfried. If you are still at Splunk, would you mind passing on a note to the docs team to incorporate this on the Dev portal at http://dev.splunk.com/view/webframework-developapps/SP-CAAAEW2?

0 Karma
Highlighted

Re: How does one access Splunk's global environment tokens from JavaScript extensions (via SplunkJS)?

Explorer

Hello,
this solution run only for Simple-XML Dashboard. Exist a solution for a HTML-Dashboard to get the environment variables?

0 Karma
Highlighted

Re: How does one access Splunk's global environment tokens from JavaScript extensions (via SplunkJS)?

Champion

It should be very similar. I haven't tested this, but basing it off of some old HTML code I have.

...

var envTokenModel = mvc.Components.getInstance('env', {create: true});
var app_val = envTokenModel.get("app")
console.log(envTokenModel.get('app'));

...
0 Karma
Highlighted

Re: How does one access Splunk's global environment tokens from JavaScript extensions (via SplunkJS)?

Builder

It should REALLY be documented that you can access the env tokens (e.g. env:version) by specifying "env" as token model.

0 Karma