Dashboards & Visualizations

How to get data from search manager?

josefa123
Explorer
var SearchManager = require("splunkjs/mvc/searchmanager");
        var searchString = '* | JOIN host [ search source="dbmon-dump://SD_DB/RAP" | eval host=device_id]  | stats first(device_id) as DeviceID,first(device_type) as DeviceType, first(rap_id) as RAPID,' +
            'first(store_name) as StoreName,first(address) as Address,first(rap_type) as RAPType,first(region) as Region, first(city) as City,first(cluster) as Cluster,first(trade_area) as TradeArea,' +
            'first(longitude) as Longitude,first(latitude) as Latitude, first(cal) as CAL, first(start_operation_date) as StartOperationDate,first(memUsedPct) as MemoryUsed,first(rKB_PS) as Read, ' +
            'first(wKB_PS) as Write, first(pctSystem) as CPU_Usage, by host| sort -_time';
        new SearchManager({
            id: "cacheSearch",
            earliest_time: "rt-3m",
            latest_time: "rt",
            autostart: true,
            search: searchString,
            preview: true,
            cache: false
        });

This is my search manager. I want to get the data that the search manager returns for my conditional statement.

For exp.
if(mySearch.data.MemoryUsed < 20){...}

Thanks in advance.

0 Karma
1 Solution

jeffland
SplunkTrust
SplunkTrust

What you need is access to the search results model. In your case, you would add the following code to your .js:

var myResults = search.data("results"); // get the data from that search
myResults.on("data", function() {
    resultArray = myResults.data().rows;
    // do stuff with the results
});

You will at that point need to know the index of the column you are interested in (i.e. if you have the columns time, machine, memory_used and want the last one, it's 2) and the index of the row you want to use (alternative would be to iterate over all results).

For example, to simply display the three values I mentioned for the first row of the results:

var myResults = search.data("results"); // get the data from that search
myResults.on("data", function() {
    resultArray = myResults.data().rows;
    alert("At " + resultArray[0][0] + ", Machine " + resultArray[0][1] + " used " + resultArray[0][2] + " KB Memory");
});

PS: You obviously need to assign your search manager a name to use it, i.e. change your line where you create the search manager to

var search = new SearchManager(...

View solution in original post

jeffland
SplunkTrust
SplunkTrust

What you need is access to the search results model. In your case, you would add the following code to your .js:

var myResults = search.data("results"); // get the data from that search
myResults.on("data", function() {
    resultArray = myResults.data().rows;
    // do stuff with the results
});

You will at that point need to know the index of the column you are interested in (i.e. if you have the columns time, machine, memory_used and want the last one, it's 2) and the index of the row you want to use (alternative would be to iterate over all results).

For example, to simply display the three values I mentioned for the first row of the results:

var myResults = search.data("results"); // get the data from that search
myResults.on("data", function() {
    resultArray = myResults.data().rows;
    alert("At " + resultArray[0][0] + ", Machine " + resultArray[0][1] + " used " + resultArray[0][2] + " KB Memory");
});

PS: You obviously need to assign your search manager a name to use it, i.e. change your line where you create the search manager to

var search = new SearchManager(...

josefa123
Explorer

data is undefined

0 Karma

jeffland
SplunkTrust
SplunkTrust

Did you make sure that search is your search manager?

0 Karma

josefa123
Explorer

yep. And I think the culprit here is the real time search. But I need to retain it to get the latest results without refreshing the dashboard everytime.

0 Karma

jeffland
SplunkTrust
SplunkTrust

Ah. Yes, with real-time searches, the results model is accessed differently - you need the "preview" data, not the "results", as explained here.

So to get it working, you would do it like this:

var myResults = search.data("preview"); // get the data from that search
myResults.on("data", function() {
    resultArray = myResults.data().rows;
    // do stuff with the results
});

Sorry that I missed that your search is a real time search.

josefa123
Explorer

Thank you so much. It worked! 🙂

0 Karma
Get Updates on the Splunk Community!

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...

New in Observability Cloud - Explicit Bucket Histograms

Splunk introduces native support for histograms as a metric data type within Observability Cloud with Explicit ...