Dashboards & Visualizations

How to insert splunk spl search in a javascript

edoardo_vicendo
Contributor

Hi All,

We would like to know if it is possible to embed a splunk spl search into a javascript.
If yes can you please provide some example?

Thanks a lot,
Edoardo

0 Karma
1 Solution

kamlesh_vaghela
SplunkTrust
SplunkTrust

Hi @edoardo_vicendone,
Can You Please try this javascript ?

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

    // Create managers
    new SearchManager({
        id: "example-search",
        earliest_time: "-24h@h",
        latest_time: "now",
        preview: true,
        cache: false,
        search: "index=_internal | stats count by sourcetype" 
    });

});

This is another example for iterating result.

require(['jquery', 'splunkjs/mvc/simplexml/ready!'], function ($) {
    $(document).ready(function () {
        var SearchManager = require('splunkjs/mvc/searchmanager');

        var searchString = 'index=_internal | stats count'

        var mySearchManager = new SearchManager({
                id : "cacheSearch",
                earliest_time : "-24h",
                latest_time : "now",
                autostart : true,
                search : searchString,
                preview : true,
                cache : false
            });

        var myResults = mySearchManager.data("results"); // get the data from that search
        myResults.on("data", function () {
            resultArray = myResults.data().rows;
            $.each(resultArray, function (index, value) {
                console.log(value);
            });
        });
    });
});

For more information check SearchManager in this link.

http://docs.splunk.com/DocumentationStatic/WebFramework/1.2/

Thanks

View solution in original post

kamlesh_vaghela
SplunkTrust
SplunkTrust

Hi @edoardo_vicendone,
Can You Please try this javascript ?

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

    // Create managers
    new SearchManager({
        id: "example-search",
        earliest_time: "-24h@h",
        latest_time: "now",
        preview: true,
        cache: false,
        search: "index=_internal | stats count by sourcetype" 
    });

});

This is another example for iterating result.

require(['jquery', 'splunkjs/mvc/simplexml/ready!'], function ($) {
    $(document).ready(function () {
        var SearchManager = require('splunkjs/mvc/searchmanager');

        var searchString = 'index=_internal | stats count'

        var mySearchManager = new SearchManager({
                id : "cacheSearch",
                earliest_time : "-24h",
                latest_time : "now",
                autostart : true,
                search : searchString,
                preview : true,
                cache : false
            });

        var myResults = mySearchManager.data("results"); // get the data from that search
        myResults.on("data", function () {
            resultArray = myResults.data().rows;
            $.each(resultArray, function (index, value) {
                console.log(value);
            });
        });
    });
});

For more information check SearchManager in this link.

http://docs.splunk.com/DocumentationStatic/WebFramework/1.2/

Thanks

edoardo_vicendo
Contributor

Hi @kamlesh_vaghela,

Really thanks, this will help us a lot.
We are currently facing some challange to set-up read only users in Splunk, that do not have access to any Search view, Alerts view, Datasets view etc... and for example only have access to dashboards.

Our idea is to load a javascript in each dashboard that search in a lookup table we have previously parameterized. In that lookup table we have defined user/roles and which view they can see. So based on the result of the search if that specific user/role cannot see the views we will then enable hideAppBar="true" hideFooter="true" etc..

I'll keep you posted.

Best Regards,
Edoardo

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

Hi @edoardo_vicendone,
Thanks for explaining your requirement. Kindly let me know if any help required in any case.

Thanks

0 Karma

edoardo_vicendo
Contributor

Hi @kamlesh_vaghela,

The script is mostly completed, but we have still one issue.
We are trying to get the "value" out of the function so that we can then pass them to the dashboard but we are still unable to get it.

We tried to search on internet and also tried several solutions but until now without success.
Do you know how to get the results stored in the value variable out from the function in order to let them being directly accessable in the javascript?

Thanks a lot,
Edoardo

0 Karma
Get Updates on the Splunk Community!

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

Let’s Get You Certified – Vegas-Style at .conf24

Are you ready to level up your Splunk game? Then, let’s get you certified live at .conf24 – our annual user ...