Dashboards & Visualizations

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

rjthibod
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.

1 Solution

ziegfried
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

ziegfried
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);
    });

});

helge
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

SK8
Explorer

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

0 Karma

rjthibod
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

rjthibod
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
Get Updates on the Splunk Community!

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...