Dashboards & Visualizations

How to insert splunk spl search in a javascript

edoardo_vicendo
Builder

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
Builder

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
Builder

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 You Read The Most: Splunk Lantern’s Most Popular Articles!

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

See your relevant APM services, dashboards, and alerts in one place with the updated ...

As a Splunk Observability user, you have a lot of data you have to manage, prioritize, and troubleshoot on a ...

Index This | What goes away as soon as you talk about it?

May 2025 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with this month’s ...