Splunk Search

POST LOGIN need to execute a search and use token across dashboard

prathikpisplunk
Explorer

Hi All,

For a particular filter which is used across my dashboard , I have run a search in javascript and set a token and use it .

This search has to be run post login.
1. WHere can run such search in javascript
2. Need to set token in that javascript function and has to be used across dashboard ( like a global variable).

Please give me inputs its kind of urgent requirement.

Regards,
Prathi

Tags (1)
0 Karma

aljohnson_splun
Splunk Employee
Splunk Employee

Hi @Prathikpisplunk,

If you already have the mvc components loaded:

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

    ...

});

You can use tokens directly:

// Access the "default" token model
var tokens = mvc.Components.get("default");

// Retrieve the value of a token $mytoken$
var tokenValue = tokens.get("mytoken");

// Change the value of a token $mytoken$
tokens.set("mytoken", "this is the new value");

Another example from here:

new DropdownView({
    id: "selIndex",
    value: mvc.tokenSafe("$indexName$"),
    el: $("#selIndex")
}).render();

new TextInputView({
    id: "txtKeywords",
    value: mvc.tokenSafe("$keywords$"),
    el: $("#txtKeywords")
}).render();

new SearchManager({
    id: "mySearch",
    search: mvc.tokenSafe("index=$indexName$ $keywords$")
});

prathikpisplunk
Explorer

Hi Xander,

Thanks for the reply. I have created one js file and running a search in that and setting the token.
Now I am planning to include this js file in all the xml files which are in my dashboard. There are about 20 xml files. So it is not a right thing to include in all 20. Also the search in js wil run 20 times based on user clicking each panel. so I wanted to know if there is any post login javascript function where I can just run the javascript function only once and set the token which can later be used globally?

-----------------Below is the javascript funtion where I am trying to see if user has admin rights. based on which in xml I need to show him the drop down options--------------
function(mvc, SearchManager) {
var userSearch = new SearchManager({
"id": "userSearch",
"preview": "false",
"cache": "false",
"search": "|rest splunk_server=local /services/authentication/current-context |table roles |mvexpand roles"
}, {tokens: false});

    userSearch.startSearch();
    var roleSearchResults = userSearch.data('results');
    roleSearchResults.on("data", function() {    
        var userRoles = roleSearchResults.data().rows;
        var tokens = mvc.Components.get("default");    
        tokens.set('RoleCheckQuery', "");        
        for(var i=0; i<userRoles.length; i++){
            console.log(userRoles[i][0]); 
              var userRole = userRoles[i][0];
            if (userRole === "admin" ) {
                tokens.set('RoleCheckQuery', "");
                break;
                    } 
            else {
                 tokens.set('RoleCheckQuery', '| join cpyKey[| rest /services/authentication/current-context|search (NOT username=\"splunk-system-user\") | fields + roles | mvexpand roles| makemv roles delim="-" | eval cpyKey=mvindex(roles, 3)|fields cpyKey]');
                  }
        }
        var tokenValue = tokens.get("RoleCheckQuery");

    });
0 Karma

aljohnson_splun
Splunk Employee
Splunk Employee

One thing you could do is output the token to lookup and then load it from the lookup in the other dashboards?

0 Karma
Get Updates on the Splunk Community!

Webinar Recap | Revolutionizing IT Operations: The Transformative Power of AI and ML ...

The Transformative Power of AI and ML in Enhancing Observability   In the realm of IT operations, the ...

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...