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!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

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