Splunk Search

How do I create a token from a hidden search in SimpleXML

joxley
Path Finder

I want to have a hidden search in my simple XML dashboard

<search id="base">
   <query>index=_internal | stats count</query>
   <earliest>-60m</earliest>
   <latest>now</latest>
   <set token="event_count">count</set>
</search>

which populates the $event_count$ token for use in later on in the dashboard, e.g. as the title of a panel

<panel>
    <title>My events ($event_count$)</title>
    <table>
0 Karma
1 Solution

jeffland
SplunkTrust
SplunkTrust

Searches produce variable results (i.e. tables, single rows, events, ...). The fact that your particular search returns only one row and one column does not mean you can just use that result and put in in a token. How would you expect a search such as index=_internal to populate a token?

What you can do however is extract that search result in Javascript and edit your title accordingly. To do that, change your XML to contain the id of the panel for later reference with jquery:

<panel id="panel_id">
    <title>My events </title>
<table>

and add the following code to your .js:

var $ = require("jquery");
var search = splunkjs.mvc.Components.getInstance("base");
var myResults = search.data("results");
var panel = $("#panel_id").find("h2").first(); // If neccessary, adjust this to the right title
myResults.on("data", function() {
    resultArray = myResults.data().rows;
    panel.append(resultArray[0][0];);
});

View solution in original post

jeffland
SplunkTrust
SplunkTrust

Searches produce variable results (i.e. tables, single rows, events, ...). The fact that your particular search returns only one row and one column does not mean you can just use that result and put in in a token. How would you expect a search such as index=_internal to populate a token?

What you can do however is extract that search result in Javascript and edit your title accordingly. To do that, change your XML to contain the id of the panel for later reference with jquery:

<panel id="panel_id">
    <title>My events </title>
<table>

and add the following code to your .js:

var $ = require("jquery");
var search = splunkjs.mvc.Components.getInstance("base");
var myResults = search.data("results");
var panel = $("#panel_id").find("h2").first(); // If neccessary, adjust this to the right title
myResults.on("data", function() {
    resultArray = myResults.data().rows;
    panel.append(resultArray[0][0];);
});
Get Updates on the Splunk Community!

Detecting Remote Code Executions With the Splunk Threat Research Team

REGISTER NOWRemote code execution (RCE) vulnerabilities pose a significant risk to organizations. If ...

Observability | Use Synthetic Monitoring for Website Metadata Verification

If you are on Splunk Observability Cloud, you may already have Synthetic Monitoringin your observability ...

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...