Splunk Search

How to store a single value search result in some variable in a Splunk HTML page to display in a javascript alert popup?

harshal_chakran
Builder

Hi,

I have a dashboard in html code with one search query which provides the result in Single Numeric Value.

Is there any way that I can take the result of my search in some variable?
I want to display it in a JavaScript alert popup i.e. alert("my search result is: " + search_result_var).
Which variable in html code of dashboard stores the search result.
Please Help..!!!

0 Karma

gun1989
New Member

Hi i am new to splunk, can you please give me a sample code which i can use?

0 Karma

Flynt
Splunk Employee
Splunk Employee

One way to achieve this is put a listener on your search and then assign the value to a token. For instance if my single panel was using search1 and the token I wanted to set was "user".

 var tokens = mvc.Components.getInstance("default");
 var singleresult=splunkjs.mvc.Components.get("search1");
     singleresult.data("results").on("data", function(results) { 
             var username = results._data['rows'][0][0];
             tokens.set("user",username);
      });

You could then use that token or variable anywhere in your HTML. You can use it in a search as any other token by calling it with $user$ or in your script by the variable alert("my search result is: " + username). If you're not using the alert inside the listener, you can retrieve the token value and set it as a variable anywhere else by -

 var desiredvariable=tokens.get("user");

djfang
Explorer

@Flynt[Splunk]

Thanks for your help! 🙂

0 Karma

Raghav2384
Motivator

Have you tried assigning the value to a input token and call it ? might work. I had a same single value panel...i created a token and assigned the single value and then called the token like

value is : $single_value$

and it displays just fine.

Hope this helps!
Thanks,
Raghav

0 Karma

naveen069
New Member

Thank you so much!! It was really helpful 🙂

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